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