1   /*
2    * The SmartWeb Framework
3    * Copyright (C) 2004-2006
4    *
5    * This library is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Lesser General Public
7    * License as published by the Free Software Foundation; either
8    * version 2.1 of the License, or (at your option) any later version.
9    *
10   * This library is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   * Lesser General Public License for more details.
14   *
15   * You should have received a copy of the GNU Lesser General Public
16   * License along with this library; if not, write to the Free Software
17   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18   *
19   * For further informations on the SmartWeb Framework please visit
20   *
21   *                        http://smartweb.sourceforge.net
22   */
23  package net.smartlab.web;
24  
25  import java.io.File;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.struts.action.ActionForm;
31  import org.apache.struts.action.ActionMapping;
32  
33  import servletunit.struts.MockStrutsTestCase;
34  
35  /**
36   * @TODO documentation
37   * @author rlogiacco
38   */
39  public class DynaActionTest extends MockStrutsTestCase {
40  
41  	/**
42  	 * @see junit.framework.TestCase#setUp()
43  	 */
44  	protected void setUp() throws Exception {
45  		super.setUp();
46  		super.setContextDirectory(new File("res/test"));
47  		super.setConfigFile("/WEB-INF/struts.xml");
48  		super.setServletConfigFile("/WEB-INF/web.xml");
49  	}
50  
51  	/**
52  	 * Test method for
53  	 * {@link net.smartlab.web.DynaAction#execute(org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.apache.struts.action.ActionMapping)}
54  	 * .
55  	 */
56  	public void testExecuteMother() {
57  		super.setRequestPathInfo("/command");
58  		super.addRequestParameter("submit", "Mamma");
59  		super.actionPerform();
60  		super.verifyForward("mother");
61  		super.assertEquals("mother", (String)super.getRequest().getAttribute("parent"));
62  		super.verifyNoActionErrors();
63  		super.setRequestPathInfo("/mother");
64  		super.actionPerform();
65  		super.verifyForward("mother");
66  		super.assertEquals("mother", (String)super.getRequest().getAttribute("parent"));
67  		super.verifyNoActionErrors();
68  	}
69  
70  	/**
71  	 * Test method for
72  	 * {@link net.smartlab.web.DynaAction#execute(org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.apache.struts.action.ActionMapping)}
73  	 * .
74  	 */
75  	public void testExecuteFather() {
76  		super.setRequestPathInfo("/command");
77  		super.addRequestParameter("submit", "PapĂ ");
78  		super.actionPerform();
79  		super.verifyForward("father");
80  		super.assertEquals("father", (String)super.getRequest().getAttribute("parent"));
81  		super.verifyNoActionErrors();
82  		super.setRequestPathInfo("/father");
83  		super.actionPerform();
84  		super.verifyForward("father");
85  		super.assertEquals("father", (String)super.getRequest().getAttribute("parent"));
86  		super.verifyNoActionErrors();
87  	}
88  
89  	/**
90  	 * Test method for
91  	 * {@link net.smartlab.web.DynaAction#execute(org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.apache.struts.action.ActionMapping)}
92  	 * .
93  	 */
94  	public void testExecuteMissing() {
95  		super.setRequestPathInfo("/command");
96  		super.addRequestParameter("submit", "Fratello");
97  		super.actionPerform();
98  		super.verifyForward("error");
99  		super.assertNull(super.getRequest().getAttribute("parent"));
100 		super.verifyActionErrors(new String[] {"action.error.method.unknown"});
101 	}
102 
103 	/**
104 	 * Test method for
105 	 * {@link net.smartlab.web.DynaAction#forward(org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, org.apache.struts.action.ActionMapping)}
106 	 * .
107 	 */
108 	public void testForward() {
109 		super.setRequestPathInfo("/forward");
110 		super.actionPerform();
111 		super.verifyForward("success");
112 		super.verifyNoActionErrors();
113 	}
114 
115 
116 	public static class Stub extends DynaAction {
117 
118 		public String father(ActionForm form, HttpServletRequest request, HttpServletResponse response,
119 				ActionMapping mapping) throws Exception {
120 			request.setAttribute("parent", "father");
121 			return "father";
122 		}
123 
124 		public String mother(ActionForm form, HttpServletRequest request, HttpServletResponse response,
125 				ActionMapping mapping) throws Exception {
126 			request.setAttribute("parent", "mother");
127 			return "mother";
128 		}
129 	}
130 }