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