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.io.IOException;
23  import java.io.InputStream;
24  import java.util.Set;
25  
26  import org.apache.maven.shared.dependency.analyzer.testcases.ArrayCases;
27  import org.apache.maven.shared.dependency.analyzer.testcases.InnerClassCase;
28  import org.apache.maven.shared.dependency.analyzer.testcases.MethodHandleCases;
29  import static org.assertj.core.api.Assertions.assertThat;
30  import org.junit.Test;
31  
32  public class ResultCollectorTest
33  {
34      Set<String> getDependencies( Class<?> inspectClass )
35          throws IOException
36      {
37          String className = inspectClass.getName();
38          String path = '/' + className.replace( '.', '/' ) + ".class";
39          DependencyClassFileVisitor visitor = new DependencyClassFileVisitor();
40          try ( InputStream is = inspectClass.getResourceAsStream( path ) )
41          {
42              visitor.visitClass( className, is );
43          }
44          return visitor.getDependencies();
45      }
46  
47      @Test
48      public void testArrayCases()
49          throws IOException
50      {
51          Set<String> dependencies = getDependencies( ArrayCases.class );
52          assertThat( dependencies ).doesNotContain( "[I" );
53          assertThat( dependencies ).allSatisfy( dependency -> assertThat( dependency ).doesNotStartWith( "[" ) );
54          assertThat( dependencies )
55              .contains( "java.lang.annotation.Annotation" )
56              .contains( "java.lang.reflect.Constructor" );
57      }
58  
59      @Test
60      public void testNoMethodHandle()
61          throws IOException
62      {
63          Set<String> dependencies = getDependencies( MethodHandleCases.class );
64          for ( String dependency : dependencies )
65          {
66              assertThat( dependency ).doesNotStartWith( "(" );
67          }
68      }
69  
70      @Test
71      public void testInnerClassAsContainer()
72          throws IOException
73      {
74          Set<String> dependencies = getDependencies( InnerClassCase.class );
75          for ( String dependency : dependencies )
76          {
77              assertThat( dependency ).doesNotContain( "$" );
78          }
79          assertThat( dependencies ).contains( "java.lang.System" );
80      }
81  }