@Test public void testFindEventsByFromAndTo() { final ArgumentCaptor<String> queryString = ArgumentCaptor.forClass(String.class); context.checking(new Expectations() {{ oneOf(sessionFactory).openSession(); will(returnValue(session)); oneOf(session).createQuery(with(queryString.getMatcher())); will(returnValue(query)); allowing(query).setCacheable(with(any(Boolean.class))); allowing(query).setParameter(with(aNonNull(String.class)), with(aNonNull(Object.class))); allowing(query).setString(with(aNonNull(String.class)), with(aNonNull(String.class))); allowing(query).setInteger(with(aNonNull(String.class)), with(aNonNull(Integer.class))); oneOf(query).list(); oneOf(session).close(); }}); Calendar calendar = Calendar.getInstance(); calendar.set(2014, 03, 27, 0, 0, 0); Date from = calendar.getTime(); calendar.set(2014, 03, 27, 14, 12, 10); Date to = calendar.getTime(); eventRepository.findEventsByFromAndTo(from, to); assertTrue(queryString.getCapturedObject().toString().matches(".*WHERE.*(e.startTime >= :from AND e.startTime <= :to).*")); assertTrue(queryString.getCapturedObject().toString().matches(".*WHERE.*(e.endTime >= :from AND e.endTime <= :to).*")); context.assertIsSatisfied(); }
@Override public List<Event> findEventsByFromAndTo(Date from, Date to) { LOG.debug("Searching events from " + from + " to " + to + "."); Session session = getSession(); List events = null; try { StringBuilder hql = new StringBuilder(); hql.append("SELECT e FROM Event e WHERE "); hql.append("(e.suppressed=false AND e.startTime >= :from AND e.startTime <= :to) OR "); hql.append("(e.suppressed=false AND e.endTime >= :from AND e.endTime <= :to) "); hql.append("ORDER BY e.startTime"); Query query = session.createQuery(hql.toString()); query.setParameter("from", from); query.setParameter("to", to); events = query.list(); } finally { session.close(); LOG.debug("Database session closed: " + session); } LOG.debug("Found " + events.size() + " matching events."); return events; }
JpaEventRepository implements EventRepository { @Override public List<Event> findEventsByFromAndTo(Date from, Date to) { LOG.debug("Searching events from " + from + " to " + to + "."); Session session = getSession(); List events = null; try { StringBuilder hql = new StringBuilder(); hql.append("SELECT e FROM Event e WHERE "); hql.append("(e.suppressed=false AND e.startTime >= :from AND e.startTime <= :to) OR "); hql.append("(e.suppressed=false AND e.endTime >= :from AND e.endTime <= :to) "); hql.append("ORDER BY e.startTime"); Query query = session.createQuery(hql.toString()); query.setParameter("from", from); query.setParameter("to", to); events = query.list(); } finally { session.close(); LOG.debug("Database session closed: " + session); } LOG.debug("Found " + events.size() + " matching events."); return events; } }
JpaEventRepository implements EventRepository { @Override public List<Event> findEventsByFromAndTo(Date from, Date to) { LOG.debug("Searching events from " + from + " to " + to + "."); Session session = getSession(); List events = null; try { StringBuilder hql = new StringBuilder(); hql.append("SELECT e FROM Event e WHERE "); hql.append("(e.suppressed=false AND e.startTime >= :from AND e.startTime <= :to) OR "); hql.append("(e.suppressed=false AND e.endTime >= :from AND e.endTime <= :to) "); hql.append("ORDER BY e.startTime"); Query query = session.createQuery(hql.toString()); query.setParameter("from", from); query.setParameter("to", to); events = query.list(); } finally { session.close(); LOG.debug("Database session closed: " + session); } LOG.debug("Found " + events.size() + " matching events."); return events; } @Inject  JpaEventRepository(SessionFactory sessionFactory, boolean readonly); }
JpaEventRepository implements EventRepository { @Override public List<Event> findEventsByFromAndTo(Date from, Date to) { LOG.debug("Searching events from " + from + " to " + to + "."); Session session = getSession(); List events = null; try { StringBuilder hql = new StringBuilder(); hql.append("SELECT e FROM Event e WHERE "); hql.append("(e.suppressed=false AND e.startTime >= :from AND e.startTime <= :to) OR "); hql.append("(e.suppressed=false AND e.endTime >= :from AND e.endTime <= :to) "); hql.append("ORDER BY e.startTime"); Query query = session.createQuery(hql.toString()); query.setParameter("from", from); query.setParameter("to", to); events = query.list(); } finally { session.close(); LOG.debug("Database session closed: " + session); } LOG.debug("Found " + events.size() + " matching events."); return events; } @Inject  JpaEventRepository(SessionFactory sessionFactory, boolean readonly); @Override List<String> getEventTypes(); @Override void save(Event event); @Override Event getEvent(long eventId); @Override List<Event> findEventsByFromAndToAndTypeAndVesselAndArea(Date from, Date to, String type, String vessel, Double north, Double east, Double south, Double west); @Override List<Event> findEventsByFromAndTo(Date from, Date to); @Override List<Event> findRecentEvents(int numberOfEvents); @Override T findOngoingEventByVessel(int mmsi, Class<T> eventClass); }
JpaEventRepository implements EventRepository { @Override public List<Event> findEventsByFromAndTo(Date from, Date to) { LOG.debug("Searching events from " + from + " to " + to + "."); Session session = getSession(); List events = null; try { StringBuilder hql = new StringBuilder(); hql.append("SELECT e FROM Event e WHERE "); hql.append("(e.suppressed=false AND e.startTime >= :from AND e.startTime <= :to) OR "); hql.append("(e.suppressed=false AND e.endTime >= :from AND e.endTime <= :to) "); hql.append("ORDER BY e.startTime"); Query query = session.createQuery(hql.toString()); query.setParameter("from", from); query.setParameter("to", to); events = query.list(); } finally { session.close(); LOG.debug("Database session closed: " + session); } LOG.debug("Found " + events.size() + " matching events."); return events; } @Inject  JpaEventRepository(SessionFactory sessionFactory, boolean readonly); @Override List<String> getEventTypes(); @Override void save(Event event); @Override Event getEvent(long eventId); @Override List<Event> findEventsByFromAndToAndTypeAndVesselAndArea(Date from, Date to, String type, String vessel, Double north, Double east, Double south, Double west); @Override List<Event> findEventsByFromAndTo(Date from, Date to); @Override List<Event> findRecentEvents(int numberOfEvents); @Override T findOngoingEventByVessel(int mmsi, Class<T> eventClass);  }