From ea519477410026c11688f6702e591ad7f51437bf Mon Sep 17 00:00:00 2001 From: Johnny Lim Date: Tue, 5 Nov 2019 21:53:33 +0900 Subject: [PATCH] Use try-with-resources blocks in JarFileArchiveTests See gh-18883 --- .../loader/archive/JarFileArchiveTests.java | 98 +++++++++---------- 1 file changed, 49 insertions(+), 49 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive/JarFileArchiveTests.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive/JarFileArchiveTests.java index 6c5baf437cd..3cc4d24b409 100755 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive/JarFileArchiveTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive/JarFileArchiveTests.java @@ -98,48 +98,48 @@ class JarFileArchiveTests { @Test void getNestedArchive() throws Exception { Entry entry = getEntriesMap(this.archive).get("nested.jar"); - Archive nested = this.archive.getNestedArchive(entry); - assertThat(nested.getUrl().toString()).isEqualTo("jar:" + this.rootJarFileUrl + "!/nested.jar!/"); - ((JarFileArchive) nested).close(); + try (Archive nested = this.archive.getNestedArchive(entry)) { + assertThat(nested.getUrl().toString()).isEqualTo("jar:" + this.rootJarFileUrl + "!/nested.jar!/"); + } } @Test void getNestedUnpackedArchive() throws Exception { setup(true); Entry entry = getEntriesMap(this.archive).get("nested.jar"); - Archive nested = this.archive.getNestedArchive(entry); - assertThat(nested.getUrl().toString()).startsWith("file:"); - assertThat(nested.getUrl().toString()).endsWith("/nested.jar"); - ((JarFileArchive) nested).close(); + try (Archive nested = this.archive.getNestedArchive(entry)) { + assertThat(nested.getUrl().toString()).startsWith("file:"); + assertThat(nested.getUrl().toString()).endsWith("/nested.jar"); + } } @Test void unpackedLocationsAreUniquePerArchive() throws Exception { setup(true); Entry entry = getEntriesMap(this.archive).get("nested.jar"); - Archive firstNested = this.archive.getNestedArchive(entry); - URL firstNestedUrl = firstNested.getUrl(); - ((JarFileArchive) firstNested).close(); + URL firstNestedUrl; + try (Archive firstNested = this.archive.getNestedArchive(entry)) { + firstNestedUrl = firstNested.getUrl(); + } this.archive.close(); setup(true); entry = getEntriesMap(this.archive).get("nested.jar"); - Archive secondNested = this.archive.getNestedArchive(entry); - URL secondNestedUrl = secondNested.getUrl(); - assertThat(secondNestedUrl).isNotEqualTo(firstNestedUrl); - ((JarFileArchive) secondNested).close(); + try (Archive secondNested = this.archive.getNestedArchive(entry)) { + URL secondNestedUrl = secondNested.getUrl(); + assertThat(secondNestedUrl).isNotEqualTo(firstNestedUrl); + } } @Test void unpackedLocationsFromSameArchiveShareSameParent() throws Exception { setup(true); - Archive nestedArchive = this.archive.getNestedArchive(getEntriesMap(this.archive).get("nested.jar")); - File nested = new File(nestedArchive.getUrl().toURI()); - Archive anotherNestedArchive = this.archive - .getNestedArchive(getEntriesMap(this.archive).get("another-nested.jar")); - File anotherNested = new File(anotherNestedArchive.getUrl().toURI()); - assertThat(nested.getParent()).isEqualTo(anotherNested.getParent()); - ((JarFileArchive) nestedArchive).close(); - ((JarFileArchive) anotherNestedArchive).close(); + try (Archive nestedArchive = this.archive.getNestedArchive(getEntriesMap(this.archive).get("nested.jar")); + Archive anotherNestedArchive = this.archive + .getNestedArchive(getEntriesMap(this.archive).get("another-nested.jar"))) { + File nested = new File(nestedArchive.getUrl().toURI()); + File anotherNested = new File(anotherNestedArchive.getUrl().toURI()); + assertThat(nested.getParent()).isEqualTo(anotherNested.getParent()); + } } @Test @@ -156,40 +156,40 @@ class JarFileArchiveTests { } @Test - void nestedZip64ArchivesAreHandledGracefully() throws IOException { + void nestedZip64ArchivesAreHandledGracefully() throws Exception { File file = new File(this.tempDir, "test.jar"); - JarOutputStream output = new JarOutputStream(new FileOutputStream(file)); - JarEntry zip64JarEntry = new JarEntry("nested/zip64.jar"); - output.putNextEntry(zip64JarEntry); - byte[] zip64JarData = writeZip64Jar(); - zip64JarEntry.setSize(zip64JarData.length); - zip64JarEntry.setCompressedSize(zip64JarData.length); - zip64JarEntry.setMethod(ZipEntry.STORED); - CRC32 crc32 = new CRC32(); - crc32.update(zip64JarData); - zip64JarEntry.setCrc(crc32.getValue()); - output.write(zip64JarData); - output.closeEntry(); - output.close(); - JarFileArchive jarFileArchive = new JarFileArchive(file); - Archive nestedArchive = jarFileArchive.getNestedArchive(getEntriesMap(jarFileArchive).get("nested/zip64.jar")); - Iterator it = nestedArchive.iterator(); - for (int i = 0; i < 65537; i++) { - assertThat(it.hasNext()).as(i + "nth file is present").isTrue(); - it.next(); + try (JarOutputStream output = new JarOutputStream(new FileOutputStream(file))) { + JarEntry zip64JarEntry = new JarEntry("nested/zip64.jar"); + output.putNextEntry(zip64JarEntry); + byte[] zip64JarData = writeZip64Jar(); + zip64JarEntry.setSize(zip64JarData.length); + zip64JarEntry.setCompressedSize(zip64JarData.length); + zip64JarEntry.setMethod(ZipEntry.STORED); + CRC32 crc32 = new CRC32(); + crc32.update(zip64JarData); + zip64JarEntry.setCrc(crc32.getValue()); + output.write(zip64JarData); + output.closeEntry(); + } + try (JarFileArchive jarFileArchive = new JarFileArchive(file); + Archive nestedArchive = jarFileArchive + .getNestedArchive(getEntriesMap(jarFileArchive).get("nested/zip64.jar"))) { + Iterator it = nestedArchive.iterator(); + for (int i = 0; i < 65537; i++) { + assertThat(it.hasNext()).as(i + "nth file is present").isTrue(); + it.next(); + } } - ((JarFileArchive) nestedArchive).close(); - jarFileArchive.close(); } private byte[] writeZip64Jar() throws IOException { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); - JarOutputStream jarOutput = new JarOutputStream(bytes); - for (int i = 0; i < 65537; i++) { - jarOutput.putNextEntry(new JarEntry(i + ".dat")); - jarOutput.closeEntry(); + try (JarOutputStream jarOutput = new JarOutputStream(bytes)) { + for (int i = 0; i < 65537; i++) { + jarOutput.putNextEntry(new JarEntry(i + ".dat")); + jarOutput.closeEntry(); + } } - jarOutput.close(); return bytes.toByteArray(); }