UserServiceImpl implements UserService { @Override public User login(String loginName, String passwd) { User dbUser = userDao.findByName(loginName); boolean isUpdateNeeded = false; if(dbUser != null) { if(dbUser.isLocked()) { throw new UserLockedException(); } else if(dbUser.isSystem()) { throw new SystemUserForbidLoginException(); } else if(dbUser.getPassword() != null) { if(dbUser.getPassword().equals(DigestUtils.md5Hex(passwd).toUpperCase())) { return dbUser; } else { isUpdateNeeded = true; } } } User user = null; try { user = ldapAuthenticationService.authenticate(loginName, passwd); } catch (Exception e) { throw new IncorrectPasswdException(); } if(user != null) { user.setPassword(DigestUtils.md5Hex(passwd).toUpperCase()); if(dbUser == null) { userDao.insertUser(user); } else if(isUpdateNeeded) { user.setId(dbUser.getId()); userDao.updatePassword(user); cacheClient.remove(ServiceConstants.CACHE_USER_PREFIX + dbUser.getId()); } return user; }else { throw new UserNotFoundException(loginName); } }  @Override List<User> findAll(); @Override Paginater<User> getUsers(UserCriteria userCriteria, Paginater<User> paginater); @Override User findUser(String name); @Override User findById(int id); @Override User findByName(String loginName); @Override User loadById(int id); @Override User findNoPasswdById(int id); @Override User loadNoPasswdById(int id); @Override User login(String loginName, String passwd); @Override int update(User user); @Override int insertUser(User user); @Override List<User> findByNameOrLoginNameLike(String name, boolean includeSystem); void setCacheClient(CacheClient cacheClient);  }
@Test public void testLoginRejectedDueLock() { try{ userService.login("lucy", "lucy"); } catch(Exception e) { assertTrue(e instanceof UserLockedException); } }
@Test public void testLoginRejectedDueSystem() { try{ userService.login("dbasys", "dbasys"); } catch(Exception e) { assertTrue(e instanceof SystemUserForbidLoginException); } }
@Test public void testLoginRejectedDueWrongUserName() { try{ userService.login("youngphy.yan", "xxx"); } catch(Exception e) { assertTrue(e instanceof UserNotFoundException); } }
@Test public void testLoginRejectedDueWrongPwd() { try{ userService.login("yong.you", "xxx"); } catch(Exception e) { assertTrue(e instanceof IncorrectPasswdException); } }
@Test public void testLoginSucceedAt2ndTime() { try{ User user = userService.login(rightUserNameForTest, rightPwdForTest); assertNotNull(user); } catch(Exception e) { } }