struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject svn commit: r1364110 - in /struts/struts2/trunk/xwork-core/src: main/java/com/opensymphony/xwork2/ test/java/com/opensymphony/xwork2/ test/resources/com/opensymphony/xwork2/
Date Sat, 21 Jul 2012 14:47:36 GMT
Author: lukaszlenart
Date: Sat Jul 21 14:47:35 2012
New Revision: 1364110

URL: http://svn.apache.org/viewvc?rev=1364110&view=rev
Log:
WW-3833 adds new getFormatted method to support localization and conversion errors

Modified:
    struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/ActionSupport.java
    struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/ActionSupportTest.java
    struts/struts2/trunk/xwork-core/src/test/resources/com/opensymphony/xwork2/ActionSupportTest$MyActionSupport_da.properties

Modified: struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/ActionSupport.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/ActionSupport.java?rev=1364110&r1=1364109&r2=1364110&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/ActionSupport.java
(original)
+++ struts/struts2/trunk/xwork-core/src/main/java/com/opensymphony/xwork2/ActionSupport.java
Sat Jul 21 14:47:35 2012
@@ -22,6 +22,7 @@ import com.opensymphony.xwork2.util.logg
 import com.opensymphony.xwork2.util.logging.LoggerFactory;
 
 import java.io.Serializable;
+import java.util.Arrays;
 import java.util.Collection;
 import java.util.List;
 import java.util.Locale;
@@ -134,6 +135,25 @@ public class ActionSupport implements Ac
         return getTextProvider().getText(key, defaultValue, args, stack);
     }
 
+    /**
+     * Dedicated method to support I10N and conversion errors
+     *
+     * @param key message which contains formatting string
+     * @param expr that should be formatted
+     * @return formatted expr with format specified by key
+     */
+    public String getFormatted(String key, String expr) {
+        Map<String, Object> conversionErrors = ActionContext.getContext().getConversionErrors();
+        if (conversionErrors.containsKey(expr)) {
+            String[] vals = (String[]) conversionErrors.get(expr);
+            return vals[0];
+        } else {
+            final ValueStack valueStack = ActionContext.getContext().getValueStack();
+            final Object val = valueStack.findValue(expr);
+            return getText(key, Arrays.asList(val));
+        }
+    }
+
     public ResourceBundle getTexts() {
         return getTextProvider().getTexts();
     }

Modified: struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/ActionSupportTest.java
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/ActionSupportTest.java?rev=1364110&r1=1364109&r2=1364110&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/ActionSupportTest.java
(original)
+++ struts/struts2/trunk/xwork-core/src/test/java/com/opensymphony/xwork2/ActionSupportTest.java
Sat Jul 21 14:47:35 2012
@@ -313,8 +313,35 @@ public class ActionSupportTest extends X
         assertEquals(rb, mas.getTexts(MyActionSupport.class.getName()));
     }
 
+    public void testFormattingSupport() throws Exception {
+        ActionContext.getContext().setLocale(new Locale("da"));
+        MyActionSupport mas = new MyActionSupport();
+        ActionContext.getContext().getValueStack().push(mas);
+
+        mas.setVal(234d);
+
+        String formatted = mas.getFormatted("format.number", "val");
+
+        assertEquals("234,0", formatted);
+    }
+
+    public void testFormattingSupportWithConversionError() throws Exception {
+        ActionContext.getContext().getConversionErrors().put("val", new String[]{"4567def"});
+        ActionContext.getContext().setLocale(new Locale("da"));
+        MyActionSupport mas = new MyActionSupport();
+        ActionContext.getContext().getValueStack().push(mas);
+
+        mas.setVal(234d);
+
+        String formatted = mas.getFormatted("format.number", "val");
+
+        assertEquals("4567def", formatted);
+    }
+
     private class MyActionSupport extends ActionSupport {
 
+        private Double val;
+
         @Override
         public String doDefault() throws Exception {
             return "santa";
@@ -325,6 +352,14 @@ public class ActionSupportTest extends X
             super.validate(); // to have code coverage
             addActionMessage("validation was called");
         }
+
+        public Double getVal() {
+            return val;
+        }
+
+        public void setVal(Double val) {
+            this.val = val;
+        }
     }
 
 }

Modified: struts/struts2/trunk/xwork-core/src/test/resources/com/opensymphony/xwork2/ActionSupportTest$MyActionSupport_da.properties
URL: http://svn.apache.org/viewvc/struts/struts2/trunk/xwork-core/src/test/resources/com/opensymphony/xwork2/ActionSupportTest%24MyActionSupport_da.properties?rev=1364110&r1=1364109&r2=1364110&view=diff
==============================================================================
--- struts/struts2/trunk/xwork-core/src/test/resources/com/opensymphony/xwork2/ActionSupportTest$MyActionSupport_da.properties
(original)
+++ struts/struts2/trunk/xwork-core/src/test/resources/com/opensymphony/xwork2/ActionSupportTest$MyActionSupport_da.properties
Sat Jul 21 14:47:35 2012
@@ -6,3 +6,5 @@
 hello=Hello World
 hello.0=Hello World {0}
 hello.1=Hello World. This is {0} speaking {1}
+
+format.number = {0,number,#0.0##}



Mime
View raw message