Commits

Anonymous committed d96fce2

fixed XW-356 (added more unit tests)

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

Comments (0)

Files changed (4)

src/java/com/opensymphony/xwork/DefaultTextProvider.java

  */
 public class DefaultTextProvider implements TextProvider, Serializable, Unchainable {
 
+    private static final Object[] EMPTY_ARGS = new Object[0];
+
     public static final DefaultTextProvider INSTANCE = new DefaultTextProvider();
 
     private DefaultTextProvider() {
         if (args != null) {
             params = args.toArray();
         } else {
-            params = new Object[0];
+            params = EMPTY_ARGS;
         }
 
         return LocalizedTextUtil.findDefaultText(key, ActionContext.getContext().getLocale(), params);
         if (args != null) {
             params = args;
         } else {
-            params = new Object[0];
+            params = EMPTY_ARGS;
         }
 
         return LocalizedTextUtil.findDefaultText(key, ActionContext.getContext().getLocale(), params);

src/java/com/opensymphony/xwork/interceptor/StaticParametersInterceptor.java

     private boolean parse;
 
     public void setParse(String value) {
-        this.parse = new Boolean(value).booleanValue();
+        this.parse = Boolean.valueOf(value).booleanValue();
     }
 
     protected void after(ActionInvocation invocation, String result) throws Exception {

src/test/com/opensymphony/xwork/DefaultTextProviderTest.java

+/*
+ * Copyright (c) 2002-2006 by OpenSymphony
+ * All rights reserved.
+ */
+package com.opensymphony.xwork;
+
+import junit.framework.TestCase;
+
+import java.util.Locale;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.ResourceBundle;
+
+import com.opensymphony.xwork.util.LocalizedTextUtil;
+import com.opensymphony.xwork.util.OgnlValueStack;
+
+/**
+ * Unit test for {@link DefaultTextProvider}.
+ *
+ * @author Claus Ibsen
+ */
+public class DefaultTextProviderTest extends TestCase {
+
+    private DefaultTextProvider tp;
+
+    public void testSimpleGetTexts() throws Exception {
+        assertEquals("Hello World", tp.getText("hello"));
+        assertEquals(null, tp.getText("not.in.bundle"));
+
+        assertEquals("Hello World", tp.getText("hello", "this is default"));
+        assertEquals("this is default", tp.getText("not.in.bundle", "this is default"));
+
+        List nullList = null;
+        assertEquals("Hello World", tp.getText("hello", nullList));
+
+        String[] nullStrings = null;
+        assertEquals("Hello World", tp.getText("hello", nullStrings));
+    }
+
+   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 {0}", tp.getText("not.in.bundle", "this is default {0}", "from me"));
+
+        assertEquals(null, 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 {0}", tp.getText("not.in.bundle", "this is default {0}", args));
+        assertEquals("this is default {0} speaking {1}", 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(null, 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 {0}", tp.getText("not.in.bundle", "this is default {0}", args));
+        assertEquals("this is default {0} speaking {1}", 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(null, tp.getText("not.in.bundle", args));
+    }
+
+    public void testGetTextsWithListAndStack() throws Exception {
+        OgnlValueStack stack = new OgnlValueStack();
+
+        List args = new ArrayList();
+        args.add("Santa");
+        args.add("loud");
+        assertEquals("Hello World", tp.getText("hello", "this is default", args, stack)); // no args in bundle
+        assertEquals("Hello World Santa", tp.getText("hello.0", "this is default", args, stack)); // only 1 arg in bundle
+        assertEquals("Hello World. This is Santa speaking loud", tp.getText("hello.1", "this is default", args, stack));
+
+        assertEquals("this is default", tp.getText("not.in.bundle", "this is default", args, stack));
+        assertEquals("this is default {0}", tp.getText("not.in.bundle", "this is default {0}", args, stack));
+        assertEquals("this is default {0} speaking {1}", tp.getText("not.in.bundle", "this is default {0} speaking {1}", args, stack));
+    }
+
+    public void testGetTextsWithArrayAndStack() throws Exception {
+        OgnlValueStack stack = new OgnlValueStack();
+
+        String[] args = { "Santa", "loud" };
+        assertEquals("Hello World", tp.getText("hello", "this is default", args, stack)); // no args in bundle
+        assertEquals("Hello World Santa", tp.getText("hello.0", "this is default", args, stack)); // only 1 arg in bundle
+        assertEquals("Hello World. This is Santa speaking loud", tp.getText("hello.1", "this is default", args, stack));
+
+        assertEquals("this is default", tp.getText("not.in.bundle", "this is default", args, stack));
+        assertEquals("this is default {0}", tp.getText("not.in.bundle", "this is default {0}", args, stack));
+        assertEquals("this is default {0} speaking {1}", tp.getText("not.in.bundle", "this is default {0} speaking {1}", args, stack));
+    }
+
+    public void testGetBundle() throws Exception {
+        assertNull(tp.getTexts()); // always returns null
+
+        ResourceBundle rb = ResourceBundle.getBundle(TextProviderSupportTest.class.getName(), Locale.CANADA);
+        assertEquals(rb, tp.getTexts(TextProviderSupportTest.class.getName()));
+    }
+
+    protected void setUp() throws Exception {
+        ActionContext.getContext().setLocale(Locale.CANADA);
+
+        LocalizedTextUtil.clearDefaultResourceBundles();
+        LocalizedTextUtil.addDefaultResourceBundle(DefaultTextProviderTest.class.getName());
+
+        tp = DefaultTextProvider.INSTANCE;
+    }
+
+    protected void tearDown() throws Exception {
+        ActionContext.getContext().setLocale(null);
+        tp = null;
+    }
+
+
+}

src/test/com/opensymphony/xwork/DefaultTextProviderTest_en_CA.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}