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