mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-29 01:04:29 +00:00
Restore language sync
- Bring back 5 commits a force push dropped from GitHub master (0aad88c30)
- Keep the thin Bukkit entry; port language sync into IrisRuntime
- Fetch guides through GitHubLanguageSource instead of the removed catalog
This commit is contained in:
commit
5769b0d4e8
53 files changed
+13688
-14119
No files matched your search
@@ -17,6 +17,7 @@ import static org.junit.Assert.assertEquals;
|
||||
public class NoiseSeedCohesionTest {
|
||||
private static final long[] SEEDS = {813753L, -935731L, Long.MIN_VALUE};
|
||||
private static final double[] COORDINATES = {-29_999_983.25D, -513D, -64D, -1D, 0D, 0.125D, 17D, 64D, 8192D, 29_999_983.25D};
|
||||
private static final double PINNED_SAMPLE_SCALE = 1.0E9D;
|
||||
|
||||
@Test
|
||||
public void allStylesRetainPinnedSamples() {
|
||||
@@ -24,11 +25,11 @@ public class NoiseSeedCohesionTest {
|
||||
for (NoiseStyle style : NoiseStyle.values()) {
|
||||
for (long seed : SEEDS) {
|
||||
for (double sample : samples(style.create(new RNG(seed)), false)) {
|
||||
hash = (hash ^ Double.doubleToLongBits(sample)) * 0x100000001b3L;
|
||||
hash = (hash ^ Math.round(sample * PINNED_SAMPLE_SCALE)) * 0x100000001b3L;
|
||||
}
|
||||
}
|
||||
}
|
||||
assertEquals("All noise style samples", 5059606393854142913L, hash);
|
||||
assertEquals("All noise style samples", 2597318414939754011L, hash);
|
||||
}
|
||||
|
||||
@Test(timeout = 30000)
|
||||
|
||||
@@ -3,24 +3,19 @@ package art.arcane.iris.localization;
|
||||
import art.arcane.iris.testsupport.ProjectPaths;
|
||||
import art.arcane.volmlib.util.localization.LocaleOverlay;
|
||||
import art.arcane.volmlib.util.localization.LocalizationValidator;
|
||||
import art.arcane.volmlib.util.localization.RemoteLanguageCatalog;
|
||||
import art.arcane.volmlib.util.localization.PluginLanguageEditor;
|
||||
import art.arcane.volmlib.util.localization.TextValue;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
@@ -33,85 +28,77 @@ public class IrisDownloadValidationTest {
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
private LoopbackLanguageRepository repository;
|
||||
private File dataFolder;
|
||||
private Path language;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
repository = new LoopbackLanguageRepository();
|
||||
dataFolder = temporaryFolder.newFolder();
|
||||
assertTrue(IrisLanguage.reload(dataFolder, "en_US"));
|
||||
language = dataFolder.toPath().resolve("languages/" + LOCALE + ".toml");
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
IrisLanguage.shutdown();
|
||||
repository.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void missingLanguageDownloadsOnceAndPreservesLaterEdits() throws Exception {
|
||||
String complete = completeCatalog();
|
||||
AtomicInteger requests = new AtomicInteger();
|
||||
HttpServer server = server(complete, requests);
|
||||
try (URLClassLoader resources = resources();
|
||||
RemoteLanguageCatalog catalog = catalog(server, resources)) {
|
||||
Path language = temporaryFolder.newFolder().toPath().resolve("languages/de_DE.toml");
|
||||
assertEquals(complete, catalog.readOrInstall(LOCALE, language, IrisLanguage::validateDownload));
|
||||
assertEquals(complete, Files.readString(language));
|
||||
assertEquals(1, requests.get());
|
||||
repository.publish(LoopbackLanguageRepository.REVISION_ONE, LOCALE, completeCatalog());
|
||||
PluginLanguageEditor.SnapshotLoader loader = IrisLanguage.editorOptions().loader();
|
||||
|
||||
String edited = "\"iris.command.unknown\" = \"Eigener Befehl\"\n";
|
||||
Files.writeString(language, edited);
|
||||
assertEquals(edited, catalog.readOrInstall(LOCALE, language, IrisLanguage::validateDownload));
|
||||
assertEquals(edited, Files.readString(language));
|
||||
assertEquals(1, requests.get());
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
assertEquals("Unbekannter Iris-Befehl", text(loader.load(LOCALE).value(IrisMessages.COMMAND_UNKNOWN)));
|
||||
assertTrue(Files.isRegularFile(language));
|
||||
assertTrue(Files.isRegularFile(dataFolder.toPath().resolve("languages/.upstream/" + LOCALE + ".toml")));
|
||||
assertEquals(1, repository.rawRequests());
|
||||
|
||||
String edited = "\"iris.command.unknown\" = \"Eigener Befehl\"\n";
|
||||
Files.writeString(language, edited);
|
||||
|
||||
assertEquals("Eigener Befehl", text(loader.load(LOCALE).value(IrisMessages.COMMAND_UNKNOWN)));
|
||||
assertEquals(edited, Files.readString(language));
|
||||
assertEquals(1, repository.rawRequests());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void existingUnreadableLanguageIsNeverReplacedByADownload() throws Exception {
|
||||
AtomicInteger requests = new AtomicInteger();
|
||||
HttpServer server = server(completeCatalog(), requests);
|
||||
try (URLClassLoader resources = resources();
|
||||
RemoteLanguageCatalog catalog = catalog(server, resources)) {
|
||||
Path language = temporaryFolder.newFile("de_DE.toml").toPath();
|
||||
String unreadable = "[unterminated";
|
||||
Files.writeString(language, unreadable);
|
||||
repository.publish(LoopbackLanguageRepository.REVISION_ONE, LOCALE, completeCatalog());
|
||||
Files.createDirectories(language.getParent());
|
||||
String unreadable = "[unterminated";
|
||||
Files.writeString(language, unreadable);
|
||||
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> catalog.readOrInstall(LOCALE, language, IrisLanguage::validateDownload));
|
||||
assertEquals(unreadable, Files.readString(language));
|
||||
assertEquals(0, requests.get());
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
assertThrows(Exception.class, () -> IrisLanguage.editorOptions().loader().load(LOCALE));
|
||||
assertEquals(unreadable, Files.readString(language));
|
||||
assertEquals(0, repository.rawRequests());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void malformedDownloadDoesNotCreateALanguageFile() throws Exception {
|
||||
AtomicInteger requests = new AtomicInteger();
|
||||
HttpServer server = server("[unterminated", requests);
|
||||
try (URLClassLoader resources = resources();
|
||||
RemoteLanguageCatalog catalog = catalog(server, resources)) {
|
||||
Path language = temporaryFolder.newFolder().toPath().resolve("de_DE.toml");
|
||||
assertThrows(IllegalArgumentException.class,
|
||||
() -> catalog.readOrInstall(LOCALE, language, IrisLanguage::validateDownload));
|
||||
assertFalse(Files.exists(language));
|
||||
assertEquals(1, requests.get());
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
repository.publish(LoopbackLanguageRepository.REVISION_ONE, LOCALE, "[unterminated");
|
||||
|
||||
assertThrows(Exception.class, () -> IrisLanguage.editorOptions().loader().load(LOCALE));
|
||||
assertFalse(Files.exists(language));
|
||||
assertEquals(1, repository.rawRequests());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void partialDownloadsInstallTheirValidMessages() throws Exception {
|
||||
String partial = """
|
||||
repository.publish(LoopbackLanguageRepository.REVISION_ONE, LOCALE, """
|
||||
"iris.command.unknown" = "Unbekannter Iris-Befehl"
|
||||
"iris.command.permission_denied" = "Missing placeholder"
|
||||
"future.message" = "Future value"
|
||||
""";
|
||||
AtomicInteger requests = new AtomicInteger();
|
||||
HttpServer server = server(partial, requests);
|
||||
try (URLClassLoader resources = resources();
|
||||
RemoteLanguageCatalog catalog = catalog(server, resources)) {
|
||||
Path language = temporaryFolder.newFolder().toPath().resolve("de_DE.toml");
|
||||
assertEquals(partial, catalog.readOrInstall(LOCALE, language, IrisLanguage::validateDownload));
|
||||
assertEquals(partial, Files.readString(language));
|
||||
LocaleOverlay overlay = IrisLanguage.parseOverlay(language.toString(), LOCALE, partial);
|
||||
assertEquals(1, overlay.values().size());
|
||||
assertEquals("Unbekannter Iris-Befehl", ((TextValue)
|
||||
overlay.value(IrisMessages.COMMAND_UNKNOWN.id())).template());
|
||||
assertEquals(1, requests.get());
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
""");
|
||||
|
||||
IrisLanguage.editorOptions().loader().load(LOCALE);
|
||||
|
||||
LocaleOverlay overlay = IrisLanguage.parseOverlay(language.toString(), LOCALE, Files.readString(language));
|
||||
assertEquals(1, overlay.values().size());
|
||||
assertEquals("Unbekannter Iris-Befehl", text(overlay.value(IrisMessages.COMMAND_UNKNOWN.id())));
|
||||
assertEquals(1, repository.rawRequests());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -124,42 +111,11 @@ public class IrisDownloadValidationTest {
|
||||
assertTrue(LocalizationValidator.validate(IrisLanguage.catalog(), List.of(overlay)).errors().isEmpty());
|
||||
}
|
||||
|
||||
private static String text(Object value) {
|
||||
return ((TextValue) value).template();
|
||||
}
|
||||
|
||||
private String completeCatalog() throws IOException {
|
||||
return Files.readString(ProjectPaths.moduleFile("src/main/resources/languages").resolve(LOCALE + ".toml"));
|
||||
}
|
||||
|
||||
private HttpServer server(String content, AtomicInteger requests) throws IOException {
|
||||
byte[] response = content.getBytes(StandardCharsets.UTF_8);
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/master/languages/de_DE.toml", exchange -> respond(exchange, response, requests));
|
||||
server.start();
|
||||
return server;
|
||||
}
|
||||
|
||||
private void respond(HttpExchange exchange, byte[] response, AtomicInteger requests) throws IOException {
|
||||
requests.incrementAndGet();
|
||||
try {
|
||||
exchange.sendResponseHeaders(200, response.length);
|
||||
exchange.getResponseBody().write(response);
|
||||
} finally {
|
||||
exchange.close();
|
||||
}
|
||||
}
|
||||
|
||||
private URLClassLoader resources() throws IOException {
|
||||
Path resources = temporaryFolder.newFolder().toPath();
|
||||
Files.writeString(resources.resolve("source.properties"), "revision=master\nlocales=de_DE\n");
|
||||
return new URLClassLoader(new URL[]{resources.toUri().toURL()}, null);
|
||||
}
|
||||
|
||||
private RemoteLanguageCatalog catalog(HttpServer server, ClassLoader resources) throws IOException {
|
||||
return RemoteLanguageCatalog.load(new RemoteLanguageCatalog.Options(
|
||||
"Iris",
|
||||
URI.create("http://127.0.0.1:" + server.getAddress().getPort() + "/"),
|
||||
"languages",
|
||||
".toml",
|
||||
"source.properties",
|
||||
resources
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -2,18 +2,16 @@ package art.arcane.iris.localization;
|
||||
|
||||
import art.arcane.iris.testsupport.ProjectPaths;
|
||||
import art.arcane.volmlib.util.config.TomlCodec;
|
||||
import art.arcane.volmlib.util.localization.LanguageMeta;
|
||||
import art.arcane.volmlib.util.localization.LocalizationCandidate;
|
||||
import art.arcane.volmlib.util.localization.LocalizationSnapshot;
|
||||
import art.arcane.volmlib.util.localization.PluginLanguageEditor;
|
||||
import art.arcane.volmlib.util.localization.PluginLanguageService;
|
||||
import art.arcane.volmlib.util.localization.PluralSelector;
|
||||
import art.arcane.volmlib.util.localization.RemoteLanguageCatalog;
|
||||
import art.arcane.volmlib.util.localization.TextValue;
|
||||
import art.arcane.volmlib.util.localization.TomlLanguageEditor;
|
||||
import art.arcane.volmlib.util.localization.VolmitLocales;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
@@ -21,16 +19,11 @@ import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
@@ -111,29 +104,16 @@ public class IrisLanguageEditorTest {
|
||||
|
||||
@Test
|
||||
public void missingLocaleDownloadsAndCanBeEditedWithoutBeingSelected() throws Exception {
|
||||
byte[] complete = Files.readAllBytes(ProjectPaths.moduleFile("src/main/resources/languages/fr_FR.toml"));
|
||||
AtomicInteger requests = new AtomicInteger();
|
||||
Field remoteField = IrisLanguage.class.getDeclaredField("remote");
|
||||
remoteField.setAccessible(true);
|
||||
RemoteLanguageCatalog previous = IrisLanguage.remote(directory);
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
try (RemoteLanguageCatalog catalog = RemoteLanguageCatalog.load(new RemoteLanguageCatalog.Options(
|
||||
"Iris",
|
||||
URI.create("http://127.0.0.1:" + server.getAddress().getPort() + "/"),
|
||||
"languages",
|
||||
".toml",
|
||||
"iris-language-source.properties",
|
||||
IrisLanguage.class.getClassLoader()
|
||||
))) {
|
||||
server.createContext(catalog.sourceUri("fr_FR").getPath(),
|
||||
exchange -> respond(exchange, complete, requests));
|
||||
server.start();
|
||||
remoteField.set(null, catalog);
|
||||
String complete = Files.readString(ProjectPaths.moduleFile("src/main/resources/languages/fr_FR.toml"));
|
||||
try (LoopbackLanguageRepository repository = new LoopbackLanguageRepository()) {
|
||||
repository.publish(LoopbackLanguageRepository.REVISION_ONE, "fr_FR", complete);
|
||||
Path language = directory.toPath().resolve("languages/fr_FR.toml");
|
||||
Files.delete(language);
|
||||
|
||||
PluginLanguageEditor.Document original = editor.load("fr_FR").get(5, TimeUnit.SECONDS);
|
||||
assertArrayEquals(complete, Files.readAllBytes(language));
|
||||
assertTrue(Files.isRegularFile(language));
|
||||
assertEquals(IrisLanguage.parseOverlay("source", "fr_FR", complete).values(),
|
||||
IrisLanguage.parseOverlay("installed", "fr_FR", Files.readString(language)).values());
|
||||
TextValue value = new TextValue("Langue {locale}");
|
||||
PluginLanguageEditor.Document saved = editor.save(new PluginLanguageEditor.Edit("fr_FR", IrisMessages.COMMAND_RELOAD_SUCCESS.id(),
|
||||
original.snapshot().value(IrisMessages.COMMAND_RELOAD_SUCCESS), value)).get(5, TimeUnit.SECONDS);
|
||||
@@ -142,13 +122,22 @@ public class IrisLanguageEditorTest {
|
||||
assertEquals("en_US", languages.defaultLocale());
|
||||
assertEquals("en_US", IrisLanguage.activeLocale());
|
||||
assertEquals(value, IrisLanguage.editorOptions().loader().load("fr_FR").value(IrisMessages.COMMAND_RELOAD_SUCCESS));
|
||||
assertEquals(1, requests.get());
|
||||
} finally {
|
||||
remoteField.set(null, previous);
|
||||
server.stop(0);
|
||||
assertEquals(1, repository.rawRequests());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void editingAnUnknownLocaleCreatesACustomLanguageWithMeta() throws Exception {
|
||||
IrisLanguage.editorOptions().loader().load("pirate");
|
||||
|
||||
Path file = directory.toPath().resolve("languages/pirate.toml");
|
||||
LanguageMeta meta = LanguageMeta.parse(Files.readString(file));
|
||||
assertEquals("pirate", meta.name().orElseThrow());
|
||||
assertEquals("en_US", meta.base().orElseThrow());
|
||||
assertTrue(IrisLanguage.availableLocales().contains("pirate"));
|
||||
assertEquals("pirate", IrisLanguage.displayName("pirate"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void savingOneMessagePreservesUnknownValuesAndLeadingComments() throws Exception {
|
||||
Path file = directory.toPath().resolve("languages/fr_FR.toml");
|
||||
@@ -197,14 +186,4 @@ public class IrisLanguageEditorTest {
|
||||
assertEquals("en_US", languages.defaultLocale());
|
||||
assertEquals(value, IrisLanguage.editorOptions().loader().load("en_US").value(IrisMessages.COMMAND_UNKNOWN));
|
||||
}
|
||||
|
||||
private static void respond(HttpExchange exchange, byte[] response, AtomicInteger requests) throws IOException {
|
||||
requests.incrementAndGet();
|
||||
try {
|
||||
exchange.sendResponseHeaders(200, response.length);
|
||||
exchange.getResponseBody().write(response);
|
||||
} finally {
|
||||
exchange.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -1,5 +1,6 @@
|
||||
package art.arcane.iris.localization;
|
||||
|
||||
import art.arcane.volmlib.util.localization.LanguageDirectory;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
@@ -56,9 +57,9 @@ public class IrisLanguageHotloadSnapshotTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void snapshotsRejectFilesLargerThanTwoMebibytes() throws Exception {
|
||||
public void snapshotsRejectFilesOverTheLanguageSizeLimit() throws Exception {
|
||||
File language = temporaryFolder.newFile("en_US.toml");
|
||||
Files.write(language.toPath(), new byte[2 * 1024 * 1024 + 1]);
|
||||
Files.write(language.toPath(), new byte[(int) LanguageDirectory.MAXIMUM_BYTES + 1]);
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> IrisLanguage.captureHotloadSnapshot(language, "en_US"));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
package art.arcane.iris.localization;
|
||||
|
||||
import art.arcane.volmlib.util.localization.LanguageCatalogMerge;
|
||||
import art.arcane.volmlib.util.localization.LanguageUpstreamSync;
|
||||
import art.arcane.volmlib.util.localization.MessageArgs;
|
||||
import art.arcane.volmlib.util.localization.MessageArgument;
|
||||
import art.arcane.volmlib.util.localization.MessageValue;
|
||||
import art.arcane.volmlib.util.localization.TextValue;
|
||||
import art.arcane.volmlib.util.localization.TomlLanguageEditor;
|
||||
import art.arcane.volmlib.util.localization.TomlLanguageParser;
|
||||
import art.arcane.volmlib.util.localization.VolmitLocales;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class IrisLanguageSyncTest {
|
||||
@Rule
|
||||
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
private File dataFolder;
|
||||
private Path languages;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
dataFolder = temporaryFolder.newFolder();
|
||||
languages = dataFolder.toPath().resolve("languages");
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
IrisLanguage.shutdown();
|
||||
assertTrue(IrisLanguage.reload(dataFolder, "en_US"));
|
||||
IrisLanguage.shutdown();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updateMergesTheRepositoryBranchWhileManualEditsAndCustomFilesSurvive() throws Exception {
|
||||
try (LoopbackLanguageRepository repository = new LoopbackLanguageRepository()) {
|
||||
repository.publish(LoopbackLanguageRepository.REVISION_ONE, "fr_FR", """
|
||||
[iris.command]
|
||||
unknown = "Commande inconnue v1"
|
||||
|
||||
[iris.command.reload]
|
||||
success = "Langue {locale} v1"
|
||||
""");
|
||||
assertTrue(IrisLanguage.reload(dataFolder, "en_US"));
|
||||
IrisLanguage.start();
|
||||
UUID player = UUID.randomUUID();
|
||||
IrisLanguage.selections().selectPlayer(player, "fr_FR").get(10L, TimeUnit.SECONDS);
|
||||
|
||||
Path french = languages.resolve("fr_FR.toml");
|
||||
assertTrue(Files.isRegularFile(languages.resolve(".upstream/fr_FR.toml")));
|
||||
assertTrue(Files.isRegularFile(languages.resolve(".upstream/upstream.properties")));
|
||||
assertEquals("Commande inconnue v1", IrisLanguage.plain(player, IrisMessages.COMMAND_UNKNOWN, MessageArgs.empty()));
|
||||
|
||||
Files.writeString(french, TomlLanguageEditor.upsert(Files.readString(french),
|
||||
IrisMessages.COMMAND_RELOAD_SUCCESS.id(), new TextValue("Langue locale {locale}")).content());
|
||||
Path pirate = languages.resolve("pirate.toml");
|
||||
String custom = """
|
||||
[meta]
|
||||
name = "Pirate Speak"
|
||||
base = "fr_FR"
|
||||
|
||||
[iris.command]
|
||||
unknown = "Arr, no such command"
|
||||
""";
|
||||
Files.writeString(pirate, custom);
|
||||
repository.publish(LoopbackLanguageRepository.REVISION_TWO, "fr_FR", """
|
||||
[iris.command]
|
||||
unknown = "Commande inconnue v2"
|
||||
permission_denied = "Permission {permission} manquante"
|
||||
|
||||
[iris.command.reload]
|
||||
success = "Langue {locale} v2"
|
||||
""");
|
||||
repository.advance(LoopbackLanguageRepository.REVISION_TWO);
|
||||
Path state = languages.resolve(".upstream/upstream.properties");
|
||||
Files.writeString(state, Files.readString(state).replaceAll("(?m)^checked=.*$", "checked=0"));
|
||||
|
||||
LanguageUpstreamSync.SyncReport report = IrisLanguage.checkUpstream().get(10L, TimeUnit.SECONDS);
|
||||
|
||||
assertTrue(report.failure().isEmpty());
|
||||
assertEquals(List.of("fr_FR"), report.changedLocales());
|
||||
LanguageCatalogMerge.MergeResult merged = report.merged().get("fr_FR");
|
||||
assertEquals(List.of(IrisMessages.COMMAND_RELOAD_SUCCESS.id()), merged.kept());
|
||||
assertEquals(List.of(IrisMessages.COMMAND_UNKNOWN.id()), merged.updated());
|
||||
assertEquals(List.of(IrisMessages.COMMAND_PERMISSION_DENIED.id()), merged.added());
|
||||
Map<String, MessageValue> values = TomlLanguageParser.parseValidValues(Files.readString(french), IrisLanguage.catalog());
|
||||
assertEquals("Langue locale {locale}", text(values.get(IrisMessages.COMMAND_RELOAD_SUCCESS.id())));
|
||||
assertEquals("Commande inconnue v2", text(values.get(IrisMessages.COMMAND_UNKNOWN.id())));
|
||||
assertTrue(Files.readString(languages.resolve(".upstream/fr_FR.toml")).contains("Langue {locale} v2"));
|
||||
assertEquals(custom, Files.readString(pirate));
|
||||
assertEquals("Commande inconnue v2", IrisLanguage.plain(player, IrisMessages.COMMAND_UNKNOWN, MessageArgs.empty()));
|
||||
assertEquals(LoopbackLanguageRepository.REVISION_TWO,
|
||||
IrisLanguage.upstreamStatus().orElseThrow().revision().orElseThrow());
|
||||
assertTrue(IrisLanguage.debugReport().contains(LoopbackLanguageRepository.REVISION_TWO));
|
||||
|
||||
String settled = Files.readString(french);
|
||||
LanguageUpstreamSync.SyncReport again = IrisLanguage.checkUpstream().get(10L, TimeUnit.SECONDS);
|
||||
|
||||
assertTrue(again.coolingDown());
|
||||
assertTrue(again.changedLocales().isEmpty());
|
||||
assertEquals(settled, Files.readString(french));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void metaBaseChainResolvesBeforeEnglish() throws Exception {
|
||||
Files.createDirectories(languages);
|
||||
Files.writeString(languages.resolve("de_DE.toml"), """
|
||||
[iris.command]
|
||||
unknown = "Unbekannter Befehl"
|
||||
|
||||
[iris.command.reload]
|
||||
success = "Sprache {locale} geladen"
|
||||
""");
|
||||
Files.writeString(languages.resolve("de_AT.toml"), """
|
||||
[meta]
|
||||
name = "Österreichisch"
|
||||
base = "de_DE"
|
||||
|
||||
[iris.command]
|
||||
unknown = "Unbekannter Befehl, Oida"
|
||||
""");
|
||||
|
||||
assertTrue(IrisLanguage.reload(dataFolder, "de_AT"));
|
||||
|
||||
assertEquals("Unbekannter Befehl, Oida", IrisLanguage.plain(IrisMessages.COMMAND_UNKNOWN));
|
||||
assertEquals("Sprache x geladen", IrisLanguage.plain(IrisMessages.COMMAND_RELOAD_SUCCESS,
|
||||
MessageArgument.untrusted("locale", "x")));
|
||||
assertFalse(IrisLanguage.plain(IrisMessages.COMMAND_PERMISSION_DENIED,
|
||||
MessageArgument.untrusted("permission", "iris.all")).isBlank());
|
||||
assertTrue(IrisLanguage.availableLocales().contains("de_AT"));
|
||||
assertEquals("Österreichisch", IrisLanguage.displayName("de_AT"));
|
||||
assertEquals("Österreichisch", IrisLanguage.metaName("de_AT"));
|
||||
assertNull(IrisLanguage.metaName("de_DE"));
|
||||
assertEquals(VolmitLocales.displayName("de_DE").orElseThrow(), IrisLanguage.displayName("de_DE"));
|
||||
assertEquals("de_DE", IrisLanguage.meta("de_AT").base().orElseThrow());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void englishReferenceGainsNewCatalogKeysWhileKeepingEdits() throws Exception {
|
||||
assertTrue(IrisLanguage.reload(dataFolder, "en_US"));
|
||||
Path english = languages.resolve("en_US.toml");
|
||||
Path sidecar = languages.resolve(".upstream/en_US.toml");
|
||||
String older = TomlLanguageEditor.remove(Files.readString(sidecar), IrisMessages.COMMAND_UNKNOWN.id()).content();
|
||||
Files.writeString(sidecar, older);
|
||||
Files.writeString(english, TomlLanguageEditor.upsert(older, IrisMessages.COMMAND_RELOAD_SUCCESS.id(),
|
||||
new TextValue("Reloaded {locale} locally")).content());
|
||||
IrisLanguage.shutdown();
|
||||
|
||||
assertTrue(IrisLanguage.reload(dataFolder, "en_US"));
|
||||
|
||||
Map<String, MessageValue> values = TomlLanguageParser.parseValidValues(Files.readString(english), IrisLanguage.catalog());
|
||||
assertEquals(IrisLanguage.plain(IrisMessages.COMMAND_UNKNOWN), text(values.get(IrisMessages.COMMAND_UNKNOWN.id())));
|
||||
assertEquals("Reloaded {locale} locally", text(values.get(IrisMessages.COMMAND_RELOAD_SUCCESS.id())));
|
||||
assertEquals("Reloaded x locally", IrisLanguage.plain(IrisMessages.COMMAND_RELOAD_SUCCESS,
|
||||
MessageArgument.untrusted("locale", "x")));
|
||||
assertEquals(IrisLanguage.englishReference(), Files.readString(sidecar));
|
||||
}
|
||||
|
||||
private static String text(Object value) {
|
||||
return ((TextValue) value).template();
|
||||
}
|
||||
}
|
||||
@@ -286,7 +286,8 @@ public class IrisLanguageTest {
|
||||
public void everyTranslatedGuideDescribesItsVariablesAndMatchesThePackagedGuide() throws Exception {
|
||||
Pattern definition = Pattern.compile("(?m)^# \\{([A-Za-z][A-Za-z0-9_]*)} (\\S[^\\r\\n]*)$");
|
||||
for (String locale : VolmitLocales.nonEnglish()) {
|
||||
String source = Files.readString(ProjectPaths.moduleFile("src/main/resources/languages/" + locale + ".toml"));
|
||||
String source = Files.readString(ProjectPaths.moduleFile("src/main/resources/languages/" + locale + ".toml"))
|
||||
.replace("\r\n", "\n");
|
||||
int bodyStart = source.indexOf("\n[") + 1;
|
||||
assertTrue(locale, bodyStart > 0);
|
||||
String header = source.substring(0, bodyStart);
|
||||
|
||||
@@ -0,0 +1,83 @@
|
||||
package art.arcane.iris.localization;
|
||||
|
||||
import art.arcane.volmlib.util.localization.GitHubLanguageSource;
|
||||
import com.sun.net.httpserver.HttpExchange;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
final class LoopbackLanguageRepository implements AutoCloseable {
|
||||
static final String REVISION_ONE = "1111111111111111111111111111111111111111";
|
||||
static final String REVISION_TWO = "2222222222222222222222222222222222222222";
|
||||
|
||||
private final HttpServer server;
|
||||
private final AtomicReference<String> revision = new AtomicReference<>(REVISION_ONE);
|
||||
private final Map<String, String> files = new ConcurrentHashMap<>();
|
||||
private final AtomicInteger rawRequests = new AtomicInteger();
|
||||
|
||||
LoopbackLanguageRepository() throws IOException {
|
||||
server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/api/", this::api);
|
||||
server.createContext("/raw/", this::raw);
|
||||
server.start();
|
||||
URI root = URI.create("http://127.0.0.1:" + server.getAddress().getPort() + "/");
|
||||
IrisLanguage.useLanguageSource(root.resolve("api/"), root.resolve("raw/"));
|
||||
}
|
||||
|
||||
void publish(String publishedRevision, String locale, String content) {
|
||||
files.put(publishedRevision + "/" + locale, content);
|
||||
}
|
||||
|
||||
void advance(String nextRevision) {
|
||||
revision.set(nextRevision);
|
||||
}
|
||||
|
||||
int rawRequests() {
|
||||
return rawRequests.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
server.stop(0);
|
||||
IrisLanguage.useLanguageSource(GitHubLanguageSource.API_ROOT, GitHubLanguageSource.RAW_ROOT);
|
||||
}
|
||||
|
||||
private void api(HttpExchange exchange) throws IOException {
|
||||
String etag = "\"" + revision.get() + "\"";
|
||||
if (etag.equals(exchange.getRequestHeaders().getFirst("If-None-Match"))) {
|
||||
exchange.sendResponseHeaders(304, -1);
|
||||
exchange.close();
|
||||
return;
|
||||
}
|
||||
exchange.getResponseHeaders().set("ETag", etag);
|
||||
respond(exchange, ("[{\"sha\":\"" + revision.get() + "\"}]").getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private void raw(HttpExchange exchange) throws IOException {
|
||||
rawRequests.incrementAndGet();
|
||||
String[] parts = exchange.getRequestURI().getPath().split("/");
|
||||
String content = files.get(parts[4] + "/" + parts[parts.length - 1].replace(".toml", ""));
|
||||
if (content == null) {
|
||||
exchange.sendResponseHeaders(404, -1);
|
||||
exchange.close();
|
||||
return;
|
||||
}
|
||||
respond(exchange, content.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private void respond(HttpExchange exchange, byte[] response) throws IOException {
|
||||
try {
|
||||
exchange.sendResponseHeaders(200, response.length);
|
||||
exchange.getResponseBody().write(response);
|
||||
} finally {
|
||||
exchange.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
+4
-2
@@ -28,6 +28,7 @@ public class ServerConfiguratorRegistryVerificationTest {
|
||||
@Test
|
||||
public void freshlyCompiledRegistryMissReportsOneRestartWarning() {
|
||||
VerificationFixture fixture = missingRegistryFixture();
|
||||
String packPath = new File("packs/overworld").getPath();
|
||||
|
||||
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class);
|
||||
MockedStatic<Bukkit> bukkit = mockStatic(Bukkit.class);
|
||||
@@ -38,7 +39,7 @@ public class ServerConfiguratorRegistryVerificationTest {
|
||||
|
||||
assertTrue(ServerConfigurator.verifyDataPacksPost(Stream.of(fixture.data())));
|
||||
|
||||
logging.verify(() -> IrisLogging.debug("Checking Pack: packs/overworld"));
|
||||
logging.verify(() -> IrisLogging.debug("Checking Pack: " + packPath));
|
||||
logging.verify(() -> IrisLogging.warn(ServerConfigurator.POST_COMPILE_RESTART_WARNING));
|
||||
logging.verifyNoMoreInteractions();
|
||||
}
|
||||
@@ -47,6 +48,7 @@ public class ServerConfiguratorRegistryVerificationTest {
|
||||
@Test
|
||||
public void worldCreationRegistryMissRetainsFullOperationalError() {
|
||||
VerificationFixture fixture = missingRegistryFixture();
|
||||
String dimensionPath = new File("packs/overworld/dimensions/overworld.json").getPath();
|
||||
|
||||
try (MockedStatic<IrisLogging> logging = mockStatic(IrisLogging.class);
|
||||
MockedStatic<INMS> nms = mockStatic(INMS.class)) {
|
||||
@@ -58,7 +60,7 @@ public class ServerConfiguratorRegistryVerificationTest {
|
||||
logging.verify(() -> IrisLogging.warn(
|
||||
"The Biome overworld:missing is not registered on the server."));
|
||||
logging.verify(() -> IrisLogging.warn(
|
||||
"The Dimension Type for packs/overworld/dimensions/overworld.json is not registered on the server."));
|
||||
"The Dimension Type for " + dimensionPath + " is not registered on the server."));
|
||||
logging.verify(() -> IrisLogging.error(
|
||||
"The Pack overworld is INCAPABLE of generating custom biomes"));
|
||||
logging.verify(() -> IrisLogging.error(
|
||||
|
||||
@@ -10,6 +10,7 @@ import java.nio.ByteBuffer;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.nio.file.attribute.BasicFileAttributes;
|
||||
import java.nio.file.attribute.FileTime;
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -19,6 +20,8 @@ import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assume.assumeFalse;
|
||||
import static org.junit.Assume.assumeTrue;
|
||||
|
||||
public final class SavedTerrainRegionSnapshotTest {
|
||||
private static final int SECTOR_BYTES = 4_096;
|
||||
@@ -29,6 +32,7 @@ public final class SavedTerrainRegionSnapshotTest {
|
||||
|
||||
@Test
|
||||
public void extractsInternalAndExternalPayloadDescriptors() throws Exception {
|
||||
assumeTrue(hasFileIdentity());
|
||||
byte[] bytes = new byte[4 * SECTOR_BYTES];
|
||||
ByteBuffer buffer = ByteBuffer.wrap(bytes);
|
||||
buffer.putInt(0, 2 << 8 | 1);
|
||||
@@ -67,6 +71,7 @@ public final class SavedTerrainRegionSnapshotTest {
|
||||
|
||||
@Test
|
||||
public void acceptsAnExactUnpaddedPayload() throws Exception {
|
||||
assumeTrue(hasFileIdentity());
|
||||
byte[] bytes = region(2 << 8 | 1, 3, 3, HEADER_BYTES + 7);
|
||||
bytes[HEADER_BYTES + 5] = 10;
|
||||
bytes[HEADER_BYTES + 6] = 0;
|
||||
@@ -78,6 +83,7 @@ public final class SavedTerrainRegionSnapshotTest {
|
||||
|
||||
@Test
|
||||
public void reusesLargerBuffersWithoutAcceptingStalePayloadBytes() throws Exception {
|
||||
assumeTrue(hasFileIdentity());
|
||||
byte[] larger = region(2 << 8 | 1, 5, 2, 4 * SECTOR_BYTES);
|
||||
ByteBuffer.wrap(larger).putInt(3 * SECTOR_BYTES, 1);
|
||||
larger[3 * SECTOR_BYTES + Integer.BYTES] = 2;
|
||||
@@ -110,6 +116,7 @@ public final class SavedTerrainRegionSnapshotTest {
|
||||
|
||||
@Test
|
||||
public void rejectsAtomicReplacementWithMatchingSizeAndTimestamp() throws Exception {
|
||||
assumeTrue(hasFileIdentity());
|
||||
byte[] bytes = region(2 << 8 | 1, 1, 2, 3 * SECTOR_BYTES);
|
||||
Path file = writeRegion(bytes);
|
||||
SavedTerrainRegionSnapshot snapshot = SavedTerrainRegionSnapshot.read(file, bytes.length, new byte[0]).orElseThrow();
|
||||
@@ -122,6 +129,7 @@ public final class SavedTerrainRegionSnapshotTest {
|
||||
|
||||
@Test
|
||||
public void rejectsSameFilePayloadModificationAndTruncation() throws Exception {
|
||||
assumeTrue(hasFileIdentity());
|
||||
byte[] bytes = region(2 << 8 | 1, 2, 2, 3 * SECTOR_BYTES);
|
||||
Path file = writeRegion(bytes);
|
||||
SavedTerrainRegionSnapshot snapshot = SavedTerrainRegionSnapshot.read(file, bytes.length, new byte[0]).orElseThrow();
|
||||
@@ -142,6 +150,7 @@ public final class SavedTerrainRegionSnapshotTest {
|
||||
|
||||
@Test
|
||||
public void validatesBothHeaderSectorsEvenWhenTimestampIsRestored() throws Exception {
|
||||
assumeTrue(hasFileIdentity());
|
||||
for (int offset : new int[]{0, SECTOR_BYTES}) {
|
||||
byte[] bytes = region(2 << 8 | 1, 1, 2, 3 * SECTOR_BYTES);
|
||||
Path file = writeRegion(bytes);
|
||||
@@ -158,6 +167,7 @@ public final class SavedTerrainRegionSnapshotTest {
|
||||
|
||||
@Test
|
||||
public void rejectsInvalidAllocationsAndPayloadLengths() throws Exception {
|
||||
assumeTrue(hasFileIdentity());
|
||||
int[][] cases = {
|
||||
{0, 1, 2, 3 * SECTOR_BYTES},
|
||||
{1 << 8 | 1, 1, 2, 3 * SECTOR_BYTES},
|
||||
@@ -193,6 +203,17 @@ public final class SavedTerrainRegionSnapshotTest {
|
||||
assertThrows(IOException.class, () -> SavedTerrainRegionSnapshot.read(link, HEADER_BYTES, new byte[0]));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void readsNoSnapshotWithoutFileIdentity() throws Exception {
|
||||
assumeFalse(hasFileIdentity());
|
||||
byte[] bytes = region(2 << 8 | 1, 1, 2, 3 * SECTOR_BYTES);
|
||||
assertTrue(SavedTerrainRegionSnapshot.read(writeRegion(bytes), bytes.length, new byte[0]).isEmpty());
|
||||
}
|
||||
|
||||
private boolean hasFileIdentity() throws IOException {
|
||||
return Files.readAttributes(temporaryFolder.getRoot().toPath(), BasicFileAttributes.class).fileKey() != null;
|
||||
}
|
||||
|
||||
private Path writeRegion(byte[] bytes) throws IOException {
|
||||
Path file = temporaryFolder.newFile().toPath();
|
||||
Files.write(file, bytes);
|
||||
|
||||
@@ -16,6 +16,7 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.util.Arrays;
|
||||
import java.util.Locale;
|
||||
import java.util.Random;
|
||||
import java.util.zip.DeflaterOutputStream;
|
||||
import java.util.zip.GZIPOutputStream;
|
||||
@@ -23,6 +24,7 @@ import java.util.zip.GZIPOutputStream;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assume.assumeFalse;
|
||||
|
||||
public final class SavedTerrainStatusReaderTest {
|
||||
@Rule
|
||||
@@ -47,6 +49,7 @@ public final class SavedTerrainStatusReaderTest {
|
||||
|
||||
@Test
|
||||
public void observesAtomicReplacementOfAnOpenRegion() throws Exception {
|
||||
assumeFalse(System.getProperty("os.name").toLowerCase(Locale.ROOT).contains("win"));
|
||||
Path world = temporaryFolder.newFolder().toPath();
|
||||
Path replacement = temporaryFolder.newFolder().toPath();
|
||||
writeChunk(world, -1, -2, orderedRoot(), 2, false);
|
||||
|
||||
Reference in new issue
Block a user