1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
82 Assertions.assertDoesNotThrow(() -> {
83 jarAnalyzer.closeQuietly();
84 jarAnalyzer.closeQuietly();
85 });
86 }
87 }