Skip to content

Commit b35573a

Browse files
committed
Merge pull request #18883 from izeye
* pr/18883: Use try-with-resources blocks in JarFileArchiveTests Closes gh-18883
2 parents 471ca01 + ea51947 commit b35573a

File tree

1 file changed

+49
-49
lines changed
  • spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive

1 file changed

+49
-49
lines changed

spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/archive/JarFileArchiveTests.java

Lines changed: 49 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -98,48 +98,48 @@ void getUrl() throws Exception {
9898
@Test
9999
void getNestedArchive() throws Exception {
100100
Entry entry = getEntriesMap(this.archive).get("nested.jar");
101-
Archive nested = this.archive.getNestedArchive(entry);
102-
assertThat(nested.getUrl().toString()).isEqualTo("jar:" + this.rootJarFileUrl + "!/nested.jar!/");
103-
((JarFileArchive) nested).close();
101+
try (Archive nested = this.archive.getNestedArchive(entry)) {
102+
assertThat(nested.getUrl().toString()).isEqualTo("jar:" + this.rootJarFileUrl + "!/nested.jar!/");
103+
}
104104
}
105105

106106
@Test
107107
void getNestedUnpackedArchive() throws Exception {
108108
setup(true);
109109
Entry entry = getEntriesMap(this.archive).get("nested.jar");
110-
Archive nested = this.archive.getNestedArchive(entry);
111-
assertThat(nested.getUrl().toString()).startsWith("file:");
112-
assertThat(nested.getUrl().toString()).endsWith("/nested.jar");
113-
((JarFileArchive) nested).close();
110+
try (Archive nested = this.archive.getNestedArchive(entry)) {
111+
assertThat(nested.getUrl().toString()).startsWith("file:");
112+
assertThat(nested.getUrl().toString()).endsWith("/nested.jar");
113+
}
114114
}
115115

116116
@Test
117117
void unpackedLocationsAreUniquePerArchive() throws Exception {
118118
setup(true);
119119
Entry entry = getEntriesMap(this.archive).get("nested.jar");
120-
Archive firstNested = this.archive.getNestedArchive(entry);
121-
URL firstNestedUrl = firstNested.getUrl();
122-
((JarFileArchive) firstNested).close();
120+
URL firstNestedUrl;
121+
try (Archive firstNested = this.archive.getNestedArchive(entry)) {
122+
firstNestedUrl = firstNested.getUrl();
123+
}
123124
this.archive.close();
124125
setup(true);
125126
entry = getEntriesMap(this.archive).get("nested.jar");
126-
Archive secondNested = this.archive.getNestedArchive(entry);
127-
URL secondNestedUrl = secondNested.getUrl();
128-
assertThat(secondNestedUrl).isNotEqualTo(firstNestedUrl);
129-
((JarFileArchive) secondNested).close();
127+
try (Archive secondNested = this.archive.getNestedArchive(entry)) {
128+
URL secondNestedUrl = secondNested.getUrl();
129+
assertThat(secondNestedUrl).isNotEqualTo(firstNestedUrl);
130+
}
130131
}
131132

132133
@Test
133134
void unpackedLocationsFromSameArchiveShareSameParent() throws Exception {
134135
setup(true);
135-
Archive nestedArchive = this.archive.getNestedArchive(getEntriesMap(this.archive).get("nested.jar"));
136-
File nested = new File(nestedArchive.getUrl().toURI());
137-
Archive anotherNestedArchive = this.archive
138-
.getNestedArchive(getEntriesMap(this.archive).get("another-nested.jar"));
139-
File anotherNested = new File(anotherNestedArchive.getUrl().toURI());
140-
assertThat(nested.getParent()).isEqualTo(anotherNested.getParent());
141-
((JarFileArchive) nestedArchive).close();
142-
((JarFileArchive) anotherNestedArchive).close();
136+
try (Archive nestedArchive = this.archive.getNestedArchive(getEntriesMap(this.archive).get("nested.jar"));
137+
Archive anotherNestedArchive = this.archive
138+
.getNestedArchive(getEntriesMap(this.archive).get("another-nested.jar"))) {
139+
File nested = new File(nestedArchive.getUrl().toURI());
140+
File anotherNested = new File(anotherNestedArchive.getUrl().toURI());
141+
assertThat(nested.getParent()).isEqualTo(anotherNested.getParent());
142+
}
143143
}
144144

145145
@Test
@@ -156,40 +156,40 @@ void filesInZip64ArchivesAreAllListed() throws IOException {
156156
}
157157

158158
@Test
159-
void nestedZip64ArchivesAreHandledGracefully() throws IOException {
159+
void nestedZip64ArchivesAreHandledGracefully() throws Exception {
160160
File file = new File(this.tempDir, "test.jar");
161-
JarOutputStream output = new JarOutputStream(new FileOutputStream(file));
162-
JarEntry zip64JarEntry = new JarEntry("nested/zip64.jar");
163-
output.putNextEntry(zip64JarEntry);
164-
byte[] zip64JarData = writeZip64Jar();
165-
zip64JarEntry.setSize(zip64JarData.length);
166-
zip64JarEntry.setCompressedSize(zip64JarData.length);
167-
zip64JarEntry.setMethod(ZipEntry.STORED);
168-
CRC32 crc32 = new CRC32();
169-
crc32.update(zip64JarData);
170-
zip64JarEntry.setCrc(crc32.getValue());
171-
output.write(zip64JarData);
172-
output.closeEntry();
173-
output.close();
174-
JarFileArchive jarFileArchive = new JarFileArchive(file);
175-
Archive nestedArchive = jarFileArchive.getNestedArchive(getEntriesMap(jarFileArchive).get("nested/zip64.jar"));
176-
Iterator<Entry> it = nestedArchive.iterator();
177-
for (int i = 0; i < 65537; i++) {
178-
assertThat(it.hasNext()).as(i + "nth file is present").isTrue();
179-
it.next();
161+
try (JarOutputStream output = new JarOutputStream(new FileOutputStream(file))) {
162+
JarEntry zip64JarEntry = new JarEntry("nested/zip64.jar");
163+
output.putNextEntry(zip64JarEntry);
164+
byte[] zip64JarData = writeZip64Jar();
165+
zip64JarEntry.setSize(zip64JarData.length);
166+
zip64JarEntry.setCompressedSize(zip64JarData.length);
167+
zip64JarEntry.setMethod(ZipEntry.STORED);
168+
CRC32 crc32 = new CRC32();
169+
crc32.update(zip64JarData);
170+
zip64JarEntry.setCrc(crc32.getValue());
171+
output.write(zip64JarData);
172+
output.closeEntry();
173+
}
174+
try (JarFileArchive jarFileArchive = new JarFileArchive(file);
175+
Archive nestedArchive = jarFileArchive
176+
.getNestedArchive(getEntriesMap(jarFileArchive).get("nested/zip64.jar"))) {
177+
Iterator<Entry> it = nestedArchive.iterator();
178+
for (int i = 0; i < 65537; i++) {
179+
assertThat(it.hasNext()).as(i + "nth file is present").isTrue();
180+
it.next();
181+
}
180182
}
181-
((JarFileArchive) nestedArchive).close();
182-
jarFileArchive.close();
183183
}
184184

185185
private byte[] writeZip64Jar() throws IOException {
186186
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
187-
JarOutputStream jarOutput = new JarOutputStream(bytes);
188-
for (int i = 0; i < 65537; i++) {
189-
jarOutput.putNextEntry(new JarEntry(i + ".dat"));
190-
jarOutput.closeEntry();
187+
try (JarOutputStream jarOutput = new JarOutputStream(bytes)) {
188+
for (int i = 0; i < 65537; i++) {
189+
jarOutput.putNextEntry(new JarEntry(i + ".dat"));
190+
jarOutput.closeEntry();
191+
}
191192
}
192-
jarOutput.close();
193193
return bytes.toByteArray();
194194
}
195195

0 commit comments

Comments
 (0)