1 package org.apache.maven.shared.utils.reflection;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.junit.Test;
23
24 import java.lang.reflect.Constructor;
25
26 import static org.hamcrest.CoreMatchers.*;
27 import static org.junit.Assert.assertThat;
28 import static org.junit.Assert.fail;
29
30 import static org.apache.maven.shared.utils.testhelpers.ExceptionHelper.*;
31
32
33
34
35 public class ReflectorTest
36 {
37 private final Reflector reflector = new Reflector();
38
39
40
41 @Test( expected = NullPointerException.class )
42 public void newInstanceNullNull()
43 throws Exception
44 {
45 reflector.newInstance( null, null );
46 }
47
48 @Test
49 public void newInstanceClassNull()
50 throws Exception
51 {
52 assertThat( reflector.newInstance( Object.class, null ), is( Object.class ) );
53 }
54
55 @Test( expected = NullPointerException.class )
56 public void newInstanceNullEmptyArray()
57 throws Exception
58 {
59 reflector.newInstance( null, new Object[0] );
60 }
61
62 @Test
63 public void newInstanceClassEmptyArray()
64 throws Exception
65 {
66 assertThat( reflector.newInstance( Object.class, new Object[0] ), is( Object.class ) );
67 }
68
69 @Test( expected = ReflectorException.class )
70 public void newInstanceClassInvalidSignature()
71 throws Exception
72 {
73 reflector.newInstance( Object.class, new Object[]{ this } );
74 }
75
76 @Test( expected = ReflectorException.class )
77 public void newInstancePrivateConstructor()
78 throws Exception
79 {
80 reflector.newInstance( ReflectorTestHelper.class, new Object[0] );
81 }
82
83 @Test( expected = IllegalArgumentException.class )
84
85 public void newInstancePackageConstructor()
86 throws Exception
87 {
88 reflector.newInstance( ReflectorTestHelper.class, new Object[]{ Boolean.FALSE } );
89 }
90
91 @Test( expected = IllegalArgumentException.class )
92
93 public void newInstancePackageConstructorThrowsSomething()
94 throws Exception
95 {
96 reflector.newInstance( ReflectorTestHelper.class, new Object[]{ Boolean.TRUE } );
97 }
98
99 @Test( expected = IllegalArgumentException.class )
100
101 public void newInstanceProtectedConstructor()
102 throws Exception
103 {
104 reflector.newInstance( ReflectorTestHelper.class, new Object[]{ Integer.valueOf( 0 ) } );
105 }
106
107 @Test( expected = IllegalArgumentException.class )
108
109 public void newInstanceProtectedConstructorThrowsSomething()
110 throws Exception
111 {
112 reflector.newInstance( ReflectorTestHelper.class, new Object[]{ Integer.valueOf( 1 ) } );
113 }
114
115 @Test
116 public void newInstancePublicConstructor()
117 throws Exception
118 {
119 assertThat( reflector.newInstance( ReflectorTestHelper.class, new Object[]{ "" } ),
120 is( ReflectorTestHelper.class ) );
121 }
122
123 @Test( expected = NullPointerException.class )
124 public void newInstancePublicConstructorNullValue()
125 throws Exception
126 {
127 reflector.newInstance( ReflectorTestHelper.class, new Object[]{ null } );
128 }
129
130 @Test
131 public void newInstancePublicConstructorThrowsSomething()
132 throws Exception
133 {
134 try
135 {
136 reflector.newInstance( ReflectorTestHelper.class, new Object[]{ "Message" } );
137 fail();
138 }
139 catch ( ReflectorException e )
140 {
141 assertThat( e, hasCause( ReflectorTestHelper.HelperException.class ) );
142 }
143 }
144
145
146
147 @Test( expected = NullPointerException.class )
148 public void getSingletonNullNull()
149 throws Exception
150 {
151 reflector.getSingleton( null, null );
152 }
153
154 @Test( expected = NullPointerException.class )
155 public void getSingletonClassNull()
156 throws Exception
157 {
158 assertThat( reflector.getSingleton( Object.class, null ), is( Object.class ) );
159 }
160
161 @Test( expected = NullPointerException.class )
162 public void getSingletonNullEmptyArray()
163 throws Exception
164 {
165 reflector.getSingleton( null, new Object[0] );
166 }
167
168 @Test( expected = ReflectorException.class )
169 public void getSingletonClassEmptyArray()
170 throws Exception
171 {
172 assertThat( reflector.getSingleton( Object.class, new Object[0] ), is( Object.class ) );
173 }
174
175 @Test( expected = ReflectorException.class )
176 public void getSingletonClassInvalidSignature()
177 throws Exception
178 {
179 reflector.getSingleton( Object.class, new Object[]{ this } );
180 }
181
182 @Test( expected = ReflectorException.class )
183 public void getSingletonPrivateMethod()
184 throws Exception
185 {
186 reflector.getSingleton( ReflectorTestHelper.class, new Object[0] );
187 }
188
189 @Test( expected = IllegalArgumentException.class )
190
191 public void getSingletonPackageMethod()
192 throws Exception
193 {
194 reflector.getSingleton( ReflectorTestHelper.class, new Object[]{ Boolean.FALSE } );
195 }
196
197 @Test( expected = IllegalArgumentException.class )
198
199 public void getSingletonPackageMethodThrowsSomething()
200 throws Exception
201 {
202 reflector.getSingleton( ReflectorTestHelper.class, new Object[]{ Boolean.TRUE } );
203 }
204
205 @Test( expected = IllegalArgumentException.class )
206
207 public void getSingletonProtectedMethod()
208 throws Exception
209 {
210 reflector.getSingleton( ReflectorTestHelper.class, new Object[]{ Integer.valueOf( 0 ) } );
211 }
212
213 @Test( expected = IllegalArgumentException.class )
214
215 public void getSingletonProtectedMethodThrowsSomething()
216 throws Exception
217 {
218 reflector.getSingleton( ReflectorTestHelper.class, new Object[]{ Integer.valueOf( 1 ) } );
219 }
220
221 @Test
222 public void getSingletonPublicMethod()
223 throws Exception
224 {
225 assertThat( reflector.getSingleton( ReflectorTestHelper.class, new Object[]{ "" } ),
226 is( ReflectorTestHelper.class ) );
227 }
228
229 @Test( expected = NullPointerException.class )
230 public void getSingletonPublicMethodNullValue()
231 throws Exception
232 {
233 reflector.getSingleton( ReflectorTestHelper.class, new Object[]{ null } );
234 }
235
236 @Test
237 public void getSingletonPublicMethodThrowsSomething()
238 throws Exception
239 {
240 try
241 {
242 reflector.getSingleton( ReflectorTestHelper.class, new Object[]{ "Message" } );
243 fail();
244 }
245 catch ( ReflectorException e )
246 {
247 assertThat( e, hasCause( ReflectorTestHelper.HelperException.class ) );
248 }
249 }
250
251 @Test( expected = NullPointerException.class )
252 public void getSingletonNonStaticMethod()
253 throws Exception
254 {
255 assertThat( reflector.getSingleton( ReflectorTestHelper.class, new Object[]{ "", Boolean.FALSE } ),
256 is( ReflectorTestHelper.class ) );
257 }
258
259
260
261 @Test( expected = NullPointerException.class )
262 public void invokeNullNullNull()
263 throws Exception
264 {
265 reflector.invoke( null, null, null );
266 }
267
268 @Test( expected = NullPointerException.class )
269 public void invokeNullNullEmpty()
270 throws Exception
271 {
272 reflector.invoke( null, null, new Object[0] );
273 }
274
275 @Test( expected = NullPointerException.class )
276 public void invokeNullEmptyNull()
277 throws Exception
278 {
279 reflector.invoke( null, "", null );
280 }
281
282 @Test( expected = NullPointerException.class )
283 public void invokeNullEmptyEmpty()
284 throws Exception
285 {
286 reflector.invoke( null, "", new Object[0] );
287 }
288
289 @Test( expected = NullPointerException.class )
290 public void invokeObjectNullNull()
291 throws Exception
292 {
293 reflector.invoke( new Object(), null, null );
294 }
295
296 @Test( expected = NullPointerException.class )
297 public void invokeObjectNullEmpty()
298 throws Exception
299 {
300 reflector.invoke( new Object(), null, new Object[0] );
301 }
302
303 @Test( expected = ReflectorException.class )
304 public void invokeObjectEmptyNull()
305 throws Exception
306 {
307 reflector.invoke( new Object(), "", null );
308 }
309
310 @Test( expected = ReflectorException.class )
311 public void invokeObjectEmptyEmpty()
312 throws Exception
313 {
314 reflector.invoke( new Object(), "", new Object[0] );
315 }
316
317 @Test
318 public void invokeObjectValidNull()
319 throws Exception
320 {
321 Object object = new Object();
322 assertThat( reflector.invoke( object, "hashCode", null ), is( (Object) Integer.valueOf( object.hashCode() ) ) );
323 }
324
325 @Test
326 public void invokeObjectValidEmpty()
327 throws Exception
328 {
329 Object object = new Object();
330 assertThat( reflector.invoke( object, "hashCode", new Object[0] ),
331 is( (Object) Integer.valueOf( object.hashCode() ) ) );
332 }
333
334 @Test( expected = ReflectorException.class )
335 public void invokeObjectValidWrongSignature()
336 throws Exception
337 {
338 reflector.invoke( new Object(), "hashCode", new Object[]{ this } );
339 }
340
341 @Test( expected = ReflectorException.class )
342 public void invokePrivateMethod()
343 throws Exception
344 {
345 class CoT
346 {
347 private Object doSomething()
348 {
349 return "Done";
350 }
351 }
352 assertThat( reflector.invoke( new CoT(), "doSomething", new Object[0] ), is( (Object) "Done" ) );
353 }
354
355 @Test( expected = ReflectorException.class )
356 public void invokePackageMethod()
357 throws Exception
358 {
359 class CoT
360 {
361 Object doSomething()
362 {
363 return "Done";
364 }
365 }
366 assertThat( reflector.invoke( new CoT(), "doSomething", new Object[0] ), is( (Object) "Done" ) );
367 }
368
369 @Test( expected = ReflectorException.class )
370 public void invokeProtectedMethod()
371 throws Exception
372 {
373 class CoT
374 {
375 protected Object doSomething()
376 {
377 return "Done";
378 }
379 }
380 assertThat( reflector.invoke( new CoT(), "doSomething", new Object[0] ), is( (Object) "Done" ) );
381 }
382
383 @Test
384 public void invokePublicMethod()
385 throws Exception
386 {
387 class CoT
388 {
389 public Object doSomething()
390 {
391 return "Done";
392 }
393 }
394 assertThat( reflector.invoke( new CoT(), "doSomething", new Object[0] ), is( (Object) "Done" ) );
395 }
396
397
398
399 @Test( expected = NullPointerException.class )
400 public void getStaticFieldNullNull()
401 throws Exception
402 {
403 reflector.getStaticField( null, null );
404 }
405
406 @Test( expected = NullPointerException.class )
407 public void getStaticFieldNullEmpty()
408 throws Exception
409 {
410 reflector.getStaticField( null, "" );
411 }
412
413 @Test( expected = NullPointerException.class )
414 public void getStaticFieldObjectNull()
415 throws Exception
416 {
417 reflector.getStaticField( Object.class, null );
418 }
419
420 @Test
421 public void getStaticFieldObjectEmpty()
422 throws Exception
423 {
424 try
425 {
426 reflector.getStaticField( Object.class, "" );
427 fail();
428 }
429 catch ( ReflectorException e )
430 {
431 assertThat( e, hasCause( NoSuchFieldException.class ) );
432 }
433 }
434
435 @Test
436 public void getStaticFieldPrivateField()
437 throws Exception
438 {
439 try
440 {
441 reflector.getStaticField( ReflectorTestHelper.class, "PRIVATE_STATIC_STRING" );
442 fail();
443 }
444 catch ( ReflectorException e )
445 {
446 assertThat( e, hasCause( NoSuchFieldException.class ) );
447 }
448 }
449
450 @Test
451 public void getStaticFieldPackageField()
452 throws Exception
453 {
454 try
455 {
456 reflector.getStaticField( ReflectorTestHelper.class, "PACKAGE_STATIC_STRING" );
457 fail();
458 }
459 catch ( ReflectorException e )
460 {
461 assertThat( e, hasCause( NoSuchFieldException.class ) );
462 }
463 }
464
465 @Test
466 public void getStaticFieldProtectedField()
467 throws Exception
468 {
469 try
470 {
471 reflector.getStaticField( ReflectorTestHelper.class, "PROTECTED_STATIC_STRING" );
472 fail();
473 }
474 catch ( ReflectorException e )
475 {
476 assertThat( e, hasCause( NoSuchFieldException.class ) );
477 }
478 }
479
480 @Test
481 public void getStaticFieldPublicField()
482 throws Exception
483 {
484 assertThat( reflector.getStaticField( ReflectorTestHelper.class, "PUBLIC_STATIC_STRING" ),
485 is( (Object) "public static string" ) );
486 }
487
488
489
490 @Test( expected = NullPointerException.class )
491 public void getFieldNullNull()
492 throws Exception
493 {
494 reflector.getField( null, null );
495 }
496
497 @Test( expected = NullPointerException.class )
498 public void getFieldNullEmpty()
499 throws Exception
500 {
501 reflector.getField( null, "" );
502 }
503
504 @Test( expected = NullPointerException.class )
505 public void getFieldObjectNull()
506 throws Exception
507 {
508 reflector.getField( new Object(), null );
509 }
510
511 @Test
512 public void getFieldObjectEmpty()
513 throws Exception
514 {
515 try
516 {
517 reflector.getField( new Object(), "" );
518 fail();
519 }
520 catch ( ReflectorException e )
521 {
522 assertThat( e, hasCause( NoSuchFieldException.class ) );
523 }
524 }
525
526 @Test
527 public void getFieldCoTValuePrivateField()
528 throws Exception
529 {
530 final String expected = "gotIt";
531 class CoT
532 {
533 private String value = expected;
534 }
535 try
536 {
537 assertThat( reflector.getField( new CoT(), "value" ), is( (Object) expected ) );
538 fail();
539 }
540 catch ( ReflectorException e )
541 {
542 assertThat( e, hasCause( IllegalAccessException.class ) );
543 }
544 }
545
546 @Test
547 public void getFieldCoTValuePackageField()
548 throws Exception
549 {
550 final String expected = "gotIt";
551 class CoT
552 {
553 String value = expected;
554 }
555 assertThat( reflector.getField( new CoT(), "value" ), is( (Object) expected ) );
556 }
557
558 @Test
559 public void getFieldCoTValueProtectedField()
560 throws Exception
561 {
562 final String expected = "gotIt";
563 class CoT
564 {
565 protected String value = expected;
566 }
567 assertThat( reflector.getField( new CoT(), "value" ), is( (Object) expected ) );
568 }
569
570 @Test
571 public void getFieldCoTValuePublicField()
572 throws Exception
573 {
574 final String expected = "gotIt";
575 class CoT
576 {
577 public String value = expected;
578 }
579 assertThat( reflector.getField( new CoT(), "value" ), is( (Object) expected ) );
580 }
581
582
583
584 @Test( expected = NullPointerException.class )
585 public void getFieldNullNullFalse()
586 throws Exception
587 {
588 reflector.getField( null, null, false );
589 }
590
591 @Test( expected = NullPointerException.class )
592 public void getFieldNullEmptyFalse()
593 throws Exception
594 {
595 reflector.getField( null, "", false );
596 }
597
598 @Test( expected = NullPointerException.class )
599 public void getFieldObjectNullFalse()
600 throws Exception
601 {
602 reflector.getField( new Object(), null, false );
603 }
604
605 @Test
606 public void getFieldObjectEmptyFalse()
607 throws Exception
608 {
609 try
610 {
611 reflector.getField( new Object(), "", false );
612 fail();
613 }
614 catch ( ReflectorException e )
615 {
616 assertThat( e, hasCause( NoSuchFieldException.class ) );
617 }
618 }
619
620 @Test
621 public void getFieldCoTValueFalsePrivateField()
622 throws Exception
623 {
624 final String expected = "gotIt";
625 class CoT
626 {
627 private String value = expected;
628 }
629 try
630 {
631 assertThat( reflector.getField( new CoT(), "value", false ), is( (Object) expected ) );
632 fail();
633 }
634 catch ( ReflectorException e )
635 {
636 assertThat( e, hasCause( IllegalAccessException.class ) );
637 }
638 }
639
640 @Test
641 public void getFieldCoTValueFalsePackageField()
642 throws Exception
643 {
644 final String expected = "gotIt";
645 class CoT
646 {
647 String value = expected;
648 }
649 assertThat( reflector.getField( new CoT(), "value", false ), is( (Object) expected ) );
650 }
651
652 @Test
653 public void getFieldCoTValueFalseProtectedField()
654 throws Exception
655 {
656 final String expected = "gotIt";
657 class CoT
658 {
659 protected String value = expected;
660 }
661 assertThat( reflector.getField( new CoT(), "value", false ), is( (Object) expected ) );
662 }
663
664 @Test
665 public void getFieldCoTValueFalsePublicField()
666 throws Exception
667 {
668 final String expected = "gotIt";
669 class CoT
670 {
671 public String value = expected;
672 }
673 assertThat( reflector.getField( new CoT(), "value", false ), is( (Object) expected ) );
674 }
675
676 @Test( expected = NullPointerException.class )
677 public void getFieldNullNullTrue()
678 throws Exception
679 {
680 reflector.getField( null, null, true );
681 }
682
683 @Test( expected = NullPointerException.class )
684 public void getFieldNullEmptyTrue()
685 throws Exception
686 {
687 reflector.getField( null, "", true );
688 }
689
690 @Test( expected = NullPointerException.class )
691 public void getFieldObjectNullTrue()
692 throws Exception
693 {
694 reflector.getField( new Object(), null, true );
695 }
696
697 @Test
698 public void getFieldObjectEmptyTrue()
699 throws Exception
700 {
701 try
702 {
703 reflector.getField( new Object(), "", true );
704 fail();
705 }
706 catch ( ReflectorException e )
707 {
708 assertThat( e, hasCause( NoSuchFieldException.class ) );
709 }
710 }
711
712 @Test
713 public void getFieldCoTValueTruePrivateField()
714 throws Exception
715 {
716 final String expected = "gotIt";
717 class CoT
718 {
719 private String value = expected;
720 }
721 assertThat( reflector.getField( new CoT(), "value", true ), is( (Object) expected ) );
722 }
723
724 @Test
725 public void getFieldCoTValueTruePackageField()
726 throws Exception
727 {
728 final String expected = "gotIt";
729 class CoT
730 {
731 String value = expected;
732 }
733 assertThat( reflector.getField( new CoT(), "value", true ), is( (Object) expected ) );
734 }
735
736 @Test
737 public void getFieldCoTValueTrueProtectedField()
738 throws Exception
739 {
740 final String expected = "gotIt";
741 class CoT
742 {
743 protected String value = expected;
744 }
745 assertThat( reflector.getField( new CoT(), "value", true ), is( (Object) expected ) );
746 }
747
748 @Test
749 public void getFieldCoTValueTruePublicField()
750 throws Exception
751 {
752 final String expected = "gotIt";
753 class CoT
754 {
755 public String value = expected;
756 }
757 assertThat( reflector.getField( new CoT(), "value", true ), is( (Object) expected ) );
758 }
759
760
761
762 @Test( expected = NullPointerException.class )
763 public void invokeStaticNullNullNull()
764 throws Exception
765 {
766 reflector.invokeStatic( null, null, null );
767 }
768
769 @Test( expected = NullPointerException.class )
770 public void invokeStaticClassNullNull()
771 throws Exception
772 {
773 assertThat( reflector.invokeStatic( Object.class, null, null ), is( Object.class ) );
774 }
775
776 @Test( expected = NullPointerException.class )
777 public void invokeStaticNullNullEmptyArray()
778 throws Exception
779 {
780 reflector.invokeStatic( null, null, new Object[0] );
781 }
782
783 @Test( expected = NullPointerException.class )
784 public void invokeStaticClassNullEmptyArray()
785 throws Exception
786 {
787 assertThat( reflector.invokeStatic( Object.class, null, new Object[0] ), is( Object.class ) );
788 }
789
790 @Test( expected = NullPointerException.class )
791 public void invokeStaticNullEmptyNull()
792 throws Exception
793 {
794 reflector.invokeStatic( null, "", null );
795 }
796
797 @Test( expected = ReflectorException.class )
798 public void invokeStaticClassEmptyNull()
799 throws Exception
800 {
801 reflector.invokeStatic( Object.class, "", null );
802 }
803
804 @Test( expected = NullPointerException.class )
805 public void invokeStaticNullEmptyEmptyArray()
806 throws Exception
807 {
808 reflector.invokeStatic( null, "", new Object[0] );
809 }
810
811 @Test( expected = ReflectorException.class )
812 public void invokeStaticClassEmptyEmptyArray()
813 throws Exception
814 {
815 assertThat( reflector.invokeStatic( Object.class, "", new Object[0] ), is( Object.class ) );
816 }
817
818 @Test( expected = IllegalArgumentException.class )
819
820 public void invokeStaticClassInvalidSignature()
821 throws Exception
822 {
823 reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ this } );
824 }
825
826 @Test( expected = ReflectorException.class )
827 public void invokeStaticPrivateMethod()
828 throws Exception
829 {
830 reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[0] );
831 }
832
833 @Test( expected = IllegalArgumentException.class )
834
835 public void invokeStaticPackageMethod()
836 throws Exception
837 {
838 reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ Boolean.FALSE } );
839 }
840
841 @Test( expected = IllegalArgumentException.class )
842
843 public void invokeStaticPackageMethodThrowsSomething()
844 throws Exception
845 {
846 reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ Boolean.TRUE } );
847 }
848
849 @Test( expected = IllegalArgumentException.class )
850
851 public void invokeStaticProtectedMethod()
852 throws Exception
853 {
854 reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ Integer.valueOf( 0 ) } );
855 }
856
857 @Test( expected = IllegalArgumentException.class )
858
859 public void invokeStaticProtectedMethodThrowsSomething()
860 throws Exception
861 {
862 reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ Integer.valueOf( 1 ) } );
863 }
864
865 @Test
866 public void invokeStaticPublicMethod()
867 throws Exception
868 {
869 assertThat( reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ "" } ),
870 is( ReflectorTestHelper.class ) );
871 }
872
873 @Test( expected = NullPointerException.class )
874 public void invokeStaticPublicMethodNullValue()
875 throws Exception
876 {
877 reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ null } );
878 }
879
880 @Test
881 public void invokeStaticPublicMethodThrowsSomething()
882 throws Exception
883 {
884 try
885 {
886 reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ "Message" } );
887 fail();
888 }
889 catch ( ReflectorException e )
890 {
891 assertThat( e, hasCause( ReflectorTestHelper.HelperException.class ) );
892 }
893 }
894
895 @Test( expected = NullPointerException.class )
896 public void invokeStaticNonStaticMethod()
897 throws Exception
898 {
899 assertThat(
900 reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ "", Boolean.FALSE } ),
901 is( ReflectorTestHelper.class ) );
902 }
903
904
905
906 @Test( expected = NullPointerException.class )
907 public void getConstructorNullNull()
908 throws Exception
909 {
910 reflector.getConstructor( null, null );
911 }
912
913 @Test( expected = NullPointerException.class )
914 public void getConstructorNullEmpty()
915 throws Exception
916 {
917 reflector.getConstructor( null, new Class[0] );
918 }
919
920 @Test( expected = NullPointerException.class )
921 public void getConstructorObjectNull()
922 throws Exception
923 {
924 assertThat( reflector.getConstructor( Object.class, null ),
925 is( (Constructor) Object.class.getDeclaredConstructor() ) );
926 }
927
928 @Test
929 public void getConstructorObjectEmpty()
930 throws Exception
931 {
932 assertThat( reflector.getConstructor( Object.class),
933 is( (Constructor) Object.class.getDeclaredConstructor() ) );
934 }
935
936 @Test( expected = ReflectorException.class )
937
938 public void getConstructorPrivate()
939 throws Exception
940 {
941 assertThat( reflector.getConstructor( ReflectorTestHelper.class),
942 is( (Constructor) ReflectorTestHelper.class.getDeclaredConstructor() ) );
943 }
944
945 @Test
946 public void getConstructorPackage()
947 throws Exception
948 {
949 assertThat( reflector.getConstructor( ReflectorTestHelper.class, Boolean.class),
950 not( is( (Constructor) ReflectorTestHelper.class.getDeclaredConstructor( Boolean.class ) ) ) );
951 }
952
953 @Test
954 public void getConstructorProtected()
955 throws Exception
956 {
957 assertThat( reflector.getConstructor( ReflectorTestHelper.class, Integer.class),
958 not( is( (Constructor) ReflectorTestHelper.class.getDeclaredConstructor( Integer.class ) ) ) );
959 }
960
961 @Test
962 public void getConstructorPublic()
963 throws Exception
964 {
965 assertThat( reflector.getConstructor( ReflectorTestHelper.class, String.class),
966 is( (Constructor) ReflectorTestHelper.class.getDeclaredConstructor( String.class ) ) );
967 }
968
969
970
971 @Test( expected = ReflectorException.class )
972 public void getObjectPropertyNullNull()
973 throws Exception
974 {
975 reflector.getObjectProperty( null, null );
976 }
977
978 @Test( expected = ReflectorException.class )
979 public void getObjectPropertyNullEmpty()
980 throws Exception
981 {
982 reflector.getObjectProperty( null, "" );
983 }
984
985 @Test( expected = ReflectorException.class )
986 public void getObjectPropertyObjectNull()
987 throws Exception
988 {
989 reflector.getObjectProperty( new Object(), null );
990 }
991
992 @Test( expected = ReflectorException.class )
993 public void getObjectPropertyObjectEmpty()
994 throws Exception
995 {
996 reflector.getObjectProperty( new Object(), "" );
997 }
998
999 @Test
1000
1001 public void getObjectPropertyViaPrivateField()
1002 throws Exception
1003 {
1004 class CoT
1005 {
1006 private int value = 42;
1007 }
1008 assertThat( reflector.getObjectProperty( new CoT(), "value" ), is( (Object) 42 ) );
1009 }
1010
1011 @Test
1012
1013 public void getObjectPropertyViaPackageField()
1014 throws Exception
1015 {
1016 class CoT
1017 {
1018 int value = 42;
1019 }
1020 assertThat( reflector.getObjectProperty( new CoT(), "value" ), is( (Object) 42 ) );
1021 }
1022
1023 @Test
1024
1025 public void getObjectPropertyViaProtectedField()
1026 throws Exception
1027 {
1028 class CoT
1029 {
1030 protected int value = 42;
1031 }
1032 assertThat( reflector.getObjectProperty( new CoT(), "value" ), is( (Object) 42 ) );
1033 }
1034
1035 @Test
1036 public void getObjectPropertyViaPublicField()
1037 throws Exception
1038 {
1039 class CoT
1040 {
1041 public int value = 42;
1042 }
1043 assertThat( reflector.getObjectProperty( new CoT(), "value" ), is( (Object) 42 ) );
1044 }
1045
1046 @Test( expected = ReflectorException.class )
1047 public void getObjectPropertyViaPrivateGetter()
1048 throws Exception
1049 {
1050 class CoT
1051 {
1052 private final int _value = 42;
1053
1054 private int getValue()
1055 {
1056 return _value;
1057 }
1058 }
1059 reflector.getObjectProperty( new CoT(), "value" );
1060 }
1061
1062 @Test( expected = ReflectorException.class )
1063 public void getObjectPropertyViaPackageGetter()
1064 throws Exception
1065 {
1066 class CoT
1067 {
1068 private final int _value = 42;
1069
1070 int getValue()
1071 {
1072 return _value;
1073 }
1074 }
1075 reflector.getObjectProperty( new CoT(), "value" );
1076 }
1077
1078 @Test( expected = ReflectorException.class )
1079 public void getObjectPropertyViaProtectedGetter()
1080 throws Exception
1081 {
1082 class CoT
1083 {
1084 private final int _value = 42;
1085
1086 protected int getValue()
1087 {
1088 return _value;
1089 }
1090 }
1091 reflector.getObjectProperty( new CoT(), "value" );
1092 }
1093
1094 @Test
1095 public void getObjectPropertyViaPublicGetter()
1096 throws Exception
1097 {
1098 class CoT
1099 {
1100 private final int _value = 42;
1101
1102 public int getValue()
1103 {
1104 return _value;
1105 }
1106 }
1107 assertThat( reflector.getObjectProperty( new CoT(), "value" ), is( (Object) 42 ) );
1108 }
1109
1110
1111
1112 @Test( expected = NullPointerException.class )
1113 public void getMethodNullNullNull()
1114 throws Exception
1115 {
1116 reflector.getMethod( null, null, null );
1117 }
1118
1119 @Test( expected = NullPointerException.class )
1120 public void getMethodNullNullEmpty()
1121 throws Exception
1122 {
1123 reflector.getMethod( null, null, new Class[0] );
1124 }
1125
1126 @Test( expected = NullPointerException.class )
1127 public void getMethodObjectNullNull()
1128 throws Exception
1129 {
1130 reflector.getMethod( Object.class, null, null );
1131 }
1132
1133 @Test( expected = NullPointerException.class )
1134 public void getMethodObjectNullEmpty()
1135 throws Exception
1136 {
1137 reflector.getMethod( Object.class, null, new Class[0] );
1138 }
1139
1140 @Test( expected = NullPointerException.class )
1141 public void getMethodNullEmptyNull()
1142 throws Exception
1143 {
1144 reflector.getMethod( null, "", null );
1145 }
1146
1147 @Test( expected = NullPointerException.class )
1148 public void getMethodNullEmptyEmpty()
1149 throws Exception
1150 {
1151 reflector.getMethod( null, "", new Class[0] );
1152 }
1153
1154 @Test( expected = NullPointerException.class )
1155 public void getMethodObjectEmptyNull()
1156 throws Exception
1157 {
1158 reflector.getMethod( Object.class, "", null );
1159 }
1160
1161 @Test( expected = ReflectorException.class )
1162 public void getMethodObjectEmptyEmpty()
1163 throws Exception
1164 {
1165 reflector.getMethod( Object.class, "", new Class[0] );
1166 }
1167
1168 @Test( expected = ReflectorException.class )
1169
1170 public void getMethodPrivate()
1171 throws Exception
1172 {
1173 assertThat( reflector.getMethod( ReflectorTestHelper.class, "getInstance", new Class[0] ),
1174 is( ReflectorTestHelper.class.getDeclaredMethod( "getInstance" ) ) );
1175 }
1176
1177 @Test
1178 public void getMethodPackage()
1179 throws Exception
1180 {
1181 assertThat( reflector.getMethod( ReflectorTestHelper.class, "getInstance", new Class[]{ Boolean.class } ),
1182 not( is( ReflectorTestHelper.class.getDeclaredMethod( "getInstance", Boolean.class ) ) ) );
1183 }
1184
1185 @Test
1186 public void getMethodProtected()
1187 throws Exception
1188 {
1189 assertThat( reflector.getMethod( ReflectorTestHelper.class, "getInstance", new Class[]{ Integer.class } ),
1190 not( is( ReflectorTestHelper.class.getDeclaredMethod( "getInstance", Integer.class ) ) ) );
1191 }
1192
1193 @Test
1194 public void getMethodPublic()
1195 throws Exception
1196 {
1197 assertThat( reflector.getMethod( ReflectorTestHelper.class, "getInstance", new Class[]{ String.class } ),
1198 is( ReflectorTestHelper.class.getDeclaredMethod( "getInstance", String.class ) ) );
1199 }
1200
1201 }