Commits

Anonymous committed 6b49def

Some more unit tests and fixed a potential bug in TextProviderSupport + polished javadocs
Issue number: XW-355
Obtained from:
Submitted by: Claus Ibsen
Reviewed by: Rainer Hermanns

git-svn-id: http://svn.opensymphony.com/svn/xwork/trunk@878e221344d-f017-0410-9bd5-d282ab1896d7

  • Participants
  • Parent commits c018683

Comments (0)

Files changed (3)

File src/java/com/opensymphony/xwork/TextProviderSupport.java

     private ResourceBundle bundle;
 
 
+    /**
+     * Constructor.
+     *
+     * @param clazz   a clazz to use for reading the resource bundle.
+     * @param provider  a locale provider.
+     */
     public TextProviderSupport(Class clazz, LocaleProvider provider) {
         this.clazz = clazz;
         this.localeProvider = provider;
     }
 
+    /**
+     * Constructor.
+     *
+     * @param bundle    the resource bundle.
+     * @param provider  a locale provider.
+     */
     public TextProviderSupport(ResourceBundle bundle, LocaleProvider provider) {
         this.bundle = bundle;
         this.localeProvider = provider;
      * a cascading style that allow global texts to be defined for an application base
      * class.
      *
-     * @param aTextName name of text to be found
+     * @param key name of text to be found
      * @return value of named text
      */
-    public String getText(String aTextName) {
-        return getText(aTextName, aTextName, Collections.EMPTY_LIST);
+    public String getText(String key) {
+        return getText(key, key, Collections.EMPTY_LIST);
     }
 
     /**
      * a cascading style that allow global texts to be defined for an application base
      * class. If no text is found for this text name, the default value is returned.
      *
-     * @param aTextName    name of text to be found
+     * @param key    name of text to be found
      * @param defaultValue the default value which will be returned if no text is found
      * @return value of named text
      */
-    public String getText(String aTextName, String defaultValue) {
-        return getText(aTextName, defaultValue, Collections.EMPTY_LIST);
+    public String getText(String key, String defaultValue) {
+        return getText(key, defaultValue, Collections.EMPTY_LIST);
     }
 
     /**
      * a cascading style that allow global texts to be defined for an application base
      * class. If no text is found for this text name, the default value is returned.
      *
-     * @param aTextName    name of text to be found
+     * @param key    name of text to be found
      * @param defaultValue the default value which will be returned if no text is found
      * @return value of named text
      */
-    public String getText(String aTextName, String defaultValue, String obj) {
+    public String getText(String key, String defaultValue, String arg) {
         List args = new ArrayList();
-        args.add(obj);
-        return getText(aTextName, defaultValue, args);
+        args.add(arg);
+        return getText(key, defaultValue, args);
     }
 
     /**
      * a cascading style that allow global texts to be defined for an application base
      * class. If no text is found for this text name, the default value is returned.
      *
-     * @param aTextName name of text to be found
+     * @param key name of text to be found
      * @param args      a List of args to be used in a MessageFormat message
      * @return value of named text
      */
-    public String getText(String aTextName, List args) {
-        return getText(aTextName, aTextName, args);
+    public String getText(String key, List args) {
+        return getText(key, key, args);
     }
 
     /**
      * a cascading style that allow global texts to be defined for an application base
      * class. If no text is found for this text name, the default value is returned.
      *
-     * @param aTextName    name of text to be found
+     * @param key    name of text to be found
      * @param defaultValue the default value which will be returned if no text is found
      * @param args         a List of args to be used in a MessageFormat message
      * @return value of named text
      */
-    public String getText(String aTextName, String defaultValue, List args) {
+    public String getText(String key, String defaultValue, List args) {
         Object[] argsArray = ((args != null && !args.equals(Collections.EMPTY_LIST)) ? args.toArray() : null);
         if (clazz != null) {
-            return LocalizedTextUtil.findText(clazz, aTextName, getLocale(), defaultValue, argsArray);
+            return LocalizedTextUtil.findText(clazz, key, getLocale(), defaultValue, argsArray);
         } else {
-            return LocalizedTextUtil.findText(bundle, aTextName, getLocale(), defaultValue, argsArray);
+            return LocalizedTextUtil.findText(bundle, key, getLocale(), defaultValue, argsArray);
         }
     }
 
      * default value is returned. Instead of using the value stack in the ActionContext
      * this version of the getText() method uses the provided value stack.
      *
-     * @param aTextName    the resource bundle key that is to be searched for
+     * @param key    the resource bundle key that is to be searched for
      * @param defaultValue the default value which will be returned if no message is found
      * @param args         a list args to be used in a {@link java.text.MessageFormat} message
      * @param stack        the value stack to use for finding the text
      * @return the message as found in the resource bundle, or defaultValue if none is found
      */
-    public String getText(String aTextName, String defaultValue, List args, OgnlValueStack stack) {
+    public String getText(String key, String defaultValue, List args, OgnlValueStack stack) {
         Object[] argsArray = ((args != null) ? args.toArray() : null);
         Locale locale = (Locale) stack.getContext().get(ActionContext.LOCALE);
         if (locale == null) {
             locale = getLocale();
         }
         if (clazz != null) {
-            return LocalizedTextUtil.findText(clazz, aTextName, locale, defaultValue, argsArray, stack);
+            return LocalizedTextUtil.findText(clazz, key, locale, defaultValue, argsArray, stack);
         } else {
-            return LocalizedTextUtil.findText(bundle, aTextName, locale, defaultValue, argsArray, stack);
+            return LocalizedTextUtil.findText(bundle, key, locale, defaultValue, argsArray, stack);
         }
     }
 
      * @return resouce bundle
      */
     public ResourceBundle getTexts() {
-        return getTexts(clazz.getName());
+        if (clazz != null) {
+            return getTexts(clazz.getName());
+        }
+        return bundle;
     }
 
+    /**
+     * Get's the locale from the localeProvider.
+     *
+     * @return the locale from the localeProvider.
+     */
     private Locale getLocale() {
         return localeProvider.getLocale();
     }

File src/test/com/opensymphony/xwork/TextProviderSupportTest.java

+/*
+ * Copyright (c) 2002-2006 by OpenSymphony
+ * All rights reserved.
+ */
+
+package com.opensymphony.xwork;
+
+import com.opensymphony.xwork.util.OgnlValueStack;
+
+import java.util.*;
+
+/**
+ * Unit test for {@link TextProviderSupport}.
+ *
+ * @author Claus Ibsen
+ */
+public class TextProviderSupportTest extends XWorkTestCase {
+
+    private TextProviderSupport tp;
+    private java.util.ResourceBundle rb;
+
+    public void testSimpleGetTexts() throws Exception {
+        assertEquals("Hello World", tp.getText("hello"));
+
+        assertEquals("Hello World", tp.getText("hello", "this is default"));
+        assertEquals("this is default", tp.getText("not.in.bundle", "this is default"));
+    }
+
+    public void testGetTextsWithArgs() throws Exception {
+        assertEquals("Hello World", tp.getText("hello", "this is default", "from me")); // no args in bundle
+        assertEquals("Hello World from me", tp.getText("hello.0", "this is default", "from me"));
+        assertEquals("this is default", tp.getText("not.in.bundle", "this is default", "from me"));
+        assertEquals("this is default from me", tp.getText("not.in.bundle", "this is default {0}", "from me"));
+
+        assertEquals("not.in.bundle", tp.getText("not.in.bundle"));
+    }
+
+    public void testGetTextsWithListArgs() throws Exception {
+        List args = new ArrayList();
+        args.add("Santa");
+        args.add("loud");
+        assertEquals("Hello World", tp.getText("hello", "this is default", args)); // no args in bundle
+        assertEquals("Hello World Santa", tp.getText("hello.0", "this is default", args)); // only 1 arg in bundle
+        assertEquals("Hello World. This is Santa speaking loud", tp.getText("hello.1", "this is default", args));
+
+        assertEquals("this is default", tp.getText("not.in.bundle", "this is default", args));
+        assertEquals("this is default Santa", tp.getText("not.in.bundle", "this is default {0}", args));
+        assertEquals("this is default Santa speaking loud", tp.getText("not.in.bundle", "this is default {0} speaking {1}", args));
+
+        assertEquals("Hello World", tp.getText("hello", args)); // no args in bundle
+        assertEquals("Hello World Santa", tp.getText("hello.0", args)); // only 1 arg in bundle
+        assertEquals("Hello World. This is Santa speaking loud", tp.getText("hello.1", args));
+
+        assertEquals("not.in.bundle", tp.getText("not.in.bundle", args));
+    }
+
+    public void testGetTextsWithArrayArgs() throws Exception {
+        String[] args = { "Santa", "loud" };
+        assertEquals("Hello World", tp.getText("hello", "this is default", args)); // no args in bundle
+        assertEquals("Hello World Santa", tp.getText("hello.0", "this is default", args)); // only 1 arg in bundle
+        assertEquals("Hello World. This is Santa speaking loud", tp.getText("hello.1", "this is default", args));
+
+        assertEquals("this is default", tp.getText("not.in.bundle", "this is default", args));
+        assertEquals("this is default Santa", tp.getText("not.in.bundle", "this is default {0}", args));
+        assertEquals("this is default Santa speaking loud", tp.getText("not.in.bundle", "this is default {0} speaking {1}", args));
+
+        assertEquals("Hello World", tp.getText("hello", args)); // no args in bundle
+        assertEquals("Hello World Santa", tp.getText("hello.0", args)); // only 1 arg in bundle
+        assertEquals("Hello World. This is Santa speaking loud", tp.getText("hello.1", args));
+
+        assertEquals("not.in.bundle", tp.getText("not.in.bundle", args));
+    }
+
+    public void testGetBundle() throws Exception {
+        assertEquals(rb, tp.getTexts());
+        assertEquals(rb, tp.getTexts(TextProviderSupportTest.class.getName()));
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        rb = ResourceBundle.getBundle(TextProviderSupportTest.class.getName(), Locale.ENGLISH);
+        tp = new TextProviderSupport(rb, new LocaleProvider() {
+            public Locale getLocale() {
+                return Locale.ENGLISH;
+            }
+        });
+    }
+
+    protected void tearDown() throws Exception {
+        rb = null;
+        tp = null;
+    }
+
+
+}
+

File src/test/com/opensymphony/xwork/TextProviderSupportTest_en.properties

+#
+# Copyright (c) 2002-2006 by OpenSymphony
+# All rights reserved.
+#
+
+hello=Hello World
+hello.0=Hello World {0}
+hello.1=Hello World. This is {0} speaking {1}