### Question:
UserHibernateDao extends GenericDao<JCUser> implements UserDao { @SuppressWarnings("unchecked") @Override public List<String> getUsernames(String pattern, int count) { pattern = SqlLikeEscaper.escapeControlCharacters(pattern); return session().getNamedQuery("getEnabledUsersNames") .setParameter("pattern", "%" + pattern.toLowerCase() + "%") .setMaxResults(count) .list(); } 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 getUsernamesResultCount() { String usernamePattern = "Us"; int resultCount = 2; createUser("User1", true); createUser("uSer2", true); createUser("user3", true); assertEquals(userDao.getUsernames(usernamePattern, resultCount).size(), 2); }
@Test public void getUsernamesEnabledUsers() { String usernamePattern = "Us"; int resultCount = 5; createUser("User1", true); createUser("uSer2", true); createUser("user3", false); assertEquals(userDao.getUsernames(usernamePattern, resultCount).size(), 2); }
@Test public void getUsernamesWithSpecialCharacters() { String usernamePattern = "@/|\"&' <>#${}()"; int resultCount = 5; createUserWithMail("Some_user1", "user1@mail.com", true); createUserWithMail("user2", "user2@mail.com", true); createUserWithMail("@/|\"&' <>#${}()", "user3@mail.com", true); assertEquals(userDao.getUsernames(usernamePattern, resultCount).size(), 1); }
@Test public void specialCharactersShouldBeEscapedCorrectly() { String usernamePattern = "_us%"; int resultCount = 5; createUserWithMail("Some_user1", "user1@mail.com", true); createUserWithMail("user2", "user2@mail.com", true); createUserWithMail("Some_us%2r", "user3@mail.com", true); assertEquals(userDao.getUsernames(usernamePattern, resultCount).size(), 1); }