@@ -98,48 +98,48 @@ void getUrl() throws Exception {
98
98
@ Test
99
99
void getNestedArchive () throws Exception {
100
100
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
+ }
104
104
}
105
105
106
106
@ Test
107
107
void getNestedUnpackedArchive () throws Exception {
108
108
setup (true );
109
109
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
+ }
114
114
}
115
115
116
116
@ Test
117
117
void unpackedLocationsAreUniquePerArchive () throws Exception {
118
118
setup (true );
119
119
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
+ }
123
124
this .archive .close ();
124
125
setup (true );
125
126
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
+ }
130
131
}
131
132
132
133
@ Test
133
134
void unpackedLocationsFromSameArchiveShareSameParent () throws Exception {
134
135
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
+ }
143
143
}
144
144
145
145
@ Test
@@ -156,40 +156,40 @@ void filesInZip64ArchivesAreAllListed() throws IOException {
156
156
}
157
157
158
158
@ Test
159
- void nestedZip64ArchivesAreHandledGracefully () throws IOException {
159
+ void nestedZip64ArchivesAreHandledGracefully () throws Exception {
160
160
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
+ }
180
182
}
181
- ((JarFileArchive ) nestedArchive ).close ();
182
- jarFileArchive .close ();
183
183
}
184
184
185
185
private byte [] writeZip64Jar () throws IOException {
186
186
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
+ }
191
192
}
192
- jarOutput .close ();
193
193
return bytes .toByteArray ();
194
194
}
195
195
0 commit comments