From 25a13442f2d67729eba3b1e8eff32ed926396ee3 Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Wed, 14 Aug 2002 01:58:44 +0000 Subject: [PATCH] Fixed bug (594469) where unit tests would fail under different locales. Also fixed a potential bug on how messages are located. --- build.xml | 2 +- .../puppycrawl/tools/checkstyle/Checker.java | 5 ++--- .../tools/checkstyle/LocalizedMessage.java | 20 +++++++------------ .../tools/checkstyle/CheckerTest.java | 6 ++++++ .../checkstyle/LocalizedMessageTest.java | 7 +++++-- 5 files changed, 21 insertions(+), 19 deletions(-) diff --git a/build.xml b/build.xml index c8c961dcb..76c3e727d 100644 --- a/build.xml +++ b/build.xml @@ -205,7 +205,7 @@ excludes="**/Generated*.java"/> - + diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java index aeca8b731..f87b9731d 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java @@ -107,9 +107,8 @@ public class Checker mCache = new PropertyCacheFile(aConfig); final Verifier v = new Verifier(aConfig); VerifierSingleton.setInstance(v); - LocalizedMessage.init(new Locale(mConfig.getLocaleLanguage(), - mConfig.getLocaleCountry()), - mConfig.getClassLoader()); + LocalizedMessage.setLocale(new Locale(mConfig.getLocaleLanguage(), + mConfig.getLocaleCountry())); } /** Cleans up the object **/ diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/LocalizedMessage.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/LocalizedMessage.java index 560188468..77a870720 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/LocalizedMessage.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/LocalizedMessage.java @@ -51,9 +51,6 @@ public class LocalizedMessage /** arguments for MessageFormat **/ private final Object[] mArgs; - /** the class loader to locate resource bundles with **/ - private static ClassLoader sLoader; - /** * Creates a new LocalizedMessage instance. * @@ -86,9 +83,12 @@ public class LocalizedMessage /** @return the translated message **/ public String getMessage() { - // Very simple approach - wait for performance problems + // Very simple approach - wait for performance problems. + // Important to use the default class loader, and not the one in the + // Configuration object. This is because the class loader in the + // Configuration is specified by the user for resolving custom classes. final ResourceBundle bundle = - ResourceBundle.getBundle(MESSAGE_BUNDLE, sLocale, sLoader); + ResourceBundle.getBundle(MESSAGE_BUNDLE, sLocale); final String pattern = bundle.getString(mKey); return MessageFormat.format(pattern, mArgs); } @@ -105,16 +105,10 @@ public class LocalizedMessage return mColNo; } - /** - * Initialise the localization of messages - * - * @param aLocale the locale to use for localization - * @param aLoader the class loader to locate resource bundles with - */ - static void init(Locale aLocale, ClassLoader aLoader) + /** @param aLocale the locale to use for localization **/ + static void setLocale(Locale aLocale) { sLocale = aLocale; - sLoader = aLoader; } //////////////////////////////////////////////////////////////////////////// diff --git a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java index 3b42eb4f1..2d8fffe5b 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/CheckerTest.java @@ -8,6 +8,8 @@ import java.io.InputStreamReader; import java.io.LineNumberReader; import java.io.OutputStream; import java.io.PrintStream; +import java.util.Locale; + import junit.framework.TestCase; import org.apache.regexp.RESyntaxException; @@ -47,6 +49,10 @@ public class CheckerTest mConfig.setLeftCurlyOptionProperty(Defn.LCURLY_TYPE_PROP, LeftCurlyOption.NL); mConfig.setRCurly(RightCurlyOption.ALONE); + mConfig.setStringProperty(Defn.LOCALE_COUNTRY_PROP, + Locale.ENGLISH.getCountry()); + mConfig.setStringProperty(Defn.LOCALE_LANGUAGE_PROP, + Locale.ENGLISH.getLanguage()); } static String getPath(String aFilename) diff --git a/src/tests/com/puppycrawl/tools/checkstyle/LocalizedMessageTest.java b/src/tests/com/puppycrawl/tools/checkstyle/LocalizedMessageTest.java index a1ffe5dd2..46717d1a5 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/LocalizedMessageTest.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/LocalizedMessageTest.java @@ -12,10 +12,13 @@ public class LocalizedMessageTest super(name); } + protected void setUp() throws Exception + { + LocalizedMessage.setLocale(Locale.ENGLISH); + } + public void testMisc() { - LocalizedMessage.init(Locale.getDefault(), - Thread.currentThread().getContextClassLoader()); LocalizedMessage lm = new LocalizedMessage(0, "DefaultLogger.auditFinished", null); assertNotNull(lm);