| 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 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 |
// no exception should be thrown |
| 94 |
Assertions.assertDoesNotThrow(() -> { |
| 95 |
jarAnalyzer.closeQuietly(); |
| 96 |
jarAnalyzer.closeQuietly(); |
| 97 |
}); |
| 98 |
} |
| 99 |
} |