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.jar.classes;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.List;
24  
25  import org.apache.bcel.classfile.ClassFormatException;
26  import org.apache.bcel.classfile.ClassParser;
27  import org.apache.bcel.classfile.DescendingVisitor;
28  import org.apache.bcel.classfile.JavaClass;
29  import org.apache.maven.shared.jar.AbstractJarAnalyzerTestCase;
30  import org.junit.jupiter.api.Test;
31  import org.junit.jupiter.params.ParameterizedTest;
32  import org.junit.jupiter.params.provider.ValueSource;
33  
34  import static org.junit.jupiter.api.Assertions.assertNotNull;
35  import static org.junit.jupiter.api.Assertions.assertTrue;
36  
37  /**
38   * Import Visitor Test
39   */
40  class ImportVisitorTest extends AbstractJarAnalyzerTestCase {
41  
42      @Test
43      void testImportsJxr() throws ClassFormatException, IOException {
44          File jxrjar = getSampleJar("jxr.jar");
45          String classname = "org/apache/maven/jxr/DirectoryIndexer.class";
46          ClassParser classParser = new ClassParser(jxrjar.getAbsolutePath(), classname);
47          JavaClass javaClass = classParser.parse();
48  
49          ImportVisitor importVisitor = new ImportVisitor(javaClass);
50          DescendingVisitor descVisitor = new DescendingVisitor(javaClass, importVisitor);
51          javaClass.accept(descVisitor);
52  
53          List<String> imports = importVisitor.getImports();
54          assertNotNull(imports, "Import List");
55  
56          assertNotContainsRegex("Import List", "[\\[\\)\\(\\;]", imports);
57  
58          assertTrue(imports.contains("org.apache.maven.jxr.pacman.PackageType"), "imports");
59          assertTrue(imports.contains("org.apache.oro.text.perl.Perl5Util"), "imports");
60      }
61  
62      @Test
63      void testImportsAnt() throws ClassFormatException, IOException {
64          File jxrjar = getSampleJar("ant.jar");
65          String classname = "org/apache/tools/ant/Target.class";
66          ClassParser classParser = new ClassParser(jxrjar.getAbsolutePath(), classname);
67          JavaClass javaClass = classParser.parse();
68  
69          ImportVisitor importVisitor = new ImportVisitor(javaClass);
70          DescendingVisitor descVisitor = new DescendingVisitor(javaClass, importVisitor);
71          javaClass.accept(descVisitor);
72  
73          List<String> imports = importVisitor.getImports();
74          assertNotNull(imports, "Import List");
75  
76          assertNotContainsRegex("Import List", "[\\[\\)\\(\\;]", imports);
77  
78          assertTrue(imports.contains("org.apache.tools.ant.Location"), "imports");
79          assertTrue(imports.contains("org.apache.tools.ant.Task"), "imports");
80      }
81  
82      @ParameterizedTest
83      @ValueSource(
84              strings = {
85                  "helloworld-1.1.jar",
86                  "helloworld-1.2.jar",
87                  "helloworld-1.3.jar",
88                  "helloworld-1.4.jar",
89                  "helloworld-1.5.jar",
90                  "helloworld-1.6.jar",
91                  "helloworld-1.7.jar",
92                  "helloworld-1.8.jar",
93                  "helloworld-9.jar",
94                  "helloworld-10.jar",
95                  "helloworld-11.jar",
96                  "helloworld-12.jar",
97                  "helloworld-13.jar",
98                  "helloworld-14.jar",
99                  "helloworld-15.jar",
100                 "helloworld-16.jar",
101                 "helloworld-17.jar",
102                 "helloworld-18.jar",
103                 "helloworld-19.jar",
104                 "helloworld-20.jar",
105                 "helloworld-21.jar",
106                 "helloworld-22.jar",
107                 "helloworld-23.jar",
108                 "helloworld-24.jar",
109                 "helloworld-25.jar"
110             })
111     void testImportByJDKVersion(String jarName) throws IOException, ClassFormatException {
112         File jarFile = getSampleJar(jarName);
113 
114         ClassParser classParser = new ClassParser(jarFile.getAbsolutePath(), "net/test/HelloWorld.class");
115         JavaClass javaClass = classParser.parse();
116 
117         ImportVisitor importVisitor = new ImportVisitor(javaClass);
118         DescendingVisitor descVisitor = new DescendingVisitor(javaClass, importVisitor);
119         javaClass.accept(descVisitor);
120 
121         List<String> imports = importVisitor.getImports();
122         assertNotNull(imports, "Import List");
123         assertTrue(imports.size() >= 4);
124     }
125 }