]> source.dussan.org Git - vaadin-framework.git/commitdiff
Fix Declarative support for primitive float and double properties (#17204)
authorJohannes Dahlström <johannesd@vaadin.com>
Wed, 18 Mar 2015 13:26:39 +0000 (15:26 +0200)
committerVaadin Code Review <review@vaadin.com>
Fri, 20 Mar 2015 09:21:23 +0000 (09:21 +0000)
Also improve tests.

Change-Id: I99e064c931770fb8af511ea5c8e2c7fc2da2464e

server/src/com/vaadin/ui/declarative/DesignFormatter.java
server/src/com/vaadin/ui/declarative/converters/DesignToStringConverter.java
server/tests/src/com/vaadin/tests/design/DesignFormatterTest.java

index d2fbf2c765626efbdda7e51e8a025616e5fb3e58..cc5071fe9dd9f1e88939982316a480bc4b4af7c5 100644 (file)
@@ -19,6 +19,7 @@ import java.io.Serializable;
 import java.math.BigDecimal;
 import java.text.DecimalFormat;
 import java.text.DecimalFormatSymbols;
+import java.text.NumberFormat;
 import java.util.Collections;
 import java.util.Date;
 import java.util.Locale;
@@ -110,22 +111,28 @@ public class DesignFormatter implements Serializable {
         converterMap.put(boolean.class, booleanConverter);
 
         // floats and doubles use formatters
-        DecimalFormatSymbols symbols = new DecimalFormatSymbols(new Locale(
-                "en_US"));
+        final DecimalFormatSymbols symbols = new DecimalFormatSymbols(
+                new Locale("en_US"));
         final DecimalFormat fmt = new DecimalFormat("0.###", symbols);
         fmt.setGroupingUsed(false);
-        converterMap.put(Float.class, new StringToFloatConverter() {
+
+        Converter<String, ?> floatConverter = new StringToFloatConverter() {
             @Override
-            protected java.text.NumberFormat getFormat(Locale locale) {
+            protected NumberFormat getFormat(Locale locale) {
                 return fmt;
             };
-        });
-        converterMap.put(Double.class, new StringToDoubleConverter() {
+        };
+        converterMap.put(Float.class, floatConverter);
+        converterMap.put(float.class, floatConverter);
+
+        Converter<String, ?> doubleConverter = new StringToDoubleConverter() {
             @Override
-            protected java.text.NumberFormat getFormat(Locale locale) {
+            protected NumberFormat getFormat(Locale locale) {
                 return fmt;
             };
-        });
+        };
+        converterMap.put(Double.class, doubleConverter);
+        converterMap.put(double.class, doubleConverter);
 
         // strings do nothing
         converterMap.put(String.class, new Converter<String, String>() {
@@ -169,7 +176,7 @@ public class DesignFormatter implements Serializable {
 
         };
         converterMap.put(Character.class, charConverter);
-        converterMap.put(Character.TYPE, charConverter);
+        converterMap.put(char.class, charConverter);
 
         converterMap.put(Date.class, new DesignDateConverter());
         converterMap.put(ShortcutAction.class,
index d80119bea1dfbfb86982e694e9a3b0fd150603a9..0c6cf55bed80c68db8ce4643a06c0420f4a425ae 100644 (file)
@@ -64,7 +64,8 @@ public class DesignToStringConverter<TYPE> implements Converter<String, TYPE> {
      *            must be public and static method that returns an object of
      *            passed type.
      */
-    public DesignToStringConverter(Class<? extends TYPE> type, String staticMethodName) {
+    public DesignToStringConverter(Class<? extends TYPE> type,
+            String staticMethodName) {
         this.type = type;
         this.staticMethodName = staticMethodName;
     }
@@ -81,7 +82,7 @@ public class DesignToStringConverter<TYPE> implements Converter<String, TYPE> {
         } catch (IllegalArgumentException e) {
             throw new Converter.ConversionException(e);
         } catch (InvocationTargetException e) {
-            throw new Converter.ConversionException(e);
+            throw new Converter.ConversionException(e.getCause());
         } catch (NoSuchMethodException e) {
             throw new Converter.ConversionException(e);
         } catch (SecurityException e) {
index c7909751a107e2ee11680b9b562ad08df0c62073..1e6d44642738988150f3e78aa2d57c7b19dae9ea 100644 (file)
@@ -53,9 +53,11 @@ public class DesignFormatterTest {
     @Test
     public void testSupportedClasses() {
 
-        for (Class<?> type : new Class<?>[] { String.class, Boolean.class,
-                Integer.class, Float.class, Byte.class, Short.class,
-                Double.class, ShortcutAction.class, Date.class,
+        for (Class<?> type : new Class<?>[] { boolean.class, char.class,
+                byte.class, short.class, int.class, long.class, float.class,
+                double.class, Boolean.class, Character.class, Byte.class,
+                Short.class, Integer.class, Long.class, Float.class,
+                Double.class, String.class, ShortcutAction.class, Date.class,
                 FileResource.class, ExternalResource.class,
                 ThemeResource.class, Resource.class, TimeZone.class }) {
             assertTrue("not supported " + type.getSimpleName(),
@@ -63,6 +65,105 @@ public class DesignFormatterTest {
         }
     }
 
+    @Test
+    public void testBoolean() {
+        assertEquals("true", formatter.format(true));
+        assertEquals("false", formatter.format(false));
+
+        assertEquals(true, formatter.parse("true", boolean.class));
+        assertEquals(true, formatter.parse("foobar", boolean.class));
+        assertEquals(true, formatter.parse("", boolean.class));
+        assertEquals(false, formatter.parse("false", boolean.class));
+
+        assertEquals(true, formatter.parse("true", Boolean.class));
+        assertEquals(true, formatter.parse("foobar", Boolean.class));
+        assertEquals(true, formatter.parse("", Boolean.class));
+        assertEquals(false, formatter.parse("false", Boolean.class));
+    }
+
+    @Test
+    public void testIntegral() {
+        byte b = 123;
+        assertEquals("123", formatter.format(b));
+        assertEquals(b, (byte) formatter.parse("123", byte.class));
+        assertEquals((Byte) b, formatter.parse("123", Byte.class));
+
+        b = -123;
+        assertEquals("-123", formatter.format(b));
+        assertEquals(b, (byte) formatter.parse("-123", byte.class));
+        assertEquals((Byte) b, formatter.parse("-123", Byte.class));
+
+        short s = 12345;
+        assertEquals("12345", formatter.format(s));
+        assertEquals(s, (short) formatter.parse("12345", short.class));
+        assertEquals((Short) s, formatter.parse("12345", Short.class));
+
+        s = -12345;
+        assertEquals("-12345", formatter.format(s));
+        assertEquals(s, (short) formatter.parse("-12345", short.class));
+        assertEquals((Short) s, formatter.parse("-12345", Short.class));
+
+        int i = 123456789;
+        assertEquals("123456789", formatter.format(i));
+        assertEquals(i, (int) formatter.parse("123456789", int.class));
+        assertEquals((Integer) i, formatter.parse("123456789", Integer.class));
+
+        i = -123456789;
+        assertEquals("-123456789", formatter.format(i));
+        assertEquals(i, (int) formatter.parse("-123456789", int.class));
+        assertEquals((Integer) i, formatter.parse("-123456789", Integer.class));
+
+        long l = 123456789123456789L;
+        assertEquals("123456789123456789", formatter.format(l));
+        assertEquals(l,
+                (long) formatter.parse("123456789123456789", long.class));
+        assertEquals((Long) l,
+                formatter.parse("123456789123456789", Long.class));
+
+        l = -123456789123456789L;
+        assertEquals("-123456789123456789", formatter.format(l));
+        assertEquals(l,
+                (long) formatter.parse("-123456789123456789", long.class));
+        assertEquals((Long) l,
+                formatter.parse("-123456789123456789", Long.class));
+    }
+
+    @Test
+    public void testFloatingPoint() {
+        float f = 123.4567f;
+        assertEquals("123.457", formatter.format(f));
+        assertEquals(f, formatter.parse("123.4567", float.class), 1e-4);
+        assertEquals(f, formatter.parse("123.4567", Float.class), 1e-4);
+
+        double d = 123456789.123456789;
+        assertEquals("123456789.123", formatter.format(d));
+        assertEquals(d, formatter.parse("123456789.123456789", double.class),
+                1e-9);
+        assertEquals(d, formatter.parse("123456789.123456789", Double.class),
+                1e-9);
+
+    }
+
+    @Test
+    public void testChar() {
+        char c = '\uABCD';
+        assertEquals("\uABCD", formatter.format(c));
+        assertEquals(c, (char) formatter.parse("\uABCD", char.class));
+        assertEquals((Character) c, formatter.parse("\uABCD", Character.class));
+
+        c = 'y';
+        assertEquals(c, (char) formatter.parse("yes", char.class));
+    }
+
+    @Test
+    public void testString() {
+
+        for (String s : new String[] { "", "foobar", "\uABCD", "驯鹿" }) {
+            assertEquals(s, formatter.format(s));
+            assertEquals(s, formatter.parse(s, String.class));
+        }
+    }
+
     @Test
     public void testDate() throws Exception {
         Date date = new SimpleDateFormat("yyyy-MM-dd").parse("2012-02-17");