@Test public void testFind() 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); 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(); Capture<Class> entityClassCapture = EasyMock.newCapture(); expect(entityManager.find(capture(entityClassCapture), eq(99))).andReturn(dynamicEntity); entityManager.close(); expect(dynamicEntity.get("DS_id")).andReturn(99); expect(dynamicEntity.get("DS_name")).andReturn("foo"); TestSubEntity subEntity = new TestSubEntity("bar"); expect(dynamicEntity.get("DS_subEntity")).andReturn(subEntity); replay(entityManagerFactory, entityManager, jpaDynamicHelper, dynamicEntity, schemaManager, jpaEntityManager, session, databaseLogin); DataStoreImpl dataStore = getDataStore(entityManagerFactory, jpaDynamicHelper, classLoader, schemaManager); TestEntity entity = dataStore.find(TestEntity.class, 99); if ((entityClassCapture.getValue() != typeCapture.getValue().getJavaClass()) && (entityClassCapture.getValue() != typeCapture2.getValue().getJavaClass())) { Assert.fail(); } Assert.assertEquals(99, (int) entity.getId()); Assert.assertEquals("foo", entity.getName()); verify(entityManagerFactory, entityManager, jpaDynamicHelper, dynamicEntity, schemaManager, jpaEntityManager, session, databaseLogin); }
@Override public <T> T find(Class<T> clazz, Object primaryKey) throws PersistenceException { checkInitialize(); EntityManager em = getEntityManager(); try { DynamicEntity dynamicEntity = null; DynamicType type = getDynamicEntityType(clazz); if (type != null) { dynamicEntity = em.find(type.getJavaClass(), primaryKey); } return dynamicEntity == null ? null : toEntity(clazz, type, dynamicEntity); } catch (Exception e) { throwPersistenceException("Caught exception trying to find " + clazz.getName() + " where key=" + primaryKey, e); } finally { em.close(); } return null; }
DataStoreImpl implements DataStore { @Override public <T> T find(Class<T> clazz, Object primaryKey) throws PersistenceException { checkInitialize(); EntityManager em = getEntityManager(); try { DynamicEntity dynamicEntity = null; DynamicType type = getDynamicEntityType(clazz); if (type != null) { dynamicEntity = em.find(type.getJavaClass(), primaryKey); } return dynamicEntity == null ? null : toEntity(clazz, type, dynamicEntity); } catch (Exception e) { throwPersistenceException("Caught exception trying to find " + clazz.getName() + " where key=" + primaryKey, e); } finally { em.close(); } return null; } }
DataStoreImpl implements DataStore { @Override public <T> T find(Class<T> clazz, Object primaryKey) throws PersistenceException { checkInitialize(); EntityManager em = getEntityManager(); try { DynamicEntity dynamicEntity = null; DynamicType type = getDynamicEntityType(clazz); if (type != null) { dynamicEntity = em.find(type.getJavaClass(), primaryKey); } return dynamicEntity == null ? null : toEntity(clazz, type, dynamicEntity); } catch (Exception e) { throwPersistenceException("Caught exception trying to find " + clazz.getName() + " where key=" + primaryKey, e); } finally { em.close(); } return null; }  }
DataStoreImpl implements DataStore { @Override public <T> T find(Class<T> clazz, Object primaryKey) throws PersistenceException { checkInitialize(); EntityManager em = getEntityManager(); try { DynamicEntity dynamicEntity = null; DynamicType type = getDynamicEntityType(clazz); if (type != null) { dynamicEntity = em.find(type.getJavaClass(), primaryKey); } return dynamicEntity == null ? null : toEntity(clazz, type, dynamicEntity); } catch (Exception e) { throwPersistenceException("Caught exception trying to find " + clazz.getName() + " where key=" + primaryKey, e); } finally { em.close(); } return null; }  @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); }
DataStoreImpl implements DataStore { @Override public <T> T find(Class<T> clazz, Object primaryKey) throws PersistenceException { checkInitialize(); EntityManager em = getEntityManager(); try { DynamicEntity dynamicEntity = null; DynamicType type = getDynamicEntityType(clazz); if (type != null) { dynamicEntity = em.find(type.getJavaClass(), primaryKey); } return dynamicEntity == null ? null : toEntity(clazz, type, dynamicEntity); } catch (Exception e) { throwPersistenceException("Caught exception trying to find " + clazz.getName() + " where key=" + primaryKey, e); } finally { em.close(); } return null; }  @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);  }