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( null, 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( null, null );
153     }
154 
155     @Test( expected = NullPointerException.class )
156     public void getSingletonClassNull()
157         throws Exception
158     {
159         assertThat( reflector.getSingleton( Object.class, 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 unlabelled 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 unlabelled 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 unlabelled 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 unlabelled 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( null, null, 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( null, "", 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(), null, 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             private Object doSomething()
349             {
350                 return "Done";
351             }
352         }
353         assertThat( reflector.invoke( new CoT(), "doSomething", new Object[0] ), is( (Object) "Done" ) );
354     }
355 
356     @Test( expected = ReflectorException.class )
357     public void invokePackageMethod()
358         throws Exception
359     {
360         class CoT
361         {
362             Object doSomething()
363             {
364                 return "Done";
365             }
366         }
367         assertThat( reflector.invoke( new CoT(), "doSomething", new Object[0] ), is( (Object) "Done" ) );
368     }
369 
370     @Test( expected = ReflectorException.class )
371     public void invokeProtectedMethod()
372         throws Exception
373     {
374         class CoT
375         {
376             protected Object doSomething()
377             {
378                 return "Done";
379             }
380         }
381         assertThat( reflector.invoke( new CoT(), "doSomething", new Object[0] ), is( (Object) "Done" ) );
382     }
383 
384     @Test
385     public void invokePublicMethod()
386         throws Exception
387     {
388         class CoT
389         {
390             public Object doSomething()
391             {
392                 return "Done";
393             }
394         }
395         assertThat( reflector.invoke( new CoT(), "doSomething", new Object[0] ), is( (Object) "Done" ) );
396     }
397 
398     //// getStaticField( Class, String )
399 
400     @Test( expected = NullPointerException.class )
401     public void getStaticFieldNullNull()
402         throws Exception
403     {
404         reflector.getStaticField( null, null );
405     }
406 
407     @Test( expected = NullPointerException.class )
408     public void getStaticFieldNullEmpty()
409         throws Exception
410     {
411         reflector.getStaticField( null, "" );
412     }
413 
414     @Test( expected = NullPointerException.class )
415     public void getStaticFieldObjectNull()
416         throws Exception
417     {
418         reflector.getStaticField( Object.class, null );
419     }
420 
421     @Test
422     public void getStaticFieldObjectEmpty()
423         throws Exception
424     {
425         try
426         {
427             reflector.getStaticField( Object.class, "" );
428             fail();
429         }
430         catch ( ReflectorException e )
431         {
432             assertThat( e, hasCause( NoSuchFieldException.class ) );
433         }
434     }
435 
436     @Test
437     public void getStaticFieldPrivateField()
438         throws Exception
439     {
440         try
441         {
442             reflector.getStaticField( ReflectorTestHelper.class, "PRIVATE_STATIC_STRING" );
443             fail();
444         }
445         catch ( ReflectorException e )
446         {
447             assertThat( e, hasCause( NoSuchFieldException.class ) );
448         }
449     }
450 
451     @Test
452     public void getStaticFieldPackageField()
453         throws Exception
454     {
455         try
456         {
457             reflector.getStaticField( ReflectorTestHelper.class, "PACKAGE_STATIC_STRING" );
458             fail();
459         }
460         catch ( ReflectorException e )
461         {
462             assertThat( e, hasCause( NoSuchFieldException.class ) );
463         }
464     }
465 
466     @Test
467     public void getStaticFieldProtectedField()
468         throws Exception
469     {
470         try
471         {
472             reflector.getStaticField( ReflectorTestHelper.class, "PROTECTED_STATIC_STRING" );
473             fail();
474         }
475         catch ( ReflectorException e )
476         {
477             assertThat( e, hasCause( NoSuchFieldException.class ) );
478         }
479     }
480 
481     @Test
482     public void getStaticFieldPublicField()
483         throws Exception
484     {
485         assertThat( reflector.getStaticField( ReflectorTestHelper.class, "PUBLIC_STATIC_STRING" ),
486                     is( (Object) "public static string" ) );
487     }
488 
489     //// getField( Object, String )
490 
491     @Test( expected = NullPointerException.class )
492     public void getFieldNullNull()
493         throws Exception
494     {
495         reflector.getField( null, null );
496     }
497 
498     @Test( expected = NullPointerException.class )
499     public void getFieldNullEmpty()
500         throws Exception
501     {
502         reflector.getField( null, "" );
503     }
504 
505     @Test( expected = NullPointerException.class )
506     public void getFieldObjectNull()
507         throws Exception
508     {
509         reflector.getField( new Object(), null );
510     }
511 
512     @Test
513     public void getFieldObjectEmpty()
514         throws Exception
515     {
516         try
517         {
518             reflector.getField( new Object(), "" );
519             fail();
520         }
521         catch ( ReflectorException e )
522         {
523             assertThat( e, hasCause( NoSuchFieldException.class ) );
524         }
525     }
526 
527     @Test
528     public void getFieldCoTValuePrivateField()
529         throws Exception
530     {
531         final String expected = "gotIt";
532         class CoT
533         {
534             private String value = expected;
535         }
536         try
537         {
538             assertThat( reflector.getField( new CoT(), "value" ), is( (Object) expected ) );
539             fail();
540         }
541         catch ( ReflectorException e )
542         {
543             assertThat( e, hasCause( IllegalAccessException.class ) );
544         }
545     }
546 
547     @Test
548     public void getFieldCoTValuePackageField()
549         throws Exception
550     {
551         final String expected = "gotIt";
552         class CoT
553         {
554             String value = expected;
555         }
556         assertThat( reflector.getField( new CoT(), "value" ), is( (Object) expected ) );
557     }
558 
559     @Test
560     public void getFieldCoTValueProtectedField()
561         throws Exception
562     {
563         final String expected = "gotIt";
564         class CoT
565         {
566             protected String value = expected;
567         }
568         assertThat( reflector.getField( new CoT(), "value" ), is( (Object) expected ) );
569     }
570 
571     @Test
572     public void getFieldCoTValuePublicField()
573         throws Exception
574     {
575         final String expected = "gotIt";
576         class CoT
577         {
578             public String value = expected;
579         }
580         assertThat( reflector.getField( new CoT(), "value" ), is( (Object) expected ) );
581     }
582 
583     //// getField( Object, String, boolean )
584 
585     @Test( expected = NullPointerException.class )
586     public void getFieldNullNullFalse()
587         throws Exception
588     {
589         reflector.getField( null, null, false );
590     }
591 
592     @Test( expected = NullPointerException.class )
593     public void getFieldNullEmptyFalse()
594         throws Exception
595     {
596         reflector.getField( null, "", false );
597     }
598 
599     @Test( expected = NullPointerException.class )
600     public void getFieldObjectNullFalse()
601         throws Exception
602     {
603         reflector.getField( new Object(), null, false );
604     }
605 
606     @Test
607     public void getFieldObjectEmptyFalse()
608         throws Exception
609     {
610         try
611         {
612             reflector.getField( new Object(), "", false );
613             fail();
614         }
615         catch ( ReflectorException e )
616         {
617             assertThat( e, hasCause( NoSuchFieldException.class ) );
618         }
619     }
620 
621     @Test
622     public void getFieldCoTValueFalsePrivateField()
623         throws Exception
624     {
625         final String expected = "gotIt";
626         class CoT
627         {
628             private String value = expected;
629         }
630         try
631         {
632             assertThat( reflector.getField( new CoT(), "value", false ), is( (Object) expected ) );
633             fail();
634         }
635         catch ( ReflectorException e )
636         {
637             assertThat( e, hasCause( IllegalAccessException.class ) );
638         }
639     }
640 
641     @Test
642     public void getFieldCoTValueFalsePackageField()
643         throws Exception
644     {
645         final String expected = "gotIt";
646         class CoT
647         {
648             String value = expected;
649         }
650         assertThat( reflector.getField( new CoT(), "value", false ), is( (Object) expected ) );
651     }
652 
653     @Test
654     public void getFieldCoTValueFalseProtectedField()
655         throws Exception
656     {
657         final String expected = "gotIt";
658         class CoT
659         {
660             protected String value = expected;
661         }
662         assertThat( reflector.getField( new CoT(), "value", false ), is( (Object) expected ) );
663     }
664 
665     @Test
666     public void getFieldCoTValueFalsePublicField()
667         throws Exception
668     {
669         final String expected = "gotIt";
670         class CoT
671         {
672             public String value = expected;
673         }
674         assertThat( reflector.getField( new CoT(), "value", false ), is( (Object) expected ) );
675     }
676 
677     @Test( expected = NullPointerException.class )
678     public void getFieldNullNullTrue()
679         throws Exception
680     {
681         reflector.getField( null, null, true );
682     }
683 
684     @Test( expected = NullPointerException.class )
685     public void getFieldNullEmptyTrue()
686         throws Exception
687     {
688         reflector.getField( null, "", true );
689     }
690 
691     @Test( expected = NullPointerException.class )
692     public void getFieldObjectNullTrue()
693         throws Exception
694     {
695         reflector.getField( new Object(), null, true );
696     }
697 
698     @Test
699     public void getFieldObjectEmptyTrue()
700         throws Exception
701     {
702         try
703         {
704             reflector.getField( new Object(), "", true );
705             fail();
706         }
707         catch ( ReflectorException e )
708         {
709             assertThat( e, hasCause( NoSuchFieldException.class ) );
710         }
711     }
712 
713     @Test
714     public void getFieldCoTValueTruePrivateField()
715         throws Exception
716     {
717         final String expected = "gotIt";
718         class CoT
719         {
720             private String value = expected;
721         }
722         assertThat( reflector.getField( new CoT(), "value", true ), is( (Object) expected ) );
723     }
724 
725     @Test
726     public void getFieldCoTValueTruePackageField()
727         throws Exception
728     {
729         final String expected = "gotIt";
730         class CoT
731         {
732             String value = expected;
733         }
734         assertThat( reflector.getField( new CoT(), "value", true ), is( (Object) expected ) );
735     }
736 
737     @Test
738     public void getFieldCoTValueTrueProtectedField()
739         throws Exception
740     {
741         final String expected = "gotIt";
742         class CoT
743         {
744             protected String value = expected;
745         }
746         assertThat( reflector.getField( new CoT(), "value", true ), is( (Object) expected ) );
747     }
748 
749     @Test
750     public void getFieldCoTValueTruePublicField()
751         throws Exception
752     {
753         final String expected = "gotIt";
754         class CoT
755         {
756             public String value = expected;
757         }
758         assertThat( reflector.getField( new CoT(), "value", true ), is( (Object) expected ) );
759     }
760 
761     //// invokeStatic( Class, String, Object[] )
762 
763     @Test( expected = NullPointerException.class )
764     public void invokeStaticNullNullNull()
765         throws Exception
766     {
767         reflector.invokeStatic( null, null, null );
768     }
769 
770     @Test( expected = NullPointerException.class )
771     public void invokeStaticClassNullNull()
772         throws Exception
773     {
774         assertThat( reflector.invokeStatic( Object.class, null, null ), is( Object.class ) );
775     }
776 
777     @Test( expected = NullPointerException.class )
778     public void invokeStaticNullNullEmptyArray()
779         throws Exception
780     {
781         reflector.invokeStatic( null, null, new Object[0] );
782     }
783 
784     @Test( expected = NullPointerException.class )
785     public void invokeStaticClassNullEmptyArray()
786         throws Exception
787     {
788         assertThat( reflector.invokeStatic( Object.class, null, new Object[0] ), is( Object.class ) );
789     }
790 
791     @Test( expected = NullPointerException.class )
792     public void invokeStaticNullEmptyNull()
793         throws Exception
794     {
795         reflector.invokeStatic( null, "", null );
796     }
797 
798     @Test( expected = ReflectorException.class )
799     public void invokeStaticClassEmptyNull()
800         throws Exception
801     {
802         reflector.invokeStatic( Object.class, "", null );
803     }
804 
805     @Test( expected = NullPointerException.class )
806     public void invokeStaticNullEmptyEmptyArray()
807         throws Exception
808     {
809         reflector.invokeStatic( null, "", new Object[0] );
810     }
811 
812     @Test( expected = ReflectorException.class )
813     public void invokeStaticClassEmptyEmptyArray()
814         throws Exception
815     {
816         assertThat( reflector.invokeStatic( Object.class, "", new Object[0] ), is( Object.class ) );
817     }
818 
819     @Test( expected = IllegalArgumentException.class )
820     // @ReproducesPlexusBug( "Looking up methods by signature has an unlabelled continue, so finds the wrong method" )
821     public void invokeStaticClassInvalidSignature()
822         throws Exception
823     {
824         reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ this } );
825     }
826 
827     @Test( expected = ReflectorException.class )
828     public void invokeStaticPrivateMethod()
829         throws Exception
830     {
831         reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[0] );
832     }
833 
834     @Test( expected = IllegalArgumentException.class )
835     // @ReproducesPlexusBug( "Looking up methods by signature has an unlabelled continue, so finds the wrong method" )
836     public void invokeStaticPackageMethod()
837         throws Exception
838     {
839         reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ Boolean.FALSE } );
840     }
841 
842     @Test( expected = IllegalArgumentException.class )
843     // @ReproducesPlexusBug( "Looking up methods by signature has an unlabelled continue, so finds the wrong method" )
844     public void invokeStaticPackageMethodThrowsSomething()
845         throws Exception
846     {
847         reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ Boolean.TRUE } );
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 invokeStaticProtectedMethod()
853         throws Exception
854     {
855         reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ 0 } );
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 invokeStaticProtectedMethodThrowsSomething()
861         throws Exception
862     {
863         reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ 1 } );
864     }
865 
866     @Test
867     public void invokeStaticPublicMethod()
868         throws Exception
869     {
870         assertTrue( reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ "" } )
871                     instanceof ReflectorTestHelper );
872     }
873 
874     @Test( expected = NullPointerException.class )
875     public void invokeStaticPublicMethodNullValue()
876         throws Exception
877     {
878         reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ null } );
879     }
880 
881     @Test
882     public void invokeStaticPublicMethodThrowsSomething()
883         throws Exception
884     {
885         try
886         {
887             reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ "Message" } );
888             fail();
889         }
890         catch ( ReflectorException e )
891         {
892             assertThat( e, hasCause( ReflectorTestHelper.HelperException.class ) );
893         }
894     }
895 
896     @Test( expected = NullPointerException.class )
897     public void invokeStaticNonStaticMethod()
898         throws Exception
899     {
900         assertTrue(
901             reflector.invokeStatic( ReflectorTestHelper.class, "getInstance", new Object[]{ "", Boolean.FALSE } )
902             instanceof ReflectorTestHelper );
903     }
904 
905     //// getConstructor( Class, Class[] )
906 
907     @Test( expected = NullPointerException.class )
908     public void getConstructorNullNull()
909         throws Exception
910     {
911         reflector.getConstructor( null, null );
912     }
913 
914     @Test( expected = NullPointerException.class )
915     public void getConstructorNullEmpty()
916         throws Exception
917     {
918         reflector.getConstructor( null, new Class[0] );
919     }
920 
921     @Test( expected = NullPointerException.class )
922     public void getConstructorObjectNull()
923         throws Exception
924     {
925         assertThat( reflector.getConstructor( Object.class, null ),
926                     is( (Constructor) Object.class.getDeclaredConstructor() ) );
927     }
928 
929     @Test
930     public void getConstructorObjectEmpty()
931         throws Exception
932     {
933         assertThat( reflector.getConstructor( Object.class),
934                     is( (Constructor) Object.class.getDeclaredConstructor() ) );
935     }
936 
937     @Test( expected = ReflectorException.class )
938     // @ReproducesPlexusBug( "Looking up methods by signature has an unlabelled continue, so finds the wrong method" )
939     public void getConstructorPrivate()
940         throws Exception
941     {
942         assertThat( reflector.getConstructor( ReflectorTestHelper.class),
943                     is( (Constructor) ReflectorTestHelper.class.getDeclaredConstructor() ) );
944     }
945 
946     @Test
947     public void getConstructorPackage()
948         throws Exception
949     {
950         assertThat( reflector.getConstructor( ReflectorTestHelper.class, Boolean.class),
951                     not( is( (Constructor) ReflectorTestHelper.class.getDeclaredConstructor( Boolean.class ) ) ) );
952     }
953 
954     @Test
955     public void getConstructorProtected()
956         throws Exception
957     {
958         assertThat( reflector.getConstructor( ReflectorTestHelper.class, Integer.class),
959                     not( is( (Constructor) ReflectorTestHelper.class.getDeclaredConstructor( Integer.class ) ) ) );
960     }
961 
962     @Test
963     public void getConstructorPublic()
964         throws Exception
965     {
966         assertThat( reflector.getConstructor( ReflectorTestHelper.class, String.class),
967                     is( (Constructor) ReflectorTestHelper.class.getDeclaredConstructor( String.class ) ) );
968     }
969 
970     //// getObjectProperty( Object, String )
971 
972     @Test( expected = ReflectorException.class )
973     public void getObjectPropertyNullNull()
974         throws Exception
975     {
976         reflector.getObjectProperty( null, null );
977     }
978 
979     @Test( expected = ReflectorException.class )
980     public void getObjectPropertyNullEmpty()
981         throws Exception
982     {
983         reflector.getObjectProperty( null, "" );
984     }
985 
986     @Test( expected = ReflectorException.class )
987     public void getObjectPropertyObjectNull()
988         throws Exception
989     {
990         reflector.getObjectProperty( new Object(), null );
991     }
992 
993     @Test( expected = ReflectorException.class )
994     public void getObjectPropertyObjectEmpty()
995         throws Exception
996     {
997         reflector.getObjectProperty( new Object(), "" );
998     }
999 
1000     @Test
1001     // @ReproducesPlexusBug( "Should only access public properties" )
1002     public void getObjectPropertyViaPrivateField()
1003         throws Exception
1004     {
1005         class CoT
1006         {
1007             private int value = 42;
1008         }
1009         assertThat( reflector.getObjectProperty( new CoT(), "value" ), is( (Object) 42 ) );
1010     }
1011 
1012     @Test
1013     // @ReproducesPlexusBug( "Should only access public properties" )
1014     public void getObjectPropertyViaPackageField()
1015         throws Exception
1016     {
1017         class CoT
1018         {
1019             int value = 42;
1020         }
1021         assertThat( reflector.getObjectProperty( new CoT(), "value" ), is( (Object) 42 ) );
1022     }
1023 
1024     @Test
1025     // @ReproducesPlexusBug( "Should only access public properties" )
1026     public void getObjectPropertyViaProtectedField()
1027         throws Exception
1028     {
1029         class CoT
1030         {
1031             protected int value = 42;
1032         }
1033         assertThat( reflector.getObjectProperty( new CoT(), "value" ), is( (Object) 42 ) );
1034     }
1035 
1036     @Test
1037     public void getObjectPropertyViaPublicField()
1038         throws Exception
1039     {
1040         class CoT
1041         {
1042             public int value = 42;
1043         }
1044         assertThat( reflector.getObjectProperty( new CoT(), "value" ), is( (Object) 42 ) );
1045     }
1046 
1047     @Test( expected = ReflectorException.class )
1048     public void getObjectPropertyViaPrivateGetter()
1049         throws Exception
1050     {
1051         class CoT
1052         {
1053             private final int _value = 42;
1054 
1055             private int getValue()
1056             {
1057                 return _value;
1058             }
1059         }
1060         reflector.getObjectProperty( new CoT(), "value" );
1061     }
1062 
1063     @Test( expected = ReflectorException.class )
1064     public void getObjectPropertyViaPackageGetter()
1065         throws Exception
1066     {
1067         class CoT
1068         {
1069             private final int _value = 42;
1070 
1071             int getValue()
1072             {
1073                 return _value;
1074             }
1075         }
1076         reflector.getObjectProperty( new CoT(), "value" );
1077     }
1078 
1079     @Test( expected = ReflectorException.class )
1080     public void getObjectPropertyViaProtectedGetter()
1081         throws Exception
1082     {
1083         class CoT
1084         {
1085             private final int _value = 42;
1086 
1087             protected int getValue()
1088             {
1089                 return _value;
1090             }
1091         }
1092         reflector.getObjectProperty( new CoT(), "value" );
1093     }
1094 
1095     @Test
1096     public void getObjectPropertyViaPublicGetter()
1097         throws Exception
1098     {
1099         class CoT
1100         {
1101             private final int _value = 42;
1102 
1103             public int getValue()
1104             {
1105                 return _value;
1106             }
1107         }
1108         assertThat( reflector.getObjectProperty( new CoT(), "value" ), is( (Object) 42 ) );
1109     }
1110 
1111     //// getMethod( Class, String, Class[] )
1112 
1113     @Test( expected = NullPointerException.class )
1114     public void getMethodNullNullNull()
1115         throws Exception
1116     {
1117         reflector.getMethod( null, null, null );
1118     }
1119 
1120     @Test( expected = NullPointerException.class )
1121     public void getMethodNullNullEmpty()
1122         throws Exception
1123     {
1124         reflector.getMethod( null, null, new Class[0] );
1125     }
1126 
1127     @Test( expected = NullPointerException.class )
1128     public void getMethodObjectNullNull()
1129         throws Exception
1130     {
1131         reflector.getMethod( Object.class, null, null );
1132     }
1133 
1134     @Test( expected = NullPointerException.class )
1135     public void getMethodObjectNullEmpty()
1136         throws Exception
1137     {
1138         reflector.getMethod( Object.class, null, new Class[0] );
1139     }
1140 
1141     @Test( expected = NullPointerException.class )
1142     public void getMethodNullEmptyNull()
1143         throws Exception
1144     {
1145         reflector.getMethod( null, "", null );
1146     }
1147 
1148     @Test( expected = NullPointerException.class )
1149     public void getMethodNullEmptyEmpty()
1150         throws Exception
1151     {
1152         reflector.getMethod( null, "", new Class[0] );
1153     }
1154 
1155     @Test( expected = NullPointerException.class )
1156     public void getMethodObjectEmptyNull()
1157         throws Exception
1158     {
1159         reflector.getMethod( Object.class, "", null );
1160     }
1161 
1162     @Test( expected = ReflectorException.class )
1163     public void getMethodObjectEmptyEmpty()
1164         throws Exception
1165     {
1166         reflector.getMethod( Object.class, "", new Class[0] );
1167     }
1168 
1169     @Test( expected = ReflectorException.class )
1170     // @ReproducesPlexusBug( "Looking up methods by signature has an unlabelled continue, so finds the wrong method" )
1171     public void getMethodPrivate()
1172         throws Exception
1173     {
1174         assertThat( reflector.getMethod( ReflectorTestHelper.class, "getInstance", new Class[0] ),
1175                     is( ReflectorTestHelper.class.getDeclaredMethod( "getInstance" ) ) );
1176     }
1177 
1178     @Test
1179     public void getMethodPackage()
1180         throws Exception
1181     {
1182         assertThat( reflector.getMethod( ReflectorTestHelper.class, "getInstance", new Class[]{ Boolean.class } ),
1183                     not( is( ReflectorTestHelper.class.getDeclaredMethod( "getInstance", Boolean.class ) ) ) );
1184     }
1185 
1186     @Test
1187     public void getMethodProtected()
1188         throws Exception
1189     {
1190         assertThat( reflector.getMethod( ReflectorTestHelper.class, "getInstance", new Class[]{ Integer.class } ),
1191                     not( is( ReflectorTestHelper.class.getDeclaredMethod( "getInstance", Integer.class ) ) ) );
1192     }
1193 
1194     @Test
1195     public void getMethodPublic()
1196         throws Exception
1197     {
1198         assertThat( reflector.getMethod( ReflectorTestHelper.class, "getInstance", new Class[]{ String.class } ),
1199                     is( ReflectorTestHelper.class.getDeclaredMethod( "getInstance", String.class ) ) );
1200     }
1201 
1202 }