KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > test > ActionFlowMVCTest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.verge.mvc.test;
8
9
10 import javax.servlet.ServletException JavaDoc;
11
12 import com.inversoft.error.BasicError;
13 import com.inversoft.junit.Request;
14 import com.inversoft.junit.URL;
15 import com.inversoft.verge.config.VergeConfigConstants;
16 import com.inversoft.verge.config.servlet.ConfigServlet;
17 import com.inversoft.verge.mvc.servlet.MVCServlet;
18 import com.inversoft.verge.util.RequestContext;
19
20
21 /**
22  * <p>
23  * This class contains all the full MVC tests for the action
24  * flow system.
25  * </p>
26  *
27  * @author Brian Pontarelli
28  */

29 public class ActionFlowMVCTest extends FullMVCTest {
30
31     /**
32      * Constructs a new <code>ActionFlowMVCTest</code>.
33      *
34      * @param name The name of the test to run
35      */

36     public ActionFlowMVCTest(String JavaDoc name) {
37         super(name);
38         setLocal(true);
39     }
40
41
42     public void beginActionFlowSuccess(Request request) {
43         addAFFormParameter(request, "customer", "firstName", "input1", "Brian");
44         addAFFormParameter(request, "customer", "lastName", "input2", "Pontarelli");
45         addAFFormParameter(request, "customer", "age", "input3", "31");
46         addAFFormParameter(request, "customer", "address.city", "input4", "Boulder");
47         addAFFormParameter(request, "customer", "address.state", "input5", "Colorado");
48         addAFFormParameter(request, "customer", "address.address1", "input6", "4019 Wonderland Hill Ave.");
49         addAFFormParameter(request, "customer", "address.zipcode", "input7", "80304");
50         addAFFormSubmit(request, "register", "submit", "37", true, "/registration.jsp");
51         addAFForm(request, "registration", "registration");
52     }
53
54     public void testActionFlowSuccess() {
55         if (isLocal()) {
56             getContext().setInitParameter(VergeConfigConstants.CONTEXT_PARAM,
57                 "src/com/inversoft/verge/mvc/test/test-actionflow-mvc.xml");
58         }
59
60         ConfigServlet cs = new ConfigServlet();
61         try {
62             cs.init(createConfig("ConfigServlet"));
63         } catch (ServletException JavaDoc se) {
64             fail(se.toString());
65         }
66
67         request.getSession().removeAttribute("customer");
68         UserValidator.handled = false;
69         UserValidator.validated = false;
70         UserRegistrationActionHandler.called = false;
71         MVCServlet ms = new MVCServlet();
72         try {
73             ms.init(createConfig("MVCServlet"));
74             ms.doGet(request, response);
75         } catch (ServletException JavaDoc se) {
76             se.printStackTrace();
77             fail(se.toString());
78         }
79
80         assertEquals("Request should be at /home.jsp", "/home.jsp",
81             getRequest().getRequestDispatcher().getURL());
82         assertTrue("Should have validated and executed", !UserValidator.handled &&
83             UserValidator.validated && UserRegistrationActionHandler.called);
84
85         Customer customer = (Customer) request.getSession().getAttribute("customer");
86         assertEquals("Brian", customer.getFirstName());
87         assertEquals("Pontarelli", customer.getLastName());
88         assertEquals(31, customer.getAge());
89         assertEquals("Boulder", customer.getAddress().getCity());
90         assertEquals("Colorado", customer.getAddress().getState());
91         assertEquals("4019 Wonderland Hill Ave.", customer.getAddress().getAddress1());
92         assertEquals("80304", customer.getAddress().getZipcode());
93     }
94
95     public void beginActionFlowValidation(Request request) {
96         addAFFormParameter(request, "customer", "firstName", "input1", "Brian");
97         addAFFormParameter(request, "customer", "lastName", "input2", "Pontarelli");
98         addAFFormParameter(request, "customer", "age", "input3", "1");
99         addAFFormParameter(request, "customer", "phone", "input4", "909-34-2");
100         addAFFormSubmit(request, "register", "submit", "37", true, "/registration.jsp");
101         addAFForm(request, "validate", "registration");
102     }
103
104     public void testActionFlowValidation() {
105         if (isLocal()) {
106             getContext().setInitParameter(VergeConfigConstants.CONTEXT_PARAM,
107                 "src/com/inversoft/verge/mvc/test/test-actionflow-mvc.xml");
108         }
109
110         ConfigServlet cs = new ConfigServlet();
111         try {
112             cs.init(createConfig("ConfigServlet"));
113         } catch (ServletException JavaDoc se) {
114             fail(se.toString());
115         }
116
117         UserValidator.handled = false;
118         UserValidator.validated = false;
119         UserRegistrationActionHandler.called = false;
120         MVCServlet ms = new MVCServlet();
121         try {
122             ms.init(createConfig("MVCServlet"));
123             ms.doGet(request, response);
124         } catch (ServletException JavaDoc se) {
125             se.printStackTrace();
126             fail(se.toString());
127         }
128
129         assertEquals("Request should be at /registration.jsp", "/registration.jsp",
130             getRequest().getRequestDispatcher().getURL());
131         assertTrue("Should done none", !UserValidator.handled &&
132             !UserValidator.validated && !UserRegistrationActionHandler.called);
133
134         Customer customer = (Customer) request.getSession().getAttribute("customer");
135         assertEquals("Brian", customer.getFirstName());
136         assertEquals("Pontarelli", customer.getLastName());
137
138         RequestContext context = new RequestContext(request);
139         assertTrue(context.hasErrors());
140         assertEquals(2, context.getAllErrors().size());
141         assertEquals("Invalid age", ((BasicError) context.getPropertyErrors("customer.age").get(0)).getMessage());
142         assertEquals("Invalid phone", ((BasicError) context.getPropertyErrors("customer.phone").get(0)).getMessage());
143     }
144
145     public void beginActionFlowPrimitiveValidation(Request request) {
146         addAFFormParameter(request, "customer", "age", "input3", "bob");
147         addAFFormSubmit(request, "register", "submit", "37", true, "/registration.jsp");
148         addAFForm(request, "validatePrimitive", "registration");
149     }
150
151     public void testActionFlowPrimitiveValidation() {
152         if (isLocal()) {
153             getContext().setInitParameter(VergeConfigConstants.CONTEXT_PARAM,
154                 "src/com/inversoft/verge/mvc/test/test-actionflow-mvc.xml");
155         }
156
157         ConfigServlet cs = new ConfigServlet();
158         try {
159             cs.init(createConfig("ConfigServlet"));
160         } catch (ServletException JavaDoc se) {
161             fail(se.toString());
162         }
163
164         UserValidator.handled = false;
165         UserValidator.validated = false;
166         UserRegistrationActionHandler.called = false;
167         MVCServlet ms = new MVCServlet();
168         try {
169             ms.init(createConfig("MVCServlet"));
170             ms.doGet(request, response);
171         } catch (ServletException JavaDoc se) {
172             se.printStackTrace();
173             fail(se.toString());
174         }
175
176         assertEquals("Request should be at /registration.jsp", "/registration.jsp",
177             getRequest().getRequestDispatcher().getURL());
178         assertTrue("Should done none", !UserValidator.handled &&
179             !UserValidator.validated && !UserRegistrationActionHandler.called);
180
181         RequestContext context = new RequestContext(request);
182         assertTrue(context.hasErrors());
183         assertEquals(1, context.getAllErrors().size());
184         assertEquals("Invalid age",
185             ((BasicError) context.getPropertyErrors("customer.age").get(0)).getMessage());
186     }
187
188     public void beginActionFlowLongTxn(Request request) {
189         addAFURL(request, null, "registration");
190         addAFFormSubmit(request, "start", "submit", "37", true, "/longTxnStart.jsp");
191     }
192
193     public void testActionFlowLongTxn() {
194         if (isLocal()) {
195             getContext().setInitParameter(VergeConfigConstants.CONTEXT_PARAM,
196                 "src/com/inversoft/verge/mvc/test/test-actionflow-mvc.xml");
197         }
198
199         ConfigServlet cs = new ConfigServlet();
200         try {
201             cs.init(createConfig("ConfigServlet"));
202         } catch (ServletException JavaDoc se) {
203             fail(se.toString());
204         }
205
206         UserValidator.handled = false;
207         UserValidator.validated = false;
208         UserRegistrationActionHandler.called = false;
209         MVCServlet ms = new MVCServlet();
210         String JavaDoc url = null;
211         boolean done = false;
212
213         // Loop over the call while the code is handling long transaction stuff
214
// This configuration should only loop twice
215
do {
216             getResponse().resetBuffer();
217             try {
218                 ms.init(createConfig("MVCServlet"));
219                 ms.doGet(request, response);
220             } catch (ServletException JavaDoc se) {
221                 se.printStackTrace();
222                 fail(se.toString());
223             }
224
225             url = getResponse().getText();
226             done = (url != null && url.trim().length() > 0);
227             if (done) {
228                 // Should still be at longTxn1.jsp
229
assertEquals("Request should be at /longTxn1.jsp", "/longTxn1.jsp",
230                     getRequest().getRequestDispatcher().getURL());
231
232                 System.out.println("The HTML written out: " + url);
233
234                 // Clear and resubmit the next part of it
235
getRequest().clearAttributes();
236                 getRequest().clearParameters();
237                 getRequest().getRequestDispatcher().clearURL();
238                 getResponse().resetBuffer();
239
240                 url = url.substring(url.indexOf("url=") + 4); // Start of parameters
241
url = url.substring(0, url.indexOf("'/>")); // End of XHTML
242

243                 System.out.println("The URL: " + url);
244
245                 URL urlObj = new URL(null, url, null, null, null, "/");
246                 Request newRequest = new Request();
247                 newRequest.setURL(urlObj);
248                 getRequest().setRequest(newRequest);
249             }
250         } while (done);
251
252         // Should be at longTxnStop.jsp at the end
253
System.out.println("The URL: " + getRequest().getRequestDispatcher().getURL());
254         assertEquals("Request should be at /longTxnStop.jsp", "/longTxnStop.jsp",
255             getRequest().getRequestDispatcher().getURL());
256     }
257 }
Popular Tags