UserDaoImpl implements UserDao { @Override @Transactional public long getNumberOfStaleUsers(Date lastAccessTime) { List<?> values = getSession().getNamedQuery("numberOfStaleUsers").setTimestamp("lastAccessTime", lastAccessTime).list(); if (values == null || values.size() == 0) return 0; Number v = (Number) values.get(0); return v.intValue(); }  @Autowired void setSessionFactory(SessionFactory sessionFactory); @Override @Transactional int getNumberOfUsers(); @SuppressWarnings("unchecked") @Override @Transactional List<Integer> getAllUserIds(); @Override @Transactional List<Integer> getAllUserIdsInRange(final int firstResult,
      final int maxResults); @Override @Transactional long getNumberOfStaleUsers(Date lastAccessTime); @Override @Transactional List<Integer> getStaleUserIdsInRange(final Date lastAccessTime, final int firstResult, final int maxResults); @Override @Transactional User getUserForId(int id); @Transactional @Override void saveOrUpdateUser(User user); @Override void saveOrUpdateUsers(User... users); @Override @Transactional void deleteUser(User user); @Override @Transactional int getNumberOfUserRoles(); @Override @Transactional int getNumberOfUsersWithRole(UserRole role); @Override @Transactional UserRole getUserRoleForName(String name); @Override @Transactional void saveOrUpdateUserRole(UserRole userRole); @SuppressWarnings("unchecked") @Override @Transactional List<String> getUserIndexKeyValuesForKeyType(String keyType); @Override @Transactional Integer getUserKeyCount(final String keyType); @Override @Transactional List<User> getUsersForKeyType(final int start, final int maxResults, final String keyType); @Transactional @Override UserIndex getUserIndexForId(UserIndexKey key); @Override void saveOrUpdateUserIndex(UserIndex userIndex); @Override @Transactional void deleteUserIndex(UserIndex index);  }
@Test @Transactional public void testGetNumberOfStaleUsers() { Calendar c = Calendar.getInstance(); c.add(Calendar.MONTH, -1); Date oneMonth = c.getTime(); c.add(Calendar.MONTH, -1); Date twoMonth = c.getTime(); c.add(Calendar.MONTH, -1); Date threeMonth = c.getTime(); assertEquals(0, _dao.getNumberOfStaleUsers(oneMonth)); User userA = new User(); userA.setCreationTime(new Date()); userA.setLastAccessTime(twoMonth); userA.setProperties(new UserPropertiesV2()); _dao.saveOrUpdateUser(userA); assertEquals(1, _dao.getNumberOfStaleUsers(oneMonth)); assertEquals(0, _dao.getNumberOfStaleUsers(twoMonth)); assertEquals(0, _dao.getNumberOfStaleUsers(threeMonth)); User userB = new User(); userB.setCreationTime(new Date()); userB.setLastAccessTime(threeMonth); userB.setProperties(new UserPropertiesV2()); _dao.saveOrUpdateUser(userB); assertEquals(2, _dao.getNumberOfStaleUsers(oneMonth)); assertEquals(1, _dao.getNumberOfStaleUsers(twoMonth)); assertEquals(0, _dao.getNumberOfStaleUsers(threeMonth)); }