UserAction extends BaseAction implements Preparable { public String save() throws Exception { Integer originalVersion = user.getVersion(); boolean isNew = ("".equals(getRequest().getParameter("user.version"))); if (getRequest().isUserInRole(Constants.ADMIN_ROLE)) { user.getRoles().clear(); String[] userRoles = getRequest().getParameterValues("userRoles"); for (int i = 0; userRoles != null && i < userRoles.length; i++) { String roleName = userRoles[i]; try { user.addRole(roleManager.getRole(roleName)); } catch (DataIntegrityViolationException e) { return showUserExistsException(originalVersion); } } } try { userManager.saveUser(user); } catch (AccessDeniedException ade) { log.warn(ade.getMessage()); getResponse().sendError(HttpServletResponse.SC_FORBIDDEN); return null; } catch (UserExistsException e) { return showUserExistsException(originalVersion); } if (!"list".equals(from)) { saveMessage(getText("user.saved")); return "home"; } else { List<Object> args = new ArrayList<Object>(); args.add(user.getFullName()); if (isNew) { saveMessage(getText("user.added", args)); mailMessage.setSubject(getText("signup.email.subject")); try { sendUserMessage(user, getText("newuser.email.message", args), RequestUtil.getAppURL(getRequest())); } catch (MailException me) { addActionError(me.getCause().getLocalizedMessage()); } return SUCCESS; } else { user.setConfirmPassword(user.getPassword()); saveMessage(getText("user.updated.byAdmin", args)); return INPUT; } } }  void prepare(); List<User> getUsers(); void setId(String id); User getUser(); void setUser(User user); void setQ(String q); String delete(); String edit(); String execute(); String cancel(); String save(); String list();  }
@Test public void testSave() throws Exception { User user = userManager.getUserByUsername("user"); user.setPassword("user"); user.setConfirmPassword("user"); action.setUser(user); action.setFrom("list"); MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("encryptPass", "true"); ServletActionContext.setRequest(request); assertEquals("input", action.save()); assertNotNull(action.getUser()); assertFalse(action.hasActionErrors()); }