mirror of
https://github.com/RonanPlugins/BetterRTP.git
synced 2026-11-28 08:44:07 +00:00
Own core and addon editor schemas with BetterSync validation
This commit is contained in:
1 parent
ab9911605c
commit
1d4389188f
10 files changed
+624
-29
No files matched your search
@@ -1,7 +1,7 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>me.SuperRonanCraft</groupId><artifactId>betterrtp-editor-tests</artifactId><version>1.0</version>
|
||||
<properties><bettersync.version>1.0.1</bettersync.version><maven.compiler.release>8</maven.compiler.release><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties>
|
||||
<properties><bettersync.version>1.1.0</bettersync.version><maven.compiler.release>8</maven.compiler.release><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties>
|
||||
<repositories><repository><id>spigot</id><url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url></repository><repository><id>jitpack.io</id><url>https://jitpack.io</url></repository></repositories>
|
||||
<dependencies>
|
||||
<dependency><groupId>com.github.RonanPlugins</groupId><artifactId>BetterSync</artifactId><version>${bettersync.version}</version></dependency>
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import com.ronanplugins.bettersync.editor.betterrtp.EditorConfig;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import java.nio.file.*;
|
||||
@@ -7,7 +6,7 @@ import java.nio.charset.StandardCharsets;
|
||||
public class EditorConfigTest {
|
||||
private final String original = "Language-File: en.yml\nSettings:\n MaxAttempts: 32\n PreloadRadius: 5\nDefault:\n MinRadius: 10\n MaxRadius: 1000\n Shape: square\n Biomes: []\n";
|
||||
private void rejected(String edited) throws Exception {
|
||||
try { EditorConfig.validate(original, edited); fail("Expected rejection"); }
|
||||
try { SchemaConfig.validate(original, edited); fail("Expected rejection"); }
|
||||
catch (java.io.IOException expected) { }
|
||||
}
|
||||
private String addons() throws Exception {
|
||||
@@ -15,15 +14,15 @@ public class EditorConfigTest {
|
||||
}
|
||||
@Test public void acceptsAddonDefaultsAndCollectionEdits() throws Exception {
|
||||
String before = addons();
|
||||
EditorConfig.validate(before, before);
|
||||
SchemaConfig.validate(before, before);
|
||||
String after = before.replace("600: ", "300: ").replace("world_nether:", "survival:").replace("Lines: 3", "Lines: 4");
|
||||
EditorConfig.validate(before, after);
|
||||
SchemaConfig.validate(before, after);
|
||||
Path dir = Files.createTempDirectory("addons-editor");
|
||||
Path file = dir.resolve("config.yml");
|
||||
Files.write(file, before.getBytes(StandardCharsets.UTF_8));
|
||||
Path backup = EditorConfig.apply(file, before.getBytes(StandardCharsets.UTF_8), after, () -> {});
|
||||
Path backup = SchemaConfig.apply(file, before.getBytes(StandardCharsets.UTF_8), after, () -> {});
|
||||
assertEquals(before, new String(Files.readAllBytes(backup), StandardCharsets.UTF_8));
|
||||
assertTrue(EditorConfig.equivalent(after, new String(Files.readAllBytes(file), StandardCharsets.UTF_8)));
|
||||
assertTrue(SchemaConfig.equivalent(after, new String(Files.readAllBytes(file), StandardCharsets.UTF_8)));
|
||||
}
|
||||
@Test public void rejectsInvalidAddonSettingsAndUnsafeYaml() throws Exception {
|
||||
String before = addons();
|
||||
@@ -32,13 +31,13 @@ public class EditorConfigTest {
|
||||
before.replace("AutoRefresh: 0", "AutoRefresh: -1"), before.replace("600:", "bad:"),
|
||||
before.replace("en.yml", "other.yml"), before + "Bad: &anchor value\n",
|
||||
before + "Bad: !!java/object something\n", before + "Bad:\n ==: org.bukkit.inventory.ItemStack\n" }) {
|
||||
try { EditorConfig.validate(before, after); fail("Expected addon validation failure"); }
|
||||
try { SchemaConfig.validate(before, after); fail("Expected addon validation failure"); }
|
||||
catch (java.io.IOException expected) { }
|
||||
}
|
||||
try { EditorConfig.validate(original, before); fail("Expected cross-plugin failure"); }
|
||||
try { SchemaConfig.validate(original, before); fail("Expected cross-plugin failure"); }
|
||||
catch (java.io.IOException expected) { }
|
||||
}
|
||||
@Test public void acceptsValidChange() throws Exception { EditorConfig.validate(original, original.replace("1000", "2000")); }
|
||||
@Test public void acceptsValidChange() throws Exception { SchemaConfig.validate(original, original.replace("1000", "2000")); }
|
||||
@Test public void rejectsWrongTypeAndRange() throws Exception {
|
||||
rejected(original.replace("1000", "false"));
|
||||
rejected(original.replace("1000", "5"));
|
||||
@@ -57,7 +56,7 @@ public class EditorConfigTest {
|
||||
}
|
||||
@Test public void validatesBundledConfig() throws Exception {
|
||||
String bundled = new String(Files.readAllBytes(Paths.get("../../BetterRTP-Core/src/main/resources/config.yml")), StandardCharsets.UTF_8);
|
||||
EditorConfig.validate(bundled, bundled.replace("MaxRadius: 1000\n", "MaxRadius: 2000\n"));
|
||||
SchemaConfig.validate(bundled, bundled.replace("MaxRadius: 1000\n", "MaxRadius: 2000\n"));
|
||||
}
|
||||
@Test public void allowsStaleWritesAndKeepsNewFileAfterReloadFailure() throws Exception {
|
||||
Path dir = Files.createTempDirectory("betterrtp-editor-apply"); Path file = dir.resolve("config.yml");
|
||||
@@ -65,22 +64,22 @@ public class EditorConfigTest {
|
||||
try {
|
||||
Files.write(file, snapshot);
|
||||
String edited = original.replace("1000", "2000");
|
||||
Path backup = EditorConfig.apply(file, snapshot, edited, () -> {});
|
||||
Path backup = SchemaConfig.apply(file, snapshot, edited, () -> {});
|
||||
assertArrayEquals(snapshot, Files.readAllBytes(backup));
|
||||
assertTrue(EditorConfig.equivalent(edited, new String(Files.readAllBytes(file), StandardCharsets.UTF_8)));
|
||||
assertTrue(SchemaConfig.equivalent(edited, new String(Files.readAllBytes(file), StandardCharsets.UTF_8)));
|
||||
// The original snapshot is now stale; applying it is intentionally allowed.
|
||||
EditorConfig.apply(file, snapshot, original, () -> {});
|
||||
SchemaConfig.apply(file, snapshot, original, () -> {});
|
||||
assertArrayEquals(snapshot, Files.readAllBytes(file));
|
||||
byte[] current = Files.readAllBytes(file);
|
||||
final int[] reloads = { 0 };
|
||||
try {
|
||||
EditorConfig.apply(file, current, edited, () -> { reloads[0]++; throw new java.io.IOException("Simulated reload failure"); });
|
||||
SchemaConfig.apply(file, current, edited, () -> { reloads[0]++; throw new java.io.IOException("Simulated reload failure"); });
|
||||
fail("Reload should fail");
|
||||
} catch (java.io.IOException expected) { }
|
||||
assertEquals(1, reloads[0]);
|
||||
assertArrayEquals(edited.getBytes(StandardCharsets.UTF_8), Files.readAllBytes(file));
|
||||
byte[] unchanged = Files.readAllBytes(file);
|
||||
try { EditorConfig.apply(file, snapshot, "broken: [", () -> fail("Must not reload")); fail("Must reject invalid config"); }
|
||||
try { SchemaConfig.apply(file, snapshot, "broken: [", () -> fail("Must not reload")); fail("Must reject invalid config"); }
|
||||
catch (Exception expected) { }
|
||||
assertArrayEquals(unchanged, Files.readAllBytes(file));
|
||||
} finally {
|
||||
@@ -96,9 +95,9 @@ public class EditorConfigTest {
|
||||
for (Path file : (Iterable<Path>) files::iterator) {
|
||||
String value = new String(Files.readAllBytes(file), StandardCharsets.UTF_8);
|
||||
if (file.getFileName().toString().startsWith("invalid-")) {
|
||||
try { EditorConfig.validate(baseline, value); fail("Expected rejection: " + file); }
|
||||
try { SchemaConfig.validate(baseline, value); fail("Expected rejection: " + file); }
|
||||
catch (java.io.IOException expected) { }
|
||||
} else EditorConfig.validate(baseline, value);
|
||||
} else SchemaConfig.validate(baseline, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -108,9 +107,9 @@ public class EditorConfigTest {
|
||||
try {
|
||||
Files.write(file, original.getBytes(StandardCharsets.UTF_8));
|
||||
byte[] replacement = original.replace("1000", "2000").getBytes(StandardCharsets.UTF_8);
|
||||
EditorConfig.replace(file, replacement); assertArrayEquals(replacement, EditorConfig.read(file));
|
||||
Files.write(file, new byte[EditorConfig.MAX_BYTES + 1]);
|
||||
try { EditorConfig.read(file); fail("Oversized read"); } catch (java.io.IOException expected) { }
|
||||
SchemaConfig.replace(file, replacement); assertArrayEquals(replacement, SchemaConfig.read(file));
|
||||
Files.write(file, new byte[SchemaConfig.MAX_BYTES + 1]);
|
||||
try { SchemaConfig.read(file); fail("Oversized read"); } catch (java.io.IOException expected) { }
|
||||
} finally { Files.deleteIfExists(file); Files.deleteIfExists(dir); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import com.ronanplugins.bettersync.editor.EditorSchema;
|
||||
import com.ronanplugins.bettersync.editor.EditorFiles;
|
||||
import java.nio.file.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
|
||||
/** Tests the actual plugin-owned resource, not BetterSync's legacy profile. */
|
||||
final class SchemaConfig {
|
||||
static final int MAX_BYTES = EditorFiles.MAX_BYTES;
|
||||
private static EditorSchema schema(String text) throws Exception {
|
||||
return new EditorSchema(new String(Files.readAllBytes(Paths.get(text.contains("Flashback:") ? "../../BetterRTPAddons/src/main/resources/META-INF/editor-schema.json" : "../../BetterRTP-Core/src/main/resources/META-INF/editor-schema.json")), StandardCharsets.UTF_8));
|
||||
}
|
||||
static void validate(String before, String after) throws Exception { schema(before).validate(before, after); }
|
||||
static boolean equivalent(String before, String after) throws Exception { return schema(before).equivalent(before, after); }
|
||||
static Path apply(Path file, byte[] before, String after, EditorFiles.Reload reload) throws Exception {
|
||||
return EditorFiles.apply(schema(new String(before, StandardCharsets.UTF_8)), file, before, after, reload);
|
||||
}
|
||||
static byte[] read(Path file) throws Exception { return EditorFiles.read(file); }
|
||||
static void replace(Path file, byte[] bytes) throws Exception { EditorFiles.replace(file, bytes); }
|
||||
}
|
||||
Reference in new issue
Block a user