This is an automated email from the ASF dual-hosted git repository.
ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-compress.git
The following commit(s) were added to refs/heads/master by this push:
new bd37482a Use try-with-resources
bd37482a is described below
commit bd37482a0c058020d987e795f4fced5082eb5e14
Author: Gary Gregory <[email protected]>
AuthorDate: Sat Nov 4 06:51:29 2023 -0400
Use try-with-resources
Use JUnit API
---
.../compress/archivers/zip/UTF8ZipFilesTest.java | 117 ++++++---------------
1 file changed, 33 insertions(+), 84 deletions(-)
diff --git
a/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
b/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
index 0eb9467b..b933502d 100644
---
a/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
+++
b/src/test/java/org/apache/commons/compress/archivers/zip/UTF8ZipFilesTest.java
@@ -22,6 +22,7 @@ import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.fail;
import java.io.File;
import java.io.IOException;
@@ -54,10 +55,7 @@ public class UTF8ZipFilesTest extends AbstractTestCase {
assertNotSame(b, ze.getRawName());
}
- private static void assertUnicodeName(final ZipArchiveEntry ze,
- final String expectedName,
- final String encoding)
- throws IOException {
+ private static void assertUnicodeName(final ZipArchiveEntry ze, final
String expectedName, final String encoding) throws IOException {
if (!expectedName.equals(ze.getName())) {
final UnicodePathExtraField ucpf = findUniCodePath(ze);
assertNotNull(ucpf);
@@ -66,41 +64,30 @@ public class UTF8ZipFilesTest extends AbstractTestCase {
final ByteBuffer ne = enc.encode(ze.getName());
final CRC32 crc = new CRC32();
- crc.update(ne.array(), ne.arrayOffset(),
- ne.limit() - ne.position());
+ crc.update(ne.array(), ne.arrayOffset(), ne.limit() -
ne.position());
assertEquals(crc.getValue(), ucpf.getNameCRC32());
assertEquals(expectedName, new String(ucpf.getUnicodeName(),
UTF_8));
}
}
- private static void createTestFile(final File file, final String encoding,
- final boolean withEFS,
- final boolean withExplicitUnicodeExtra)
- throws IOException {
+ private static void createTestFile(final File file, final String encoding,
final boolean withEFS, final boolean withExplicitUnicodeExtra)
+ throws IOException {
final ZipEncoding zipEncoding =
ZipEncodingHelper.getZipEncoding(encoding);
try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(file)) {
zos.setEncoding(encoding);
zos.setUseLanguageEncodingFlag(withEFS);
- zos.setCreateUnicodeExtraFields(withExplicitUnicodeExtra ?
- ZipArchiveOutputStream
- .UnicodeExtraFieldPolicy.NEVER
- : ZipArchiveOutputStream
- .UnicodeExtraFieldPolicy.ALWAYS);
+ zos.setCreateUnicodeExtraFields(
+ withExplicitUnicodeExtra ?
ZipArchiveOutputStream.UnicodeExtraFieldPolicy.NEVER :
ZipArchiveOutputStream.UnicodeExtraFieldPolicy.ALWAYS);
ZipArchiveEntry ze = new ZipArchiveEntry(OIL_BARREL_TXT);
- if (withExplicitUnicodeExtra
- && !zipEncoding.canEncode(ze.getName())) {
+ if (withExplicitUnicodeExtra &&
!zipEncoding.canEncode(ze.getName())) {
final ByteBuffer en = zipEncoding.encode(ze.getName());
- ze.addExtraField(new UnicodePathExtraField(ze.getName(),
- en.array(),
- en.arrayOffset(),
- en.limit()
- - en.position()));
+ ze.addExtraField(new UnicodePathExtraField(ze.getName(),
en.array(), en.arrayOffset(), en.limit() - en.position()));
}
zos.putArchiveEntry(ze);
@@ -108,16 +95,11 @@ public class UTF8ZipFilesTest extends AbstractTestCase {
zos.closeArchiveEntry();
ze = new ZipArchiveEntry(EURO_FOR_DOLLAR_TXT);
- if (withExplicitUnicodeExtra
- && !zipEncoding.canEncode(ze.getName())) {
+ if (withExplicitUnicodeExtra &&
!zipEncoding.canEncode(ze.getName())) {
final ByteBuffer en = zipEncoding.encode(ze.getName());
- ze.addExtraField(new UnicodePathExtraField(ze.getName(),
- en.array(),
- en.arrayOffset(),
- en.limit()
- - en.position()));
+ ze.addExtraField(new UnicodePathExtraField(ze.getName(),
en.array(), en.arrayOffset(), en.limit() - en.position()));
}
zos.putArchiveEntry(ze);
@@ -126,16 +108,11 @@ public class UTF8ZipFilesTest extends AbstractTestCase {
ze = new ZipArchiveEntry(ASCII_TXT);
- if (withExplicitUnicodeExtra
- && !zipEncoding.canEncode(ze.getName())) {
+ if (withExplicitUnicodeExtra &&
!zipEncoding.canEncode(ze.getName())) {
final ByteBuffer en = zipEncoding.encode(ze.getName());
- ze.addExtraField(new UnicodePathExtraField(ze.getName(),
- en.array(),
- en.arrayOffset(),
- en.limit()
- - en.position()));
+ ze.addExtraField(new UnicodePathExtraField(ze.getName(),
en.array(), en.arrayOffset(), en.limit() - en.position()));
}
zos.putArchiveEntry(ze);
@@ -147,32 +124,22 @@ public class UTF8ZipFilesTest extends AbstractTestCase {
}
private static UnicodePathExtraField findUniCodePath(final ZipArchiveEntry
ze) {
- return (UnicodePathExtraField)
- ze.getExtraField(UnicodePathExtraField.UPATH_ID);
+ return (UnicodePathExtraField)
ze.getExtraField(UnicodePathExtraField.UPATH_ID);
}
- private static void testFile(final File file, final String encoding)
- throws IOException {
- ZipFile zf = null;
- try {
- zf = new ZipFile(file, encoding, false);
-
+ private static void testFile(final File file, final String encoding)
throws IOException {
+ try (ZipFile zf = new ZipFile(file, encoding, false)) {
final Enumeration<ZipArchiveEntry> e = zf.getEntries();
while (e.hasMoreElements()) {
final ZipArchiveEntry ze = e.nextElement();
-
if (ze.getName().endsWith("sser.txt")) {
assertUnicodeName(ze, OIL_BARREL_TXT, encoding);
-
} else if (ze.getName().endsWith("_for_Dollar.txt")) {
assertUnicodeName(ze, EURO_FOR_DOLLAR_TXT, encoding);
} else if (!ze.getName().equals(ASCII_TXT)) {
- throw new AssertionError("Unrecognized ZIP entry with name
["
- + ze.getName() + "] found.");
+ fail("Unrecognized ZIP entry with name [" + ze.getName() +
"] found.");
}
}
- } finally {
- ZipFile.closeQuietly(zf);
}
}
@@ -186,26 +153,22 @@ public class UTF8ZipFilesTest extends AbstractTestCase {
}
@Test
- public void testASCIIFileRoundtripExplicitUnicodeExtra()
- throws IOException {
+ public void testASCIIFileRoundtripExplicitUnicodeExtra() throws
IOException {
testFileRoundtrip(CharsetNames.US_ASCII, false, true);
}
@Test
- public void testASCIIFileRoundtripImplicitUnicodeExtra()
- throws IOException {
+ public void testASCIIFileRoundtripImplicitUnicodeExtra() throws
IOException {
testFileRoundtrip(CharsetNames.US_ASCII, false, false);
}
@Test
- public void testCP437FileRoundtripExplicitUnicodeExtra()
- throws IOException {
+ public void testCP437FileRoundtripExplicitUnicodeExtra() throws
IOException {
testFileRoundtrip(CP437, false, true);
}
@Test
- public void testCP437FileRoundtripImplicitUnicodeExtra()
- throws IOException {
+ public void testCP437FileRoundtripImplicitUnicodeExtra() throws
IOException {
testFileRoundtrip(CP437, false, false);
}
@@ -223,8 +186,7 @@ public class UTF8ZipFilesTest extends AbstractTestCase {
}
@Test
- public void testRawNameReadFromZipFile()
- throws IOException {
+ public void testRawNameReadFromZipFile() throws IOException {
final File archive = getFile("utf8-7zip-test.zip");
ZipFile zf = null;
try {
@@ -238,8 +200,7 @@ public class UTF8ZipFilesTest extends AbstractTestCase {
/*
* 7-ZIP created archive, uses EFS to signal UTF-8 file names.
*
- * 7-ZIP doesn't use EFS for strings that can be encoded in CP437
- * - which is true for OIL_BARREL_TXT.
+ * 7-ZIP doesn't use EFS for strings that can be encoded in CP437 - which
is true for OIL_BARREL_TXT.
*/
@Test
public void testRead7ZipArchive() throws IOException {
@@ -265,8 +226,7 @@ public class UTF8ZipFilesTest extends AbstractTestCase {
}
/*
- * WinZIP created archive, uses Unicode Extra Fields but only in
- * the central directory.
+ * WinZIP created archive, uses Unicode Extra Fields but only in the
central directory.
*/
@Test
public void testReadWinZipArchive() throws IOException {
@@ -309,7 +269,7 @@ public class UTF8ZipFilesTest extends AbstractTestCase {
@Test
public void testStreamSkipsOverUnicodeExtraFieldWithUnsupportedVersion()
throws IOException {
try (InputStream archive = newInputStream("COMPRESS-479.zip");
- ZipArchiveInputStream zi = new ZipArchiveInputStream(archive)) {
+ ZipArchiveInputStream zi = new ZipArchiveInputStream(archive))
{
assertEquals(OIL_BARREL_TXT, zi.getNextEntry().getName());
assertEquals("%U20AC_for_Dollar.txt", zi.getNextEntry().getName());
assertEquals(ASCII_TXT, zi.getNextEntry().getName());
@@ -317,26 +277,22 @@ public class UTF8ZipFilesTest extends AbstractTestCase {
}
@Test
- public void testUtf8FileRoundtripExplicitUnicodeExtra()
- throws IOException {
+ public void testUtf8FileRoundtripExplicitUnicodeExtra() throws IOException
{
testFileRoundtrip(CharsetNames.UTF_8, true, true);
}
@Test
- public void testUtf8FileRoundtripImplicitUnicodeExtra()
- throws IOException {
+ public void testUtf8FileRoundtripImplicitUnicodeExtra() throws IOException
{
testFileRoundtrip(CharsetNames.UTF_8, true, false);
}
@Test
- public void testUtf8FileRoundtripNoEFSExplicitUnicodeExtra()
- throws IOException {
+ public void testUtf8FileRoundtripNoEFSExplicitUnicodeExtra() throws
IOException {
testFileRoundtrip(CharsetNames.UTF_8, false, true);
}
@Test
- public void testUtf8FileRoundtripNoEFSImplicitUnicodeExtra()
- throws IOException {
+ public void testUtf8FileRoundtripNoEFSImplicitUnicodeExtra() throws
IOException {
testFileRoundtrip(CharsetNames.UTF_8, false, false);
}
@@ -345,8 +301,8 @@ public class UTF8ZipFilesTest extends AbstractTestCase {
final File file1 = getFile("utf8-7zip-test.zip");
final File file2 = getFile("utf8-winzip-test.zip");
- testFile(file1,CP437);
- testFile(file2,CP437);
+ testFile(file1, CP437);
+ testFile(file2, CP437);
}
@@ -368,17 +324,11 @@ public class UTF8ZipFilesTest extends AbstractTestCase {
@Test
public void testZipFileReadsUnicodeFields() throws IOException {
final File file = createTempFile("unicode-test", ".zip");
- ZipArchiveInputStream zi = null;
- try {
- createTestFile(file, CharsetNames.US_ASCII, false, true);
- zi = new
ZipArchiveInputStream(Files.newInputStream(file.toPath()),
CharsetNames.US_ASCII, true);
+ createTestFile(file, CharsetNames.US_ASCII, false, true);
+ try (ZipArchiveInputStream zi = new
ZipArchiveInputStream(Files.newInputStream(file.toPath()),
CharsetNames.US_ASCII, true)) {
assertEquals(OIL_BARREL_TXT, zi.getNextEntry().getName());
assertEquals(EURO_FOR_DOLLAR_TXT, zi.getNextEntry().getName());
assertEquals(ASCII_TXT, zi.getNextEntry().getName());
- } finally {
- if (zi != null) {
- zi.close();
- }
}
}
@@ -394,4 +344,3 @@ public class UTF8ZipFilesTest extends AbstractTestCase {
}
}
}
-