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 javax.inject.Inject;
22  
23  import java.io.File;
24  
25  import org.apache.maven.shared.jar.AbstractJarAnalyzerTestCase;
26  import org.apache.maven.shared.jar.JarAnalyzer;
27  import org.codehaus.plexus.testing.PlexusTest;
28  import org.junit.jupiter.api.Test;
29  import org.junit.jupiter.params.ParameterizedTest;
30  import org.junit.jupiter.params.provider.MethodSource;
31  
32  import static org.junit.jupiter.api.Assertions.assertEquals;
33  import static org.junit.jupiter.api.Assertions.assertFalse;
34  import static org.junit.jupiter.api.Assertions.assertNotNull;
35  import static org.junit.jupiter.api.Assertions.assertNull;
36  import static org.junit.jupiter.api.Assertions.assertTrue;
37  
38  /**
39   * JarAnalyzer Classes Test Case
40   */
41  @PlexusTest
42  class JarClassesAnalyzerTest extends AbstractJarAnalyzerTestCase {
43  
44      @Inject
45      private JarClassesAnalysis analyzer;
46  
47      @Test
48      void testAnalyzeJXR() throws Exception {
49          JarClasses jclass = getJarClasses("jxr.jar");
50  
51          assertFalse(jclass.getImports().isEmpty(), "classes.imports.length > 0");
52          assertFalse(jclass.getPackages().isEmpty(), "classes.packages.length > 0");
53          assertFalse(jclass.getMethods().isEmpty(), "classes.methods.length > 0");
54  
55          assertNotContainsRegex("Import List", "[\\[\\)\\(\\;]", jclass.getImports());
56  
57          // TODO: test for classes, methods, etc.
58  
59          assertTrue(jclass.getImports().contains("org.apache.maven.jxr.JXR"), "classes.imports");
60          assertTrue(jclass.getImports().contains("org.apache.oro.text.perl.Perl5Util"), "classes.imports");
61          assertTrue(jclass.getPackages().contains("org.apache.maven.jxr.pacman"), "classes.packages");
62      }
63  
64      @Test
65      void testAnalyzeANT() throws Exception {
66          JarClasses jclass = getJarClasses("ant.jar");
67  
68          assertFalse(jclass.getImports().isEmpty(), "classes.imports.length > 0");
69          assertFalse(jclass.getPackages().isEmpty(), "classes.packages.length > 0");
70          assertFalse(jclass.getMethods().isEmpty(), "classes.methods.length > 0");
71  
72          assertNotContainsRegex("Import List", "[\\[\\)\\(\\;]", jclass.getImports());
73  
74          assertTrue(jclass.getImports().contains("java.util.zip.GZIPInputStream"), "classes.imports");
75          assertTrue(jclass.getImports().contains("org.apache.tools.ant.XmlLogger$TimedElement"), "classes.imports");
76          assertTrue(jclass.getImports().contains("org.apache.tools.mail.MailMessage"), "classes.imports");
77          assertTrue(jclass.getPackages().contains("org.apache.tools.ant"), "classes.packages");
78          assertTrue(jclass.getPackages().contains("org.apache.tools.bzip2"), "classes.packages");
79      }
80  
81      @Test
82      void testAnalyzeJarWithInvalidClassFile() throws Exception {
83          JarClasses jclass = getJarClasses("invalid-class-file.jar");
84  
85          // Doesn't fail, as exceptions are ignored.
86          assertTrue(jclass.getClassNames().isEmpty());
87          assertTrue(jclass.getPackages().isEmpty());
88          assertTrue(jclass.getImports().isEmpty());
89          assertNull(jclass.getJdkRevision());
90          assertTrue(jclass.getMethods().isEmpty());
91      }
92  
93      @Test
94      void testAnalyzeJarWithDebug() throws Exception {
95          JarClasses jclass = getJarClasses("helloworld-1.4-debug.jar");
96  
97          assertTrue(jclass.isDebugPresent(), "has debug");
98      }
99  
100     @Test
101     void testAnalyzeJarWithoutDebug() throws Exception {
102         JarClasses jclass = getJarClasses("helloworld-1.4.jar");
103 
104         assertFalse(jclass.isDebugPresent(), "no debug present");
105     }
106 
107     static String[][] testAnalyzeJarVersion() {
108         return new String[][] {
109             {"helloworld-1.1.jar", "1.1"},
110             {"helloworld-1.2.jar", "1.2"},
111             {"helloworld-1.3.jar", "1.3"},
112             {"helloworld-1.4.jar", "1.4"},
113             {"helloworld-1.5.jar", "1.5"},
114             {"helloworld-1.6.jar", "1.6"},
115             {"helloworld-1.7.jar", "1.7"},
116             {"helloworld-1.8.jar", "1.8"},
117             {"helloworld-9.jar", "9"},
118             {"helloworld-10.jar", "10"},
119             {"helloworld-11.jar", "11"},
120             {"helloworld-12.jar", "12"},
121             {"helloworld-13.jar", "13"},
122             {"helloworld-14.jar", "14"},
123             {"helloworld-15.jar", "15"},
124             {"helloworld-16.jar", "16"},
125             {"helloworld-17.jar", "17"},
126             {"helloworld-18.jar", "18"},
127             {"helloworld-19.jar", "19"},
128             {"helloworld-20.jar", "20"}
129         };
130     }
131 
132     @ParameterizedTest
133     @MethodSource
134     void testAnalyzeJarVersion(String jarName, String expectedRevision) throws Exception {
135         JarClasses jclass = getJarClasses(jarName);
136 
137         assertEquals(expectedRevision, jclass.getJdkRevision());
138     }
139 
140     private JarClasses getJarClasses(String filename) throws Exception {
141         File file = getSampleJar(filename);
142 
143         JarClasses jclass = analyzer.analyze(new JarAnalyzer(file));
144         assertNotNull(jclass, "JarClasses");
145 
146         return jclass;
147     }
148 }