svn commit: r178904 - /jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/locale/LocaleBeanificationTestCase.java

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r178904 - /jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/locale/LocaleBeanificationTestCase.java

Simon Kitching
Author: skitching
Date: Sat May 28 16:34:52 2005
New Revision: 178904

URL: http://svn.apache.org/viewcvs?rev=178904&view=rev
Log:
Added test method to check that Locale-aware converters can happily be
registered with the non-locale-aware ConvertUtils class.

Modified:
    jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/locale/LocaleBeanificationTestCase.java

Modified: jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/locale/LocaleBeanificationTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/locale/LocaleBeanificationTestCase.java?rev=178904&r1=178903&r2=178904&view=diff
==============================================================================
--- jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/locale/LocaleBeanificationTestCase.java (original)
+++ jakarta/commons/proper/beanutils/trunk/src/test/org/apache/commons/beanutils/locale/LocaleBeanificationTestCase.java Sat May 28 16:34:52 2005
@@ -30,6 +30,13 @@
 
 import org.apache.commons.beanutils.ContextClassLoaderLocal;
 import org.apache.commons.beanutils.PrimitiveBean;
+import org.apache.commons.beanutils.BeanUtilsBean;
+import org.apache.commons.beanutils.Converter;
+import org.apache.commons.beanutils.ConvertUtils;
+import org.apache.commons.beanutils.ConversionException;
+import org.apache.commons.beanutils.locale.converters.LongLocaleConverter;
+
+import java.util.Locale;
 
 /**
  * <p>
@@ -457,6 +464,52 @@
         assertEquals("Start thread gets right instance", beanOne, ccll.get());
         ccll.unset();
         assertTrue("Unset works", !beanOne.equals(ccll.get()));
+    }
+    
+    /**
+     * Test registering a locale-aware converter with the standard ConvertUtils.
+     */
+    public void testLocaleAwareConverterInConvertUtils() throws Exception {
+        try {
+            // first use the default non-locale-aware converter
+            try {
+                Long data = (Long) ConvertUtils.convert("777", Long.class);
+                assertEquals("Standard format long converted ok", 777, data.longValue());
+            }
+            catch(ConversionException ex) {
+                fail("Unable to convert non-locale-aware number 777");
+            }
+
+            // now try default converter with special delimiters
+            try {
+                // This conversion will cause an error. But the default
+                // Long converter is set up to return a default value of
+                // zero on error.
+                Long data = (Long) ConvertUtils.convert("1.000.000", Long.class);
+                assertEquals("Standard format behaved as expected", 0, data.longValue());
+            }
+            catch(ConversionException ex) {
+                fail("Unexpected exception from standard Long converter.");
+            }
+            
+            // Now try using a locale-aware converter together with
+            // locale-specific input string. Note that in the german locale,
+            // large numbers can be split up into groups of three digits
+            // using a dot character (and comma is the decimal-point indicator).
+            try {
+                
+                Locale germanLocale = Locale.GERMAN;
+                LongLocaleConverter longLocaleConverter = new LongLocaleConverter(germanLocale);
+                ConvertUtils.register(longLocaleConverter, Long.class);
+
+                Long data = (Long) ConvertUtils.convert("1.000.000", Long.class);
+                assertEquals("German-format long converted ok", 1000000, data.longValue());
+            } catch(ConversionException ex) {
+                fail("Unable to convert german-format number");
+            }
+        } finally {
+            ConvertUtils.deregister();
+        }
     }
     
     private boolean isPre14JVM() {



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]