@Test public void testDetermineCollectionSize() { final EntityManager entityManager = context.mock(EntityManager.class); final Session hibernateSession = context.mock(Session.class); final Query hibernateQuery = context.mock(Query.class); final Collection<Object> collectionToSize = new ArrayList<Object>(); final List<Long> resultList = new ArrayList<Long>(); resultList.add(new Long(RESULT_COUNT)); QueryOptimizer optimizer = new QueryOptimizer(); optimizer.setEntityManager(entityManager); context.checking(new Expectations() { { one(entityManager).getDelegate(); will(returnValue(hibernateSession)); one(hibernateSession).createFilter(collectionToSize, "select count(*)"); will(returnValue(hibernateQuery)); one(hibernateQuery).list(); will(returnValue(resultList)); } }); assertEquals(RESULT_COUNT, optimizer.determineCollectionSize(collectionToSize)); context.assertIsSatisfied(); }
public long determineCollectionSize(final Collection<?> collection) { if (entityManager == null) { throw new NullPointerException("EntityManager is null - needs to be injected."); } Session session = (Session) entityManager.getDelegate(); return ((Long) session.createFilter(collection, "select count(*)").list().get(0)).longValue(); }
QueryOptimizer { public long determineCollectionSize(final Collection<?> collection) { if (entityManager == null) { throw new NullPointerException("EntityManager is null - needs to be injected."); } Session session = (Session) entityManager.getDelegate(); return ((Long) session.createFilter(collection, "select count(*)").list().get(0)).longValue(); } }
QueryOptimizer { public long determineCollectionSize(final Collection<?> collection) { if (entityManager == null) { throw new NullPointerException("EntityManager is null - needs to be injected."); } Session session = (Session) entityManager.getDelegate(); return ((Long) session.createFilter(collection, "select count(*)").list().get(0)).longValue(); }  }
QueryOptimizer { public long determineCollectionSize(final Collection<?> collection) { if (entityManager == null) { throw new NullPointerException("EntityManager is null - needs to be injected."); } Session session = (Session) entityManager.getDelegate(); return ((Long) session.createFilter(collection, "select count(*)").list().get(0)).longValue(); }  @PersistenceContext void setEntityManager(final EntityManager newEntityManager); long determineCollectionSize(final Collection<?> collection); }
QueryOptimizer { public long determineCollectionSize(final Collection<?> collection) { if (entityManager == null) { throw new NullPointerException("EntityManager is null - needs to be injected."); } Session session = (Session) entityManager.getDelegate(); return ((Long) session.createFilter(collection, "select count(*)").list().get(0)).longValue(); }  @PersistenceContext void setEntityManager(final EntityManager newEntityManager); long determineCollectionSize(final Collection<?> collection);  }