@Test public void testUserExistsException() { final User user = new User(Constants.DEFAULT_ADMIN_USERNAME); user.setEmail("matt@raibledesigns.com"); final Exception ex = new DataIntegrityViolationException(""); context.checking(new Expectations() {{ one(userDao).saveUser(with(same(user))); will(throwException(ex)); }}); try { userManager.saveUser(user); fail("Expected UserExistsException not thrown"); } catch (UserExistsException e) { log.debug("expected exception: " + e.getMessage()); assertNotNull(e); } }
public User saveUser(User user) throws UserExistsException { if (user.getVersion() == null) { user.setUsername(user.getUsername().toLowerCase()); } boolean passwordChanged = false; if (passwordEncoder != null) { if (user.getVersion() == null) { passwordChanged = true; } else { String currentPassword = dao.getUserPassword(user.getUsername()); if (currentPassword == null) { passwordChanged = true; } else { if (!currentPassword.equals(user.getPassword())) { passwordChanged = true; } } } if (passwordChanged) { user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null)); } } else { log.warn("PasswordEncoder not set, skipping password encryption..."); } try { return dao.saveUser(user); } catch (DataIntegrityViolationException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } catch (EntityExistsException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } }
UserManagerImpl extends UniversalManagerImpl implements UserManager, UserService { public User saveUser(User user) throws UserExistsException { if (user.getVersion() == null) { user.setUsername(user.getUsername().toLowerCase()); } boolean passwordChanged = false; if (passwordEncoder != null) { if (user.getVersion() == null) { passwordChanged = true; } else { String currentPassword = dao.getUserPassword(user.getUsername()); if (currentPassword == null) { passwordChanged = true; } else { if (!currentPassword.equals(user.getPassword())) { passwordChanged = true; } } } if (passwordChanged) { user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null)); } } else { log.warn("PasswordEncoder not set, skipping password encryption..."); } try { return dao.saveUser(user); } catch (DataIntegrityViolationException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } catch (EntityExistsException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } } }
UserManagerImpl extends UniversalManagerImpl implements UserManager, UserService { public User saveUser(User user) throws UserExistsException { if (user.getVersion() == null) { user.setUsername(user.getUsername().toLowerCase()); } boolean passwordChanged = false; if (passwordEncoder != null) { if (user.getVersion() == null) { passwordChanged = true; } else { String currentPassword = dao.getUserPassword(user.getUsername()); if (currentPassword == null) { passwordChanged = true; } else { if (!currentPassword.equals(user.getPassword())) { passwordChanged = true; } } } if (passwordChanged) { user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null)); } } else { log.warn("PasswordEncoder not set, skipping password encryption..."); } try { return dao.saveUser(user); } catch (DataIntegrityViolationException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } catch (EntityExistsException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } }  }
UserManagerImpl extends UniversalManagerImpl implements UserManager, UserService { public User saveUser(User user) throws UserExistsException { if (user.getVersion() == null) { user.setUsername(user.getUsername().toLowerCase()); } boolean passwordChanged = false; if (passwordEncoder != null) { if (user.getVersion() == null) { passwordChanged = true; } else { String currentPassword = dao.getUserPassword(user.getUsername()); if (currentPassword == null) { passwordChanged = true; } else { if (!currentPassword.equals(user.getPassword())) { passwordChanged = true; } } } if (passwordChanged) { user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null)); } } else { log.warn("PasswordEncoder not set, skipping password encryption..."); } try { return dao.saveUser(user); } catch (DataIntegrityViolationException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } catch (EntityExistsException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } }  @Required void setPasswordEncoder(PasswordEncoder passwordEncoder); User getUser(String userId); List<User> getUsers(User user); String createSession(User user); User authenticate(String username, String password); User authenticate(String sessionKey); void logout(UserSession userSession); User saveUser(User user); void removeUser(String userId); User getUserByUsername(String username); UserSessionDao getUserSessionDao(); void setUserSessionDao(UserSessionDao userSessionDao); @Required void setUserDao(UserDao dao); }
UserManagerImpl extends UniversalManagerImpl implements UserManager, UserService { public User saveUser(User user) throws UserExistsException { if (user.getVersion() == null) { user.setUsername(user.getUsername().toLowerCase()); } boolean passwordChanged = false; if (passwordEncoder != null) { if (user.getVersion() == null) { passwordChanged = true; } else { String currentPassword = dao.getUserPassword(user.getUsername()); if (currentPassword == null) { passwordChanged = true; } else { if (!currentPassword.equals(user.getPassword())) { passwordChanged = true; } } } if (passwordChanged) { user.setPassword(passwordEncoder.encodePassword(user.getPassword(), null)); } } else { log.warn("PasswordEncoder not set, skipping password encryption..."); } try { return dao.saveUser(user); } catch (DataIntegrityViolationException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } catch (EntityExistsException e) { e.printStackTrace(); log.warn(e.getMessage()); throw new UserExistsException("User '" + user.getUsername() + "' already exists!"); } }  @Required void setPasswordEncoder(PasswordEncoder passwordEncoder); User getUser(String userId); List<User> getUsers(User user); String createSession(User user); User authenticate(String username, String password); User authenticate(String sessionKey); void logout(UserSession userSession); User saveUser(User user); void removeUser(String userId); User getUserByUsername(String username); UserSessionDao getUserSessionDao(); void setUserSessionDao(UserSessionDao userSessionDao); @Required void setUserDao(UserDao dao);  }