mirror of
https://github.com/RonanPlugins/BetterRTP.git
synced 2026-11-28 16:55:07 +00:00
web editor
This commit is contained in:
1 parent
4697f06163
commit
66fe5b9001
10 files changed
+491
No files matched your search
@@ -0,0 +1,18 @@
|
||||
<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><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></repositories>
|
||||
<dependencies>
|
||||
<dependency><groupId>org.spigotmc</groupId><artifactId>spigot-api</artifactId><version>1.8.8-R0.1-SNAPSHOT</version><exclusions><exclusion><groupId>net.md-5</groupId><artifactId>bungeecord-chat</artifactId></exclusion></exclusions></dependency>
|
||||
<dependency><groupId>net.md-5</groupId><artifactId>bungeecord-chat</artifactId><version>1.16-R0.4</version></dependency>
|
||||
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<sourceDirectory>../../src/main/java</sourceDirectory>
|
||||
<plugins>
|
||||
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><version>3.14.0</version><configuration><includes><include>**/web/editor/*.java</include></includes></configuration></plugin>
|
||||
<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><version>3.5.3</version></plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,69 @@
|
||||
import me.SuperRonanCraft.BetterRTP.references.web.editor.EditorConfig;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import java.nio.file.*;
|
||||
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"); }
|
||||
catch (java.io.IOException expected) { }
|
||||
}
|
||||
@Test public void acceptsValidChange() throws Exception { EditorConfig.validate(original, original.replace("1000", "2000")); }
|
||||
@Test public void rejectsWrongTypeAndRange() throws Exception {
|
||||
rejected(original.replace("1000", "false"));
|
||||
rejected(original.replace("1000", "5"));
|
||||
rejected(original.replace("square", "triangle"));
|
||||
rejected(original.replace("PreloadRadius: 5", "PreloadRadius: 5000"));
|
||||
}
|
||||
@Test public void rejectsSchemaAndLanguageChanges() throws Exception {
|
||||
rejected(original + "Injected: true\n");
|
||||
rejected(original.replace("en.yml", "../secrets.yml"));
|
||||
rejected(original.replace(" Shape: square\n", ""));
|
||||
}
|
||||
@Test public void rejectsAliasesAndTags() throws Exception {
|
||||
rejected(original + "Alias: &x value\n");
|
||||
rejected(original + "Tag: !!java/object foo\n");
|
||||
rejected(original + "Object:\n ==: org.bukkit.inventory.ItemStack\n");
|
||||
}
|
||||
@Test public void validatesBundledConfig() throws Exception {
|
||||
String bundled = new String(Files.readAllBytes(Paths.get("../../src/main/resources/config.yml")), StandardCharsets.UTF_8);
|
||||
EditorConfig.validate(bundled, bundled.replace("MaxRadius: 1000\n", "MaxRadius: 2000\n"));
|
||||
}
|
||||
@Test public void refusesStaleWritesAndRestoresAfterReloadFailure() throws Exception {
|
||||
Path dir = Files.createTempDirectory("betterrtp-editor-apply"); Path file = dir.resolve("config.yml");
|
||||
byte[] snapshot = original.getBytes(StandardCharsets.UTF_8);
|
||||
try {
|
||||
Files.write(file, snapshot);
|
||||
String edited = original.replace("1000", "2000");
|
||||
Path backup = EditorConfig.apply(file, snapshot, edited, () -> {});
|
||||
assertArrayEquals(snapshot, Files.readAllBytes(backup));
|
||||
assertTrue(EditorConfig.equivalent(edited, new String(Files.readAllBytes(file), StandardCharsets.UTF_8)));
|
||||
try { EditorConfig.apply(file, snapshot, original, () -> {}); fail("Stale overwrite"); }
|
||||
catch (java.io.IOException expected) { }
|
||||
byte[] current = Files.readAllBytes(file);
|
||||
final int[] reloads = { 0 };
|
||||
try {
|
||||
EditorConfig.apply(file, current, original, () -> { if (reloads[0]++ == 0) throw new java.io.IOException("Simulated reload failure"); });
|
||||
fail("Reload should fail");
|
||||
} catch (java.io.IOException expected) { }
|
||||
assertEquals(2, reloads[0]);
|
||||
assertArrayEquals(current, Files.readAllBytes(file));
|
||||
} finally {
|
||||
try (java.util.stream.Stream<Path> files = Files.walk(dir)) {
|
||||
files.sorted(java.util.Comparator.reverseOrder()).forEach(p -> { try { Files.delete(p); } catch (java.io.IOException e) { throw new RuntimeException(e); } });
|
||||
}
|
||||
}
|
||||
}
|
||||
@Test public void replacesAtomicallyAndBoundsReads() throws Exception {
|
||||
Path dir = Files.createTempDirectory("betterrtp-editor-test"); Path file = dir.resolve("config.yml");
|
||||
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) { }
|
||||
} finally { Files.deleteIfExists(file); Files.deleteIfExists(dir); }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user