SignupAction extends BaseAction { public String save() throws Exception { user.setEnabled(true); user.addRole(roleManager.getRole(Constants.USER_ROLE)); try { user = userManager.saveUser(user); } catch (AccessDeniedException ade) { log.warn(ade.getMessage()); getResponse().sendError(HttpServletResponse.SC_FORBIDDEN); return null; } catch (UserExistsException e) { log.warn(e.getMessage()); List<Object> args = new ArrayList<Object>(); args.add(user.getUsername()); args.add(user.getEmail()); addActionError(getText("errors.existing.user", args)); user.setPassword(user.getConfirmPassword()); return INPUT; } saveMessage(getText("user.registered")); getSession().setAttribute(Constants.REGISTERED, Boolean.TRUE); UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken( user.getUsername(), user.getConfirmPassword(), user.getAuthorities()); auth.setDetails(user); SecurityContextHolder.getContext().setAuthentication(auth); mailMessage.setSubject(getText("signup.email.subject")); try { sendUserMessage(user, getText("signup.email.message"), RequestUtil.getAppURL(getRequest())); } catch (MailException me) { addActionError(me.getMostSpecificCause().getMessage()); } return SUCCESS; }  void setCancel(String cancel); void setUser(User user); User getUser(); String execute(); String doDefault(); String save();  }
@Test public void testSave() throws Exception { final User user = createUser(); final User user2 = createUser(); action.setUser(user); ServletActionContext.setResponse(new MockHttpServletResponse()); final Wiser wiser = startWiser(getSmtpPort()); assertNull(action.getUser().getId()); assertEquals("success", action.save()); assertFalse(action.hasActionErrors()); assertNotNull(action.getUser().getId()); wiser.stop(); assertTrue(wiser.getMessages().size() == 1); assertNotNull(action.getSession().getAttribute(Constants.REGISTERED)); action.setUser(user2); assertEquals(Action.INPUT, action.save()); assertTrue(action.hasActionErrors()); SecurityContextHolder.getContext().setAuthentication(null); }