11package edu .kit .datamanager .ro_crate .writer ;
22
3- import edu .kit .datamanager .ro_crate .Crate ;
43import edu .kit .datamanager .ro_crate .HelpFunctions ;
54import edu .kit .datamanager .ro_crate .RoCrate ;
65import edu .kit .datamanager .ro_crate .entities .data .DataSetEntity ;
7- import edu .kit .datamanager .ro_crate .entities .data .FileEntity ;
8- import edu .kit .datamanager .ro_crate .preview .AutomaticPreview ;
9- import edu .kit .datamanager .ro_crate .preview .PreviewGenerator ;
10- import net .lingala .zip4j .ZipFile ;
6+
117import org .apache .commons .io .FileUtils ;
12- import org .junit .jupiter .api .Assertions ;
138import org .junit .jupiter .api .Test ;
149import org .junit .jupiter .api .io .TempDir ;
1510
1611import java .io .IOException ;
17- import java .io .InputStream ;
1812import java .nio .charset .Charset ;
1913import java .nio .file .Files ;
2014import java .nio .file .Path ;
2115
2216import static org .junit .jupiter .api .Assertions .assertFalse ;
2317import static org .junit .jupiter .api .Assertions .assertTrue ;
2418
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 ;
19+ interface CommonWriterTest extends TestableWriterStrategy {
3520
3621 /**
3722 * Test where the writer needs to rename files or folders in order to make a valid crate.
@@ -41,11 +26,11 @@ abstract class CrateWriterTest {
4126 * @throws IOException if an error occurs while writing the crate
4227 */
4328 @ Test
44- void testFilesBeingAdjusted (@ TempDir Path tempDir ) throws IOException {
29+ default void testFilesBeingAdjusted (@ TempDir Path tempDir ) throws IOException {
4530 Path correctCrate = tempDir .resolve ("compare_with_me" );
4631 Path pathToFile = correctCrate .resolve ("you-will-need-to-rename-this-file.ai" );
4732 Path pathToDir = correctCrate .resolve ("you-will-need-to-rename-this-dir" );
48- this . createManualCrateStructure (correctCrate , pathToFile , pathToDir );
33+ createManualCrateStructure (correctCrate , pathToFile , pathToDir );
4934
5035 Path writtenCrate = tempDir .resolve ("written-crate" );
5136 Path extractionPath = tempDir .resolve ("checkMe" );
@@ -60,11 +45,11 @@ void testFilesBeingAdjusted(@TempDir Path tempDir) throws IOException {
6045 )
6146 .build ();
6247 this .saveCrate (builtCrate , writtenCrate );
63- this . ensureCrateIsExtractedIn (writtenCrate , extractionPath );
48+ ensureCrateIsExtractedIn (writtenCrate , extractionPath );
6449 }
6550
66- printFileTree (correctCrate );
67- printFileTree (extractionPath );
51+ HelpFunctions . printFileTree (correctCrate );
52+ HelpFunctions . printFileTree (extractionPath );
6853
6954 // The actual file name should **not** appear in the crate
7055 String fileName = pathToFile .getFileName ().toString ();
@@ -111,15 +96,15 @@ void testFilesBeingAdjusted(@TempDir Path tempDir) throws IOException {
11196 * @throws IOException if an error occurs while writing the crate
11297 */
11398 @ Test
114- void testWritingMakesCopy (@ TempDir Path tempDir ) throws IOException {
99+ default void testWritingMakesCopy (@ TempDir Path tempDir ) throws IOException {
115100 // We need a correct directory to compare with.
116101 // It is built manually to ensure we meet our expectations.
117102 // Reader-writer-consistency is tested at {@link CrateReaderTest}
118103 Path correctCrate = tempDir .resolve ("compare_with_me" );
119104 Path pathToFile = correctCrate .resolve ("cp7glop.ai" );
120105 Path pathToDir = correctCrate .resolve ("lots_of_little_files" );
121106
122- this . createManualCrateStructure (correctCrate , pathToFile , pathToDir );
107+ createManualCrateStructure (correctCrate , pathToFile , pathToDir );
123108
124109 // Now use the builder to build the same crate independently.
125110 // The files will be reused (we need a place to take a copy from)
@@ -130,9 +115,9 @@ void testWritingMakesCopy(@TempDir Path tempDir) throws IOException {
130115
131116 // extract the zip file to a temporary directory
132117 Path extractionPath = tempDir .resolve ("extracted_for_testing" );
133- this . ensureCrateIsExtractedIn (pathToZip , extractionPath );
134- printFileTree (correctCrate );
135- printFileTree (extractionPath );
118+ ensureCrateIsExtractedIn (pathToZip , extractionPath );
119+ HelpFunctions . printFileTree (correctCrate );
120+ HelpFunctions . printFileTree (extractionPath );
136121
137122 // compare the extracted directory with the correct one
138123 assertTrue (HelpFunctions .compareTwoDir (
@@ -151,12 +136,12 @@ void testWritingMakesCopy(@TempDir Path tempDir) throws IOException {
151136 * @throws IOException if an error occurs while writing the crate
152137 */
153138 @ Test
154- void testWritingOnlyConsidersAddedFiles (@ TempDir Path tempDir ) throws IOException {
139+ default void testWritingOnlyConsidersAddedFiles (@ TempDir Path tempDir ) throws IOException {
155140 Path correctCrate = tempDir .resolve ("compare_with_me" );
156141 Path pathToFile = correctCrate .resolve ("cp7glop.ai" );
157142 Path pathToDir = correctCrate .resolve ("lots_of_little_files" );
158143
159- this . createManualCrateStructure (correctCrate , pathToFile , pathToDir );
144+ createManualCrateStructure (correctCrate , pathToFile , pathToDir );
160145 {
161146 // This file is not part of the crate, and should therefore not be present
162147 Path falseFile = correctCrate .resolve ("new" );
@@ -176,8 +161,8 @@ void testWritingOnlyConsidersAddedFiles(@TempDir Path tempDir) throws IOExceptio
176161 // extract and compare
177162 Path extractionPath = tempDir .resolve ("extracted_for_testing" );
178163 ensureCrateIsExtractedIn (pathToZip , extractionPath );
179- printFileTree (correctCrate );
180- printFileTree (extractionPath );
164+ HelpFunctions . printFileTree (correctCrate );
165+ HelpFunctions . printFileTree (extractionPath );
181166
182167 assertFalse (HelpFunctions .compareTwoDir (
183168 correctCrate .toFile (),
@@ -187,116 +172,4 @@ void testWritingOnlyConsidersAddedFiles(@TempDir Path tempDir) throws IOExceptio
187172 roCrate ,
188173 "/json/crate/fileAndDir.json" );
189174 }
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- }
302175}
0 commit comments