AbstractOwnedEntityDao extends AbstractEntityDao<T> implements OwnedEntityDao<T> { @SuppressWarnings("unchecked") @Override public List<T> findByUser(String userId) { Validate.notNull(userId, "User identifier can't be null"); String query = "select e from " + clazz.getSimpleName() + " as e where e.userId = ?1 order by e.id"; return getJpaTemplate().find(query, userId); } AbstractOwnedEntityDao(Class<T> clazz); @SuppressWarnings("unchecked") @Override List<T> findByUser(String userId);  }
@Test public void findByUser() { OwnedEntityTable entity = setupOwnedEntity("user", "name"); List<OwnedEntityTable> list = Collections.singletonList(entity); assertEquals("lists not equal.", list, dao.findByUser("user")); assertEquals("user id is not case insensitive.", list, dao .findByUser("USER")); }
@Test public void findByUser2() { List<OwnedEntityTable> list = new ArrayList<OwnedEntityTable>(2); list.add(setupOwnedEntity("user", "name1")); list.add(setupOwnedEntity("user", "name2")); assertEquals("collections not equal.", list, dao.findByUser("user")); assertEquals("user id is not case insensitive.", list, dao .findByUser("USER")); }
@Test public void findByUser3() { List<OwnedEntityTable> list1 = Collections .singletonList(setupOwnedEntity("user1", "name")); List<OwnedEntityTable> list2 = Collections .singletonList(setupOwnedEntity("user2", "name")); assertEquals("collections not equal.", list1, dao.findByUser("user1")); assertEquals("user id is not case insensitive.", list1, dao .findByUser("USER1")); assertEquals("collections not equal.", list2, dao.findByUser("user2")); assertEquals("user id is not case insensitive.", list2, dao .findByUser("USER2")); }
@Test(expected = IllegalArgumentException.class) public void findByUserNullUserId() { dao.findByUser(null); }
@Test public void findByUserNonExistingUserId() { assertTrue(dao.findByUser("non-existing-user-id").isEmpty()); }