View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.shared.dependency.analyzer.asm;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.nio.file.Files;
24  import java.nio.file.Path;
25  import java.nio.file.Paths;
26  import java.util.Set;
27  
28  import org.apache.maven.shared.dependency.analyzer.testcases.ArrayCases;
29  import org.apache.maven.shared.dependency.analyzer.testcases.InnerClassCase;
30  import org.apache.maven.shared.dependency.analyzer.testcases.MethodHandleCases;
31  import org.junit.jupiter.api.Test;
32  
33  import static org.assertj.core.api.Assertions.assertThat;
34  
35  class ResultCollectorTest {
36      Set<String> getDependencies(Class<?> inspectClass) throws IOException {
37          String className = inspectClass.getName();
38          String path = '/' + className.replace('.', '/') + ".class";
39          DependencyClassFileVisitor visitor = new DependencyClassFileVisitor();
40          try (InputStream is = inspectClass.getResourceAsStream(path)) {
41              visitor.visitClass(className, is);
42          }
43          return visitor.getDependencies();
44      }
45  
46      @Test
47      void testJava11Invoke() throws IOException {
48          String className = "issue362.Bcel362";
49          Path path = Paths.get(
50                  "src/test/resources/org/apache/maven/shared/dependency/analyzer/commons-bcel-issue362/Bcel362.classx");
51          DependencyClassFileVisitor visitor = new DependencyClassFileVisitor();
52          try (InputStream is = Files.newInputStream(path)) {
53              visitor.visitClass(className, is);
54          }
55      }
56  
57      @Test
58      void testArrayCases() throws IOException {
59          Set<String> dependencies = getDependencies(ArrayCases.class);
60          assertThat(dependencies).doesNotContain("[I");
61          assertThat(dependencies).allSatisfy(dependency -> assertThat(dependency).doesNotStartWith("["));
62          assertThat(dependencies).contains("java.lang.annotation.Annotation").contains("java.lang.reflect.Constructor");
63      }
64  
65      @Test
66      void testNoMethodHandle() throws IOException {
67          Set<String> dependencies = getDependencies(MethodHandleCases.class);
68          for (String dependency : dependencies) {
69              assertThat(dependency).doesNotStartWith("(");
70          }
71      }
72  
73      @Test
74      void testInnerClassAsContainer() throws IOException {
75          Set<String> dependencies = getDependencies(InnerClassCase.class);
76          for (String dependency : dependencies) {
77              assertThat(dependency).doesNotContain("$");
78          }
79          assertThat(dependencies).contains("java.lang.System");
80      }
81  }