Author: niallp
Date: Wed Aug 31 22:54:57 2005
New Revision: 265658

URL: http://svn.apache.org/viewcvs?rev=265658&view=rev
Log:
Remove I18nFactorySet copied code - highlighted by Sam Ruby

Modified:
    struts/core/branches/STRUTS_1_2_BRANCH/build-tests.xml
    
struts/core/branches/STRUTS_1_2_BRANCH/src/share/org/apache/struts/tiles/xmlDefinition/I18nFactorySet.java
    
struts/core/branches/STRUTS_1_2_BRANCH/src/test/org/apache/struts/tiles/TestTilesPlugin.java

Modified: struts/core/branches/STRUTS_1_2_BRANCH/build-tests.xml
URL: 
http://svn.apache.org/viewcvs/struts/core/branches/STRUTS_1_2_BRANCH/build-tests.xml?rev=265658&r1=265657&r2=265658&view=diff
==============================================================================
--- struts/core/branches/STRUTS_1_2_BRANCH/build-tests.xml (original)
+++ struts/core/branches/STRUTS_1_2_BRANCH/build-tests.xml Wed Aug 31 22:54:57 
2005
@@ -796,6 +796,7 @@
         <test       name="org.apache.struts.action.TestDynaActionFormClass"/>
         <test       name="org.apache.struts.config.TestModuleConfig"/>
         <test       name="org.apache.struts.config.TestActionConfigMatcher"/>
+        <test       name="org.apache.struts.tiles.TestTilesPlugin"/>
 
         <!-- Tests for org.apache.struts.validator -->
         <test       name="org.apache.struts.validator.TestValidWhen"/>

Modified: 
struts/core/branches/STRUTS_1_2_BRANCH/src/share/org/apache/struts/tiles/xmlDefinition/I18nFactorySet.java
URL: 
http://svn.apache.org/viewcvs/struts/core/branches/STRUTS_1_2_BRANCH/src/share/org/apache/struts/tiles/xmlDefinition/I18nFactorySet.java?rev=265658&r1=265657&r2=265658&view=diff
==============================================================================
--- 
struts/core/branches/STRUTS_1_2_BRANCH/src/share/org/apache/struts/tiles/xmlDefinition/I18nFactorySet.java
 (original)
+++ 
struts/core/branches/STRUTS_1_2_BRANCH/src/share/org/apache/struts/tiles/xmlDefinition/I18nFactorySet.java
 Wed Aug 31 22:54:57 2005
@@ -1,7 +1,7 @@
 /*
  * $Id$ 
  *
- * Copyright 1999-2004 The Apache Software Foundation.
+ * Copyright 1999-2005 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -87,12 +87,6 @@
             "/WEB-INF/componentDefinitions.xml",
             "/WEB-INF/instanceDefinitions.xml" };
 
-    /**
-     * Maximum length of one branch of the resource search path tree.
-     * Used in getBundle().
-     */
-    private static final int MAX_BUNDLES_SEARCHED = 2;
-
     /** 
      * Default filenames extension. 
      */
@@ -332,7 +326,7 @@
         }
 
         // Build possible postfixes
-        List possiblePostfixes = calculatePostixes("", (Locale) key);
+        List possiblePostfixes = calculateSuffixes((Locale) key);
 
         // Search last postix corresponding to a config file to load.
         // First check if something is loaded for this postfix.
@@ -387,50 +381,37 @@
     }
 
     /**
-     * Calculate the postixes along the search path from the base bundle to the
-     * bundle specified by baseName and locale.
-     * Method copied from java.util.ResourceBundle
-     * @param baseName the base bundle name
+     * Calculate the suffixes based on the locale.
      * @param locale the locale
      */
-    private static List calculatePostixes(String baseName, Locale locale) {
-        final List result = new ArrayList(MAX_BUNDLES_SEARCHED);
-        final String language = locale.getLanguage();
-        final int languageLength = language.length();
-        final String country = locale.getCountry();
-        final int countryLength = country.length();
-        final String variant = locale.getVariant();
-        final int variantLength = variant.length();
-
-        if (languageLength + countryLength + variantLength == 0) {
-            //The locale is "", "", "".
-            return result;
-        }
-
-        final StringBuffer temp = new StringBuffer(baseName);
-        temp.append('_');
-        temp.append(language);
-
-        if (languageLength > 0)
-            result.add(temp.toString());
-
-        if (countryLength + variantLength == 0)
-            return result;
-
-        temp.append('_');
-        temp.append(country);
-
-        if (countryLength > 0)
-            result.add(temp.toString());
-
-        if (variantLength == 0) {
-            return result;
-        } else {
-            temp.append('_');
-            temp.append(variant);
-            result.add(temp.toString());
-            return result;
+    private List calculateSuffixes(Locale locale) {
+
+        List suffixes = new ArrayList(3);
+        String language = locale.getLanguage();
+        String country  = locale.getCountry();
+        String variant  = locale.getVariant();
+
+        StringBuffer suffix = new StringBuffer();
+        suffix.append('_');
+        suffix.append(language);
+        if (language.length() > 0) {
+            suffixes.add(suffix.toString());
         }
+
+        suffix.append('_');
+        suffix.append(country);
+        if (country.length() > 0) {
+            suffixes.add(suffix.toString());
+        }
+
+        suffix.append('_');
+        suffix.append(variant);
+        if (variant.length() > 0) {
+            suffixes.add(suffix.toString());
+        }
+
+        return suffixes;
+
     }
 
     /**

Modified: 
struts/core/branches/STRUTS_1_2_BRANCH/src/test/org/apache/struts/tiles/TestTilesPlugin.java
URL: 
http://svn.apache.org/viewcvs/struts/core/branches/STRUTS_1_2_BRANCH/src/test/org/apache/struts/tiles/TestTilesPlugin.java?rev=265658&r1=265657&r2=265658&view=diff
==============================================================================
--- 
struts/core/branches/STRUTS_1_2_BRANCH/src/test/org/apache/struts/tiles/TestTilesPlugin.java
 (original)
+++ 
struts/core/branches/STRUTS_1_2_BRANCH/src/test/org/apache/struts/tiles/TestTilesPlugin.java
 Wed Aug 31 22:54:57 2005
@@ -1,7 +1,7 @@
 /*
  * $Id$ 
  *
- * Copyright 1999-2004 The Apache Software Foundation.
+ * Copyright 1999-2005 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -24,6 +24,11 @@
 import junit.framework.Test;
 import junit.framework.TestSuite;
 
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Locale;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletRequest;
 import org.apache.commons.beanutils.BeanUtils;
 import org.apache.struts.Globals;
 import org.apache.struts.action.PlugIn;
@@ -33,6 +38,7 @@
 import org.apache.struts.mock.MockActionServlet;
 import org.apache.struts.mock.TestMockBase;
 import org.apache.struts.util.RequestUtils;
+import org.apache.struts.tiles.xmlDefinition.I18nFactorySet;
 
 /**
  * <p>Unit tests for <code>org.apache.struts.tiles.*</code>.</p>
@@ -225,6 +231,160 @@
     // Check that factory are different
   assertEquals("Same factory", factory1, factory2);
   }
+
+  /**
+   * Test I18nFactorySet.
+   */
+  public void testI18FactorySet_A() {
+
+     Locale locale = null;
+     ComponentDefinition definition = null;
+     org.apache.struts.tiles.xmlDefinition.DefinitionsFactory factory = null;
+
+     Map properties = new HashMap();
+
+     // Set the file name
+     properties.put(I18nFactorySet.DEFINITIONS_CONFIG_PARAMETER_NAME,
+                    "config/I18nFactorySet-A.xml");
+
+     try {
+         TestI18nFactorySet i18nFactorySet = new TestI18nFactorySet(context, 
properties);
+         String defName = "A-DEFAULT";
+
+         // Default Locale
+         locale = new Locale("", "", "");
+         factory = i18nFactorySet.createFactory(locale , request, context);
+         assertNotNull("DefinitionsFactory is nullfor locale='" + 
print(locale) + "'", factory);
+         definition = factory.getDefinition(defName, request, context);
+         assertNotNull("Definition '" + defName + "' Not Found for locale='" + 
print(locale) + "'", definition);
+         assertEquals("Definition '" + defName + "' for locale='" + 
print(locale) + "'", defName, definition.getName());
+
+         // Variant Only
+         locale = new Locale("", "", "XX");
+         factory = i18nFactorySet.createFactory(locale , request, context);
+         assertNotNull("DefinitionsFactory is null for locale='" + 
print(locale) + "'", factory);
+         definition = factory.getDefinition(defName, request, context);
+         assertNotNull("Definition '" + defName + "' Not Found for locale='" + 
print(locale) + "'", definition);
+         assertEquals("Definition '" + defName + "' for locale='" + 
print(locale) + "'", defName, definition.getName());
+
+         // No Language, Country & Variant Locale
+         locale = new Locale("", "US", "XX");
+         factory = i18nFactorySet.createFactory(locale , request, context);
+         assertNotNull("DefinitionsFactory is null for locale='" + 
print(locale) + "'", factory);
+         definition = factory.getDefinition(defName, request, context);
+         assertNotNull("Definition '" + defName + "' Not Found for locale='" + 
print(locale) + "'", definition);
+         assertEquals("Definition '" + defName + "' for locale='" + 
print(locale) + "'", defName, definition.getName());
+
+         // Language & Country
+         locale = new Locale("en", "US");
+         factory = i18nFactorySet.createFactory(locale , request, context);
+         assertNotNull("DefinitionsFactory is null for locale='" + 
print(locale) + "'", factory);
+         definition = factory.getDefinition(defName, request, context);
+         assertNotNull("Definition '" + defName + "' Not Found for locale='" + 
print(locale) + "'", definition);
+         assertEquals("Definition '" + defName + "' for locale='" + 
print(locale) + "'", defName, definition.getName());
+
+     } catch(Exception ex) {
+         fail(ex.toString());
+     }
+  }
+
+
+  /**
+   * Test I18nFactorySet.
+   */
+  public void testI18FactorySet_B() {
+
+     Locale locale = null;
+     ComponentDefinition definition = null;
+     org.apache.struts.tiles.xmlDefinition.DefinitionsFactory factory = null;
+
+     Map properties = new HashMap();
+
+     // Set the file name
+     properties.put(I18nFactorySet.DEFINITIONS_CONFIG_PARAMETER_NAME,
+                    "config/I18nFactorySet-B.xml");
+
+     try {
+
+         TestI18nFactorySet i18nFactorySet = new TestI18nFactorySet(context, 
properties);
+         String defName = null;
+
+         // Default Locale
+         locale = new Locale("", "", "");
+         factory = i18nFactorySet.createFactory(locale , request, context);
+         assertNotNull("1. DefinitionsFactory is nullfor locale='" + 
print(locale) + "'", factory);
+         defName = "B-DEFAULT";
+         definition = factory.getDefinition(defName, request, context);
+         assertNotNull("2. Definition '" + defName + "' Not Found for 
locale='" + print(locale) + "'", definition);
+         assertEquals("3. Definition '" + defName + "' for locale='" + 
print(locale) + "'", defName, definition.getName());
+
+         // Variant Only
+         locale = new Locale("", "", "XX");
+         factory = i18nFactorySet.createFactory(locale , request, context);
+         assertNotNull("4. DefinitionsFactory is null for locale='" + 
print(locale) + "'", factory);
+         defName = "B___XX";
+         definition = factory.getDefinition(defName, request, context);
+         assertNotNull("5. Definition '" + defName + "' Not Found for 
locale='" + print(locale) + "'", definition);
+         assertEquals("6. Definition '" + defName + "' for locale='" + 
print(locale) + "'", defName, definition.getName());
+         defName = "B-DEFAULT";
+         definition = factory.getDefinition(defName, request, context);
+         assertNotNull("7. Definition '" + defName + "' Not Found for 
locale='" + print(locale) + "'", definition);
+         assertEquals("8. Definition '" + defName + "' for locale='" + 
print(locale) + "'", defName, definition.getName());
+
+         // No Language, Country & Unknown Variant
+         locale = new Locale("", "US", "XX");
+         factory = i18nFactorySet.createFactory(locale , request, context);
+         assertNotNull("9. DefinitionsFactory is null for locale='" + 
print(locale) + "'", factory);
+         defName = "B__US";
+         definition = factory.getDefinition(defName, request, context);
+         assertNotNull("10. Definition '" + defName + "' Not Found for 
locale='" + print(locale) + "'", definition);
+         assertEquals("11. Definition '" + defName + "' for locale='" + 
print(locale) + "'", defName, definition.getName());
+
+         // Language & Country
+         locale = new Locale("en", "US");
+         factory = i18nFactorySet.createFactory(locale , request, context);
+         assertNotNull("12. DefinitionsFactory is null for locale='" + 
print(locale) + "'", factory);
+         defName = "B_en_US";
+         definition = factory.getDefinition(defName, request, context);
+         assertNotNull("13. Definition '" + defName + "' Not Found for 
locale='" + print(locale) + "'", definition);
+         assertEquals("14. Definition '" + defName + "' for locale='" + 
print(locale) + "'", defName, definition.getName());
+
+         // Language, Country & Unknown Variant
+         locale = new Locale("en", "GB", "XX");
+         factory = i18nFactorySet.createFactory(locale , request, context);
+         assertNotNull("15. DefinitionsFactory is null for locale='" + 
print(locale) + "'", factory);
+         defName = "B_en_GB";
+         definition = factory.getDefinition(defName, request, context);
+         assertNotNull("16. Definition '" + defName + "' Not Found for 
locale='" + print(locale) + "'", definition);
+         assertEquals("17. Definition '" + defName + "' for locale='" + 
print(locale) + "'", defName, definition.getName());
+
+         // Language, Unknown Country & Unknown Variant
+         locale = new Locale("en", "FR", "XX");
+         factory = i18nFactorySet.createFactory(locale , request, context);
+         assertNotNull("18. DefinitionsFactory is null for locale='" + 
print(locale) + "'", factory);
+         defName = "B_en";
+         definition = factory.getDefinition(defName, request, context);
+         assertNotNull("19. Definition '" + defName + "' Not Found for 
locale='" + print(locale) + "'", definition);
+         assertEquals("20. Definition '" + defName + "' for locale='" + 
print(locale) + "'", defName, definition.getName());
+
+     } catch(Exception ex) {
+         fail(ex.toString());
+     }
+
+  }
+
+  /**
+   * String representation of a Locale. A bug in the
+   * Locale.toString() method results in Locales with
+   * just a variant being incorrectly displayed.
+   */
+  private String print(Locale locale) {
+         
+      return locale.getLanguage() + "_" +
+                locale.getCountry() + "_" +
+                locale.getVariant();
+  }
+
 
 
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to