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;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.zip.ZipException;
24  
25  import org.junit.jupiter.api.AfterEach;
26  import org.junit.jupiter.api.Assertions;
27  import org.junit.jupiter.api.Test;
28  
29  import static org.junit.jupiter.api.Assertions.assertFalse;
30  import static org.junit.jupiter.api.Assertions.assertThrows;
31  import static org.junit.jupiter.api.Assertions.assertTrue;
32  
33  /**
34   * Tests for the JarAnalyzer class.
35   */
36  class JarAnalyzerTest extends AbstractJarAnalyzerTestCase {
37      private JarAnalyzer jarAnalyzer;
38  
39      @AfterEach
40      protected void tearDown() throws Exception {
41          if (jarAnalyzer != null) {
42              jarAnalyzer.closeQuietly();
43          }
44      }
45  
46      private JarData getJarData(String filename) throws Exception {
47          jarAnalyzer = getJarAnalyzer(filename);
48          return jarAnalyzer.getJarData();
49      }
50  
51      private JarAnalyzer getJarAnalyzer(String filename) throws IOException {
52          return new JarAnalyzer(getSampleJar(filename));
53      }
54  
55      @Test
56      void testSealed() throws Exception {
57          JarData jarData = getJarData("evil-sealed-regex-1.0.jar");
58          assertTrue(jarData.isSealed());
59      }
60  
61      @Test
62      void testNotSealed() throws Exception {
63          JarData jarData = getJarData("codec.jar");
64          assertFalse(jarData.isSealed());
65      }
66  
67      @Test
68      void testMissingFile() {
69          assertThrows(IOException.class, () -> new JarAnalyzer(new File("foo-bar-this-should-not-exist.jar")));
70      }
71  
72      @Test
73      void testInvalidJarFile() throws Exception {
74          assertThrows(ZipException.class, () -> getJarAnalyzer("invalid.jar"));
75      }
76  
77      @Test
78      void testCloseTwice() throws Exception {
79          JarAnalyzer jarAnalyzer = getJarAnalyzer("codec.jar");
80  
81          // no exception should be thrown
82          Assertions.assertDoesNotThrow(() -> {
83              jarAnalyzer.closeQuietly();
84              jarAnalyzer.closeQuietly();
85          });
86      }
87  }