DataStoreImpl implements DataStore { @Override public void remove(Object entity) throws PersistenceException { checkInitialize(); EntityManager em = getEntityManager(); try { Class clazz = entity.getClass(); String id = getIdFieldName(clazz); DynamicType type = getDynamicEntityType(clazz); if (type != null) { try { Map<String, Object> properties = getEntityProperties(entity); DynamicEntity dynamicEntity = em.getReference(type.getJavaClass(), properties.get(id)); if (dynamicEntity != null) { em.getTransaction().begin(); try { em.remove(dynamicEntity); em.getTransaction().commit(); } catch (Exception e) { rollbackTransaction(em.getTransaction()); throwPersistenceException("Caught exception trying to remove view entity " + entity, e); } } } catch (Exception e) { throwPersistenceException("Caught exception trying to remove view entity " + entity, e); } } } finally { em.close(); } }  @Override void store(Object entity); @Override void remove(Object entity); @Override T find(Class<T> clazz, Object primaryKey); @Override Collection<T> findAll(Class<T> clazz, String whereClause);  }
@Test public void testRemove() throws Exception { DynamicClassLoader classLoader = new DynamicClassLoader(DataStoreImplTest.class.getClassLoader()); JpaEntityManager jpaEntityManager = createMock(JpaEntityManager.class); ServerSession session = createMock(ServerSession.class); DatabaseLogin databaseLogin = createMock(DatabaseLogin.class); EntityManagerFactory entityManagerFactory = createMock(EntityManagerFactory.class); EntityManager entityManager = createMock(EntityManager.class); JPADynamicHelper jpaDynamicHelper = createNiceMock(JPADynamicHelper.class); SchemaManager schemaManager = createNiceMock(SchemaManager.class); EntityTransaction transaction = createMock(EntityTransaction.class); DynamicEntity dynamicEntity = createMock(DynamicEntity.class); PowerMock.mockStatic(JpaHelper.class); expect(JpaHelper.getEntityManager(entityManager)).andReturn(jpaEntityManager).anyTimes(); PowerMock.replay(JpaHelper.class); expect(jpaEntityManager.getServerSession()).andReturn(session).anyTimes(); expect(session.getLogin()).andReturn(databaseLogin).anyTimes(); Capture<Sequence> sequenceCapture = EasyMock.newCapture(); databaseLogin.addSequence(capture(sequenceCapture)); EasyMock.expectLastCall().anyTimes(); Capture<DynamicType> typeCapture = EasyMock.newCapture(); Capture<DynamicType> typeCapture2 = EasyMock.newCapture(); jpaDynamicHelper.addTypes(eq(true), eq(true), capture(typeCapture), capture(typeCapture2)); expect(entityManagerFactory.createEntityManager()).andReturn(entityManager).anyTimes(); expect(entityManager.getTransaction()).andReturn(transaction).anyTimes(); Capture<Class> entityClassCapture = EasyMock.newCapture(); expect(entityManager.getReference(capture(entityClassCapture), eq(99))).andReturn(dynamicEntity); entityManager.remove(dynamicEntity); entityManager.close(); transaction.begin(); transaction.commit(); replay(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity, jpaEntityManager, session, databaseLogin); DataStoreImpl dataStore = getDataStore(entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager); dataStore.remove(new TestEntity(99, "foo", new TestSubEntity("bar"))); if ((entityClassCapture.getValue() != typeCapture.getValue().getJavaClass()) && (entityClassCapture.getValue() != typeCapture2.getValue().getJavaClass())) { Assert.fail(); } verify(entityManagerFactory, entityManager, jpaDynamicHelper, transaction, schemaManager, dynamicEntity, jpaEntityManager, session, databaseLogin); }