Commits

Anonymous committed 6321831

XW-259: Now a new ActionContext with nothing pushed onto the value stack can still find default message texts because there is always a DefaultTextProvider pushed on when the OgnlValueStack is created.

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

Comments (0)

Files changed (7)

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

+/*
+ * Copyright (c) 2005 Opensymphony. All Rights Reserved.
+ */
+package com.opensymphony.xwork;
+
+import com.opensymphony.xwork.util.OgnlValueStack;
+import com.opensymphony.xwork.util.LocalizedTextUtil;
+
+import java.util.List;
+import java.util.ResourceBundle;
+import java.io.Serializable;
+import java.io.ObjectStreamException;
+
+/**
+ * DefaultTextProvider gets texts from only the default resource bundles associated with the
+ * LocalizedTextUtil.
+ *
+ * @see LocalizedTextUtil#addDefaultResourceBundle(String)
+ *
+ * @author Jason Carreira <jcarreira@gmail.com>
+ */
+public class DefaultTextProvider implements TextProvider, Serializable {
+
+    public static final DefaultTextProvider INSTANCE = new DefaultTextProvider();
+
+    private DefaultTextProvider() {
+    }
+
+    public String getText(String key) {
+        return LocalizedTextUtil.findDefaultText(key,ActionContext.getContext().getLocale());
+    }
+
+    public String getText(String key, String defaultValue) {
+        String text = getText(key);
+        if (text == null) {
+            return defaultValue;
+        }
+        return text;
+    }
+
+    public String getText(String key, List args) {
+        return LocalizedTextUtil.findDefaultText(key,ActionContext.getContext().getLocale(),args.toArray());
+    }
+
+    public String getText(String key, String defaultValue, List args) {
+        String text = getText(key,args);
+        if (text == null) {
+            return defaultValue;
+        }
+        return text;
+    }
+
+    public String getText(String key, String defaultValue, List args, OgnlValueStack stack) {
+        //we're not using the value stack here
+        return getText(key,defaultValue,args);
+    }
+
+    public ResourceBundle getTexts(String bundleName) {
+        return LocalizedTextUtil.findResourceBundle(bundleName,ActionContext.getContext().getLocale());
+    }
+
+    public ResourceBundle getTexts() {
+        return null;
+    }
+
+    private Object readResolve() throws ObjectStreamException {
+        return INSTANCE;
+    }
+}

src/java/com/opensymphony/xwork/util/OgnlValueStack.java

  */
 package com.opensymphony.xwork.util;
 
+import com.opensymphony.xwork.DefaultTextProvider;
 import ognl.Ognl;
 import ognl.OgnlContext;
 import ognl.OgnlException;
 
     public OgnlValueStack() {
         setRoot(new CompoundRoot());
+        push(DefaultTextProvider.INSTANCE);
     }
 
     public OgnlValueStack(OgnlValueStack vs) {

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

  */
 package com.opensymphony.xwork;
 
+import com.opensymphony.xwork.util.OgnlValueStack;
 import junit.framework.TestCase;
 
 import java.util.HashMap;
     //~ Methods ////////////////////////////////////////////////////////////////
 
     public void setUp() {
-        Map extraContext = new HashMap();
+        OgnlValueStack valueStack = new OgnlValueStack();
+        Map extraContext = valueStack.getContext();
         Map application = new HashMap();
         application.put(APPLICATION_KEY, APPLICATION_KEY);
 
         ActionContext threadContext = ActionContext.getContext();
         assertEquals(context, threadContext);
     }
+
+    public void testNewActionContextCanFindDefaultTexts() {
+        OgnlValueStack valueStack = context.getValueStack();
+        String actionErrorMessage = (String) valueStack.findValue("getText('xwork.error.action.execution')");
+        assertNotNull(actionErrorMessage);
+        assertEquals("Error during Action invocation",actionErrorMessage);
+    }
 }

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

 import com.opensymphony.xwork.config.entities.ActionConfig;
 import com.opensymphony.xwork.config.entities.PackageConfig;
 import com.opensymphony.xwork.util.OgnlValueStack;
-
 import junit.framework.TestCase;
 
-import java.util.HashMap;
 import java.util.Map;
+import java.util.HashMap;
 
 
 /**
         assertEquals(context, ActionContext.getContext());
     }
 
-    public void testNestedNoValueStack() {
+    public void testNestedNoValueStack() throws Exception {
         OgnlValueStack stack = ActionContext.getContext().getValueStack();
         assertEquals(VALUE, stack.findValue(KEY));
 
-        try {
-            ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(NAMESPACE, NO_STACK_ACTION_NAME, null);
-            proxy.execute();
-            stack = ActionContext.getContext().getValueStack();
-            assertEquals(stack.findValue(KEY), VALUE);
-            assertNull(stack.findValue(NESTED_KEY));
-        } catch (Exception e) {
-            e.printStackTrace();
-            fail();
-        }
+         ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(NAMESPACE, NO_STACK_ACTION_NAME, null);
+        proxy.execute();
+        stack = ActionContext.getContext().getValueStack();
+        assertEquals(stack.findValue(KEY), VALUE);
+        assertNull(stack.findValue(NESTED_KEY));
     }
 
-    public void testNestedValueStack() {
+    public void testNestedValueStack() throws Exception {
         OgnlValueStack stack = ActionContext.getContext().getValueStack();
         assertEquals(VALUE, stack.findValue(KEY));
 
-        try {
-            HashMap extraContext = new HashMap();
-            extraContext.put(ActionContext.VALUE_STACK, stack);
+        HashMap extraContext = new HashMap();
+        extraContext.put(ActionContext.VALUE_STACK, stack);
 
-            ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(NAMESPACE, STACK_ACTION_NAME, extraContext);
-            proxy.execute();
-            assertEquals(context, ActionContext.getContext());
-            assertEquals(stack, ActionContext.getContext().getValueStack());
-            assertEquals(VALUE, stack.findValue(KEY));
-            assertEquals(NESTED_VALUE, stack.findValue(NESTED_KEY));
-            assertEquals(2, stack.size());
-        } catch (Exception e) {
-            e.printStackTrace();
-            fail();
-        }
+        ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(NAMESPACE, STACK_ACTION_NAME, extraContext);
+        proxy.execute();
+        assertEquals(context, ActionContext.getContext());
+        assertEquals(stack, ActionContext.getContext().getValueStack());
+        assertEquals(VALUE, stack.findValue(KEY));
+        assertEquals(NESTED_VALUE, stack.findValue(NESTED_KEY));
+        assertEquals(3, stack.size());
     }
 
     //~ Inner Classes //////////////////////////////////////////////////////////

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

 
     public String noStack() {
         OgnlValueStack stack = ActionContext.getContext().getValueStack();
-        Assert.assertEquals(1, stack.size());
+        // Action + DefaultTextProvider on the stack
+        Assert.assertEquals(2, stack.size());
         Assert.assertNull(stack.findValue(ActionNestingTest.KEY));
         Assert.assertEquals(ActionNestingTest.NESTED_VALUE, stack.findValue(ActionNestingTest.NESTED_KEY));
 
 
     public String stack() {
         OgnlValueStack stack = ActionContext.getContext().getValueStack();
-        Assert.assertEquals(2, stack.size());
+        //DefaultTextProvider, NestedActionTest pushed on by the test, and the NestedAction
+        Assert.assertEquals(3, stack.size());
         Assert.assertNotNull(stack.findValue(ActionNestingTest.KEY));
         Assert.assertEquals(ActionContext.getContext().getValueStack().findValue(ActionNestingTest.KEY), ActionNestingTest.VALUE);
         Assert.assertEquals(ActionNestingTest.NESTED_VALUE, stack.findValue(ActionNestingTest.NESTED_KEY));

src/test/com/opensymphony/xwork/interceptor/ChainingInterceptorTest.java

         mockInvocation = new Mock(ActionInvocation.class);
         mockInvocation.expectAndReturn("getStack", stack);
         mockInvocation.expectAndReturn("invoke", Action.SUCCESS);
-        mockInvocation.expectAndReturn("getInvocationContext", new ActionContext(new HashMap()));
+        mockInvocation.matchAndReturn("getInvocationContext", new ActionContext(new HashMap()));
         invocation = (ActionInvocation) mockInvocation.proxy();
         interceptor = new ChainingInterceptor();
     }

src/test/com/opensymphony/xwork/util/OgnlValueStackTest.java

 import com.opensymphony.xwork.ActionContext;
 import com.opensymphony.xwork.SimpleAction;
 import com.opensymphony.xwork.TestBean;
+import com.opensymphony.xwork.DefaultTextProvider;
 
 import junit.framework.TestCase;
 
         assertEquals("Jack", vs.findValue("top.name"));
     }
 
-    public void testTopNull() {
+    public void testTopIsDefaultTextProvider() {
         OgnlValueStack vs = new OgnlValueStack();
 
-        assertNull(vs.findValue("top"));
+        assertEquals(DefaultTextProvider.INSTANCE, vs.findValue("top"));
     }
 
     public void testTwoDogs() {