1
1
package edu .kit .datamanager .ro_crate .preview ;
2
2
3
- import gg .jte .output .FileOutput ;
4
3
import net .lingala .zip4j .ZipFile ;
5
4
import net .lingala .zip4j .io .outputstream .ZipOutputStream ;
6
5
import net .lingala .zip4j .model .ZipParameters ;
@@ -86,12 +85,12 @@ void staticPreviewSaveToZipStream(@TempDir Path dir) throws IOException {
86
85
FileUtils .writeStringToFile (fileInDir .toFile (), "dajkdlfjdsklafj alksfjdalk fjl" , Charset .defaultCharset ());
87
86
StaticPreview preview = new StaticPreview (file1 .toFile (), file2 .toFile ());
88
87
89
- ZipOutputStream stream = new ZipOutputStream (new FileOutputStream (dir .resolve ("destination.zip" ).toFile ()));
90
- preview .saveAllToStream (
91
- null , // static preview does not need metadata
92
- stream );
93
- stream .flush ();
94
- stream . close ();
88
+ try ( ZipOutputStream stream = new ZipOutputStream (new FileOutputStream (dir .resolve ("destination.zip" ).toFile ()))) {
89
+ preview .saveAllToStream (
90
+ null , // static preview does not need metadata
91
+ stream );
92
+ stream .flush ();
93
+ }
95
94
96
95
try (ZipFile zf = new ZipFile (dir .resolve ("destination.zip" ).toFile ())) {
97
96
zf .extractAll (dir .resolve ("extracted" ).toAbsolutePath ().toString ());
@@ -115,11 +114,13 @@ void staticPreviewSaveToZipStream(@TempDir Path dir) throws IOException {
115
114
void testAutomaticPreviewAddToFolder (@ TempDir Path dir ) throws IOException {
116
115
AutomaticPreview automaticPreview = new AutomaticPreview ();
117
116
118
- InputStream crateJson = PreviewTest .class .getResourceAsStream ("/crates/other/idrc_project/ro-crate-metadata.json" );
119
117
Path crate = dir .resolve ("crate" );
120
118
// this crate will not have a json file
121
119
FileUtils .forceMkdir (crate .toFile ());
122
- FileUtils .copyInputStreamToFile (crateJson , crate .resolve ("ro-crate-metadata.json" ).toFile ());
120
+ try (InputStream crateJson = PreviewTest .class .getResourceAsStream ("/crates/other/idrc_project/ro-crate-metadata.json" )) {
121
+ Assertions .assertNotNull (crateJson );
122
+ FileUtils .copyInputStreamToFile (crateJson , crate .resolve ("ro-crate-metadata.json" ).toFile ());
123
+ }
123
124
automaticPreview .saveAllToFolder (crate .toFile ());
124
125
125
126
// there should be a html file generated
@@ -129,14 +130,14 @@ void testAutomaticPreviewAddToFolder(@TempDir Path dir) throws IOException {
129
130
@ Test
130
131
void testAutomaticPreviewZip (@ TempDir Path dir ) throws IOException {
131
132
AutomaticPreview automaticPreview = new AutomaticPreview ();
132
- InputStream crateJson = PreviewTest .class .getResourceAsStream ("/crates/other/idrc_project/ro-crate-metadata.json" );
133
133
Path crate = dir .resolve ("crate" );
134
134
ZipParameters zipParameters = new ZipParameters ();
135
135
zipParameters .setFileNameInZip ("ro-crate-metadata.json" );
136
136
137
137
ZipFile zipFile = new ZipFile (dir .resolve ("test.zip" ).toFile ());
138
- zipFile .addStream (crateJson , zipParameters );
139
- crateJson .close ();
138
+ try (InputStream crateJson = PreviewTest .class .getResourceAsStream ("/crates/other/idrc_project/ro-crate-metadata.json" )) {
139
+ zipFile .addStream (crateJson , zipParameters );
140
+ }
140
141
141
142
automaticPreview .saveAllToZip (zipFile );
142
143
@@ -159,82 +160,88 @@ void testAutomaticPreviewZipStream(@TempDir Path dir) throws IOException {
159
160
Path crate = dir .resolve ("crate" );
160
161
161
162
File zipFile = dir .resolve ("test.zip" ).toFile ();
162
- {
163
- ZipFile zip = new ZipFile (zipFile );
163
+ try (
164
+ ZipFile zip = new ZipFile (zipFile );
165
+ InputStream crateJson = PreviewTest .class .getResourceAsStream (metadataPath )
166
+ ) {
164
167
ZipParameters zipParameters = new ZipParameters ();
165
168
zipParameters .setFileNameInZip ("ro-crate-metadata.json" );
166
- InputStream crateJson = PreviewTest .class .getResourceAsStream (metadataPath );
167
169
zip .addStream (crateJson , zipParameters );
168
- crateJson .close ();
169
170
}
170
- String metadata = new String (
171
- PreviewTest .class .getResourceAsStream (metadataPath )
172
- .readAllBytes ());
173
- ZipOutputStream stream = new ZipOutputStream (new FileOutputStream (zipFile ));
174
- preview .saveAllToStream (metadata , stream );
175
- stream .flush ();
176
- stream .close ();
171
+
172
+ String metadata ;
173
+ try (InputStream metadataStream = PreviewTest .class .getResourceAsStream (metadataPath )) {
174
+ Assertions .assertNotNull (metadataStream );
175
+ metadata = new String (metadataStream .readAllBytes ());
176
+ }
177
+
178
+ try (ZipOutputStream stream = new ZipOutputStream (new FileOutputStream (zipFile ))) {
179
+ preview .saveAllToStream (metadata , stream );
180
+ stream .flush ();
181
+ }
177
182
178
183
try {
179
- // this should trow an exception but not stop the execution
184
+ // this should throw an exception but not stop the execution
180
185
ZipFile randomZipFile = new ZipFile (dir .resolve ("dddd.zip" ).toFile ());
181
186
preview .saveAllToZip (randomZipFile );
182
187
Assertions .fail ("Expected IOException when providing invalid ZIP file for preview." );
183
188
} catch (IOException ex ) {
184
189
//ok
185
190
}
186
- new ZipFile (zipFile ).extractAll (crate .toString ());
191
+
192
+ try (ZipFile zipReader = new ZipFile (zipFile )) {
193
+ zipReader .extractAll (crate .toString ());
194
+ }
187
195
assertTrue (Files .isRegularFile (crate .resolve ("ro-crate-preview.html" )));
188
196
}
189
197
190
198
@ Test
191
199
void testCustomPreviewAddToFolder (@ TempDir Path dir ) throws IOException {
192
200
CustomPreview customPreview = new CustomPreview ();
193
-
194
- InputStream crateJson = PreviewTest .class .getResourceAsStream ("/crates/other/idrc_project/ro-crate-metadata.json" );
195
201
Path crate = dir .resolve ("crate" );
196
- // this crate will not have a json file
197
202
Path fakeCrate = dir .resolve ("fakeCrate" );
198
203
FileUtils .forceMkdir (crate .toFile ());
199
- FileUtils .copyInputStreamToFile (crateJson , crate .resolve ("ro-crate-metadata.json" ).toFile ());
204
+
205
+ try (InputStream crateJson = PreviewTest .class .getResourceAsStream ("/crates/other/idrc_project/ro-crate-metadata.json" )) {
206
+ Assertions .assertNotNull (crateJson );
207
+ FileUtils .copyInputStreamToFile (crateJson , crate .resolve ("ro-crate-metadata.json" ).toFile ());
208
+ }
200
209
201
210
customPreview .saveAllToFolder (crate .toFile ());
202
211
203
- try {
204
- // this should trow an exception but not stop the execution
212
+ try {
213
+ // this should throw an exception but not stop the execution
205
214
customPreview .saveAllToFolder (fakeCrate .toFile ());
206
215
Assertions .fail ("Expected IOException when providing invalid ZIP file for preview." );
207
216
} catch (IOException ex ) {
208
217
//ok
209
218
}
210
219
211
- // there should be a html file generated
212
220
assertTrue (Files .isRegularFile (crate .resolve ("ro-crate-preview.html" )));
213
221
}
214
222
215
223
@ Test
216
224
void testCustomPreviewZip (@ TempDir Path tmp ) throws IOException {
217
225
CustomPreview customPreview = new CustomPreview ();
218
- InputStream crateJson = PreviewTest .class .getResourceAsStream ("/crates/other/idrc_project/ro-crate-metadata.json" );
219
226
Path crate = tmp .resolve ("crate" );
220
227
ZipParameters zipParameters = new ZipParameters ();
221
228
zipParameters .setFileNameInZip ("ro-crate-metadata.json" );
222
229
223
- ZipFile zipFile = new ZipFile (tmp .resolve ("test.zip" ).toFile ());
224
- zipFile .addStream (crateJson , zipParameters );
225
- crateJson .close ();
226
-
227
- customPreview .saveAllToZip (zipFile );
228
-
229
- try {
230
- // this should trow an exception but not stop the execution
231
- ZipFile randomZipFile = new ZipFile (tmp .resolve ("dddd.zip" ).toFile ());
232
- customPreview .saveAllToZip (randomZipFile );
233
- Assertions .fail ("Expected IOException when providing invalid input to preview." );
234
- } catch (IOException ex ) {
235
- //ok
230
+ try (ZipFile zipFile = new ZipFile (tmp .resolve ("test.zip" ).toFile ());
231
+ InputStream crateJson = PreviewTest .class .getResourceAsStream ("/crates/other/idrc_project/ro-crate-metadata.json" )) {
232
+ zipFile .addStream (crateJson , zipParameters );
233
+ customPreview .saveAllToZip (zipFile );
234
+
235
+ try {
236
+ // this should throw an exception but not stop the execution
237
+ ZipFile randomZipFile = new ZipFile (tmp .resolve ("dddd.zip" ).toFile ());
238
+ customPreview .saveAllToZip (randomZipFile );
239
+ Assertions .fail ("Expected IOException when providing invalid input to preview." );
240
+ } catch (IOException ex ) {
241
+ //ok
242
+ }
243
+ zipFile .extractAll (crate .toString ());
236
244
}
237
- zipFile .extractAll (crate .toString ());
238
245
assertTrue (Files .isRegularFile (crate .resolve ("ro-crate-preview.html" )));
239
246
}
240
247
@@ -243,33 +250,39 @@ void testCustomPreviewZipStream(@TempDir Path tmp) throws IOException {
243
250
CustomPreview preview = new CustomPreview ();
244
251
String metadataPath = "/crates/other/idrc_project/ro-crate-metadata.json" ;
245
252
Path crate = tmp .resolve ("crate" );
246
- ZipParameters zipParameters = new ZipParameters ();
247
- zipParameters .setFileNameInZip ("ro-crate-metadata.json" );
248
-
249
253
File zipFile = tmp .resolve ("test.zip" ).toFile ();
250
- {
251
- ZipFile zip = new ZipFile (zipFile );
252
- InputStream crateJson = PreviewTest .class .getResourceAsStream (metadataPath );
254
+
255
+ try (ZipFile zip = new ZipFile (zipFile );
256
+ InputStream crateJson = PreviewTest .class .getResourceAsStream (metadataPath )) {
257
+ ZipParameters zipParameters = new ZipParameters ();
258
+ zipParameters .setFileNameInZip ("ro-crate-metadata.json" );
253
259
zip .addStream (crateJson , zipParameters );
254
- crateJson .close ();
255
260
}
256
- String metadata = new String (
257
- PreviewTest .class .getResourceAsStream (metadataPath )
258
- .readAllBytes ());
259
- ZipOutputStream stream = new ZipOutputStream (new FileOutputStream (zipFile ));
260
- preview .saveAllToStream (metadata , stream );
261
- stream .flush ();
262
- stream .close ();
261
+
262
+ String metadata ;
263
+ try (InputStream metadataStream = PreviewTest .class .getResourceAsStream (metadataPath )) {
264
+ Assertions .assertNotNull (metadataStream );
265
+ metadata = new String (metadataStream .readAllBytes ());
266
+ }
267
+
268
+ try (ZipOutputStream stream = new ZipOutputStream (new FileOutputStream (zipFile ))) {
269
+ preview .saveAllToStream (metadata , stream );
270
+ stream .flush ();
271
+ }
263
272
264
273
try {
265
- // this should trow an exception but not stop the execution
274
+ // this should throw an exception but not stop the execution
266
275
ZipFile randomZipFile = new ZipFile (tmp .resolve ("dddd.zip" ).toFile ());
267
276
preview .saveAllToZip (randomZipFile );
268
277
Assertions .fail ("Expected IOException when providing invalid input to preview." );
269
278
} catch (IOException ex ) {
270
279
//ok
271
280
}
272
- new ZipFile (zipFile ).extractAll (crate .toString ());
281
+
282
+ try (ZipFile zipReader = new ZipFile (zipFile )) {
283
+ zipReader .extractAll (crate .toString ());
284
+ }
273
285
assertTrue (Files .isRegularFile (crate .resolve ("ro-crate-preview.html" )));
274
286
}
275
287
}
288
+
0 commit comments