At line 89 changed 1 line. |
[{Java2HtmlPlugin attachment='PersonActionTest.java'}] |
[{Java2HtmlPlugin |
At line 91 added 58 lines. |
package org.appfuse.webapp.action; |
|
import org.appfuse.Constants; |
import org.appfuse.webapp.form.PersonForm; |
|
public class PersonActionTest extends BaseStrutsTestCase { |
|
public PersonActionTest(String name) { |
super(name); |
} |
|
public void testEdit() throws Exception { |
setRequestPathInfo("/editPerson"); |
addRequestParameter("action", "Edit"); |
addRequestParameter("id", "1"); |
actionPerform(); |
|
verifyForward("edit"); |
assertTrue(request.getAttribute(Constants.PERSON_KEY) != null); |
verifyNoActionErrors(); |
} |
|
public void testSave() throws Exception { |
setRequestPathInfo("/editPerson"); |
addRequestParameter("action", "Edit"); |
addRequestParameter("id", "1"); |
|
actionPerform(); |
|
PersonForm personForm = |
(PersonForm) request.getAttribute(Constants.PERSON_KEY); |
assertTrue(personForm != null); |
|
setRequestPathInfo("/savePerson"); |
addRequestParameter("action", "Save"); |
|
// update the form from the edit and add it back to the request |
personForm.setLastName("Feltz"); |
request.setAttribute(Constants.PERSON_KEY, personForm); |
|
actionPerform(); |
|
verifyForward("edit"); |
verifyNoActionErrors(); |
} |
|
public void testRemove() throws Exception { |
setRequestPathInfo("/editPerson"); |
addRequestParameter("action", "Delete"); |
addRequestParameter("id", "2"); |
actionPerform(); |
|
verifyForward("mainMenu"); |
verifyNoActionErrors(); |
} |
} |
}] |
|
At line 107 changed 1 line. |
[{Java2HtmlPlugin attachment='PersonAction.java'}] |
[{Java2HtmlPlugin |
At line 167 added 120 lines. |
package org.appfuse.webapp.action; |
|
import javax.servlet.http.HttpServletRequest; |
import javax.servlet.http.HttpServletResponse; |
|
import org.apache.struts.action.ActionForm; |
import org.apache.struts.action.ActionForward; |
import org.apache.struts.action.ActionMapping; |
import org.apache.struts.action.ActionMessage; |
import org.apache.struts.action.ActionMessages; |
|
import org.appfuse.model.Person; |
import org.appfuse.service.PersonManager; |
import org.appfuse.webapp.form.PersonForm; |
|
/** |
* @struts.action name="personForm" path="/editPerson" scope="request" |
* validate="false" parameter="method" input="mainMenu" |
*/ |
public final class PersonAction extends BaseAction { |
|
public ActionForward cancel(ActionMapping mapping, ActionForm form, |
HttpServletRequest request, |
HttpServletResponse response) |
throws Exception { |
return mapping.findForward("mainMenu"); |
} |
|
public ActionForward delete(ActionMapping mapping, ActionForm form, |
HttpServletRequest request, |
HttpServletResponse response) |
throws Exception { |
if (log.isDebugEnabled()) { |
log.debug("Entering 'delete' method"); |
} |
|
ActionMessages messages = new ActionMessages(); |
PersonForm personForm = (PersonForm) form; |
|
// Exceptions are caught by ActionExceptionHandler |
PersonManager mgr = (PersonManager) getBean("personManager"); |
mgr.removePerson(personForm.getId()); |
|
messages.add(ActionMessages.GLOBAL_MESSAGE, |
new ActionMessage("person.deleted", |
personForm.getFirstName() + ' ' + |
personForm.getLastName())); |
|
saveMessages(request, messages); |
|
return mapping.findForward("mainMenu"); |
} |
|
public ActionForward edit(ActionMapping mapping, ActionForm form, |
HttpServletRequest request, |
HttpServletResponse response) |
throws Exception { |
if (log.isDebugEnabled()) { |
log.debug("Entering 'edit' method"); |
} |
|
PersonForm personForm = (PersonForm) form; |
|
// if an id is passed in, look up the user - otherwise |
// don't do anything - user is doing an add |
if (personForm.getId() != null) { |
PersonManager mgr = (PersonManager) getBean("personManager"); |
Person person = mgr.getPerson(personForm.getId()); |
personForm = (PersonForm) convert(person); |
updateFormBean(mapping, request, personForm); |
} |
|
return mapping.findForward("edit"); |
} |
|
public ActionForward save(ActionMapping mapping, ActionForm form, |
HttpServletRequest request, |
HttpServletResponse response) |
throws Exception { |
if (log.isDebugEnabled()) { |
log.debug("Entering 'save' method"); |
} |
|
// Extract attributes and parameters we will need |
ActionMessages messages = new ActionMessages(); |
PersonForm personForm = (PersonForm) form; |
boolean isNew = ("".equals(personForm.getId())); |
|
if (log.isDebugEnabled()) { |
log.debug("saving person: " + personForm); |
} |
|
PersonManager mgr = (PersonManager) getBean("personManager"); |
Person person = (Person) convert(personForm); |
mgr.savePerson(person); |
|
// add success messages |
if (isNew) { |
messages.add(ActionMessages.GLOBAL_MESSAGE, |
new ActionMessage("person.added", |
personForm.getFirstName() + " " + |
personForm.getLastName())); |
|
// save messages in session to survive a redirect |
saveMessages(request.getSession(), messages); |
|
return mapping.findForward("mainMenu"); |
} else { |
messages.add(ActionMessages.GLOBAL_MESSAGE, |
new ActionMessage("person.updated", |
personForm.getFirstName() + " " + |
personForm.getLastName())); |
saveMessages(request, messages); |
|
return mapping.findForward("edit"); |
} |
} |
} |
}] |
|