KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.inversoft.junit.Request;
11 import com.inversoft.junit.WebTestCase;
12 import com.inversoft.junit.internal.http.MockServletContext;
13 import com.inversoft.verge.mvc.DefaultMVCMediator;
14 import com.inversoft.verge.mvc.MVCConstants;
15 import com.inversoft.verge.mvc.MVCException;
16 import com.inversoft.verge.mvc.MVCRegistry;
17
18
19 /**
20  * This class contains the TestCases for the default Inversoft
21  * MVC implementation
22  *
23  * @author Brian Pontarelli
24  */

25 public class DefaultMVCMediatorTest extends WebTestCase {
26
27     /**
28      * Constructs a new <code>DefaultMVCMediatorTest</code> TestCase instance
29      */

30     public DefaultMVCMediatorTest(String JavaDoc name) {
31         super(name);
32         setLocal(true);
33     }
34
35
36     public void beginRequest(Request request) {
37         request.addParameter(MVCConstants.CONTROLLER_REQUEST_PARAM, "test");
38         request.addParameter(MVCConstants.MODEL_REQUEST_PARAM, "test");
39         request.addParameter(MVCConstants.VALIDATOR_REQUEST_PARAM, "test");
40     }
41
42     /**
43      * Tests mediator correctly finds the class using the request parameters
44      */

45     public void testRequest() {
46
47         TestControllerParser cp = new TestControllerParser();
48         TestModelParser mp = new TestModelParser();
49         TestValidatorParser vp = new TestValidatorParser();
50         MVCRegistry.register("test", cp);
51         MVCRegistry.register("test", mp);
52         MVCRegistry.register("test", vp);
53         DefaultMVCMediator mediator = new DefaultMVCMediator();
54
55         try {
56             mediator.mediate(request, response);
57             assertTrue(cp.called > 0);
58             assertTrue(mp.called > 0);
59             assertTrue(vp.called > 0);
60         } catch (MVCException mvce) {
61             fail(mvce.toString());
62         }
63     }
64
65     /**
66      * Tests mediator correctly finds the class using the context parameters
67      */

68     public void testContext() {
69         if (isLocal()) {
70             MockServletContext context = getContext();
71             context.setInitParameter(MVCConstants.CONTROLLER_CONTEXT_PARAM, "test");
72             context.setInitParameter(MVCConstants.MODEL_CONTEXT_PARAM, "test");
73             context.setInitParameter(MVCConstants.VALIDATOR_CONTEXT_PARAM, "test");
74
75             TestControllerParser cp = new TestControllerParser();
76             TestModelParser mp = new TestModelParser();
77             TestValidatorParser vp = new TestValidatorParser();
78             MVCRegistry.register("test", cp);
79             MVCRegistry.register("test", mp);
80             MVCRegistry.register("test", vp);
81             DefaultMVCMediator mediator = new DefaultMVCMediator();
82     
83             try {
84                 mediator.mediate(request, response);
85                 assertTrue(cp.called > 0);
86                 assertTrue(mp.called > 0);
87                 assertTrue(vp.called > 0);
88             } catch (MVCException mvce) {
89                 fail(mvce.toString());
90             }
91         }
92     }
93
94     /**
95      * Tests mediator correctly uses the controller default
96      */

97     public void testControllerDefault() {
98         if (isLocal()) {
99             MockServletContext context = getContext();
100             context.setInitParameter(MVCConstants.MODEL_CONTEXT_PARAM, "test");
101             context.setInitParameter(MVCConstants.VALIDATOR_CONTEXT_PARAM, "test");
102
103             TestControllerParser cp = new TestControllerParser();
104             TestModelParser mp = new TestModelParser();
105             TestValidatorParser vp = new TestValidatorParser();
106             MVCRegistry.register(MVCConstants.DEFAULT_NAME, cp);
107             MVCRegistry.register("test", mp);
108             MVCRegistry.register("test", vp);
109             DefaultMVCMediator mediator = new DefaultMVCMediator();
110     
111             try {
112                 mediator.mediate(request, response);
113                 assertTrue(cp.called > 0);
114                 assertTrue(mp.called > 0);
115                 assertTrue(vp.called > 0);
116             } catch (MVCException mvce) {
117                 fail("Should have used the controller default [" + mvce + "]");
118             }
119         }
120     }
121
122     /**
123      * Tests mediator correctly uses the model default
124      */

125     public void testModelDefault() {
126         if (isLocal()) {
127             MockServletContext context = getContext();
128             context.setInitParameter(MVCConstants.CONTROLLER_CONTEXT_PARAM, "test");
129             context.setInitParameter(MVCConstants.VALIDATOR_CONTEXT_PARAM, "test");
130         }
131
132         TestControllerParser cp = new TestControllerParser();
133         TestModelParser mp = new TestModelParser();
134         TestValidatorParser vp = new TestValidatorParser();
135         MVCRegistry.register("test", cp);
136         MVCRegistry.register(MVCConstants.DEFAULT_NAME, mp);
137         MVCRegistry.register("test", vp);
138         DefaultMVCMediator mediator = new DefaultMVCMediator();
139
140         try {
141             mediator.mediate(request, response);
142             assertTrue(cp.called > 0);
143             assertTrue(mp.called > 0);
144             assertTrue(vp.called > 0);
145         } catch (MVCException mvce) {
146             fail("Should have used the model default");
147         }
148     }
149
150     /**
151      * Tests mediator correctly uses the validator default
152      */

153     public void testValidatorDefault() {
154         if (isLocal()) {
155             MockServletContext context = getContext();
156             context.setInitParameter(MVCConstants.MODEL_CONTEXT_PARAM, "test");
157             context.setInitParameter(MVCConstants.CONTROLLER_CONTEXT_PARAM, "test");
158
159             TestControllerParser cp = new TestControllerParser();
160             TestModelParser mp = new TestModelParser();
161             TestValidatorParser vp = new TestValidatorParser();
162             MVCRegistry.register("test", cp);
163             MVCRegistry.register("test", mp);
164             MVCRegistry.register(MVCConstants.DEFAULT_NAME, vp);
165             DefaultMVCMediator mediator = new DefaultMVCMediator();
166
167             try {
168                 mediator.mediate(request, response);
169                 assertTrue(cp.called > 0);
170                 assertTrue(mp.called > 0);
171                 assertTrue(vp.called > 0);
172             } catch (MVCException mvce) {
173                 fail("Should have used the validator default");
174             }
175         }
176     }
177
178     /**
179      * Tests the mediation order
180      */

181     public void testOrder() {
182         if (isLocal()) {
183             MockServletContext context = getContext();
184             context.setInitParameter(MVCConstants.CONTROLLER_CONTEXT_PARAM, "test");
185             context.setInitParameter(MVCConstants.MODEL_CONTEXT_PARAM, "test");
186             context.setInitParameter(MVCConstants.VALIDATOR_CONTEXT_PARAM, "test");
187         }
188
189         TestControllerParser cp = new TestControllerParser();
190         TestValidatorParser vp = new TestValidatorParser();
191         TestModelParser mp = new TestModelParser();
192
193         MVCRegistry.register("test", cp);
194         MVCRegistry.register("test", mp);
195         MVCRegistry.register("test", vp);
196         DefaultMVCMediator mediator = new DefaultMVCMediator();
197
198         try {
199             mediator.mediate(request, response);
200             assertTrue("Should have called controller parser", cp.getCalled() > 0);
201             assertTrue("Should have called model parser", mp.getCalled() > 0);
202             assertTrue("Should have called validator parser", vp.getCalled() > 0);
203             assertTrue("Should have called validator parser second", vp.getCalled() > mp.getCalled());
204             assertTrue("Should have called controller parser last ", cp.getCalled() > mp.getCalled() &&
205                 cp.getCalled() > vp.getCalled());
206         } catch (MVCException mvce) {
207             fail(mvce.toString());
208         }
209     }
210 }
Popular Tags