11package edu .kit .datamanager .ro_crate .preview ;
22
3- import gg .jte .output .FileOutput ;
43import net .lingala .zip4j .ZipFile ;
54import net .lingala .zip4j .io .outputstream .ZipOutputStream ;
65import net .lingala .zip4j .model .ZipParameters ;
@@ -86,12 +85,12 @@ void staticPreviewSaveToZipStream(@TempDir Path dir) throws IOException {
8685 FileUtils .writeStringToFile (fileInDir .toFile (), "dajkdlfjdsklafj alksfjdalk fjl" , Charset .defaultCharset ());
8786 StaticPreview preview = new StaticPreview (file1 .toFile (), file2 .toFile ());
8887
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+ }
9594
9695 try (ZipFile zf = new ZipFile (dir .resolve ("destination.zip" ).toFile ())) {
9796 zf .extractAll (dir .resolve ("extracted" ).toAbsolutePath ().toString ());
@@ -115,11 +114,13 @@ void staticPreviewSaveToZipStream(@TempDir Path dir) throws IOException {
115114 void testAutomaticPreviewAddToFolder (@ TempDir Path dir ) throws IOException {
116115 AutomaticPreview automaticPreview = new AutomaticPreview ();
117116
118- InputStream crateJson = PreviewTest .class .getResourceAsStream ("/crates/other/idrc_project/ro-crate-metadata.json" );
119117 Path crate = dir .resolve ("crate" );
120118 // this crate will not have a json file
121119 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+ }
123124 automaticPreview .saveAllToFolder (crate .toFile ());
124125
125126 // there should be a html file generated
@@ -129,14 +130,14 @@ void testAutomaticPreviewAddToFolder(@TempDir Path dir) throws IOException {
129130 @ Test
130131 void testAutomaticPreviewZip (@ TempDir Path dir ) throws IOException {
131132 AutomaticPreview automaticPreview = new AutomaticPreview ();
132- InputStream crateJson = PreviewTest .class .getResourceAsStream ("/crates/other/idrc_project/ro-crate-metadata.json" );
133133 Path crate = dir .resolve ("crate" );
134134 ZipParameters zipParameters = new ZipParameters ();
135135 zipParameters .setFileNameInZip ("ro-crate-metadata.json" );
136136
137137 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+ }
140141
141142 automaticPreview .saveAllToZip (zipFile );
142143
@@ -159,82 +160,88 @@ void testAutomaticPreviewZipStream(@TempDir Path dir) throws IOException {
159160 Path crate = dir .resolve ("crate" );
160161
161162 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+ ) {
164167 ZipParameters zipParameters = new ZipParameters ();
165168 zipParameters .setFileNameInZip ("ro-crate-metadata.json" );
166- InputStream crateJson = PreviewTest .class .getResourceAsStream (metadataPath );
167169 zip .addStream (crateJson , zipParameters );
168- crateJson .close ();
169170 }
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+ }
177182
178183 try {
179- // this should trow an exception but not stop the execution
184+ // this should throw an exception but not stop the execution
180185 ZipFile randomZipFile = new ZipFile (dir .resolve ("dddd.zip" ).toFile ());
181186 preview .saveAllToZip (randomZipFile );
182187 Assertions .fail ("Expected IOException when providing invalid ZIP file for preview." );
183188 } catch (IOException ex ) {
184189 //ok
185190 }
186- new ZipFile (zipFile ).extractAll (crate .toString ());
191+
192+ try (ZipFile zipReader = new ZipFile (zipFile )) {
193+ zipReader .extractAll (crate .toString ());
194+ }
187195 assertTrue (Files .isRegularFile (crate .resolve ("ro-crate-preview.html" )));
188196 }
189197
190198 @ Test
191199 void testCustomPreviewAddToFolder (@ TempDir Path dir ) throws IOException {
192200 CustomPreview customPreview = new CustomPreview ();
193-
194- InputStream crateJson = PreviewTest .class .getResourceAsStream ("/crates/other/idrc_project/ro-crate-metadata.json" );
195201 Path crate = dir .resolve ("crate" );
196- // this crate will not have a json file
197202 Path fakeCrate = dir .resolve ("fakeCrate" );
198203 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+ }
200209
201210 customPreview .saveAllToFolder (crate .toFile ());
202211
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
205214 customPreview .saveAllToFolder (fakeCrate .toFile ());
206215 Assertions .fail ("Expected IOException when providing invalid ZIP file for preview." );
207216 } catch (IOException ex ) {
208217 //ok
209218 }
210219
211- // there should be a html file generated
212220 assertTrue (Files .isRegularFile (crate .resolve ("ro-crate-preview.html" )));
213221 }
214222
215223 @ Test
216224 void testCustomPreviewZip (@ TempDir Path tmp ) throws IOException {
217225 CustomPreview customPreview = new CustomPreview ();
218- InputStream crateJson = PreviewTest .class .getResourceAsStream ("/crates/other/idrc_project/ro-crate-metadata.json" );
219226 Path crate = tmp .resolve ("crate" );
220227 ZipParameters zipParameters = new ZipParameters ();
221228 zipParameters .setFileNameInZip ("ro-crate-metadata.json" );
222229
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 ());
236244 }
237- zipFile .extractAll (crate .toString ());
238245 assertTrue (Files .isRegularFile (crate .resolve ("ro-crate-preview.html" )));
239246 }
240247
@@ -243,33 +250,39 @@ void testCustomPreviewZipStream(@TempDir Path tmp) throws IOException {
243250 CustomPreview preview = new CustomPreview ();
244251 String metadataPath = "/crates/other/idrc_project/ro-crate-metadata.json" ;
245252 Path crate = tmp .resolve ("crate" );
246- ZipParameters zipParameters = new ZipParameters ();
247- zipParameters .setFileNameInZip ("ro-crate-metadata.json" );
248-
249253 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" );
253259 zip .addStream (crateJson , zipParameters );
254- crateJson .close ();
255260 }
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+ }
263272
264273 try {
265- // this should trow an exception but not stop the execution
274+ // this should throw an exception but not stop the execution
266275 ZipFile randomZipFile = new ZipFile (tmp .resolve ("dddd.zip" ).toFile ());
267276 preview .saveAllToZip (randomZipFile );
268277 Assertions .fail ("Expected IOException when providing invalid input to preview." );
269278 } catch (IOException ex ) {
270279 //ok
271280 }
272- new ZipFile (zipFile ).extractAll (crate .toString ());
281+
282+ try (ZipFile zipReader = new ZipFile (zipFile )) {
283+ zipReader .extractAll (crate .toString ());
284+ }
273285 assertTrue (Files .isRegularFile (crate .resolve ("ro-crate-preview.html" )));
274286 }
275287}
288+
0 commit comments