2222import static org .junit .jupiter .api .Assertions .assertFalse ;
2323import static org .junit .jupiter .api .Assertions .assertTrue ;
2424
25- abstract class CrateWriterTest {
26-
27- /**
28- * Saves the crate with the writer fitting to this test class.
29- *
30- * @param crate the crate to save
31- * @param target the target path to the save location
32- * @throws IOException if an error occurs while saving the crate
33- */
34- abstract protected void saveCrate (Crate crate , Path target ) throws IOException ;
25+ interface CommonWriterTest {
3526
3627 /**
3728 * Test where the writer needs to rename files or folders in order to make a valid crate.
@@ -41,11 +32,11 @@ abstract class CrateWriterTest {
4132 * @throws IOException if an error occurs while writing the crate
4233 */
4334 @ Test
44- void testFilesBeingAdjusted (@ TempDir Path tempDir ) throws IOException {
35+ default void testFilesBeingAdjusted (@ TempDir Path tempDir ) throws IOException {
4536 Path correctCrate = tempDir .resolve ("compare_with_me" );
4637 Path pathToFile = correctCrate .resolve ("you-will-need-to-rename-this-file.ai" );
4738 Path pathToDir = correctCrate .resolve ("you-will-need-to-rename-this-dir" );
48- this . createManualCrateStructure (correctCrate , pathToFile , pathToDir );
39+ createManualCrateStructure (correctCrate , pathToFile , pathToDir );
4940
5041 Path writtenCrate = tempDir .resolve ("written-crate" );
5142 Path extractionPath = tempDir .resolve ("checkMe" );
@@ -60,7 +51,7 @@ void testFilesBeingAdjusted(@TempDir Path tempDir) throws IOException {
6051 )
6152 .build ();
6253 this .saveCrate (builtCrate , writtenCrate );
63- this . ensureCrateIsExtractedIn (writtenCrate , extractionPath );
54+ ensureCrateIsExtractedIn (writtenCrate , extractionPath );
6455 }
6556
6657 printFileTree (correctCrate );
@@ -111,15 +102,15 @@ void testFilesBeingAdjusted(@TempDir Path tempDir) throws IOException {
111102 * @throws IOException if an error occurs while writing the crate
112103 */
113104 @ Test
114- void testWritingMakesCopy (@ TempDir Path tempDir ) throws IOException {
105+ default void testWritingMakesCopy (@ TempDir Path tempDir ) throws IOException {
115106 // We need a correct directory to compare with.
116107 // It is built manually to ensure we meet our expectations.
117108 // Reader-writer-consistency is tested at {@link CrateReaderTest}
118109 Path correctCrate = tempDir .resolve ("compare_with_me" );
119110 Path pathToFile = correctCrate .resolve ("cp7glop.ai" );
120111 Path pathToDir = correctCrate .resolve ("lots_of_little_files" );
121112
122- this . createManualCrateStructure (correctCrate , pathToFile , pathToDir );
113+ createManualCrateStructure (correctCrate , pathToFile , pathToDir );
123114
124115 // Now use the builder to build the same crate independently.
125116 // The files will be reused (we need a place to take a copy from)
@@ -130,7 +121,7 @@ void testWritingMakesCopy(@TempDir Path tempDir) throws IOException {
130121
131122 // extract the zip file to a temporary directory
132123 Path extractionPath = tempDir .resolve ("extracted_for_testing" );
133- this . ensureCrateIsExtractedIn (pathToZip , extractionPath );
124+ ensureCrateIsExtractedIn (pathToZip , extractionPath );
134125 printFileTree (correctCrate );
135126 printFileTree (extractionPath );
136127
@@ -151,12 +142,12 @@ void testWritingMakesCopy(@TempDir Path tempDir) throws IOException {
151142 * @throws IOException if an error occurs while writing the crate
152143 */
153144 @ Test
154- void testWritingOnlyConsidersAddedFiles (@ TempDir Path tempDir ) throws IOException {
145+ default void testWritingOnlyConsidersAddedFiles (@ TempDir Path tempDir ) throws IOException {
155146 Path correctCrate = tempDir .resolve ("compare_with_me" );
156147 Path pathToFile = correctCrate .resolve ("cp7glop.ai" );
157148 Path pathToDir = correctCrate .resolve ("lots_of_little_files" );
158149
159- this . createManualCrateStructure (correctCrate , pathToFile , pathToDir );
150+ createManualCrateStructure (correctCrate , pathToFile , pathToDir );
160151 {
161152 // This file is not part of the crate, and should therefore not be present
162153 Path falseFile = correctCrate .resolve ("new" );
@@ -187,116 +178,4 @@ void testWritingOnlyConsidersAddedFiles(@TempDir Path tempDir) throws IOExceptio
187178 roCrate ,
188179 "/json/crate/fileAndDir.json" );
189180 }
190-
191- /**
192- * Prints the file tree of the given directory for debugging and understanding
193- * a test more quickly.
194- *
195- * @param directoryToPrint the directory to print
196- * @throws IOException if an error occurs while printing the file tree
197- */
198- @ SuppressWarnings ("resource" )
199- protected static void printFileTree (Path directoryToPrint ) throws IOException {
200- // Print all files recursively in a tree structure for debugging
201- System .out .printf ("Files in %s:%n" , directoryToPrint .getFileName ().toString ());
202- Files .walk (directoryToPrint )
203- .forEach (path -> {
204- if (!path .toAbsolutePath ().equals (directoryToPrint .toAbsolutePath ())) {
205- int depth = path .relativize (directoryToPrint ).getNameCount ();
206- String prefix = " " .repeat (depth );
207- System .out .printf ("%s%s%s%n" , prefix , "└── " , path .getFileName ());
208- }
209- });
210- }
211-
212- /**
213- * Ensures the crate is in extracted form in the given path.
214- *
215- * @param pathToCrate the path to the crate, may not be a folder yet
216- * @param expectedPath the path where the crate should be in extracted form
217- * @throws IOException if an error occurs while extracting the crate
218- */
219- protected void ensureCrateIsExtractedIn (Path pathToCrate , Path expectedPath ) throws IOException {
220- try (ZipFile zf = new ZipFile (pathToCrate .toFile ())) {
221- zf .extractAll (expectedPath .toFile ().getAbsolutePath ());
222- }
223- }
224-
225- /**
226- * Creates a crate structure manually.
227- *
228- * @param correctCrate the path to the crate
229- * @param pathToFile the path to the file
230- * @param pathToDir the path to the directory
231- * @throws IOException if an error occurs while creating the crate structure
232- */
233- protected void createManualCrateStructure (Path correctCrate , Path pathToFile , Path pathToDir ) throws IOException {
234- FileUtils .forceMkdir (correctCrate .toFile ());
235- InputStream fileJson = ZipStreamStrategyTest .class
236- .getResourceAsStream ("/json/crate/fileAndDir.json" );
237- Assertions .assertNotNull (fileJson );
238- // fill the directory with expected files and dirs
239- // starting with the .json of our crate
240- Path json = correctCrate .resolve ("ro-crate-metadata.json" );
241- FileUtils .copyInputStreamToFile (fileJson , json .toFile ());
242- // create preview
243- PreviewGenerator .generatePreview (correctCrate .toFile ().getAbsolutePath ());
244- // create the files and directories
245- FileUtils .writeStringToFile (pathToFile .toFile (), "content of Local File" , Charset .defaultCharset ());
246- // creates the directory and a subdirectory
247- Path subdir = pathToDir .resolve ("subdir" );
248- FileUtils .forceMkdir (subdir .toFile ());
249- FileUtils .writeStringToFile (
250- subdir .resolve ("subsubfirst.txt" ).toFile (),
251- "content of subsub file in subsubdir" ,
252- Charset .defaultCharset ());
253- FileUtils .writeStringToFile (
254- pathToDir .resolve ("first.txt" ).toFile (),
255- "content of first file in dir" ,
256- Charset .defaultCharset ());
257- FileUtils .writeStringToFile (
258- pathToDir .resolve ("second.txt" ).toFile (),
259- "content of second file in dir" ,
260- Charset .defaultCharset ());
261- FileUtils .writeStringToFile (
262- pathToDir .resolve ("third.txt" ).toFile (),
263- "content of third file in dir" ,
264- Charset .defaultCharset ());
265- }
266-
267- /**
268- * Creates a crate resembling the one we manually create in these tests.
269- *
270- * @param pathToFile the file to add
271- * @param pathToSubdir the directory to add
272- * @return the crate builder
273- */
274- protected RoCrate .RoCrateBuilder getCrateWithFileAndDir (Path pathToFile , Path pathToSubdir ) {
275- return new RoCrate .RoCrateBuilder (
276- "Example RO-Crate" ,
277- "The RO-Crate Root Data Entity" ,
278- "2024" ,
279- "https://creativecommons.org/licenses/by-nc-sa/3.0/au/"
280- )
281- .addDataEntity (
282- new FileEntity .FileEntityBuilder ()
283- .addProperty ("name" , "Diagram showing trend to increase" )
284- .addProperty ("contentSize" , "383766" )
285- .addProperty ("description" , "Illustrator file for Glop Pot" )
286- .setEncodingFormat ("application/pdf" )
287- .setLocationWithExceptions (pathToFile )
288- .setId ("cp7glop.ai" )
289- .build ()
290- )
291- .addDataEntity (
292- new DataSetEntity .DataSetBuilder ()
293- .addProperty ("name" , "Too many files" )
294- .addProperty ("description" ,
295- "This directory contains many small files, that we're not going to describe in detail." )
296- .setLocationWithExceptions (pathToSubdir )
297- .setId ("lots_of_little_files/" )
298- .build ()
299- )
300- .setPreview (new AutomaticPreview ());
301- }
302181}
0 commit comments