-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-72449] Specify that no fallback to the default locale should…
… be used when looking up a resource bundle via `I18n` action. (#8776) [JENKINS-72449] Specify that no fallback to the default locale should be used when looking up a resource bundle When running the JVM with a default locale that is not english, the resource bundle lookup for english would return a bundle with that default locale, instead of using the "default" that is english. Also changed bundle resolution to use uberClassloader rather than iterating on all plugin classloaders
- Loading branch information
Showing
2 changed files
with
40 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,48 +24,53 @@ | |
|
||
package jenkins.util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertSame; | ||
import static org.junit.Assert.assertThrows; | ||
|
||
import java.util.Locale; | ||
import java.util.MissingResourceException; | ||
import net.sf.json.JSONObject; | ||
import org.junit.Assert; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
|
||
/** | ||
* @author <a href="mailto:[email protected]">[email protected]</a> | ||
*/ | ||
public class ResourceBundleUtilTest { | ||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
|
||
/** | ||
* Test resource bundle loading for a defined locale. | ||
*/ | ||
@Test | ||
public void test_known_locale() { | ||
JSONObject bundle = ResourceBundleUtil.getBundle("hudson.logging.Messages", Locale.GERMAN); | ||
Assert.assertEquals("Initialisiere Log-Rekorder", bundle.getString("LogRecorderManager.init")); | ||
assertEquals("Initialisiere Log-Rekorder", bundle.getString("LogRecorderManager.init")); | ||
bundle = ResourceBundleUtil.getBundle("hudson.logging.Messages", new Locale("de")); | ||
Assert.assertEquals("Initialisiere Log-Rekorder", bundle.getString("LogRecorderManager.init")); | ||
assertEquals("Initialisiere Log-Rekorder", bundle.getString("LogRecorderManager.init")); | ||
|
||
// Test caching - should get the same bundle instance back... | ||
Assert.assertSame(ResourceBundleUtil.getBundle("hudson.logging.Messages", new Locale("de")), bundle); | ||
assertSame(ResourceBundleUtil.getBundle("hudson.logging.Messages", new Locale("de")), bundle); | ||
} | ||
|
||
@Test | ||
public void noFallbackLocale() { | ||
try (var ignored = new DefaultLocale(new Locale("fr"))) { | ||
var bundle = ResourceBundleUtil.getBundle("hudson.logging.Messages", new Locale("en")); | ||
assertEquals("System Log", bundle.getString("LogRecorderManager.DisplayName")); | ||
} | ||
} | ||
|
||
/** | ||
* Test that we get the "default" bundle for an unknown locale. | ||
*/ | ||
@Test | ||
public void test_unknown_locale() { | ||
Locale defaultOSLocale = Locale.getDefault(); | ||
try { | ||
//Set Default-Locale to english | ||
Locale.setDefault(new Locale("en", "US")); | ||
|
||
JSONObject bundle = ResourceBundleUtil.getBundle("hudson.logging.Messages", new Locale("kok")); // konkani | ||
Assert.assertEquals("Initializing log recorders", bundle.getString("LogRecorderManager.init")); | ||
} finally { | ||
Locale.setDefault(defaultOSLocale); | ||
} | ||
JSONObject bundle = ResourceBundleUtil.getBundle("hudson.logging.Messages", new Locale("kok")); // konkani | ||
assertEquals("Initializing log recorders", bundle.getString("LogRecorderManager.init")); | ||
} | ||
|
||
/** | ||
|
@@ -75,4 +80,18 @@ public void test_unknown_locale() { | |
public void test_unknown_bundle() { | ||
assertThrows(MissingResourceException.class, () -> ResourceBundleUtil.getBundle("hudson.blah.Whatever")); | ||
} | ||
|
||
private static class DefaultLocale implements AutoCloseable { | ||
private Locale previous; | ||
|
||
DefaultLocale(Locale locale) { | ||
previous = Locale.getDefault(); | ||
Locale.setDefault(locale); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
Locale.setDefault(previous); | ||
} | ||
} | ||
} |