KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > verge > mvc > model > test > DefaultModelParserTest


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.model.test;
8
9
10 import com.inversoft.junit.Request;
11 import com.inversoft.junit.WebTestCase;
12 import com.inversoft.verge.mvc.MVCException;
13 import com.inversoft.verge.mvc.MVCRegistry;
14 import com.inversoft.verge.mvc.MVCRequest;
15 import com.inversoft.verge.mvc.controller.ControllerMVCInfo;
16 import com.inversoft.verge.mvc.model.DefaultModelParser;
17 import com.inversoft.verge.mvc.validator.ValidatorConstants;
18 import com.inversoft.verge.mvc.validator.test.TestValidator1;
19
20
21 /**
22  * <p>
23  * This class contains the TestCases for the default model
24  * parser.
25  * </p>
26  *
27  * @author Brian Pontarelli
28  */

29 public class DefaultModelParserTest extends WebTestCase {
30
31     /**
32      * Constructs a new <code>DefaultModelParserTest</code> TestCase instance
33      */

34     public DefaultModelParserTest(String JavaDoc name) {
35         super(name);
36         setLocal(true);
37     }
38
39
40     /**
41      * Tests that the definition gets properly passed to the resolver and the
42      * handler
43      */

44     public void beginResolverDefinition(Request request) {
45         request.addParameter("resolverTestDefinition", "true");
46         request.addParameter(DefaultModelParser.INPUT_PARAMETER,
47             "inputName|default|test");
48         request.addParameter("inputName", "foo");
49     }
50     public void testResolverDefinition() {
51         TestModelResolver mr = new TestModelResolver();
52         TestModelHandler mh = new TestModelHandler();
53         MVCRegistry.register("default", mr);
54         MVCRegistry.register("default", mh);
55
56         DefaultModelParser mp = new DefaultModelParser();
57         MVCRequest mvcRequest = new MVCRequest(request, response);
58         try {
59             mp.execute(mvcRequest);
60             assertTrue("should have called resolver definition only",
61                 mr.calledDefinition && !mr.calledNull && !mr.calledParams &&
62                 !mh.calledDefinition && !mh.calledModel && !mh.calledParams);
63         } catch (MVCException mvce) {
64             fail(mvce.toString());
65         }
66     }
67
68     /**
69      * tests definition failure
70      */

71     public void beginResolverDefinitionFail(Request request) {
72         request.addParameter("resolverTestDefinition", "true");
73         request.addParameter(DefaultModelParser.INPUT_PARAMETER,
74             "inputName|default|fail");
75         request.addParameter("inputName", "foo");
76     }
77     public void testResolverDefinitionFail() {
78         TestModelResolver mr = new TestModelResolver();
79         TestModelHandler mh = new TestModelHandler();
80         MVCRegistry.register("default", mr);
81         MVCRegistry.register("default", mh);
82
83         DefaultModelParser mp = new DefaultModelParser();
84         MVCRequest mvcRequest = new MVCRequest(request, response);
85         try {
86             mp.execute(mvcRequest);
87             fail("Should have failed");
88         } catch (MVCException mvce) {
89             assertTrue("should have called resolver definition only",
90                 mr.calledDefinition && !mr.calledNull && !mr.calledParams &&
91                 !mh.calledDefinition && !mh.calledModel && !mh.calledParams);
92         }
93     }
94
95     /**
96      * Tests that the definition gets properly passed to the resolver and the
97      * handler
98      */

99     public void beginHandlerDefinition(Request request) {
100         request.addParameter("handlerTestDefinition", "true");
101         request.addParameter(DefaultModelParser.INPUT_PARAMETER,
102             "inputName|default|test");
103         request.addParameter("inputName", "foo");
104     }
105     public void testHandlerDefinition() {
106         TestModelResolver mr = new TestModelResolver();
107         TestModelHandler mh = new TestModelHandler();
108         MVCRegistry.register("default", mr);
109         MVCRegistry.register("default", mh);
110
111         DefaultModelParser mp = new DefaultModelParser();
112         MVCRequest mvcRequest = new MVCRequest(request, response);
113         try {
114             mp.execute(mvcRequest);
115             assertTrue("should have called handler definition only",
116                 !mr.calledDefinition && !mr.calledNull && !mr.calledParams &&
117                 mh.calledDefinition && !mh.calledModel && !mh.calledParams);
118         } catch (MVCException mvce) {
119             fail(mvce.toString());
120         }
121     }
122
123     /**
124      * tests definition failure
125      */

126     public void beginHandlerDefinitionFail(Request request) {
127         request.addParameter("handlerTestDefinition", "true");
128         request.addParameter(DefaultModelParser.INPUT_PARAMETER,
129             "inputName|default|fail");
130         request.addParameter("inputName", "foo");
131     }
132     public void testHandlerDefinitionFail() {
133         TestModelResolver mr = new TestModelResolver();
134         TestModelHandler mh = new TestModelHandler();
135         MVCRegistry.register("default", mr);
136         MVCRegistry.register("default", mh);
137
138         DefaultModelParser mp = new DefaultModelParser();
139         MVCRequest mvcRequest = new MVCRequest(request, response);
140         try {
141             mp.execute(mvcRequest);
142             fail("Should have failed");
143         } catch (MVCException mvce) {
144             assertTrue("should have called handler definition only",
145                 !mr.calledDefinition && !mr.calledNull && !mr.calledParams &&
146                 mh.calledDefinition && !mh.calledModel && !mh.calledParams);
147         }
148     }
149
150     /**
151      * Tests that the extra params get setup correctly
152      */

153     public void beginResolverParams(Request request) {
154         request.addParameter("resolverTestParams", "true");
155         request.addParameter(DefaultModelParser.INPUT_PARAMETER,
156             "inputName|default|foo|param=test");
157         request.addParameter("inputName", "foo");
158     }
159     public void testResolverParams() {
160         TestModelResolver mr = new TestModelResolver();
161         TestModelHandler mh = new TestModelHandler();
162         MVCRegistry.register("default", mr);
163         MVCRegistry.register("default", mh);
164
165         DefaultModelParser mp = new DefaultModelParser();
166         MVCRequest mvcRequest = new MVCRequest(request, response);
167         try {
168             mp.execute(mvcRequest);
169             assertTrue("should have called resolver params only",
170                 !mr.calledDefinition && !mr.calledNull && mr.calledParams &&
171                 !mh.calledDefinition && !mh.calledModel && !mh.calledParams);
172         } catch (MVCException mvce) {
173             fail(mvce.toString());
174         }
175     }
176
177     /**
178      * tests params failure
179      */

180     public void beginResolverParamsFail(Request request) {
181         request.addParameter("resolverTestParams", "true");
182         request.addParameter(DefaultModelParser.INPUT_PARAMETER,
183             "inputName|default|foo|param=fail");
184         request.addParameter("inputName", "foo");
185     }
186     public void testResolverParamsFail() {
187         TestModelResolver mr = new TestModelResolver();
188         TestModelHandler mh = new TestModelHandler();
189         MVCRegistry.register("default", mr);
190         MVCRegistry.register("default", mh);
191
192         DefaultModelParser mp = new DefaultModelParser();
193         MVCRequest mvcRequest = new MVCRequest(request, response);
194         try {
195             mp.execute(mvcRequest);
196             fail("Should have failed");
197         } catch (MVCException mvce) {
198             assertTrue("should have called resolver params only",
199                 !mr.calledDefinition && !mr.calledNull && mr.calledParams &&
200                 !mh.calledDefinition && !mh.calledModel && !mh.calledParams);
201         }
202     }
203
204     /**
205      * Tests that the params gets setup properly
206      */

207     public void beginHandlerParams(Request request) {
208         request.addParameter("handlerTestParams", "true");
209         request.addParameter(DefaultModelParser.INPUT_PARAMETER,
210             "inputName|default|foo|param=test");
211         request.addParameter("inputName", "foo");
212     }
213     public void testHandlerParams() {
214         TestModelResolver mr = new TestModelResolver();
215         TestModelHandler mh = new TestModelHandler();
216         MVCRegistry.register("default", mr);
217         MVCRegistry.register("default", mh);
218
219         DefaultModelParser mp = new DefaultModelParser();
220         MVCRequest mvcRequest = new MVCRequest(request, response);
221         try {
222             mp.execute(mvcRequest);
223             assertTrue("should have called handler params only",
224                 !mr.calledDefinition && !mr.calledNull && !mr.calledParams &&
225                 !mh.calledDefinition && !mh.calledModel && mh.calledParams);
226         } catch (MVCException mvce) {
227             fail(mvce.toString());
228         }
229     }
230
231     /**
232      * tests params failure
233      */

234     public void beginHandlerParamsFail(Request request) {
235         request.addParameter("handlerTestParams", "true");
236         request.addParameter(DefaultModelParser.INPUT_PARAMETER,
237             "inputName|default|foo|param=fail");
238         request.addParameter("inputName", "foo");
239     }
240     public void testHandlerParamsFail() {
241         TestModelResolver mr = new TestModelResolver();
242         TestModelHandler mh = new TestModelHandler();
243         MVCRegistry.register("default", mr);
244         MVCRegistry.register("default", mh);
245
246         DefaultModelParser mp = new DefaultModelParser();
247         MVCRequest mvcRequest = new MVCRequest(request, response);
248         try {
249             mp.execute(mvcRequest);
250             fail("Should have failed");
251         } catch (MVCException mvce) {
252             assertTrue("should have called handler params only",
253                 !mr.calledDefinition && !mr.calledNull && !mr.calledParams &&
254                 !mh.calledDefinition && !mh.calledModel && mh.calledParams);
255         }
256     }
257
258     /**
259      * Tests that the params gets setup properly
260      */

261     public void beginResolverNull(Request request) {
262         request.addParameter("resolverTestNull", "true");
263         request.addParameter(DefaultModelParser.INPUT_PARAMETER,
264             "inputName|default|foo");
265         request.addParameter("inputName", "foo");
266     }
267     public void testResolverNull() {
268         TestModelResolver mr = new TestModelResolver();
269         TestModelHandler mh = new TestModelHandler();
270         MVCRegistry.register("default", mr);
271         MVCRegistry.register("default", mh);
272
273         DefaultModelParser mp = new DefaultModelParser();
274         MVCRequest mvcRequest = new MVCRequest(request, response);
275         try {
276             mp.execute(mvcRequest);
277             fail("Should return null and fail");
278         } catch (MVCException mvce) {
279             assertTrue("should have called resolver null only",
280                 !mr.calledDefinition && mr.calledNull && !mr.calledParams &&
281                 !mh.calledDefinition && !mh.calledModel && !mh.calledParams);
282         }
283     }
284
285     /**
286      * tests params failure
287      */

288     public void beginHandlerModel(Request request) {
289         request.addParameter("handlerTestModel", "true");
290         request.addParameter(DefaultModelParser.INPUT_PARAMETER,
291             "inputName|default|foo");
292         request.addParameter("inputName", "foo");
293     }
294     public void testHandlerModel() {
295         TestModelResolver mr = new TestModelResolver();
296         TestModelHandler mh = new TestModelHandler();
297         MVCRegistry.register("default", mr);
298         MVCRegistry.register("default", mh);
299
300         DefaultModelParser mp = new DefaultModelParser();
301         MVCRequest mvcRequest = new MVCRequest(request, response);
302         try {
303             mp.execute(mvcRequest);
304             assertTrue("should have called handler model only",
305                 !mr.calledDefinition && !mr.calledNull && !mr.calledParams &&
306                 !mh.calledDefinition && mh.calledModel && !mh.calledParams);
307         } catch (MVCException mvce) {
308             fail(mvce.toString());
309         }
310     }
311
312     /**
313      * tests that the model handler gets null values correctly
314      */

315     public void beginHandlerNullValue(Request request) {
316         //request.addParameter("handlerTestModel", "true");
317
request.addParameter(DefaultModelParser.INPUT_PARAMETER,
318             "inputName|default|foo");
319         //request.addParameter("inputName", "foo");
320
}
321     public void testHandlerNullValue() {
322         TestModelResolver mr = new TestModelResolver();
323         TestModelHandler mh = new TestModelHandler();
324         MVCRegistry.register("default", mr);
325         MVCRegistry.register("default", mh);
326
327         DefaultModelParser mp = new DefaultModelParser();
328         MVCRequest mvcRequest = new MVCRequest(request, response);
329         try {
330             mp.execute(mvcRequest);
331             assertTrue("should have called handler model with null",
332                 !mr.calledDefinition && !mr.calledNull && !mr.calledParams &&
333                 !mh.calledDefinition && !mh.calledModel && !mh.calledParams &&
334                 mh.calledNullValue);
335         } catch (MVCException mvce) {
336             fail(mvce.toString());
337         }
338     }
339
340     /**
341      * tests that the DefaultModelParser converts single empty string values to
342      * nulls correctly
343      */

344     public void beginHandlerEmptyValue(Request request) {
345         //request.addParameter("handlerTestModel", "true");
346
request.addParameter(DefaultModelParser.INPUT_PARAMETER,
347             "inputName|default|foo");
348         request.addParameter("inputName", "");
349     }
350     public void testHandlerEmptyValue() {
351         TestModelResolver mr = new TestModelResolver();
352         TestModelHandler mh = new TestModelHandler();
353         MVCRegistry.register("default", mr);
354         MVCRegistry.register("default", mh);
355
356         DefaultModelParser mp = new DefaultModelParser();
357         MVCRequest mvcRequest = new MVCRequest(request, response);
358         try {
359             mp.execute(mvcRequest);
360             assertTrue("should have called handler model with null",
361                 !mr.calledDefinition && !mr.calledNull && !mr.calledParams &&
362                 !mh.calledDefinition && !mh.calledModel && !mh.calledParams &&
363                 mh.calledNullValue);
364         } catch (MVCException mvce) {
365             fail(mvce.toString());
366         }
367     }
368     
369     /**
370      * Tests that the overall DefaultModelParser correctly skips the entire call
371      * for model handling if the meta data has model turned off
372      */

373     public void beginModelDisabled(Request request) {
374         request.addParameter("resolverTestDefinition", "true");
375         request.addParameter("resolverTestParams", "true");
376         request.addParameter("resolverTestNull", "true");
377         request.addParameter("handlerTestDefinition", "true");
378         request.addParameter("handlerTestParams", "true");
379         request.addParameter("handlerTestModel", "true");
380         request.addParameter(DefaultModelParser.INPUT_PARAMETER,
381             "inputName|default|test");
382         request.addParameter("inputName", "foo");
383         request.addParameter(ValidatorConstants.VALIDATOR_PARAMETER,
384             "com.inversoft.verge.mvc.validator.test.TestValidator1");
385     }
386     public void testModelDisabled() {
387         TestModelResolver mr = new TestModelResolver();
388         TestModelHandler mh = new TestModelHandler();
389         MVCRegistry.register("default", mr);
390         MVCRegistry.register("default", mh);
391
392         TestValidator1.validated = false;
393         DefaultModelParser mp = new DefaultModelParser();
394         try {
395             ControllerMVCInfo info = new ControllerMVCInfo("/default/f.form/a.action");
396             MVCRequest mvcRequest = new MVCRequest(request, response);
397             mvcRequest.setModelEnabled(false);
398             mvcRequest.setControllerInfo(info);
399             mp.execute(mvcRequest);
400             assertTrue("Should NOT have called any model class at all",
401                 !mr.calledDefinition && !mr.calledNull && !mr.calledParams &&
402                 !mh.calledDefinition && !mh.calledModel && !mh.calledParams);
403         } catch (MVCException mvce) {
404             fail(mvce.toString());
405         }
406     }
407 }
Popular Tags