Ensures usage of UTF-8 in SongodaYamlConfig

Depending on the environment the default system charset might not be UTF-8 beaking
messages files using non-english language etc.

I'm not sure but Spigot might even set it to ASCII? The tests succeed locally because I am
using Manjaro Linux which uses UTF-8 by default in the JVM. But testing a plugin and logging
the default charset returns ASCII instead (on the same machine).
This commit is contained in:
Christian Koop 2023-01-08 12:46:42 +01:00
parent 5f762c9417
commit 339a4d6f6c
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
3 changed files with 16 additions and 6 deletions

View File

@ -11,11 +11,10 @@ import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
@ -131,7 +130,7 @@ public class SongodaYamlConfig extends YamlConfiguration {
}
public void load() throws IOException {
try (Reader reader = new FileReader(this.file)) {
try (Reader reader = Files.newBufferedReader(this.file.toPath(), StandardCharsets.UTF_8)) {
load(reader);
} catch (FileNotFoundException ignore) {
} catch (IOException ex) {
@ -142,7 +141,8 @@ public class SongodaYamlConfig extends YamlConfiguration {
public void save() throws IOException {
Files.createDirectories(this.file.toPath().getParent());
try (Writer writer = new FileWriter(this.file)) {
try (Writer writer = Files.newBufferedWriter(this.file.toPath(), StandardCharsets.UTF_8)) {
super.save(writer);
} catch (IOException ex) {
throw new IOException("Unable to save '" + this.file.getPath() + "'", ex);

View File

@ -11,6 +11,7 @@ import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Collections;
import java.util.LinkedList;
@ -45,7 +46,7 @@ public class LocaleFileManager {
throw new IOException("Failed to download language file " + languageFileName); // TODO: Better exception
}
try (Writer writer = new FileWriter(languageFile)) {
try (Writer writer = Files.newBufferedWriter(languageFile.toPath(), StandardCharsets.UTF_8)) {
writer.write(languageFileContents);
}

View File

@ -73,6 +73,10 @@ class SongodaYamlConfigRoundtripTest {
.withDefaultValue("Default value")
.withComment("This is the entry without an upgrade step");
ConfigEntry entryWithCyrillic = cfg.createEntry("entryWithCyrillic")
.withDefaultValue("Кириллица")
.withComment("This is the entry with cyrillic characters");
assertTrue(cfg.init());
assertNull(cfg.get("messages.fooSuccess"));
@ -89,6 +93,9 @@ class SongodaYamlConfigRoundtripTest {
assertTrue(entryWithoutUpgradeStep.has());
assertEquals(cfg.get("entryWithoutUpgradeStep"), entryWithoutUpgradeStep.get());
assertTrue(entryWithCyrillic.has());
assertEquals(cfg.get("entryWithCyrillic"), entryWithCyrillic.get());
assertEquals("# Don't touch this it's used to track the version of the config.\n" +
"version: 3\n" +
"command:\n" +
@ -100,6 +107,8 @@ class SongodaYamlConfigRoundtripTest {
"# This is the incrementer of the 'foo' command\n" +
"incrementer: 0\n" +
"# This is the entry without an upgrade step\n" +
"entryWithoutUpgradeStep: Default value\n", new String(Files.readAllBytes(testFilePath)));
"entryWithoutUpgradeStep: Default value\n" +
"# This is the entry with cyrillic characters\n" +
"entryWithCyrillic: Кириллица\n", new String(Files.readAllBytes(testFilePath)));
}
}