View Javadoc

1   package org.apache.maven.shared.dependency.analyzer.asm;
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 java.util.Arrays;
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.Set;
26  
27  import junit.framework.TestCase;
28  
29  import org.objectweb.asm.Label;
30  import org.objectweb.asm.Opcodes;
31  import org.objectweb.asm.Type;
32  
33  /**
34   * Tests <code>DependencyVisitor</code>.
35   * 
36   * @author <a href="mailto:markhobson@gmail.com">Mark Hobson</a>
37   * @version $Id: DependencyVisitorTest.java 1173378 2011-09-20 21:17:48Z hboutemy $
38   * @see DependencyVisitor
39   */
40  public class DependencyVisitorTest
41      extends TestCase
42  {
43      // TODO: finish tests
44  
45      // fields -----------------------------------------------------------------
46  
47      private DependencyVisitor visitor;
48  
49      // TestCase methods -------------------------------------------------------
50  
51      /*
52       * @see junit.framework.TestCase#setUp()
53       */
54      protected void setUp()
55          throws Exception
56      {
57          visitor = new DependencyVisitor();
58      }
59  
60      // visit tests ------------------------------------------------------------
61  
62      public void testVisitWithDefaultSuperclass()
63      {
64          // class a.b.c
65          visitor.visit( 50, 0, "a/b/c", null, "java/lang/Object", null );
66  
67          assertClasses( "java.lang.Object" );
68      }
69  
70      public void testVisitWithSuperclass()
71      {
72          // class a.b.c
73          visitor.visit( 50, 0, "a/b/c", null, "x/y/z", null );
74  
75          assertClasses( "x.y.z" );
76      }
77  
78      public void testVisitWithInterface()
79      {
80          // class a.b.c implements x.y.z
81          visitor.visit( 50, 0, "a/b/c", null, "java/lang/Object", new String[] { "x/y/z" } );
82  
83          assertClasses( "java.lang.Object", "x.y.z" );
84      }
85  
86      public void testVisitWithInterfaces()
87      {
88          // class a.b.c implements p.q.r, x.y.z
89          visitor.visit( 50, 0, "a/b/c", null, "java/lang/Object", new String[] { "p/q/r", "x/y/z" } );
90  
91          assertClasses( "java.lang.Object", "p.q.r", "x.y.z" );
92      }
93  
94      public void testVisitWithUnboundedClassTypeParameter()
95      {
96          // class a.b.c<T>
97          String signature = "<T:Ljava/lang/Object;>Ljava/lang/Object;";
98  
99          visitor.visit( 50, 0, "a/b/c", signature, "java/lang/Object", null );
100 
101         assertClasses( "java.lang.Object" );
102     }
103 
104     public void testVisitWithBoundedClassTypeParameter()
105     {
106         // class a.b.c<T extends x.y.z>
107         String signature = "<T:Lx/y/z;>Ljava/lang/Object;";
108 
109         visitor.visit( 50, 0, "a/b/c", signature, "java/lang/Object", null );
110 
111         assertClasses( "java.lang.Object", "x.y.z" );
112     }
113 
114     public void testVisitWithBoundedClassTypeParameters()
115     {
116         // class a.b.c<K extends p.q.r, V extends x.y.z>
117         String signature = "<K:Lp/q/r;V:Lx/y/z;>Ljava/lang/Object;";
118 
119         visitor.visit( 50, 0, "a/b/c", signature, "java/lang/Object", null );
120 
121         assertClasses( "java.lang.Object", "p.q.r", "x.y.z" );
122     }
123 
124     public void testVisitWithGenericInterface()
125     {
126         // class a.b.c implements p.q.r<x.y.z>
127         String signature = "Ljava/lang/Object;Lp/q/r<Lx/y/z;>;";
128 
129         visitor.visit( 50, 0, "a/b/c", signature, "java/lang/Object", new String[] { "p.q.r" } );
130 
131         assertClasses( "java.lang.Object", "p.q.r", "x.y.z" );
132     }
133 
134     public void testVisitWithInterfaceBound()
135     {
136         // class a.b.c<T> implements x.y.z<T>
137         String signature = "<T:Ljava/lang/Object;>Ljava/lang/Object;Lx/y/z<TT;>;";
138 
139         visitor.visit( 50, 0, "a/b/c", signature, "java/lang/Object", new String[] { "x.y.z" } );
140 
141         assertClasses( "java.lang.Object", "x.y.z" );
142     }
143 
144     // visitSource tests ------------------------------------------------------
145 
146     public void testVisitSource()
147     {
148         visitor.visitSource( null, null );
149 
150         assertNoClasses();
151     }
152 
153     // visitOuterClass tests --------------------------------------------------
154 
155     public void testVisitOuterClass()
156     {
157         // class a.b.c
158         // {
159         //     class ...
160         //     {
161         //     }
162         // }
163         visitor.visitOuterClass( "a/b/c", null, null );
164 
165         assertNoClasses();
166     }
167 
168     public void testVisitOuterClassInMethod()
169     {
170         // class a.b.c
171         // {
172         //     x.y.z x(p.q.r p)
173         //     {
174         //         class ...
175         //         {
176         //         }
177         //     }
178         // }
179         visitor.visitOuterClass( "a/b/c", "x", "(Lp/q/r;)Lx/y/z;" );
180 
181         assertNoClasses();
182     }
183 
184     // visitAnnotation tests --------------------------------------------------
185 
186     public void testVisitAnnotation()
187     {
188         assertVisitor( visitor.visitAnnotation( "La/b/c;", false ) );
189 
190         assertClasses( "a.b.c" );
191     }
192 
193     public void testVisitAnnotationWithRuntimeVisibility()
194     {
195         assertVisitor( visitor.visitAnnotation( "La/b/c;", true ) );
196 
197         assertClasses( "a.b.c" );
198     }
199 
200     // visitAttribute tests ---------------------------------------------------
201 
202     public void testVisitAttribute()
203     {
204         visitor.visitAttribute( new MockAttribute( "a" ) );
205 
206         assertNoClasses();
207     }
208 
209     // visitInnerClass tests --------------------------------------------------
210 
211     public void testVisitInnerClass()
212     {
213         // TODO: ensure innerName is correct
214 
215         // class a.b.c { class x.y.z { } }
216         visitor.visitInnerClass( "x/y/z", "a/b/c", "z", 0 );
217 
218         assertNoClasses();
219     }
220 
221     public void testVisitInnerClassAnonymous()
222     {
223         // class a.b.c { new class x.y.z { } }
224         visitor.visitInnerClass( "x/y/z$1", "a/b/c", null, 0 );
225 
226         assertNoClasses();
227     }
228 
229     // visitField tests -------------------------------------------------------
230 
231     public void testVisitField()
232     {
233         // a.b.c a
234         assertVisitor( visitor.visitField( 0, "a", "La/b/c;", null, null ) );
235 
236         assertClasses( "a.b.c" );
237     }
238 
239     // TODO: determine actual use of default values
240     // public void testVisitFieldWithValue()
241     // {
242     // }
243 
244     public void testVisitFieldArray()
245     {
246         // a.b.c[] a
247         assertVisitor( visitor.visitField( 0, "a", "[La/b/c;", null, null ) );
248 
249         assertClasses( "a.b.c" );
250     }
251 
252     public void testVisitFieldGeneric()
253     {
254         // a.b.c<x.y.z> a
255         assertVisitor( visitor.visitField( 0, "a", "La/b/c;", "La/b/c<Lx/y/z;>;", null ) );
256 
257         assertClasses( "a.b.c", "x.y.z" );
258     }
259 
260     // visitMethod tests ------------------------------------------------------
261 
262     public void testVisitMethod()
263     {
264         // void a()
265         assertVisitor( visitor.visitMethod( 0, "a", "()V", null, null ) );
266 
267         assertNoClasses();
268     }
269 
270     public void testVisitMethodWithPrimitiveArgument()
271     {
272         // void a(int)
273         assertVisitor( visitor.visitMethod( 0, "a", "(I)V", null, null ) );
274 
275         assertNoClasses();
276     }
277 
278     public void testVisitMethodWithPrimitiveArrayArgument()
279     {
280         // void a(int[])
281         assertVisitor( visitor.visitMethod( 0, "a", "([I)V", null, null ) );
282 
283         assertNoClasses();
284     }
285 
286     public void testVisitMethodWithObjectArgument()
287     {
288         // void a(a.b.c)
289         assertVisitor( visitor.visitMethod( 0, "a", "(La/b/c;)V", null, null ) );
290 
291         assertClasses( "a.b.c" );
292     }
293 
294     public void testVisitMethodWithObjectArguments()
295     {
296         // void a(a.b.c, x.y.z)
297         assertVisitor( visitor.visitMethod( 0, "a", "(La/b/c;Lx/y/z;)V", null, null ) );
298 
299         assertClasses( "a.b.c", "x.y.z" );
300     }
301 
302     public void testVisitMethodWithObjectArrayArgument()
303     {
304         // void a(a.b.c[])
305         assertVisitor( visitor.visitMethod( 0, "a", "([La/b/c;)V", null, null ) );
306 
307         assertClasses( "a.b.c" );
308     }
309 
310     public void testVisitMethodWithGenericArgument()
311     {
312         // void a(a.b.c<x.y.z>)
313         assertVisitor( visitor.visitMethod( 0, "a", "(La/b/c;)V", "(La/b/c<Lx/y/z;>;)V", null ) );
314 
315         assertClasses( "a.b.c", "x.y.z" );
316     }
317 
318     public void testVisitMethodWithPrimitiveReturnType()
319     {
320         // int a()
321         assertVisitor( visitor.visitMethod( 0, "a", "()I", null, null ) );
322 
323         assertNoClasses();
324     }
325 
326     public void testVisitMethodWithPrimitiveArrayReturnType()
327     {
328         // int[] a()
329         assertVisitor( visitor.visitMethod( 0, "a", "()[I", null, null ) );
330 
331         assertNoClasses();
332     }
333 
334     public void testVisitMethodWithObjectReturnType()
335     {
336         // a.b.c a()
337         assertVisitor( visitor.visitMethod( 0, "a", "()La/b/c;", null, null ) );
338 
339         assertClasses( "a.b.c" );
340     }
341 
342     public void testVisitMethodWithObjectArrayReturnType()
343     {
344         // a.b.c[] a()
345         assertVisitor( visitor.visitMethod( 0, "a", "()[La/b/c;", null, null ) );
346 
347         assertClasses( "a.b.c" );
348     }
349 
350     public void testVisitMethodWithException()
351     {
352         // void a() throws a.b.c
353         assertVisitor( visitor.visitMethod( 0, "a", "()V", null, new String[] { "a/b/c" } ) );
354 
355         assertClasses( "a.b.c" );
356     }
357 
358     public void testVisitMethodWithExceptions()
359     {
360         // void a() throws a.b.c, x.y.z
361         assertVisitor( visitor.visitMethod( 0, "a", "()V", null, new String[] { "a/b/c", "x/y/z" } ) );
362 
363         assertClasses( "a.b.c", "x.y.z" );
364     }
365 
366     // visitAnnotationDefault tests -------------------------------------------
367 
368     public void testVisitAnnotationDefault()
369     {
370         assertVisitor( visitor.visitAnnotationDefault() );
371         assertNoClasses();
372     }
373 
374     // visitParameterAnnotation tests -------------------------------------------
375 
376     public void testVisitParameterAnnotation()
377     {
378         // @a.b.c
379         assertVisitor( visitor.visitParameterAnnotation( 0, "La/b/c;", false ) );
380 
381         assertClasses( "a.b.c" );
382     }
383 
384     // visitCode tests --------------------------------------------------------
385 
386     public void testVisitCode()
387     {
388         visitor.visitCode();
389 
390         assertNoClasses();
391     }
392 
393     // visitFrame tests -------------------------------------------------------
394 
395     public void testVisitFrame()
396     {
397         visitor.visitFrame( Opcodes.F_NEW, 0, new Object[0], 0, new Object[0] );
398 
399         assertNoClasses();
400     }
401 
402     // visitInsn tests --------------------------------------------------------
403 
404     public void testVisitInsn()
405     {
406         visitor.visitInsn( Opcodes.NOP );
407 
408         assertNoClasses();
409     }
410 
411     // visitIntInsn tests -----------------------------------------------------
412 
413     public void testVisitIntInsn()
414     {
415         visitor.visitIntInsn( Opcodes.BIPUSH, 0 );
416 
417         assertNoClasses();
418     }
419 
420     // visitVarInsn tests -----------------------------------------------------
421 
422     public void testVisitVarInsn()
423     {
424         visitor.visitVarInsn( Opcodes.ILOAD, 0 );
425 
426         assertNoClasses();
427     }
428 
429     // visitTypeInsn tests ----------------------------------------------------
430 
431     public void testVisitTypeInsn()
432     {
433         visitor.visitTypeInsn( Opcodes.NEW, "a/b/c" );
434 
435         assertClasses( "a.b.c" );
436     }
437 
438     // visitFieldInsn tests ---------------------------------------------------
439 
440     public void testVisitFieldInsnWithPrimitive()
441     {
442         visitor.visitFieldInsn( Opcodes.GETFIELD, "a/b/c", "x", "I" );
443 
444         assertClasses( "a.b.c" );
445     }
446 
447     public void testVisitFieldInsnWithObject()
448     {
449         visitor.visitFieldInsn( Opcodes.GETFIELD, "a/b/c", "x", "Lx/y/z;" );
450 
451         assertClasses( "a.b.c" );
452     }
453 
454     // visitMethodInsn tests --------------------------------------------------
455 
456     public void testVisitMethodInsn()
457     {
458         visitor.visitMethodInsn( Opcodes.INVOKEVIRTUAL, "a/b/c", "x", "()V" );
459 
460         assertClasses( "a.b.c" );
461     }
462 
463     public void testVisitMethodInsnWithPrimitiveArgument()
464     {
465         visitor.visitMethodInsn( Opcodes.INVOKEVIRTUAL, "a/b/c", "x", "(I)V" );
466 
467         assertClasses( "a.b.c" );
468     }
469 
470     public void testVisitMethodInsnWithPrimitiveArrayArgument()
471     {
472         visitor.visitMethodInsn( Opcodes.INVOKEVIRTUAL, "a/b/c", "x", "([I)V" );
473 
474         assertClasses( "a.b.c" );
475     }
476 
477     public void testVisitMethodInsnWithObjectArgument()
478     {
479         visitor.visitMethodInsn( Opcodes.INVOKEVIRTUAL, "a/b/c", "x", "(Lx/y/z;)V" );
480 
481         assertClasses( "a.b.c" );
482     }
483 
484     public void testVisitMethodInsnWithObjectArguments()
485     {
486         visitor.visitMethodInsn( Opcodes.INVOKEVIRTUAL, "a/b/c", "x", "(Lp/q/r;Lx/y/z;)V" );
487 
488         assertClasses( "a.b.c" );
489     }
490 
491     public void testVisitMethodInsnWithObjectArrayArgument()
492     {
493         visitor.visitMethodInsn( Opcodes.INVOKEVIRTUAL, "a/b/c", "x", "([Lx/y/z;)V" );
494 
495         assertClasses( "a.b.c" );
496     }
497 
498     public void testVisitMethodInsnWithPrimitiveReturnType()
499     {
500         visitor.visitMethodInsn( Opcodes.INVOKEVIRTUAL, "a/b/c", "x", "()I" );
501 
502         assertClasses( "a.b.c" );
503     }
504 
505     public void testVisitMethodInsnWithPrimitiveArrayReturnType()
506     {
507         visitor.visitMethodInsn( Opcodes.INVOKEVIRTUAL, "a/b/c", "x", "()[I" );
508 
509         assertClasses( "a.b.c" );
510     }
511 
512     public void testVisitMethodInsnWithObjectReturnType()
513     {
514         visitor.visitMethodInsn( Opcodes.INVOKEVIRTUAL, "a/b/c", "x", "()Lx/y/z;" );
515 
516         assertClasses( "a.b.c" );
517     }
518 
519     public void testVisitMethodInsnWithObjectArrayReturnType()
520     {
521         visitor.visitMethodInsn( Opcodes.INVOKEVIRTUAL, "a/b/c", "x", "()[Lx/y/z;" );
522 
523         assertClasses( "a.b.c" );
524     }
525 
526     // visitJumpInsn tests ----------------------------------------------------
527 
528     public void testVisitJumpInsn()
529     {
530         visitor.visitJumpInsn( Opcodes.IFEQ, new Label() );
531 
532         assertNoClasses();
533     }
534 
535     // visitLabel tests -------------------------------------------------------
536 
537     public void testVisitLabel()
538     {
539         visitor.visitLabel( new Label() );
540 
541         assertNoClasses();
542     }
543 
544     // visitLdcInsn tests -----------------------------------------------------
545 
546     public void testVisitLdcInsnWithNonType()
547     {
548         visitor.visitLdcInsn( "a" );
549 
550         assertNoClasses();
551     }
552 
553     public void testVisitLdcInsnWithPrimitiveType()
554     {
555         visitor.visitLdcInsn( Type.INT_TYPE );
556 
557         assertNoClasses();
558     }
559 
560     public void testVisitLdcInsnWithObjectType()
561     {
562         visitor.visitLdcInsn( Type.getType( "La/b/c;" ) );
563 
564         assertClasses( "a.b.c" );
565     }
566 
567     // visitIincInsn tests ----------------------------------------------------
568 
569     public void testVisitIincInsn()
570     {
571         visitor.visitIincInsn( 0, 1 );
572 
573         assertNoClasses();
574     }
575 
576     // visitTableSwitchInsn tests ---------------------------------------------
577 
578     public void testVisitTableSwitchInsn()
579     {
580         visitor.visitTableSwitchInsn( 0, 1, new Label(), new Label[] { new Label() } );
581 
582         assertNoClasses();
583     }
584 
585     // visitLookupSwitchInsn tests --------------------------------------------
586 
587     public void testVisitLookupSwitchInsn()
588     {
589         visitor.visitLookupSwitchInsn( new Label(), new int[] { 0 }, new Label[] { new Label() } );
590 
591         assertNoClasses();
592     }
593 
594     // visitMultiANewArrayInsn tests ------------------------------------------
595 
596     public void testVisitMultiANewArrayInsnWithPrimitive()
597     {
598         visitor.visitMultiANewArrayInsn( "I", 2 );
599 
600         assertNoClasses();
601     }
602 
603     public void testVisitMultiANewArrayInsnWithObject()
604     {
605         visitor.visitMultiANewArrayInsn( "La/b/c;", 2 );
606 
607         assertClasses( "a.b.c" );
608     }
609 
610     // visitTryCatchBlock tests -----------------------------------------------
611 
612     public void testVisitTryCatchBlock()
613     {
614         visitor.visitTryCatchBlock( new Label(), new Label(), new Label(), "a/b/c" );
615 
616         assertClasses( "a.b.c" );
617     }
618 
619     public void testVisitTryCatchBlockForFinally()
620     {
621         visitor.visitTryCatchBlock( new Label(), new Label(), new Label(), null );
622 
623         assertNoClasses();
624     }
625 
626     // visitLocalVariable tests -----------------------------------------------
627 
628     public void testVisitLocalVariableWithPrimitive()
629     {
630         visitor.visitLocalVariable( "a", "I", null, new Label(), new Label(), 0 );
631 
632         assertNoClasses();
633     }
634 
635     public void testVisitLocalVariableWithPrimitiveArray()
636     {
637         visitor.visitLocalVariable( "a", "[I", null, new Label(), new Label(), 0 );
638 
639         assertNoClasses();
640     }
641 
642     public void testVisitLocalVariableWithObject()
643     {
644         visitor.visitLocalVariable( "a", "La/b/c;", null, new Label(), new Label(), 0 );
645 
646         assertClasses( "a.b.c" );
647     }
648 
649     public void testVisitLocalVariableWithObjectArray()
650     {
651         visitor.visitLocalVariable( "a", "[La/b/c;", null, new Label(), new Label(), 0 );
652 
653         assertClasses( "a.b.c" );
654     }
655 
656     public void testVisitLocalVariableWithGenericObject()
657     {
658         visitor.visitLocalVariable( "a", "La/b/c;", "La/b/c<Lx/y/z;>;", new Label(), new Label(), 0 );
659 
660         assertClasses( "a.b.c", "x.y.z" );
661     }
662 
663     public void testVisitLocalVariableWithGenericObjectArray()
664     {
665         visitor.visitLocalVariable( "a", "La/b/c;", "[La/b/c<Lx/y/z;>;", new Label(), new Label(), 0 );
666 
667         assertClasses( "a.b.c", "x.y.z" );
668     }
669 
670     // visitLineNumber tests --------------------------------------------------
671 
672     public void testVisitLineNumber()
673     {
674         visitor.visitLineNumber( 0, new Label() );
675 
676         assertNoClasses();
677     }
678 
679     // visitMaxs tests --------------------------------------------------------
680 
681     public void testVisitMaxs()
682     {
683         visitor.visitMaxs( 0, 0 );
684 
685         assertNoClasses();
686     }
687 
688     // private methods --------------------------------------------------------
689 
690     private void assertVisitor( Object actualVisitor )
691     {
692         assertEquals( visitor, actualVisitor );
693     }
694 
695     private void assertNoClasses()
696     {
697         assertClasses( Collections.<String>emptySet() );
698     }
699 
700     private void assertClasses( String element )
701     {
702         assertClasses( Collections.singleton( element ) );
703     }
704 
705     private void assertClasses( String expectedClass1, String expectedClass2 )
706     {
707         assertClasses( new String[] { expectedClass1, expectedClass2 } );
708     }
709 
710     private void assertClasses( String expectedClass1, String expectedClass2, String expectedClass3 )
711     {
712         assertClasses( new String[] { expectedClass1, expectedClass2, expectedClass3 } );
713     }
714 
715     private void assertClasses( String[] expectedClasses )
716     {
717         assertClasses( new HashSet<String>( Arrays.asList( expectedClasses ) ) );
718     }
719 
720     private void assertClasses( Set<String> expectedClasses )
721     {
722         assertEquals( expectedClasses, visitor.getClasses() );
723     }
724 }