Commits

davsclaus  committed 148ca94

Fixed XW-404:
o Discovery of resource bundles was flawed when no default bundlle was provided.

git-svn-id: http://svn.opensymphony.com/svn/xwork/branches/2.0@1423e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits 83b391e
  • Branches 2.0, xwork_2_0_7

Comments (0)

Files changed (4)

File src/java/com/opensymphony/xwork2/util/LocalizedTextUtil.java

     private static List DEFAULT_RESOURCE_BUNDLES = null;
     private static final Log LOG = LogFactory.getLog(LocalizedTextUtil.class);
     private static boolean reloadBundles = false;
-    private static final Collection misses = new HashSet();
+    private static final Map<String, String> misses = new HashMap<String, String>();
     private static final Map messageFormats = new HashMap();
 
     static {
      */
     public static ResourceBundle findResourceBundle(String aBundleName, Locale locale) {
         synchronized (misses) {
+            String key = createMissesKey(aBundleName, locale);
             try {
-                if (!misses.contains(aBundleName)) {
+                if (!misses.containsKey(key)) {
                     return ResourceBundle.getBundle(aBundleName, locale, Thread.currentThread().getContextClassLoader());
                 }
             } catch (MissingResourceException ex) {
-                misses.add(aBundleName);
+                misses.put(key, aBundleName);
             }
         }
 
     }
 
     /**
+     * Creates a key to used for lookup/storing in the bundle misses cache.
+     *
+     * @param aBundleName  the name of the bundle (usually it's FQN classname).
+     * @param locale       the locale.
+     * @return the key to use for lookup/storing in the bundle misses cache.
+     */
+    private static String createMissesKey(String aBundleName, Locale locale) {
+        return aBundleName + "_" + locale.toString();
+    }
+
+    /**
      * Calls {@link #findText(Class aClass, String aTextName, Locale locale, String defaultMessage, Object[] args)}
      * with aTextName as the default message.
      *
      * Determines if we found the text in the bundles.
      * 
      * @param result   the result so far
-     * @param locale   the locale
-     * @param key1     the first key used for lookup
-     * @param key2     the second key (indexed) used for kookup
-     * @param defaultMessage  the provided default message
      * @return  <tt>true</tt> if we could <b>not</b> find the text, <tt>false</tt> if the text was found (=success). 
      */
     private static boolean unableToFindTextForKey(GetDefaultMessageReturnArg result) {

File src/test/com/opensymphony/xwork2/util/LocalizedTextUtilTest.java

 import java.util.Collections;
 import java.util.Date;
 import java.util.Locale;
+import java.util.ResourceBundle;
 
 
 /**
         assertEquals("Santa", text5); // should not log
     }
 
+    public void testXW404() {
+        // This tests will try to load bundles from the 3 locales but we only have files for France and Germany.
+        // Before this fix loading the bundle for Germany failed since Italy have previously failed and thus the misses cache
+        // contained a false entry
+        ResourceBundle rbFrance = LocalizedTextUtil.findResourceBundle("com/opensymphony/xwork2/util/XW404", Locale.FRANCE);
+        ResourceBundle rbItaly = LocalizedTextUtil.findResourceBundle("com/opensymphony/xwork2/util/XW404", Locale.ITALY);
+        ResourceBundle rbGermany = LocalizedTextUtil.findResourceBundle("com/opensymphony/xwork2/util/XW404", Locale.GERMANY);
+
+        assertNotNull(rbFrance);
+        assertEquals("Bonjour", rbFrance.getString("hello"));
+
+        assertNull(rbItaly);
+
+        assertNotNull(rbGermany);
+        assertEquals("Hallo", rbGermany.getString("hello"));
+    }
+
     protected void setUp() throws Exception {
         super.setUp();
         loadConfigurationProviders(new XmlConfigurationProvider("xwork-sample.xml"));
 
-
         ValueStack stack = ValueStackFactory.getFactory().createValueStack();
         ActionContext.setContext(new ActionContext(stack.getContext()));
         ActionContext.getContext().setLocale(Locale.US);

File src/test/com/opensymphony/xwork2/util/XW404_de.properties

+# Do not create a default bundle, XW404 tests a condition where there are no default bundle
+
+hello=Hallo

File src/test/com/opensymphony/xwork2/util/XW404_fr.properties

+# Do not create a default bundle, XW404 tests a condition where there are no default bundle
+
+hello=Bonjour