1 /*
2 * Scope: a generic MVC framework.
3 * Copyright (c) 2000-2002, The Scope team
4 * All rights reserved.
5 *
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * Neither the name "Scope" nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
27 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
29 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 *
36 * $Id: TestController.java,v 1.4 2002/09/12 19:09:34 ludovicc Exp $
37 */
38 package test.controller.basic;
39
40 import java.util.*;
41 import junit.framework.TestCase;
42 import org.scopemvc.controller.basic.*;
43
44 import org.scopemvc.core.*;
45
46 /***
47 * <P>
48 *
49 * </P>
50 *
51 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A>
52 * @created 05 September 2002
53 * @version $Revision: 1.4 $ $Date: 2002/09/12 19:09:34 $
54 */
55 public final class TestController extends TestCase {
56
57 /***
58 * TODO: describe of the Field
59 */
60 public static boolean controlError = false;
61 /***
62 * TODO: describe of the Field
63 */
64 public static boolean doneControl = false;
65
66 private final static String CONTROL_1_ID = "CONTROL1";
67 private final static String CONTROL_2_ID = "CONTROL2";
68
69
70 /***
71 * Constructor for the TestController object
72 *
73 * @param inName Name of the test
74 */
75 public TestController(String inName) {
76 super(inName);
77 }
78
79
80 /***
81 * A unit test for JUnit
82 */
83 public void testControllerSetup() {
84
85 DummyController controller = new DummyController();
86 DummyController parentController = new DummyController();
87 Object model = new Object();
88 View view = new DummyView();
89
90 parentController.addChild(controller);
91 controller.setModel(model);
92 controller.setView(view);
93 assertEquals(parentController, controller.getParent());
94 assertEquals(model, controller.getModel());
95 assertEquals(view, controller.getView());
96 }
97
98
99 /***
100 * A unit test for JUnit
101 *
102 * @throws Exception Any abnormal exception
103 */
104 public void testControllerExecute() throws Exception {
105 DummyController parent = new DummyController();
106 DummyController child = new DummyController();
107 parent.addChild(child);
108
109 // Neither Controller is enabled to handle any Controls
110 // ... so test that the Control goes unhandled
111
112 // No control handlers should fail silently with debug output (assertion failures)
113 controlError = false;
114 doneControl = false;
115
116 child.handleControl(new Control(CONTROL_1_ID));
117 assertTrue(!doneControl);
118
119 parent.handleControl(new Control(CONTROL_1_ID));
120 assertTrue(!doneControl);
121
122 // Make child handle Control1 and execute to throw ControlException
123 controlError = false;
124 child.setupForControl1();
125 child.handleControl(new Control(CONTROL_1_ID));
126 assertTrue("Expected control exception", controlError);
127
128 // Make parent handle Control2 and execute it
129 doneControl = false;
130 parent.setupForControl2();
131 parent.handleControl(new Control(CONTROL_2_ID));
132 assertTrue("Expected control done", doneControl);
133
134 // Make parent handle Control2 and execute it
135 doneControl = false;
136 child.handleControl(new Control(CONTROL_2_ID));
137 assertTrue("Expected control done (from child)", doneControl);
138 }
139
140
141 /***
142 * A unit test for JUnit
143 */
144 public void testControllerSetView() {
145
146 Object model = new Object();
147 View view = new DummyView();
148
149 Controller controller = new DummyController();
150 controller.setModel(model);
151 controller.setView(view);
152 assertSame(view, controller.getView());
153 assertSame(model, controller.getModel());
154 assertSame(model, view.getBoundModel());
155 assertSame(controller, view.getController());
156
157 View view2 = new DummyView();
158 controller.setView(view2);
159 assertSame(view2, controller.getView());
160 assertSame(model, controller.getModel());
161 assertSame(model, view2.getBoundModel());
162 assertSame(controller, view2.getController());
163
164 assertNull(view.getController());
165 assertNull(view.getBoundModel());
166 }
167
168
169 /***
170 * A unit test for JUnit
171 *
172 * @throws Exception Any abnormal exception
173 */
174 public void testTopController() throws Exception {
175 DummyController parent = new DummyController();
176 DummyController child = new DummyController();
177 parent.addChild(child);
178
179 assertSame(parent.getTopParent(), parent);
180 assertSame(child.getTopParent(), parent);
181 }
182
183
184 /***
185 * The JUnit setup method
186 */
187 protected void setUp() {
188 ViewContext.setGlobalContext(new DummyContext());
189 }
190
191
192 class DummyController extends BasicController {
193 private boolean canDoControl1 = false;
194 private boolean canDoControl2 = false;
195
196 /***
197 * TODO: document the method
198 */
199 public void setupForControl1() {
200 canDoControl1 = true;
201 }
202
203 /***
204 * TODO: document the method
205 */
206 public void setupForControl2() {
207 canDoControl2 = true;
208 }
209
210 /***
211 * TODO: document the method
212 *
213 * @param inControl TODO: Describe the Parameter
214 * @throws ControlException Any abnormal exception
215 */
216 protected void doHandleControl(Control inControl) throws ControlException {
217 if (canDoControl1 && inControl.matchesID(CONTROL_1_ID)) {
218 doControl1();
219 } else if (canDoControl2 && inControl.matchesID(CONTROL_2_ID)) {
220 doControl2();
221 }
222 }
223
224 /***
225 * TODO: document the method
226 *
227 * @param inException TODO: Describe the Parameter
228 */
229 protected void handleControlException(ControlException inException) {
230 TestController.controlError = true;
231 }
232
233 /***
234 * TODO: document the method
235 *
236 * @throws ControlException Any abnormal exception
237 */
238 protected void doControl1() throws ControlException {
239 throw new ControlException("DUMMY_MESSAGE_ID");
240 }
241
242 /***
243 * TODO: document the method
244 */
245 protected void doControl2() {
246 TestController.doneControl = true;
247 }
248 }
249
250
251 class DummyView implements View, ModelChangeListener {
252 private Object model;
253 private Controller controller;
254
255 /***
256 * Gets the bound model
257 *
258 * @return The boundModel value
259 */
260 public Object getBoundModel() {
261 return model;
262 }
263
264 /***
265 * Gets the controller
266 *
267 * @return The controller value
268 */
269 public Controller getController() {
270 return controller;
271 }
272
273 /***
274 * TODO: document the method
275 *
276 * @param inControl TODO: Describe the Parameter
277 */
278 public void issueControl(Control inControl) { }
279
280 /***
281 * Sets the bound model
282 *
283 * @param inModel The new boundModel value
284 */
285 public void setBoundModel(Object inModel) {
286 model = inModel;
287 }
288
289 /***
290 * Sets the controller
291 *
292 * @param inController The new controller value
293 */
294 public void setController(Controller inController) {
295 controller = inController;
296 }
297
298 /***
299 * TODO: document the method
300 *
301 * @param inEvent TODO: Describe the Parameter
302 */
303 public void modelChanged(ModelChangeEvent inEvent) { }
304 }
305 }
306
This page was automatically generated by Maven