View Javadoc
1   package org.apache.maven.shared.jar.classes;
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 org.apache.maven.shared.jar.AbstractJarAnalyzerTestCase;
23  import org.apache.maven.shared.jar.JarAnalyzer;
24  
25  import java.io.File;
26  
27  
28  /**
29   * JarAnalyzer Classes Test Case
30   */
31  public class JarClassesAnalyzerTest
32      extends AbstractJarAnalyzerTestCase
33  {
34      private JarClassesAnalysis analyzer;
35  
36      public void setUp()
37          throws Exception
38      {
39          super.setUp();
40  
41          analyzer = (JarClassesAnalysis) lookup( JarClassesAnalysis.class.getName() );
42      }
43  
44      public void testAnalyzeJXR()
45          throws Exception
46      {
47          JarClasses jclass = getJarClasses( "jxr.jar" );
48  
49          assertFalse( "classes.imports.length > 0", jclass.getImports().isEmpty() );
50          assertFalse( "classes.packages.length > 0", jclass.getPackages().isEmpty() );
51          assertFalse( "classes.methods.length > 0", jclass.getMethods().isEmpty() );
52  
53          assertNotContainsRegex( "Import List", "[\\[\\)\\(\\;]", jclass.getImports() );
54  
55          // TODO: test for classes, methods, etc.
56  
57          assertTrue( "classes.imports", jclass.getImports().contains( "org.apache.maven.jxr.JXR" ) );
58          assertTrue( "classes.imports", jclass.getImports().contains( "org.apache.oro.text.perl.Perl5Util" ) );
59          assertTrue( "classes.packages", jclass.getPackages().contains( "org.apache.maven.jxr.pacman" ) );
60      }
61  
62      public void testAnalyzeANT()
63          throws Exception
64      {
65          JarClasses jclass = getJarClasses( "ant.jar" );
66  
67          assertFalse( "classes.imports.length > 0", jclass.getImports().isEmpty() );
68          assertFalse( "classes.packages.length > 0", jclass.getPackages().isEmpty() );
69          assertFalse( "classes.methods.length > 0", jclass.getMethods().isEmpty() );
70  
71          assertNotContainsRegex( "Import List", "[\\[\\)\\(\\;]", jclass.getImports() );
72  
73          assertTrue( "classes.imports", jclass.getImports().contains( "java.util.zip.GZIPInputStream" ) );
74          assertTrue( "classes.imports", jclass.getImports().contains( "org.apache.tools.ant.XmlLogger$TimedElement" ) );
75          assertTrue( "classes.imports", jclass.getImports().contains( "org.apache.tools.mail.MailMessage" ) );
76          assertTrue( "classes.packages", jclass.getPackages().contains( "org.apache.tools.ant" ) );
77          assertTrue( "classes.packages", jclass.getPackages().contains( "org.apache.tools.bzip2" ) );
78      }
79  
80      public void testAnalyzeJarWithInvalidClassFile()
81          throws Exception
82      {
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      public void testAnalyzeJarWithDebug()
94          throws Exception
95      {
96          JarClasses jclass = getJarClasses( "helloworld-1.4-debug.jar" );
97  
98          assertTrue( "has debug", jclass.isDebugPresent() );
99      }
100 
101     public void testAnalyzeJarWithoutDebug()
102         throws Exception
103     {
104         JarClasses jclass = getJarClasses( "helloworld-1.4.jar" );
105 
106         assertFalse( "no debug present", jclass.isDebugPresent() );
107     }
108 
109     public void testAnalyzeJarVersion18()
110         throws Exception
111     {
112         JarClasses jclass = getJarClasses( "helloworld-1.8.jar" );
113 
114         assertEquals( "jdkrevision", "1.8", jclass.getJdkRevision() );
115     }
116 
117     public void testAnalyzeJarVersion17()
118         throws Exception
119     {
120         JarClasses jclass = getJarClasses( "helloworld-1.7.jar" );
121 
122         assertEquals( "jdkrevision", "1.7", jclass.getJdkRevision() );
123     }
124 
125     public void testAnalyzeJarVersion16()
126         throws Exception
127     {
128         JarClasses jclass = getJarClasses( "helloworld-1.6.jar" );
129 
130         assertEquals( "jdkrevision", "1.6", jclass.getJdkRevision() );
131     }
132 
133     public void testAnalyzeJarVersion15()
134         throws Exception
135     {
136         JarClasses jclass = getJarClasses( "helloworld-1.5.jar" );
137 
138         assertEquals( "jdkrevision", "1.5", jclass.getJdkRevision() );
139     }
140 
141     public void testAnalyzeJarVersion14()
142         throws Exception
143     {
144         JarClasses jclass = getJarClasses( "helloworld-1.4.jar" );
145 
146         assertEquals( "jdkrevision", "1.4", jclass.getJdkRevision() );
147     }
148 
149     public void testAnalyzeJarVersion13()
150         throws Exception
151     {
152         JarClasses jclass = getJarClasses( "helloworld-1.3.jar" );
153 
154         assertEquals( "jdkrevision", "1.3", jclass.getJdkRevision() );
155     }
156 
157     public void testAnalyzeJarVersion12()
158         throws Exception
159     {
160         JarClasses jclass = getJarClasses( "helloworld-1.2.jar" );
161 
162         assertEquals( "jdkrevision", "1.2", jclass.getJdkRevision() );
163     }
164 
165     public void testAnalyzeJarVersion11()
166         throws Exception
167     {
168         JarClasses jclass = getJarClasses( "helloworld-1.1.jar" );
169 
170         assertEquals( "jdkrevision", "1.1", jclass.getJdkRevision() );
171     }
172 
173     private JarClasses getJarClasses( String filename )
174         throws Exception
175     {
176         File file = getSampleJar( filename );
177 
178         JarClasses jclass = analyzer.analyze( new JarAnalyzer( file ) );
179         assertNotNull( "JarClasses", jclass );
180 
181         return jclass;
182     }
183 }