### Question:
UserHibernateDao extends GenericDao<JCUser> implements UserDao { @SuppressWarnings("unchecked") @Override public JCUser getByUsername(String username) { List<JCUser> users = session().createCriteria(JCUser.class) .add(Restrictions.eq("username", username).ignoreCase()).list(); if (users.size() == 1) { return users.get(0); } else { for (JCUser user : users) { if (user.getUsername().equals(username)) { return user; } } return null; } } UserHibernateDao(SessionFactory sessionFactory); @Override User getCommonUserByUsername(String username); @SuppressWarnings("unchecked") @Override JCUser getByUsername(String username); @Override JCUser getByEmail(String email); @SuppressWarnings("unchecked") @Override Collection<JCUser> getNonActivatedUsers(); @Override JCUser getByUuid(String uuid); @Override List<JCUser> getByUsernames(Set<String> usernames); @SuppressWarnings("unchecked") @Override List<String> getUsernames(String pattern, int count); @SuppressWarnings("unchecked") @Override List<JCUser> findByUsernameOrEmail(String pattern, int count); @Override JCUser loadById(Long id); @SuppressWarnings("unchecked") @Override List<UserDto> findByUsernameOrEmailNotInGroup(String pattern, long groupId, int count);  }### Answer:
@Test public void testGetByUsernameSameCase() { JCUser user = ObjectsFactory.getDefaultUser(); session.save(user); JCUser result = userDao.getByUsername(user.getUsername()); assertNotNull(result); assertReflectionEquals(user, result); }
@Test public void testGetByUsernameDifferentCases() { JCUser user = ObjectsFactory.getDefaultUser(); session.save(user); JCUser result = userDao.getByUsername(user.getUsername().toUpperCase()); assertNotNull(result); assertReflectionEquals(user, result); }
@Test public void testGetByUsernameMultipleUsersWithSameNameWhenIgnoringCase() { JCUser user = ObjectsFactory.getUser("usernamE", "username@mail.com"); session.save(user); session.save(ObjectsFactory.getUser("Username", "Username@mail.com")); JCUser result = userDao.getByUsername("usernamE"); assertNotNull(result); assertReflectionEquals(user, result); }
@Test public void testGetByUsernameNotExist() { JCUser user = ObjectsFactory.getDefaultUser(); session.save(user); JCUser result = userDao.getByUsername("Name"); assertNull(result); }
@Test public void testGetByUsernameNotFoundWhenMultipleUsersWithSameNameWhenIgnoringCase() { session.save(ObjectsFactory.getUser("usernamE", "username@mail.com")); session.save(ObjectsFactory.getUser("Username", "Username@mail.com")); JCUser result = userDao.getByUsername("username"); assertNull(result); }