1   package org.apache.maven.shared.jar;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.util.zip.ZipException;
6   
7   /*
8    * Licensed to the Apache Software Foundation (ASF) under one
9    * or more contributor license agreements.  See the NOTICE file
10   * distributed with this work for additional information
11   * regarding copyright ownership.  The ASF licenses this file
12   * to you under the Apache License, Version 2.0 (the
13   * "License"); you may not use this file except in compliance
14   * with the License.  You may obtain a copy of the License at
15   *
16   *   http://www.apache.org/licenses/LICENSE-2.0
17   *
18   * Unless required by applicable law or agreed to in writing,
19   * software distributed under the License is distributed on an
20   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
21   * KIND, either express or implied.  See the License for the
22   * specific language governing permissions and limitations
23   * under the License.
24   */
25  
26  /**
27   * Tests for the JarAnalyzer class.
28   */
29  public class JarAnalyzerTest
30      extends AbstractJarAnalyzerTestCase
31  {
32      private JarAnalyzer jarAnalyzer;
33  
34  
35      protected void tearDown()
36          throws Exception
37      {
38          super.tearDown();
39  
40          if ( jarAnalyzer != null )
41          {
42              jarAnalyzer.closeQuietly();
43          }
44      }
45  
46      private JarData getJarData( String filename )
47          throws Exception
48      {
49          jarAnalyzer = getJarAnalyzer( filename );
50          return jarAnalyzer.getJarData();
51      }
52  
53      private JarAnalyzer getJarAnalyzer( String filename )
54          throws IOException
55      {
56          return new JarAnalyzer( getSampleJar( filename ) );
57      }
58  
59      public void testSealed()
60          throws Exception
61      {
62          JarData jarData = getJarData( "evil-sealed-regex-1.0.jar" );
63          assertTrue( jarData.isSealed() );
64      }
65  
66      public void testNotSealed()
67          throws Exception
68      {
69          JarData jarData = getJarData( "codec.jar" );
70          assertFalse( jarData.isSealed() );
71      }
72  
73      public void testMissingFile()
74          throws Exception
75      {
76          try
77          {
78              jarAnalyzer = new JarAnalyzer( new File( "foo-bar-this-should-not-exist.jar" ) );
79              fail( "Should not have succeeded to get the missing JAR" );
80          }
81          catch ( IOException e )
82          {
83              assertTrue( true );
84          }
85      }
86  
87      public void testInvalidJarFile()
88          throws Exception
89      {
90          try
91          {
92              getJarAnalyzer( "invalid.jar" );
93              fail( "Should not have succeeded to get the invalid JAR" );
94          }
95          catch ( ZipException e )
96          {
97              assertTrue( true );
98          }
99      }
100 
101     public void testCloseTwice()
102         throws Exception
103     {
104         JarAnalyzer jarAnalyzer = getJarAnalyzer( "codec.jar" );
105 
106         // no exception should be thrown
107         jarAnalyzer.closeQuietly();
108         jarAnalyzer.closeQuietly();
109         assertTrue( true );
110     }
111 }