View Javadoc
1   
2   package eu.simuline.testhelpers;
3   
4   import eu.simuline.testhelpers.Actions;
5   //import eu.simuline.testhelpers.Accessor;
6   //import static eu.simuline.testhelpers.Assert;
7   
8   import static org.junit.Assert.assertEquals;
9   // import static org.junit.Assert.assertTrue;
10  // import static org.junit.Assert.assertNull;
11  // import static org.junit.Assert.fail;
12  
13  import org.junit.Test;
14  import org.junit.Before;
15  import org.junit.runner.RunWith;
16  import org.junit.runners.Suite;
17  import org.junit.runners.Suite.SuiteClasses;
18  
19  /**
20   * Testclass for class <code>Accessor</code>. 
21   * This is rudimentary only. 
22   *
23   * @author <a href="mailto:e.reissner@rose.de">Ernst Reissner</a>
24   * @version 1.0
25   */
26  @RunWith(Suite.class)
27  @SuiteClasses({AccessorTest.TestAll.class})
28  public class AccessorTest {
29  
30      /* -------------------------------------------------------------------- *
31       * framework.                                                           *
32       * -------------------------------------------------------------------- */
33  
34      static AccessorTest TEST = new AccessorTest();
35  
36      public static class TestAll {
37  	@Test public void testGetField() throws Exception {
38  	    AccessorTest.TEST.testGetField();	    
39  	}
40  	@Test public void testSetField() throws Exception {
41  	    AccessorTest.TEST.testSetField();	    
42  	}
43  	@Test public void testCreate() throws Exception {
44  	    AccessorTest.TEST.testCreate();	    
45  	}
46  	@Test public void testInvoke() throws Exception {
47  	    AccessorTest.TEST.testInvoke();	    
48  	}
49  	@Test public void testGetInnerClass() {
50  	    AccessorTest.TEST.testGetInnerClass();	    
51  	}
52      } // class TestAll 
53  
54  
55  
56  
57      /* -------------------------------------------------------------------- *
58       * methods for tests.                                                   *
59       * -------------------------------------------------------------------- */
60  
61      Integer test = Integer.valueOf(0);
62  
63      class NonStatic {
64  	class NonStatic1 {
65  	}
66  
67      }
68  
69      static class Static {
70  	static class Static2 {
71  	}
72      }
73  
74      static class ForTests {
75  	private int aPrimitiveField = 3;
76  	private static int aStaticPrimitiveField = 4;
77  	private final static int aFinalField = -1;
78  	static {
79  	    System.out.println("ForTests:static");
80  	}
81  
82  	private int privateMethod(int i) {
83  	    return aPrimitiveField*i;
84  	}
85  	protected int protectedMethod(int i) {
86  	    return aPrimitiveField*i*10;
87  	}
88  	private static int privateStaticMethod(int i) {
89  	    return aStaticPrimitiveField*i;
90  	}
91  
92      }
93  
94      static class ForTestsB extends ForTests {
95  	private int aPrimitiveField = 33;
96  	private static int aStaticPrimitiveField = 43;
97  	static {
98  	    System.out.println("ForTestsB:static");
99  	}
100 
101 	private int privateMethod(int i) {
102 	    return aPrimitiveField*i;
103 	}
104 	protected int protectedMethod(int i) {
105 	    return aPrimitiveField*i*10;
106 	}
107 	private static int privateStaticMethod(int i) {
108 	    return aStaticPrimitiveField*i;
109 	}
110     }
111 
112     /**
113      * Initializes this class and all its inner classes
114      */
115     private void setUp() {
116 	try {
117 	    Class.forName("eu.simuline.testhelpers.AccessorTest");
118 	    Class.forName("eu.simuline.testhelpers.AccessorTest.Static");
119 	    Class.forName("eu.simuline.testhelpers.AccessorTest.Static.Static2");
120 	    Class.forName("eu.simuline.testhelpers.AccessorTest.ForTests");
121 	    Class.forName("eu.simuline.testhelpers.AccessorTest.ForTestsB");
122 	} catch (ClassNotFoundException e) {
123 	    throw new IllegalStateException("****");
124 	}
125     }
126 
127 
128     public void testGetField() throws Exception {
129 	Object obj;
130 
131 	// testcase 1
132 	//
133 	// non-static field 
134 	//
135 	obj = new ForTests();
136 	assertEquals(Integer.valueOf(3),
137 		     (Integer)Accessor.getField(obj,"aPrimitiveField"));
138 
139 
140 	// testcase 2
141 	//
142 	// non-static field without target 
143 	//
144 	try {
145 	     Accessor.getField((Object)null,"aPrimitiveField");
146 	} catch (IllegalArgumentException e) {
147 	    assertEquals("Specified null-target. ",e.getMessage());
148 	} // end of try-catch
149 	
150 		     
151 	// testcase 3
152 	//
153 	// static field 
154 	//
155 	assertEquals(Integer.valueOf(4),
156 		     (Integer)Accessor.getField(ForTests.class,
157 						"aStaticPrimitiveField"));
158 
159 
160 	// testcase 4
161 	//
162 	// static field without specifying a class 
163 	//
164 	try {
165 	     Accessor.getField((Class)null,"aStaticPrimitiveField");
166 	} catch (IllegalArgumentException e) {
167 	    assertEquals("Specified null-class. ",e.getMessage());
168 	} // end of try-catch
169 
170 
171 	// testcase 5
172 	//
173 	// static field 
174 	//
175 	try {
176 	     Accessor.getField(null,null,"aStaticPrimitiveField");
177 	} catch (IllegalArgumentException e) {
178 	    assertEquals("Specified null-class. ",e.getMessage());
179 	} // end of try-catch
180 
181 
182 	// testcase 6
183 	//
184 	// non-static overwritten field 
185 	//
186 	assertEquals(Integer.valueOf(3),
187 		     (Integer)Accessor.getField(ForTests.class,
188 						new ForTestsB(),
189 						"aPrimitiveField"));
190 	     
191 	// testcase 7
192 	//
193 	// non-static not overwritten field 
194 	//
195 	assertEquals(Integer.valueOf(33),
196 		     (Integer)Accessor.getField(ForTestsB.class,
197 						new ForTestsB(),
198 						"aPrimitiveField"));
199 	     
200 	// testcase 8
201 	//
202 	// static field 
203 	//
204 	assertEquals(Integer.valueOf(4),
205 		     (Integer)Accessor.getField(ForTests.class,
206 						null,
207 						"aStaticPrimitiveField"));
208 	     
209 	// testcase 9
210 	//
211 	// static field 
212 	//
213 	assertEquals(Integer.valueOf(43),
214 		     (Integer)Accessor.getField(ForTestsB.class,
215 						null,
216 						"aStaticPrimitiveField"));
217 	     
218 // java.lang.AssertionError: expected:<43> but was:<430>
219 //         at org.junit.Assert.fail(Assert.java:91)
220 //         at org.junit.Assert.failNotEquals(Assert.java:645)
221 //         at org.junit.Assert.assertEquals(Assert.java:126)
222 //         at org.junit.Assert.assertEquals(Assert.java:145)
223 //         at eu.simuline.testhelpers.AccessorTest.testGetField(AccessorTest.java:195)
224 //         at eu.simuline.testhelpers.AccessorTest$TestAll.testGetField(AccessorTest.java:41)
225 
226 	// testcase 10
227 	//
228 	// non-static field 
229 	//
230 	try {
231 	     Accessor.getField(Integer.class,null,"aNonExistingField");
232 	} catch (NoSuchFieldException e) {
233 	    assertEquals("aNonExistingField",e.getMessage());
234 	} // end of try-catch
235 
236 
237 	// testcase 11
238 	//
239 	// non-static field 
240 	//
241 	try {
242 	     Accessor.getField(ForTests.class,
243 			       new ForTests(),
244 			       "aStaticPrimitiveField");
245 	} catch (IllegalArgumentException e) {
246 	    assertEquals("The specified field 'aStaticPrimitiveField' should " + 
247 			 "not be static. ",
248 			 e.getMessage());
249 	} // end of try-catch
250 
251 
252 	// testcase 12
253 	//
254 	// non-static field 
255 	//
256 	try {
257 	     Accessor.getField(ForTests.class,
258 			       null,
259 			       "aPrimitiveField");
260 	} catch (IllegalArgumentException e) {
261 	    assertEquals("The specified field 'aPrimitiveField' should " + 
262 			 "be static. ",
263 			 e.getMessage());
264 	} // end of try-catch
265 
266     } // testGetField 
267 
268 
269     public void testSetField() throws Exception {
270 
271 	Object obj;
272 
273 	// testcase 1
274 	//
275 	// non-static field 
276 	//
277 	obj = new ForTests();
278 	Accessor.setField(obj,"aPrimitiveField",Integer.valueOf(30));
279 
280 	assertEquals(Integer.valueOf(30),
281 		     (Integer)Accessor.getField(obj,"aPrimitiveField"));
282 
283 
284 
285 	// testcase 2
286 	//
287 	// non-static field without target 
288 	//
289 	try {
290 	     Accessor.setField((Object)null,"aPrimitiveField",null);
291 	} catch (IllegalArgumentException e) {
292 	    assertEquals("Specified null-target. ",e.getMessage());
293 	} // end of try-catch
294 
295 
296 	// testcase 3
297 	//
298 	// static field 
299 	//
300 	Accessor.setField(ForTests.class,
301 			  "aStaticPrimitiveField",
302 			  Integer.valueOf(40));
303 	assertEquals(Integer.valueOf(40),
304 		     (Integer)Accessor.getField(ForTests.class,
305 						"aStaticPrimitiveField"));
306 
307 
308 	// testcase 4
309 	//
310 	// static field without specifying a class 
311 	//
312 	try {
313 	     Accessor.setField((Class)null,
314 			       "aStaticPrimitiveField",
315 			       Integer.valueOf(-1));
316 	} catch (IllegalArgumentException e) {
317 	    assertEquals("Specified null-class. ",e.getMessage());
318 	} // end of try-catch
319 
320 	// testcase 5
321 	//
322 	// static field 
323 	//
324 	try {
325 	     Accessor.setField(null,
326 			       null,
327 			       "aStaticPrimitiveField",
328 			       Integer.valueOf(-1));
329 	} catch (IllegalArgumentException e) {
330 	    assertEquals("Specified null-class. ",e.getMessage());
331 	} // end of try-catch
332 
333 
334 	// testcase 6
335 	//
336 	// non-static overwritten field 
337 	//
338 	obj = new ForTestsB();
339 	Accessor.setField(ForTests.class,
340 			  obj,
341 			  "aPrimitiveField",
342 			  Integer.valueOf(30));
343 	assertEquals(Integer.valueOf(30),
344 		     (Integer)Accessor.getField(ForTests.class,
345 						obj,
346 						"aPrimitiveField"));
347 
348 
349 	// testcase 7
350 	//
351 	// non-static not overwritten field 
352 	//
353 	obj = new ForTestsB();
354 	Accessor.setField(ForTestsB.class,
355 			  obj,
356 			  "aPrimitiveField",
357 			  Integer.valueOf(330));
358 	assertEquals(Integer.valueOf(330),
359 		     (Integer)Accessor.getField(ForTestsB.class,
360 						obj,
361 						"aPrimitiveField"));
362 
363 	// testcase 8
364 	//
365 	// static field 
366 	//
367 	Accessor.setField(ForTests.class,
368 			  null,
369 			  "aStaticPrimitiveField",
370 			  Integer.valueOf(40));
371 	assertEquals(Integer.valueOf(40),
372 		     (Integer)Accessor.getField(ForTests.class,
373 						null,
374 						"aStaticPrimitiveField"));
375 
376 
377 	// testcase 9
378 	//
379 	// static field 
380 	//
381 	Accessor.setField(ForTestsB.class,
382 			  null,
383 			  "aStaticPrimitiveField",
384 			  Integer.valueOf(430));
385 	assertEquals(Integer.valueOf(430),
386 		     (Integer)Accessor.getField(ForTestsB.class,
387 						null,
388 						"aStaticPrimitiveField"));
389 
390 	// testcase 10
391 	//
392 	// non-static field 
393 	//
394 	try {
395 	     Accessor.setField(Integer.class,
396 			       null,
397 			       "aNonExistingField",
398 			       Integer.valueOf(-1));
399 	} catch (NoSuchFieldException e) {
400 	    assertEquals("aNonExistingField",e.getMessage());
401 	} // end of try-catch
402 
403 
404 	// testcase 11
405 	//
406 	// non-static field 
407 	//
408 	try {
409 	     Accessor.setField(ForTests.class,
410 			       new ForTests(),
411 			       "aStaticPrimitiveField",
412 			       Integer.valueOf(-1));
413 	} catch (IllegalArgumentException e) {
414 	    assertEquals("The specified field 'aStaticPrimitiveField' should " + 
415 			 "not be static. ",
416 			 e.getMessage());
417 	} // end of try-catch
418 
419 
420 	// testcase 12
421 	//
422 	// non-static field 
423 	//
424 	try {
425 	     Accessor.setField(ForTests.class,
426 			       null,
427 			       "aPrimitiveField",
428 			       Integer.valueOf(-1));
429 	} catch (IllegalArgumentException e) {
430 	    assertEquals("The specified field 'aPrimitiveField' should " + 
431 			 "be static. ",
432 			 e.getMessage());
433 	} // end of try-catch
434 
435 
436 	// testcase 12
437 	//
438 	// final field 
439 	//
440 	try {
441 	     Accessor.setField(ForTests.class,
442 			       "aFinalField",
443 			       Integer.valueOf(-1));
444 	} catch (IllegalArgumentException e) {
445 	    assertEquals("Field 'aFinalField' in class '" + 
446 			 ForTests.class.getName() + 
447 			 "' is declared final and is hence not accessible. ",
448 			 e.getMessage());
449 	} // end of try-catch
450 
451 
452 	// testcase 13
453 	//
454 	// primitive field tried to assign a null value. 
455 	//
456 	try {
457 	    Accessor.setField(ForTests.class,
458 			      "aStaticPrimitiveField",
459 			      null);
460 	} catch (IllegalArgumentException e) {
461 	    assertEquals("Tried to assign null-value " + 
462 			 "to field 'aStaticPrimitiveField'" +
463 			 " in class '" + ForTests.class.getName() + 
464 			 "' although its type '" + Integer.TYPE + 
465 			 "' is primitive. ",
466 			 e.getMessage());
467 	} // end of try-catch
468 
469     } // testSetField 
470 
471     public void testCreate() throws Exception {
472 	ForTests ft;
473 
474 	// testcase 1
475 	//
476 	// default constructor 
477 	//
478 	ft = Accessor.create(ForTests.class);
479 
480 	// testcase 2
481 	//
482 	// default constructor 
483 	//
484 	ft = Accessor.create(ForTests.class,
485 			     new Class[] {},
486 			     new Object[] {});
487     } // testCreate 
488 
489     public void testInvoke() throws Exception {
490 	int result;
491 
492 
493 	// testcase 1
494 	//
495 	//
496 	//
497 	result = ((Integer)Accessor.invoke(new ForTestsB(),
498 					   "privateMethod",
499 					   Integer.valueOf(2))
500 		      ).intValue();
501 	assertEquals(66,result);
502 
503 
504 	// testcase 2
505 	//
506 	//
507 	//
508 	result = ((Integer)Accessor.invoke(new ForTests(),
509 					   "privateMethod",
510 					   Integer.valueOf(2))
511 		      ).intValue();
512 	assertEquals(6,result);
513 
514 
515 	// testcase 3
516 	//
517 	//
518 	//
519 	result = ((Integer)Accessor.invoke(new ForTestsB(),
520 					   "protectedMethod",
521 					   Integer.valueOf(2))
522 		      ).intValue();
523 	assertEquals(660,result);
524 
525 
526 	// testcase 4
527 	//
528 	//
529 	//
530 	result = ((Integer)Accessor.invoke(new ForTests(),
531 					   "protectedMethod",
532 					   Integer.valueOf(2))
533 		      ).intValue();
534 	assertEquals(60,result);
535 
536 
537 	// testcase 5
538 	//
539 	//
540 	//
541 	Accessor.setField(ForTests.class,
542 			  "aStaticPrimitiveField",
543 			  Integer.valueOf(4));
544 	result = ((Integer)Accessor.invokeStatic(ForTests.class,
545 						 "privateStaticMethod",
546 						 Integer.valueOf(2))
547 		      ).intValue();
548 	assertEquals(8,result);
549 
550     } // testInvoke 
551 
552     public void testGetInnerClass() {
553 
554 	 // testcase 1
555 	 //
556 	 // get inner static class 
557 	 //
558 	 try {
559 	     Accessor.getInnerClass(null,"ForTests");
560 	 } catch (IllegalArgumentException e) {
561 	     assertEquals("Specified null-class. ",e.getMessage());
562 	 } // end of try-catch
563 		      
564 
565 	 // testcase 2
566 	 //
567 	 // get inner static class 
568 	 //
569 	 try {
570 	     Accessor.getInnerClass(AccessorTest.class,(String)null);
571 	 } catch (IllegalArgumentException e) {
572 	     assertEquals("Specified null-class-name. ",e.getMessage());
573 	 } // end of try-catch
574 		      
575 
576 	 // testcase 3
577 	 //
578 	 // get inner static class 
579 	 //
580 	 try {
581 	     Accessor.getInnerClass(AccessorTest.class,"noInnerClass");
582 	 } catch (IllegalArgumentException e) {
583 	     assertEquals("Class '" + AccessorTest.class.getName() + 
584 			  "' has no inner class named '" + "noInnerClass" + "'. ",
585 			  e.getMessage());
586 	 } // end of try-catch
587 
588 
589 	 // testcase 4
590 	 //
591 	 // get inner static class 
592 	 //
593 	 assertEquals(ForTests.class,
594 		      Accessor.getInnerClass(AccessorTest.class,
595 					     "ForTests"));
596     } // testGetInnerClass 
597 
598    /* -------------------------------------------------------------------- *
599      * framework.                                                           *
600      * -------------------------------------------------------------------- */
601 
602 
603     /**
604      * Runs the test case.
605      *
606      * Uncomment either the textual UI, Swing UI, or AWT UI.
607      */
608     public static void main(String args[]) {
609 	Actions.runFromMain();
610     }
611 
612 }