From 22a7c5ae94b2a65eed12d4f6e6958d1fb3591467 Mon Sep 17 00:00:00 2001 From: "main()" Date: Fri, 2 Dec 2011 18:39:05 +0100 Subject: [PATCH] Fixed some nasty bugs --- .../LocalizationLoadingException.java | 4 ++ .../localization/SimpleMessageProvider.java | 41 ++++++++++++------- .../MultiverseCore/test/TestLocalization.java | 12 +++--- .../test/utils/TestInstanceCreator.java | 6 +++ 4 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/onarandombox/MultiverseCore/localization/LocalizationLoadingException.java b/src/main/java/com/onarandombox/MultiverseCore/localization/LocalizationLoadingException.java index f8341364..ec57fa4e 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/localization/LocalizationLoadingException.java +++ b/src/main/java/com/onarandombox/MultiverseCore/localization/LocalizationLoadingException.java @@ -35,4 +35,8 @@ public class LocalizationLoadingException extends IOException { return locale; } + public String getMessage() { + return super.getMessage() + " (While trying to load localization for locale " + getLocale() + ")"; + } + } diff --git a/src/main/java/com/onarandombox/MultiverseCore/localization/SimpleMessageProvider.java b/src/main/java/com/onarandombox/MultiverseCore/localization/SimpleMessageProvider.java index 504a4728..c4a1dece 100644 --- a/src/main/java/com/onarandombox/MultiverseCore/localization/SimpleMessageProvider.java +++ b/src/main/java/com/onarandombox/MultiverseCore/localization/SimpleMessageProvider.java @@ -7,7 +7,6 @@ import java.io.InputStream; import java.util.HashMap; import java.util.Locale; import java.util.Set; -import java.util.logging.Level; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; @@ -30,7 +29,7 @@ public class SimpleMessageProvider implements LazyLocaleMessageProvider { try { loadLocale(locale); } catch (NoSuchLocalizationException e) { - //let's take the defaults from the enum! + // let's take the defaults from the enum! } } @@ -39,11 +38,12 @@ public class SimpleMessageProvider implements LazyLocaleMessageProvider { try { loadLocale(locale); } catch (NoSuchLocalizationException e) { - core.log(Level.WARNING, "An error occured while trying to get the localization for: " - + e.getLocale().getDisplayCountry(DEFAULT_LOCALE)); throw e; } } + if (!isLocaleLoaded(locale)) + throw new LocalizationLoadingException("Couldn't load the localization: " + + locale.toString(), locale); } /** @@ -51,26 +51,38 @@ public class SimpleMessageProvider implements LazyLocaleMessageProvider { */ @Override public void loadLocale(Locale l) throws NoSuchLocalizationException { - InputStream stream = null; + messages.remove(l); + + InputStream resstream = null; + InputStream filestream = null; try { - stream = new FileInputStream(new File(core.getDataFolder(), l.getLanguage() + ".yml")); + filestream = new FileInputStream(new File(core.getDataFolder(), l.getLanguage() + ".yml")); } catch (FileNotFoundException e) { } - // only if that file didn't exist, we try to get the localization from the JAR. - if (stream == null) // this way, users can easily overwrite localizations from the JAR. - stream = core.getResource(new StringBuilder(LOCALIZATION_FOLDER_NAME).append("/") + try { + resstream = core.getResource(new StringBuilder(LOCALIZATION_FOLDER_NAME).append("/") .append(l.getLanguage()).append(".yml").toString()); + } catch (Exception e) { + } - if (stream == null) + if ((resstream == null) && (filestream == null)) throw new NoSuchLocalizationException(l); messages.put(l, new HashMap(MultiverseMessage.values().length)); - FileConfiguration config = YamlConfiguration.loadConfiguration(stream); + FileConfiguration resconfig = (resstream == null) ? null : YamlConfiguration.loadConfiguration(resstream); + FileConfiguration fileconfig = (filestream == null) ? null : YamlConfiguration.loadConfiguration(filestream); for (MultiverseMessage m : MultiverseMessage.values()) { - messages.get(l).put(m, config.getString(m.toString(), m.getDefault())); + String value = m.getDefault(); + + if (resconfig != null) + value = resconfig.getString(m.toString(), value); + if (fileconfig != null) + value = fileconfig.getString(m.toString(), value); + + messages.get(l).put(m, value); } } @@ -95,8 +107,9 @@ public class SimpleMessageProvider implements LazyLocaleMessageProvider { */ @Override public String getMessage(MultiverseMessage key) { - if (!isLocaleLoaded(DEFAULT_LOCALE)) + if (!isLocaleLoaded(locale)) { return key.getDefault(); + } else return messages.get(locale).get(key); } @@ -108,11 +121,11 @@ public class SimpleMessageProvider implements LazyLocaleMessageProvider { public String getMessage(MultiverseMessage key, Locale locale) { try { maybeLoadLocale(locale); - return messages.get(locale).get(key); } catch (LocalizationLoadingException e) { e.printStackTrace(); return getMessage(key); } + return messages.get(locale).get(key); } /** diff --git a/src/test/java/com/onarandombox/MultiverseCore/test/TestLocalization.java b/src/test/java/com/onarandombox/MultiverseCore/test/TestLocalization.java index 1a40da0c..a4b46c1a 100644 --- a/src/test/java/com/onarandombox/MultiverseCore/test/TestLocalization.java +++ b/src/test/java/com/onarandombox/MultiverseCore/test/TestLocalization.java @@ -66,8 +66,6 @@ public class TestLocalization { // This should be the same core as creator.getCore() assertEquals(core, creator.getCore()); - new File(TestInstanceCreator.pluginDirectory, "en.yml").delete(); - // Make sure there is neither the file nor the resource assertNull(core.getResource("localization/en.yml")); assertFalse(new File(TestInstanceCreator.pluginDirectory, "en.yml").exists()); @@ -98,8 +96,6 @@ public class TestLocalization { // This should be the same core as creator.getCore() assertEquals(core, creator.getCore()); - new File(TestInstanceCreator.pluginDirectory, "en.yml").delete(); - // Make sure there is no file, only the resource assertFalse(new File(TestInstanceCreator.pluginDirectory, "en.yml").exists()); assertTrue(new File("src/main/resources/localization/en.yml").exists()); @@ -150,13 +146,14 @@ public class TestLocalization { assertEquals(core, creator.getCore()); // Create the file - BufferedWriter bwriter = new BufferedWriter(new FileWriter(new File(TestInstanceCreator.pluginDirectory, "en.yml"))); + File file = new File(TestInstanceCreator.pluginDirectory, "en.yml"); + BufferedWriter bwriter = new BufferedWriter(new FileWriter(file)); String expected = "a test-string from the user-file"; bwriter.write("TEST_STRING: " + expected); bwriter.close(); // Make sure there is the file and the resource - assertTrue(new File(core.getDataFolder(), "en.yml").exists()); + assertTrue(file.exists()); assertTrue(new File("src/main/resources/localization/en.yml").exists()); doAnswer(new Answer() { public InputStream answer(InvocationOnMock invocation) throws Throwable { @@ -182,5 +179,8 @@ public class TestLocalization { String actual = core.getMessageProvider().getMessage(MultiverseMessage.TEST_STRING); assertEquals(expected, actual); + + // Clean up afterwards: + assertTrue(file.delete()); } } diff --git a/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java b/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java index ba506a6d..3463ebb7 100644 --- a/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java +++ b/src/test/java/com/onarandombox/MultiverseCore/test/utils/TestInstanceCreator.java @@ -34,6 +34,8 @@ import org.powermock.api.mockito.PowerMockito; import com.onarandombox.MultiverseCore.MultiverseCore; import com.onarandombox.MultiverseCore.api.MultiverseWorld; +import com.onarandombox.MultiverseCore.localization.MessageProvider; +import com.onarandombox.MultiverseCore.localization.SimpleMessageProvider; import com.onarandombox.MultiverseCore.utils.FileUtils; import com.onarandombox.MultiverseCore.utils.WorldManager; @@ -136,6 +138,10 @@ public class TestInstanceCreator { worldmanagerfield.setAccessible(true); worldmanagerfield.set(core, wm); + // Set messageProvider + MessageProvider messageProvider = PowerMockito.spy(new SimpleMessageProvider(core)); + core.setMessageProvider(messageProvider); + // Init our command sender commandSender = spy(new TestCommandSender(mockServer)); Bukkit.setServer(mockServer);