Fixed bug (594469) where unit tests would fail under different locales. Also

fixed a potential bug on how messages are located.
This commit is contained in:
Oliver Burn 2002-08-14 01:58:44 +00:00
parent 816c6b33c9
commit 25a13442f2
5 changed files with 21 additions and 19 deletions

View File

@ -205,7 +205,7 @@
excludes="**/Generated*.java"/>
<formatter type="plain"/>
<formatter type="xml" toFile="target/cs_errors.xml"/>
<classpath refid="run.classpath"/>
<classpath refid="build.classpath"/>
</checkstyle>
</target>

View File

@ -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 **/

View File

@ -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 <code>LocalizedMessage</code> 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;
}
////////////////////////////////////////////////////////////////////////////

View File

@ -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)

View File

@ -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);