View Javadoc
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: TestBasicController.java,v 1.4 2002/09/12 19:09:33 ludovicc Exp $ 37 */ 38 package test.controller.basic; 39 40 import java.util.*; 41 import junit.framework.*; 42 import org.scopemvc.controller.basic.*; 43 44 import org.scopemvc.core.*; 45 import org.scopemvc.model.basic.*; 46 47 /*** 48 * <P> 49 * 50 * ***** Need to add tests for showView(View) and hideView(View). </P> 51 * 52 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A> 53 * @created 05 September 2002 54 * @version $Revision: 1.4 $ $Date: 2002/09/12 19:09:33 $ 55 */ 56 public final class TestBasicController extends TestCase { 57 58 static ControlException controlException; 59 60 private Object model1, model2; 61 private View view1, view2; 62 private BasicController controller1, controller2; 63 private DummyContext context; 64 65 66 /*** 67 * Constructor for the TestBasicController object 68 * 69 * @param inName Name of the test 70 */ 71 public TestBasicController(String inName) { 72 super(inName); 73 } 74 75 76 /*** 77 * A unit test for JUnit 78 */ 79 public void testNewController() { 80 assertNull(controller1.getParent()); 81 assertNull(controller1.getView()); 82 assertNull(controller1.getModel()); 83 } 84 85 86 /*** 87 * A unit test for JUnit 88 */ 89 public void testHookupModelAndView() { 90 controller1.setModelAndView(model1, view1); 91 92 assertNull(controller1.getParent()); 93 assertSame(controller1.getView(), view1); 94 assertSame(controller1.getModel(), model1); 95 assertSame(view1.getBoundModel(), model1); 96 assertSame(view1.getController(), controller1); 97 } 98 99 100 /*** 101 * A unit test for JUnit 102 */ 103 public void testHookupModelFirst() { 104 // Set a model and a view for controller to hookup with setModel first 105 controller1.setModel(model1); 106 controller1.setView(view1); 107 108 assertNull(controller1.getParent()); 109 assertSame(controller1.getView(), view1); 110 assertSame(controller1.getModel(), model1); 111 assertSame(view1.getBoundModel(), model1); 112 assertSame(view1.getController(), controller1); 113 } 114 115 116 /*** 117 * A unit test for JUnit 118 */ 119 public void testHookupViewFirst() { 120 // Set a model and a view for controller to hookup with setModel first 121 controller1.setView(view1); 122 controller1.setModel(model1); 123 124 assertNull(controller1.getParent()); 125 assertSame(controller1.getView(), view1); 126 assertSame(controller1.getModel(), model1); 127 assertSame(view1.getBoundModel(), model1); 128 assertSame(view1.getController(), controller1); 129 } 130 131 132 /*** 133 * A unit test for JUnit 134 */ 135 public void testStartup() { 136 controller1.setModelAndView(model1, view1); 137 controller1.startup(); 138 assertSame(view1, context.lastShownView); 139 } 140 141 142 /*** 143 * A unit test for JUnit 144 */ 145 public void testShutdown() { 146 controller1.setModelAndView(model1, view1); 147 controller1.startup(); 148 assertSame(view1, context.lastShownView); 149 controller1.shutdown(); 150 assertSame(view1, context.lastHiddenView); 151 } 152 153 154 /*** 155 * A unit test for JUnit 156 */ 157 public void testExit() { 158 assertTrue(!context.exited); 159 controller1.addChild(controller2); 160 controller2.handleControl(new Control(BasicController.EXIT_CONTROL_ID)); 161 assertTrue(context.exited); 162 } 163 164 165 /*** 166 * A unit test for JUnit 167 * 168 * @throws Exception Any abnormal exception 169 */ 170 public void testChildController() throws Exception { 171 controller1.setModelAndView(model1, view1); 172 controller2.addChild(controller1); 173 174 assertSame(controller1.getParent(), controller2); 175 assertSame(controller1.getView(), view1); 176 assertSame(view1.getController(), controller1); 177 assertSame(model1, controller1.getModel()); 178 assertSame(model1, view1.getBoundModel()); 179 180 assertTrue(controller2.getChildren().contains(controller1)); 181 182 controller2.removeChild(controller1); 183 assertTrue(!controller2.getChildren().contains(controller1)); 184 assertTrue(controller1.getParent() == null); 185 } 186 187 188 /*** 189 * A unit test for JUnit 190 */ 191 public void testChangeModel1() { 192 controller1.setView(view1); 193 controller1.setModel(model1); 194 195 assertSame(view1, controller1.getView()); 196 assertSame(model1, controller1.getModel()); 197 198 Control control = new Control(BasicController.CHANGE_MODEL_CONTROL_ID, model2); 199 controller1.handleControl(control); 200 201 assertSame(view1, controller1.getView()); 202 assertSame(model2, controller1.getModel()); 203 } 204 205 206 /*** 207 * A unit test for JUnit 208 */ 209 public void testControlError() { 210 assertNull(controlException); 211 Control control = new Control(TestController.MAKE_EXCEPTION); 212 controller1.handleControl(control); 213 assertEquals(TestController.MAKE_EXCEPTION, controlException.getLocalizedSourceControlName()); 214 } 215 216 217 /*** 218 * The JUnit setup method 219 */ 220 protected void setUp() { 221 context = new DummyContext(); 222 ViewContext.setGlobalContext(context); 223 224 model1 = new DummyModel("Model1"); 225 model2 = new DummyModel("Model2"); 226 view1 = new DummyView("View1"); 227 view2 = new DummyView("View2"); 228 controller1 = new TestController("Controller1"); 229 controller2 = new TestController("Controller2"); 230 controlException = null; 231 } 232 233 234 // ------------------------ Inner classes ---------------------------- 235 236 class TestController extends BasicController { 237 final static String MAKE_EXCEPTION = "makeError"; 238 private String name; 239 240 /*** 241 * Constructor for the TestController object 242 * 243 * @param inName Name of the test 244 */ 245 TestController(String inName) { 246 super(); 247 name = inName; 248 } 249 250 /*** 251 * TODO: document the method 252 * 253 * @param inControl TODO: Describe the Parameter 254 * @throws ControlException Any abnormal exception 255 */ 256 protected void doHandleControl(Control inControl) throws ControlException { 257 if (inControl.matchesID(MAKE_EXCEPTION)) { 258 throw new ControlException("test"); 259 } 260 } 261 262 /*** 263 * TODO: document the method 264 * 265 * @param inException TODO: Describe the Parameter 266 */ 267 protected void handleControlException(ControlException inException) { 268 controlException = inException; 269 } 270 271 /*** 272 * Gets the name 273 * 274 * @return The name value 275 */ 276 String getName() { 277 return name; 278 } 279 } 280 281 282 class DummyModel extends BasicModel { 283 private String name; 284 285 /*** 286 * Constructor for the DummyModel object 287 * 288 * @param inName Name of the test 289 */ 290 DummyModel(String inName) { 291 name = inName; 292 } 293 294 /*** 295 * TODO: document the method 296 * 297 * @return TODO: Describe the Return Value 298 */ 299 public String toString() { 300 return getName(); 301 } 302 303 /*** 304 * Gets the name 305 * 306 * @return The name value 307 */ 308 String getName() { 309 return name; 310 } 311 } 312 313 314 class DummyView implements View, ModelChangeListener { 315 private String name; 316 private Controller controller; 317 private Object model; 318 319 /*** 320 * Constructor for the DummyView object 321 * 322 * @param inName Name of the test 323 */ 324 DummyView(String inName) { 325 name = inName; 326 } 327 328 /*** 329 * Gets the controller 330 * 331 * @return The controller value 332 */ 333 public Controller getController() { 334 return controller; 335 } 336 337 /*** 338 * Gets the bound model 339 * 340 * @return The boundModel value 341 */ 342 public Object getBoundModel() { 343 return model; 344 } 345 346 /*** 347 * TODO: document the method 348 * 349 * @param inControl TODO: Describe the Parameter 350 */ 351 public void issueControl(Control inControl) { } 352 353 /*** 354 * Sets the controller 355 * 356 * @param inController The new controller value 357 */ 358 public void setController(Controller inController) { 359 controller = inController; 360 } 361 362 /*** 363 * Sets the bound model 364 * 365 * @param inModel The new boundModel value 366 */ 367 public void setBoundModel(Object inModel) { 368 model = inModel; 369 } 370 371 /*** 372 * TODO: document the method 373 * 374 * @param inEvent TODO: Describe the Parameter 375 */ 376 public void modelChanged(ModelChangeEvent inEvent) { } 377 378 /*** 379 * TODO: document the method 380 * 381 * @return TODO: Describe the Return Value 382 */ 383 public String toString() { 384 return getName(); 385 } 386 387 /*** 388 * Gets the name 389 * 390 * @return The name value 391 */ 392 String getName() { 393 return name; 394 } 395 } 396 } 397

This page was automatically generated by Maven