velocity-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cbris...@apache.org
Subject svn commit: r1847458 - in /velocity/engine/trunk/velocity-engine-core/src: main/java/org/apache/velocity/util/introspection/ test/java/org/apache/velocity/test/util/introspection/ test/resources/conversion/ test/resources/conversion/compare/
Date Mon, 26 Nov 2018 10:31:53 GMT
Author: cbrisson
Date: Mon Nov 26 10:31:53 2018
New Revision: 1847458

URL: http://svn.apache.org/viewvc?rev=1847458&view=rev
Log:
[engine] Add String -> Locale in provided default explicit conversions

Modified:
    velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/ConversionHandlerImpl.java
    velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java
    velocity/engine/trunk/velocity-engine-core/src/test/resources/conversion/compare/test_conv_with_handler.cmp
    velocity/engine/trunk/velocity-engine-core/src/test/resources/conversion/compare/test_conv_without_handler.cmp
    velocity/engine/trunk/velocity-engine-core/src/test/resources/conversion/test_conv.vtl

Modified: velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/ConversionHandlerImpl.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/ConversionHandlerImpl.java?rev=1847458&r1=1847457&r2=1847458&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/ConversionHandlerImpl.java
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/main/java/org/apache/velocity/util/introspection/ConversionHandlerImpl.java
Mon Nov 26 10:31:53 2018
@@ -19,9 +19,11 @@ package org.apache.velocity.util.introsp
  * under the License.
  */
 
+import org.apache.commons.lang3.LocaleUtils;
 import org.apache.velocity.util.Pair;
 
 import java.util.HashMap;
+import java.util.Locale;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
@@ -468,7 +470,7 @@ public class ConversionHandlerImpl imple
         standardConverterMap.put(new Pair<>(Integer.TYPE, Boolean.class), booleanToInteger);
         standardConverterMap.put(new Pair<>(Integer.TYPE, Boolean.TYPE), booleanToInteger);
 
-        /* boolean to lonf */
+        /* boolean to long */
         Converter<Long> booleanToLong = new Converter<Long>()
         {
             @Override
@@ -491,6 +493,17 @@ public class ConversionHandlerImpl imple
                 return String.valueOf(o);
             }
         };
+
+        /* string to locale */
+        Converter<Locale> stringToLocale = new Converter<Locale>()
+        {
+            @Override
+            public Locale convert(Object o)
+            {
+                return o == null ? null : LocaleUtils.toLocale((String)o);
+            }
+        };
+        standardConverterMap.put(new Pair<>(Locale.class, String.class), stringToLocale);
     }
 
     /**

Modified: velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java?rev=1847458&r1=1847457&r2=1847458&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/java/org/apache/velocity/test/util/introspection/ConversionHandlerTestCase.java
Mon Nov 26 10:31:53 2018
@@ -41,6 +41,7 @@ import java.io.FileOutputStream;
 import java.io.OutputStreamWriter;
 import java.io.StringWriter;
 import java.io.Writer;
+import java.util.Locale;
 import java.util.Map;
 import java.util.TreeMap;
 
@@ -236,6 +237,7 @@ public class ConversionHandlerTestCase e
         map.put("S. string-big-integral", new String("12345678"));
         map.put("T. string-floating", new String("123.345"));
         map.put("U. null", null);
+        map.put("V. locale", "fr_FR");
         context.put("map", map);
         context.put("target", new Obj());
         Class[] types =
@@ -289,6 +291,7 @@ public class ConversionHandlerTestCase e
         public String objectObject(Object o) { return "Object ok: " + o; }
         public String objectString(String s) { return "String ok: " + s; }
         public String objectEnum(Color c) { return "Enum ok: " + c; }
+        public String locale(Locale loc) { return "Locale ok: " + loc; }
 
         public String toString() { return "instance of Obj"; }
     }

Modified: velocity/engine/trunk/velocity-engine-core/src/test/resources/conversion/compare/test_conv_with_handler.cmp
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/resources/conversion/compare/test_conv_with_handler.cmp?rev=1847458&r1=1847457&r2=1847458&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/resources/conversion/compare/test_conv_with_handler.cmp
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/resources/conversion/compare/test_conv_with_handler.cmp
Mon Nov 26 10:31:53 2018
@@ -27,6 +27,7 @@ A. bool-true Value java.lang.Boolean tru
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 B. bool-false Value java.lang.Boolean false
   boolean ok: false
   byte ok: 0
@@ -56,6 +57,7 @@ B. bool-false Value java.lang.Boolean fa
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 C. byte-0 Value java.lang.Byte 0
   boolean ok: false
   byte ok: 0
@@ -85,6 +87,7 @@ C. byte-0 Value java.lang.Byte 0
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 D. byte-1 Value java.lang.Byte 1
   boolean ok: true
   byte ok: 1
@@ -114,6 +117,7 @@ D. byte-1 Value java.lang.Byte 1
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 E. short Value java.lang.Short 125
   boolean ok: true
   byte ok: 125
@@ -143,6 +147,7 @@ E. short Value java.lang.Short 125
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 F. int Value java.lang.Integer 24323
   boolean ok: true
   integralByte -> NumberFormatException: value out of range for byte type: 24323
@@ -172,6 +177,7 @@ F. int Value java.lang.Integer 24323
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 G. long Value java.lang.Long 5235235
   boolean ok: true
   integralByte -> NumberFormatException: value out of range for byte type: 5235235
@@ -201,6 +207,7 @@ G. long Value java.lang.Long 5235235
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 H. float Value java.lang.Float 34523.344
   boolean ok: true
   integralByte -> NumberFormatException: value out of range for byte type: 34523
@@ -230,6 +237,7 @@ H. float Value java.lang.Float 34523.344
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 I. double Value java.lang.Double 54235.3253
   boolean ok: true
   integralByte -> NumberFormatException: value out of range for byte type: 54235
@@ -259,6 +267,7 @@ I. double Value java.lang.Double 54235.3
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 J. char Value java.lang.Character @
   boolean ok: true
   $target.integralByte($value)
@@ -288,6 +297,7 @@ J. char Value java.lang.Character @
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 K. object Value org.apache.velocity.test.util.introspection.ConversionHandlerTestCase$Obj
instance of Obj
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -317,6 +327,7 @@ K. object Value org.apache.velocity.test
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 L. enum Value org.apache.velocity.test.util.introspection.ConversionHandlerTestCase$Obj$Color
GREEN
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -346,6 +357,7 @@ L. enum Value org.apache.velocity.test.u
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 M. string Value java.lang.String foo
   boolean ok: false
   integralByte -> NumberFormatException: For input string: "foo"
@@ -375,6 +387,7 @@ M. string Value java.lang.String foo
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  Locale ok: foo
 M. string-green Value java.lang.String green
   boolean ok: false
   integralByte -> NumberFormatException: For input string: "green"
@@ -404,6 +417,7 @@ M. string-green Value java.lang.String g
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  locale -> IllegalArgumentException: Invalid locale format: green
 N. string-empty Value java.lang.String 
   boolean ok: false
   integralByte -> NumberFormatException: For input string: ""
@@ -433,6 +447,7 @@ N. string-empty Value java.lang.String
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  Locale ok: 
 O. string-false Value java.lang.String false
   boolean ok: false
   integralByte -> NumberFormatException: For input string: "false"
@@ -462,6 +477,7 @@ O. string-false Value java.lang.String f
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  locale -> IllegalArgumentException: Invalid locale format: false
 P. string-true Value java.lang.String true
   boolean ok: true
   integralByte -> NumberFormatException: For input string: "true"
@@ -491,6 +507,7 @@ P. string-true Value java.lang.String tr
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  locale -> IllegalArgumentException: Invalid locale format: true
 Q. string-zero Value java.lang.String 0
   boolean ok: false
   byte ok: 0
@@ -520,6 +537,7 @@ Q. string-zero Value java.lang.String 0
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  locale -> IllegalArgumentException: Invalid locale format: 0
 R. string-integral Value java.lang.String 123
   boolean ok: false
   byte ok: 123
@@ -549,6 +567,7 @@ R. string-integral Value java.lang.Strin
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  locale -> IllegalArgumentException: Invalid locale format: 123
 S. string-big-integral Value java.lang.String 12345678
   boolean ok: false
   integralByte -> NumberFormatException: Value out of range. Value:"12345678" Radix:10
@@ -578,6 +597,7 @@ S. string-big-integral Value java.lang.S
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  locale -> IllegalArgumentException: Invalid locale format: 12345678
 T. string-floating Value java.lang.String 123.345
   boolean ok: false
   integralByte -> NumberFormatException: For input string: "123.345"
@@ -607,6 +627,7 @@ T. string-floating Value java.lang.Strin
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  locale -> IllegalArgumentException: Invalid locale format: 123.345
 U. null Value $value.class.name $value
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -636,3 +657,34 @@ U. null Value $value.class.name $value
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  Locale ok: null
+V. locale Value java.lang.String fr_FR
+  boolean ok: false
+  integralByte -> NumberFormatException: For input string: "fr_FR"
+  integralShort -> NumberFormatException: For input string: "fr_FR"
+  integralInt -> NumberFormatException: For input string: "fr_FR"
+  integralLong -> NumberFormatException: For input string: "fr_FR"
+  integralFloat -> NumberFormatException: For input string: "fr_FR"
+  integralDouble -> NumberFormatException: For input string: "fr_FR"
+  $target.integralChar($value)
+  Boolean ok: false
+  objectByte -> NumberFormatException: For input string: "fr_FR"
+  objectShort -> NumberFormatException: For input string: "fr_FR"
+  objectInt -> NumberFormatException: For input string: "fr_FR"
+  objectLong -> NumberFormatException: For input string: "fr_FR"
+  objectFloat -> NumberFormatException: For input string: "fr_FR"
+  objectDouble -> NumberFormatException: For input string: "fr_FR"
+  $target.objectCharacter($value)
+  $target.objectNumber($value)
+  Object ok: fr_FR
+  objectEnum -> IllegalArgumentException: No enum constant org.apache.velocity.test.util.introspection.ConversionHandlerTestCase.Obj.Color.fr_FR
+  String ok: fr_FR
+  $target.valueOfBoolean($value)
+  $target.valueOfShort($value)
+  $target.valueOfByte($value)
+  $target.valueOfInt($value)
+  $target.valueOfLong($value)
+  $target.valueOfFloat($value)
+  $target.valueOfDouble($value)
+  $target.valueOfString($value)
+  Locale ok: fr_FR

Modified: velocity/engine/trunk/velocity-engine-core/src/test/resources/conversion/compare/test_conv_without_handler.cmp
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/resources/conversion/compare/test_conv_without_handler.cmp?rev=1847458&r1=1847457&r2=1847458&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/resources/conversion/compare/test_conv_without_handler.cmp
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/resources/conversion/compare/test_conv_without_handler.cmp
Mon Nov 26 10:31:53 2018
@@ -27,6 +27,7 @@ A. bool-true Value java.lang.Boolean tru
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 B. bool-false Value java.lang.Boolean false
   boolean ok: false
   $target.integralByte($value)
@@ -56,6 +57,7 @@ B. bool-false Value java.lang.Boolean fa
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 C. byte-0 Value java.lang.Byte 0
   $target.integralBoolean($value)
   byte ok: 0
@@ -85,6 +87,7 @@ C. byte-0 Value java.lang.Byte 0
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 D. byte-1 Value java.lang.Byte 1
   $target.integralBoolean($value)
   byte ok: 1
@@ -114,6 +117,7 @@ D. byte-1 Value java.lang.Byte 1
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 E. short Value java.lang.Short 125
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -143,6 +147,7 @@ E. short Value java.lang.Short 125
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 F. int Value java.lang.Integer 24323
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -172,6 +177,7 @@ F. int Value java.lang.Integer 24323
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 G. long Value java.lang.Long 5235235
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -201,6 +207,7 @@ G. long Value java.lang.Long 5235235
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 H. float Value java.lang.Float 34523.344
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -230,6 +237,7 @@ H. float Value java.lang.Float 34523.344
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 I. double Value java.lang.Double 54235.3253
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -259,6 +267,7 @@ I. double Value java.lang.Double 54235.3
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 J. char Value java.lang.Character @
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -288,6 +297,7 @@ J. char Value java.lang.Character @
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 K. object Value org.apache.velocity.test.util.introspection.ConversionHandlerTestCase$Obj
instance of Obj
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -317,6 +327,7 @@ K. object Value org.apache.velocity.test
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 L. enum Value org.apache.velocity.test.util.introspection.ConversionHandlerTestCase$Obj$Color
GREEN
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -346,6 +357,7 @@ L. enum Value org.apache.velocity.test.u
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 M. string Value java.lang.String foo
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -375,6 +387,7 @@ M. string Value java.lang.String foo
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 M. string-green Value java.lang.String green
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -404,6 +417,7 @@ M. string-green Value java.lang.String g
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 N. string-empty Value java.lang.String 
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -433,6 +447,7 @@ N. string-empty Value java.lang.String
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 O. string-false Value java.lang.String false
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -462,6 +477,7 @@ O. string-false Value java.lang.String f
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 P. string-true Value java.lang.String true
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -491,6 +507,7 @@ P. string-true Value java.lang.String tr
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 Q. string-zero Value java.lang.String 0
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -520,6 +537,7 @@ Q. string-zero Value java.lang.String 0
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 R. string-integral Value java.lang.String 123
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -549,6 +567,7 @@ R. string-integral Value java.lang.Strin
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 S. string-big-integral Value java.lang.String 12345678
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -578,6 +597,7 @@ S. string-big-integral Value java.lang.S
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 T. string-floating Value java.lang.String 123.345
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -607,6 +627,7 @@ T. string-floating Value java.lang.Strin
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 U. null Value $value.class.name $value
   $target.integralBoolean($value)
   $target.integralByte($value)
@@ -636,3 +657,34 @@ U. null Value $value.class.name $value
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  Locale ok: null
+V. locale Value java.lang.String fr_FR
+  $target.integralBoolean($value)
+  $target.integralByte($value)
+  $target.integralShort($value)
+  $target.integralInt($value)
+  $target.integralLong($value)
+  $target.integralFloat($value)
+  $target.integralDouble($value)
+  $target.integralChar($value)
+  $target.objectBoolean($value)
+  $target.objectByte($value)
+  $target.objectShort($value)
+  $target.objectInt($value)
+  $target.objectLong($value)
+  $target.objectFloat($value)
+  $target.objectDouble($value)
+  $target.objectCharacter($value)
+  $target.objectNumber($value)
+  Object ok: fr_FR
+  $target.objectEnum($value)
+  String ok: fr_FR
+  $target.valueOfBoolean($value)
+  $target.valueOfShort($value)
+  $target.valueOfByte($value)
+  $target.valueOfInt($value)
+  $target.valueOfLong($value)
+  $target.valueOfFloat($value)
+  $target.valueOfDouble($value)
+  $target.valueOfString($value)
+  $target.locale($value)

Modified: velocity/engine/trunk/velocity-engine-core/src/test/resources/conversion/test_conv.vtl
URL: http://svn.apache.org/viewvc/velocity/engine/trunk/velocity-engine-core/src/test/resources/conversion/test_conv.vtl?rev=1847458&r1=1847457&r2=1847458&view=diff
==============================================================================
--- velocity/engine/trunk/velocity-engine-core/src/test/resources/conversion/test_conv.vtl
(original)
+++ velocity/engine/trunk/velocity-engine-core/src/test/resources/conversion/test_conv.vtl
Mon Nov 26 10:31:53 2018
@@ -29,4 +29,5 @@ $key Value $value.class.name $value
   $target.valueOfFloat($value)
   $target.valueOfDouble($value)
   $target.valueOfString($value)
+  $target.locale($value)
 #end##
\ No newline at end of file



Mime
View raw message