Skip to content

Commit 0e820cd

Browse files
committed
WIP writing ELN writer test
1 parent e41cfcf commit 0e820cd

7 files changed

Lines changed: 245 additions & 137 deletions

File tree

src/test/java/edu/kit/datamanager/ro_crate/HelpFunctions.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import java.io.File;
1818
import java.io.IOException;
1919
import java.io.InputStream;
20+
import java.nio.file.Files;
21+
import java.nio.file.Path;
2022
import java.util.Map;
2123
import java.util.function.Function;
2224
import java.util.stream.Collectors;
@@ -152,4 +154,25 @@ public static boolean compareTwoDir(File dir1, File dir2) throws IOException {
152154
}
153155
return true;
154156
}
157+
158+
/**
159+
* Prints the file tree of the given directory for debugging and understanding
160+
* a test more quickly.
161+
*
162+
* @param directoryToPrint the directory to print
163+
* @throws IOException if an error occurs while printing the file tree
164+
*/
165+
@SuppressWarnings("resource")
166+
void printFileTree(Path directoryToPrint) throws IOException {
167+
// Print all files recursively in a tree structure for debugging
168+
System.out.printf("Files in %s:%n", directoryToPrint.getFileName().toString());
169+
Files.walk(directoryToPrint)
170+
.forEach(path -> {
171+
if (!path.toAbsolutePath().equals(directoryToPrint.toAbsolutePath())) {
172+
int depth = path.relativize(directoryToPrint).getNameCount();
173+
String prefix = " ".repeat(depth);
174+
System.out.printf("%s%s%s%n", prefix, "└── ", path.getFileName());
175+
}
176+
});
177+
}
155178
}

src/test/java/edu/kit/datamanager/ro_crate/writer/CrateWriterTest.java renamed to src/test/java/edu/kit/datamanager/ro_crate/writer/CommonWriterTest.java

Lines changed: 9 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,7 @@
2222
import static org.junit.jupiter.api.Assertions.assertFalse;
2323
import 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
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package edu.kit.datamanager.ro_crate.writer;
2+
3+
import edu.kit.datamanager.ro_crate.Crate;
4+
import edu.kit.datamanager.ro_crate.HelpFunctions;
5+
import edu.kit.datamanager.ro_crate.RoCrate;
6+
import edu.kit.datamanager.ro_crate.reader.GenericReaderStrategy;
7+
import edu.kit.datamanager.ro_crate.reader.TestableReaderStrategy;
8+
import org.apache.commons.io.FileUtils;
9+
import org.junit.jupiter.api.Test;
10+
import org.junit.jupiter.api.io.TempDir;
11+
import org.junit.jupiter.params.ParameterizedTest;
12+
import org.junit.jupiter.params.provider.ValueSource;
13+
14+
import java.io.IOException;
15+
import java.net.URI;
16+
import java.net.URL;
17+
import java.nio.file.Path;
18+
19+
import static org.junit.jupiter.api.Assertions.*;
20+
21+
public interface ElnFileFormatTest<
22+
SOURCE_T,
23+
READER_STRATEGY extends GenericReaderStrategy<SOURCE_T>
24+
>
25+
extends TestableReaderStrategy<SOURCE_T, READER_STRATEGY>
26+
{
27+
28+
/**
29+
* Write in ELN format style, meaning with a subfolder in the zip file.
30+
*
31+
* @param crate the crate to write
32+
* @param target the target path to the save location
33+
* @throws IOException if an error occurs
34+
*/
35+
void saveCrateElnStyle(Crate crate, Path target) throws IOException;
36+
37+
@Test
38+
default void testMakesElnStyleCrate(@TempDir Path tempDir) throws IOException {
39+
// We need a correct directory to compare with.
40+
// It is built manually to ensure we meet our expectations.
41+
// Reader-writer-consistency is tested at {@link CrateReaderTest}
42+
43+
// We compare the ELN style like this:
44+
// tempDir
45+
// └── compare_with_me
46+
// └── crate-subfolder
47+
// ├── ...
48+
// └── extracted_for_testing
49+
// └── crate-subfolder
50+
// ├── ...
51+
String crateName = "crate-subfolder";
52+
Path correctCrate = tempDir
53+
.resolve("compare_with_me")
54+
.resolve(crateName);
55+
Path pathToFile = correctCrate.resolve("cp7glop.ai");
56+
Path pathToDir = correctCrate.resolve("lots_of_little_files");
57+
58+
createManualCrateStructure(correctCrate, pathToFile, pathToDir);
59+
60+
// Now use the builder to build the same crate independently.
61+
// The files will be reused (we need a place to take a copy from)
62+
RoCrate builtCrate = getCrateWithFileAndDir(pathToFile, pathToDir).build();
63+
64+
Path pathToZip = tempDir.resolve("%s.zip".formatted(crateName));
65+
this.saveCrateElnStyle(builtCrate, pathToZip);
66+
67+
// extract the zip file to a temporary directory
68+
Path extractionPath = tempDir.resolve("extracted_for_testing");
69+
ensureCrateIsExtractedIn(pathToZip, extractionPath);
70+
printFileTree(correctCrate);
71+
printFileTree(extractionPath);
72+
73+
// compare the extracted directory with the correct one
74+
assertTrue(HelpFunctions.compareTwoDir(
75+
correctCrate.toFile(),
76+
extractionPath.toFile()));
77+
HelpFunctions.compareCrateJsonToFileInResources(
78+
builtCrate,
79+
"/json/crate/fileAndDir.json");
80+
}
81+
}

src/test/java/edu/kit/datamanager/ro_crate/writer/FolderWriterTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
* @author Nikola Tzotchev on 9.2.2022 г.
1212
* @version 1
1313
*/
14-
class FolderWriterTest extends CrateWriterTest {
14+
class FolderWriterTest implements CommonWriterTest {
1515

1616
@Override
17-
protected void saveCrate(Crate crate, Path target) throws IOException {
17+
public void saveCrate(Crate crate, Path target) throws IOException {
1818
Writers.newFolderWriter()
1919
.save(crate, target.toAbsolutePath().toString());
2020
}
2121

2222
@Override
23-
protected void ensureCrateIsExtractedIn(Path pathToCrate, Path expectedPath) throws IOException {
23+
public void ensureCrateIsExtractedIn(Path pathToCrate, Path expectedPath) throws IOException {
2424
FileUtils.copyDirectory(pathToCrate.toFile(), expectedPath.toFile());
2525
}
2626
}

0 commit comments

Comments
 (0)