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: TestArrayModel.java,v 1.6 2002/09/12 19:09:35 ludovicc Exp $ 37 */ 38 package test.model.collection; 39 40 41 import junit.framework.TestCase; 42 import org.scopemvc.core.ModelChangeEvent; 43 import org.scopemvc.core.ModelChangeListener; 44 import org.scopemvc.model.collection.ArrayModel; 45 import test.model.basic.BasicTestModel; 46 47 /*** 48 * <P> 49 * 50 * </P> 51 * 52 * @author <A HREF="mailto:smeyfroi@users.sourceforge.net">Steve Meyfroidt</A> 53 * @created 05 September 2002 54 * @version $Revision: 1.6 $ $Date: 2002/09/12 19:09:35 $ 55 */ 56 public final class TestArrayModel extends TestCase implements ModelChangeListener { 57 58 private ArrayModel arrayModel; 59 private BasicTestModel model, submodel; 60 private Object[] array; 61 62 private boolean modelChanged; 63 private String modelChangedName; 64 65 66 /*** 67 * Constructor for the TestArrayModel object 68 * 69 * @param inName Name of the test 70 */ 71 public TestArrayModel(String inName) { 72 super(inName); 73 } 74 75 76 /*** 77 * A unit test for JUnit 78 * 79 * @throws Exception Any abnormal exception 80 */ 81 public void testArrayModelDefaultConstructor() throws Exception { 82 ArrayModel m = new ArrayModel(); 83 assertTrue(m.getSize() == 0); 84 try { 85 m.get(0); 86 } catch (NullPointerException e) { 87 // We expect this 88 try { 89 m.set(1, "test"); 90 fail("get/set on no array"); 91 } catch (NullPointerException e1) { 92 // expected 93 } 94 } 95 } 96 97 98 /*** 99 * A unit test for JUnit 100 * 101 * @throws Exception Any abnormal exception 102 */ 103 public void testArrayModelBooleanConstructor1() throws Exception { 104 ArrayModel m = new ArrayModel(false); 105 assertTrue(m.getSize() == 0); 106 try { 107 m.get(0); 108 fail("get/set on no array"); 109 } catch (NullPointerException e) { 110 // expected 111 } 112 try { 113 m.set(1, "test"); 114 fail("get/set on no array"); 115 } catch (NullPointerException e1) { 116 // expected 117 } 118 } 119 120 121 /*** 122 * A unit test for JUnit 123 * 124 * @throws Exception Any abnormal exception 125 */ 126 public void testArrayModelBooleanConstructor2() throws Exception { 127 ArrayModel m = new ArrayModel(true); 128 assertTrue(m.getSize() == 0); 129 try { 130 m.get(0); 131 fail("get/set on no array"); 132 } catch (NullPointerException e) { 133 // expected 134 } 135 try { 136 m.set(-1, "test"); 137 fail("get/set on no array"); 138 } catch (NullPointerException e1) { 139 // expected 140 } 141 } 142 143 144 /*** 145 * A unit test for JUnit 146 * 147 * @throws Exception Any abnormal exception 148 */ 149 public void testArrayModelIntConstructor() throws Exception { 150 ArrayModel m = new ArrayModel(10); 151 assertTrue(m.getSize() == 10); 152 m.set(0, "test"); 153 assertEquals("test", m.get(0)); 154 m.set(9, "test1"); 155 assertEquals("test1", m.get(9)); 156 try { 157 m.get(10); 158 fail("get/set beyond bounds of array"); 159 } catch (IndexOutOfBoundsException e) { 160 // expected 161 } 162 try { 163 m.set(-1, "test"); 164 fail("get/set beyond bounds of array"); 165 } catch (ArrayIndexOutOfBoundsException e1) { 166 // expected 167 } 168 } 169 170 171 /*** 172 * A unit test for JUnit 173 * 174 * @throws Exception Any abnormal exception 175 */ 176 public void testArrayModelBooleanIntConstructor1() throws Exception { 177 ArrayModel m = new ArrayModel(true, 10); 178 assertTrue(m.getSize() == 10); 179 m.set(0, "test"); 180 assertEquals("test", m.get(0)); 181 m.set(9, "test1"); 182 assertEquals("test1", m.get(9)); 183 try { 184 m.get(10); 185 fail("get/set beyond bounds of array"); 186 } catch (IndexOutOfBoundsException e) { 187 // expected 188 } 189 try { 190 m.set(-1, "test"); 191 fail("get/set beyond bounds of array"); 192 } catch (ArrayIndexOutOfBoundsException e1) { 193 // expected 194 } 195 } 196 197 198 /*** 199 * A unit test for JUnit 200 * 201 * @throws Exception Any abnormal exception 202 */ 203 public void testArrayModelBooleanIntConstructor2() throws Exception { 204 ArrayModel m = new ArrayModel(false, 10); 205 assertTrue(m.getSize() == 10); 206 m.set(0, "test"); 207 assertEquals(m.get(0), "test"); 208 m.set(9, "test1"); 209 assertEquals(m.get(9), "test1"); 210 try { 211 m.get(10); 212 fail("get/set beyond bounds of array"); 213 } catch (IndexOutOfBoundsException e) { 214 // expected 215 } 216 try { 217 m.set(-1, "test"); 218 fail("get/set beyond bounds of array"); 219 } catch (ArrayIndexOutOfBoundsException e1) { 220 // expected 221 } 222 } 223 224 225 /*** 226 * A unit test for JUnit 227 * 228 * @throws Exception Any abnormal exception 229 */ 230 public void testArrayModelArrayConstructor() throws Exception { 231 ArrayModel m = new ArrayModel(array); 232 assertTrue(m.getSize() == array.length); 233 for (int i = 0; i < m.getSize(); ++i) { 234 assertEquals(m.get(i), array[i]); 235 } 236 try { 237 m.get(array.length + 1); 238 } catch (IndexOutOfBoundsException e) { 239 // We expect this 240 try { 241 m.set(-1, "test"); 242 } catch (ArrayIndexOutOfBoundsException e1) { 243 // We expect this 244 return; 245 } 246 } 247 fail("get/set beyond bounds of array"); 248 } 249 250 251 /*** 252 * A unit test for JUnit 253 * 254 * @throws Exception Any abnormal exception 255 */ 256 public void testArrayModelBooleanArrayConstructor1() throws Exception { 257 ArrayModel m = new ArrayModel(true, array); 258 assertTrue(m.getSize() == array.length); 259 for (int i = 0; i < m.getSize(); ++i) { 260 assertEquals(m.get(i), array[i]); 261 } 262 try { 263 m.get(array.length + 1); 264 } catch (IndexOutOfBoundsException e) { 265 // We expect this 266 try { 267 m.set(-1, "test"); 268 } catch (ArrayIndexOutOfBoundsException e1) { 269 // We expect this 270 return; 271 } 272 } 273 fail("get/set beyond bounds of array"); 274 } 275 276 277 /*** 278 * A unit test for JUnit 279 * 280 * @throws Exception Any abnormal exception 281 */ 282 public void testArrayModelBooleanArrayConstructor2() throws Exception { 283 ArrayModel m = new ArrayModel(false, array); 284 assertTrue(m.getSize() == array.length); 285 for (int i = 0; i < m.getSize(); ++i) { 286 assertEquals(m.get(i), array[i]); 287 } 288 try { 289 m.get(array.length + 1); 290 } catch (IndexOutOfBoundsException e) { 291 // We expect this 292 try { 293 m.set(-1, "test"); 294 } catch (ArrayIndexOutOfBoundsException e1) { 295 // We expect this 296 return; 297 } 298 } 299 fail("get/set beyond bounds of array"); 300 } 301 302 303 /*** 304 * A unit test for JUnit 305 * 306 * @throws Exception Any abnormal exception 307 */ 308 public void testArrayModelSelectors() throws Exception { 309 for (int i = 0; i < arrayModel.size(); ++i) { 310 assertEquals(arrayModel.get(i), new Integer(i)); 311 } 312 for (int i = 0; i < arrayModel.size(); ++i) { 313 arrayModel.set(i, new Integer(i + 100)); 314 } 315 for (int i = 0; i < arrayModel.size(); ++i) { 316 assertEquals(arrayModel.get(i), new Integer(i + 100)); 317 } 318 arrayModel.setArray(array); 319 assertSame(arrayModel.getArray(), array); 320 } 321 322 323 /*** 324 * A unit test for JUnit 325 * 326 * @throws Exception Any abnormal exception 327 */ 328 public void testArrayModelSetArray() throws Exception { 329 Object[] o = new Object[]{ 330 new Integer(99), 331 }; 332 arrayModel.setArray(o); 333 assertTrue(arrayModel.getSize() == 1); 334 assertEquals(arrayModel.get(0), new Integer(99)); 335 } 336 337 338 /*** 339 * A unit test for JUnit 340 * 341 * @throws Exception Any abnormal exception 342 */ 343 public void testArrayModelChangeEventPropagation() throws Exception { 344 arrayModel.set(0, model); 345 arrayModel.addModelChangeListener(this); 346 347 modelChanged = false; 348 modelChangedName = ""; 349 350 submodel.setName("xxx"); 351 352 assertTrue(modelChangedName, modelChangedName.lastIndexOf("0.subModel.name") != -1); 353 } 354 355 356 /*** 357 * TODO: document the method 358 * 359 * @param inEvent TODO: Describe the Parameter 360 */ 361 public void modelChanged(ModelChangeEvent inEvent) { 362 modelChanged = true; 363 modelChangedName = "" + inEvent; 364 } 365 366 367 /*** 368 * The JUnit setup method 369 * 370 * @throws Exception Any abnormal exception 371 */ 372 protected void setUp() throws Exception { 373 arrayModel = new ArrayModel(10); 374 for (int i = 0; i < arrayModel.getSize(); ++i) { 375 arrayModel.set(i, new Integer(i)); 376 } 377 378 model = new BasicTestModel("model"); 379 submodel = new BasicTestModel("submodel"); 380 model.setSubModel(submodel); 381 382 array = new String[10]; 383 for (int i = 0; i < array.length; ++i) { 384 array[i] = "test" + i; 385 } 386 modelChanged = false; 387 modelChangedName = ""; 388 } 389 } 390

This page was automatically generated by Maven