Skip to content

Commit

Permalink
Make resource tests more resilient against different locales
Browse files Browse the repository at this point in the history
Also the byte string formatter uses the Locale.ROOT for formatting to ensure invariance.
  • Loading branch information
steffen-wilke committed Dec 27, 2023
1 parent 505a074 commit 82b4f2c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -133,11 +134,11 @@ public static String getFileName(final String path, boolean extension) {

final int lastBackslash = name.lastIndexOf(FILE_SEPARATOR);
if (lastBackslash != -1) {
name = name.substring(lastBackslash + 1, name.length());
name = name.substring(lastBackslash + 1);
} else {
final int lastForwardSlash = name.lastIndexOf(FILE_SEPARATOR_WIN);
if (lastForwardSlash != -1) {
name = name.substring(lastForwardSlash + 1, name.length());
name = name.substring(lastForwardSlash + 1);
}
}

Expand Down Expand Up @@ -217,6 +218,6 @@ public static String humanReadableByteCount(long bytes, boolean decimal) {
int exp = (int) (Math.log(bytes) / Math.log(unit));
String pre = new String[] {"K", "M", "G", "T", "P", "E"}[exp - 1];
pre = decimal ? pre : pre + "i";
return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
return String.format(Locale.ROOT, "%.1f %sB", bytes / Math.pow(unit, exp), pre);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
import de.gurkenlabs.litiengine.Game;
import de.gurkenlabs.litiengine.environment.tilemap.IMap;
import de.gurkenlabs.litiengine.sound.Sound;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Locale;

import org.junit.jupiter.api.Test;

class ResourcesTests {
Expand Down Expand Up @@ -51,16 +54,16 @@ void testResourceContainer() {
@Test
void testMapResourcesAlias() {
IMap map =
Resources.maps().get("de/gurkenlabs/litiengine/environment/tilemap/xml/test-map.tmx");
Resources.maps().get("de/gurkenlabs/litiengine/environment/tilemap/xml/test-map.tmx");

assertEquals(map, Resources.maps().get("test-map"));
}

@Test
void testResourceFromWeb() throws IOException {
try (InputStream stream =
Resources.get(
"/~https://github.com/gurkenlabs/litiengine/blob/1ab49e67edf67242f1e1e6a67b54f36dd9b09c7e/resources/litiengine-banner.png?raw=true")) {
Resources.get(
"/~https://github.com/gurkenlabs/litiengine/blob/1ab49e67edf67242f1e1e6a67b54f36dd9b09c7e/resources/litiengine-banner.png?raw=true")) {
assertNotNull(stream);
}
}
Expand All @@ -78,23 +81,23 @@ void testSoundResources() {
@Test
void testReadStringResources() {
String fileContent =
Resources.read("de/gurkenlabs/litiengine/resources/stringfile-utf8.txt");
Resources.read("de/gurkenlabs/litiengine/resources/stringfile-utf8.txt");
assertEquals("my utf8 èncöded strîng!!1$", fileContent);
}

@Test
void testReadStringCharsetResources() {
String fileContent =
Resources.read(
"de/gurkenlabs/litiengine/resources/stringfile-iso8859-1.txt",
StandardCharsets.ISO_8859_1);
Resources.read(
"de/gurkenlabs/litiengine/resources/stringfile-iso8859-1.txt",
StandardCharsets.ISO_8859_1);
assertEquals("my iso8859 èncöded strîng!!1$", fileContent);
}

@Test
void testStringList() {
String[] strings =
Resources.strings().getList("de/gurkenlabs/litiengine/resources/test.txt");
Resources.strings().getList("de/gurkenlabs/litiengine/resources/test.txt");

assertEquals(4, strings.length);

Expand All @@ -107,13 +110,23 @@ void testStringList() {
@Test
void testLocalizableString() {
final String bundleName = "de/gurkenlabs/litiengine/resources/custom-strings";
String myString = Resources.strings().getFrom(bundleName, "mystring");
String myOtherString = Resources.strings().getFrom(bundleName, "myOtherString");
String lowerCase = Resources.strings().getFrom(bundleName, "myotherstring");
String oldLang = Game.config().client().getLanguage();
String oldCountry = Game.config().client().getCountry();
Game.config().client().setLanguage(Locale.ROOT.getLanguage());
Game.config().client().setCountry(Locale.ROOT.getCountry());
try {
String myString = Resources.strings().getFrom(bundleName, "mystring");
String myOtherString = Resources.strings().getFrom(bundleName, "myOtherString");
String lowerCase = Resources.strings().getFrom(bundleName, "myotherstring");


assertEquals("test me once", myString);
assertEquals("test me twice", myOtherString);
assertEquals("i'm lower case", lowerCase);
assertEquals("test me once", myString);
assertEquals("test me twice", myOtherString);
assertEquals("i'm lower case", lowerCase);
} finally {
Game.config().client().setLanguage(oldLang);
Game.config().client().setCountry(oldCountry);
}
}

@Test
Expand All @@ -136,12 +149,20 @@ void testLocalizedString() {
}
}

@Test
@Test()
void testStringsWithNoArgs() {
final String bundleName = "de/gurkenlabs/litiengine/resources/custom-strings";
String oldLang = Game.config().client().getLanguage();
String oldCountry = Game.config().client().getCountry();
Game.config().client().setLanguage(Locale.ROOT.getLanguage());
Game.config().client().setCountry(Locale.ROOT.getCountry());
try {
String result = Resources.strings().getFrom(bundleName, "mystring", new Object[1]);

String result = Resources.strings().getFrom(bundleName, "mystring", new Object[1]);

assertEquals("test me once", result);
assertEquals("test me once", result);
} finally {
Game.config().client().setLanguage(oldLang);
Game.config().client().setCountry(oldCountry);
}
}
}

0 comments on commit 82b4f2c

Please sign in to comment.