-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Sugarcane data files by support differently named JSON files …
…at the root of a Parchment ZIP-File. (#5)
- Loading branch information
Showing
4 changed files
with
134 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...c/test/java/net/neoforged/jst/parchment/namesanddocs/parchment/ParchmentDatabaseTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
package net.neoforged.jst.parchment.namesanddocs.parchment; | ||
|
||
import org.intellij.lang.annotations.Language; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.io.TempDir; | ||
|
||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipOutputStream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
|
||
class ParchmentDatabaseTest { | ||
// A minimal Parchment JSON to validate that it loaded *something* | ||
@Language("JSON") | ||
private static final String EXAMPLE_JSON = """ | ||
{ | ||
"version": "1.1.0", | ||
"classes": [ | ||
{ | ||
"name": "TestClass" | ||
} | ||
] | ||
} | ||
"""; | ||
|
||
@TempDir | ||
Path tempDir; | ||
|
||
@Test | ||
void testLoadFromJsonReader() { | ||
var db = ParchmentDatabase.loadJson(new StringReader(EXAMPLE_JSON)); | ||
assertNotNull(db.getClass("TestClass")); | ||
} | ||
|
||
@Test | ||
void testLoadFromJsonFile() throws IOException { | ||
var tempFile = tempDir.resolve("test.json"); | ||
Files.writeString(tempFile, EXAMPLE_JSON); | ||
|
||
var db = ParchmentDatabase.loadJson(tempFile); | ||
assertNotNull(db.getClass("TestClass")); | ||
} | ||
|
||
@Test | ||
void testLoadFromZipWithParchmentJson() throws IOException { | ||
var tempFile = tempDir.resolve("temp.zip"); | ||
writeZip(tempFile, Map.of( | ||
"unrelated/parchment.json", "INVALID", | ||
"parchment.json", EXAMPLE_JSON | ||
)); | ||
|
||
var db = ParchmentDatabase.loadZip(tempFile); | ||
assertNotNull(db.getClass("TestClass")); | ||
} | ||
|
||
@Test | ||
void testLoadFromZipWithOtherJson() throws IOException { | ||
var tempFile = tempDir.resolve("temp.zip"); | ||
writeZip(tempFile, Map.of( | ||
"unrelated/parchment.json", "INVALID", | ||
"other.json", EXAMPLE_JSON | ||
)); | ||
|
||
var db = ParchmentDatabase.loadZip(tempFile); | ||
assertNotNull(db.getClass("TestClass")); | ||
} | ||
|
||
@Test | ||
void testLoadFromZipWithMultipleOtherJson() throws IOException { | ||
var tempFile = tempDir.resolve("temp.zip"); | ||
writeZip(tempFile, Map.of( | ||
"unrelated/parchment.json", "INVALID", | ||
"other.json", EXAMPLE_JSON, | ||
"yet_another_json.json", EXAMPLE_JSON | ||
)); | ||
|
||
assertThrows(FileNotFoundException.class, () -> ParchmentDatabase.loadZip(tempFile)); | ||
} | ||
|
||
@Test | ||
void testLoadFromEmptyZip() throws IOException { | ||
var tempFile = tempDir.resolve("temp.zip"); | ||
writeZip(tempFile, Map.of()); | ||
|
||
assertThrows(FileNotFoundException.class, () -> ParchmentDatabase.loadZip(tempFile)); | ||
} | ||
|
||
private static void writeZip(Path tempFile, Map<String, String> entries) throws IOException { | ||
try (var zf = new ZipOutputStream(Files.newOutputStream(tempFile))) { | ||
for (var entry : entries.entrySet()) { | ||
zf.putNextEntry(new ZipEntry(entry.getKey())); | ||
zf.write(entry.getValue().getBytes(StandardCharsets.UTF_8)); | ||
zf.closeEntry(); | ||
} | ||
} | ||
} | ||
} |