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 testMultiRelease() throws Exception {
69 JarData jarData = getJarData("multi-release-test-0.0.1.jar");
70 assertTrue(jarData.isMultiRelease());
71 }
72
73 @Test
74 void testNotMultiRelease() throws Exception {
75 JarData jarData = getJarData("codec.jar");
76 assertFalse(jarData.isMultiRelease());
77 }
78
79 @Test
80 void testMissingFile() {
81 assertThrows(IOException.class, () -> new JarAnalyzer(new File("foo-bar-this-should-not-exist.jar")));
82 }
83
84 @Test
85 void testInvalidJarFile() throws Exception {
86 assertThrows(ZipException.class, () -> getJarAnalyzer("invalid.jar"));
87 }
88
89 @Test
90 void testCloseTwice() throws Exception {
91 JarAnalyzer jarAnalyzer = getJarAnalyzer("codec.jar");
92
93
94 Assertions.assertDoesNotThrow(() -> {
95 jarAnalyzer.closeQuietly();
96 jarAnalyzer.closeQuietly();
97 });
98 }
99 }