From: Johannes Dahlström Date: Wed, 18 Mar 2015 13:26:39 +0000 (+0200) Subject: Fix Declarative support for primitive float and double properties (#17204) X-Git-Tag: 7.5.0.alpha1~8^2~2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=7d4de11c54060286bbc7adfeefa2891f3a461b1d;p=vaadin-framework.git Fix Declarative support for primitive float and double properties (#17204) Also improve tests. Change-Id: I99e064c931770fb8af511ea5c8e2c7fc2da2464e --- diff --git a/server/src/com/vaadin/ui/declarative/DesignFormatter.java b/server/src/com/vaadin/ui/declarative/DesignFormatter.java index d2fbf2c765..cc5071fe9d 100644 --- a/server/src/com/vaadin/ui/declarative/DesignFormatter.java +++ b/server/src/com/vaadin/ui/declarative/DesignFormatter.java @@ -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 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 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() { @@ -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, diff --git a/server/src/com/vaadin/ui/declarative/converters/DesignToStringConverter.java b/server/src/com/vaadin/ui/declarative/converters/DesignToStringConverter.java index d80119bea1..0c6cf55bed 100644 --- a/server/src/com/vaadin/ui/declarative/converters/DesignToStringConverter.java +++ b/server/src/com/vaadin/ui/declarative/converters/DesignToStringConverter.java @@ -64,7 +64,8 @@ public class DesignToStringConverter implements Converter { * must be public and static method that returns an object of * passed type. */ - public DesignToStringConverter(Class type, String staticMethodName) { + public DesignToStringConverter(Class type, + String staticMethodName) { this.type = type; this.staticMethodName = staticMethodName; } @@ -81,7 +82,7 @@ public class DesignToStringConverter implements Converter { } 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) { diff --git a/server/tests/src/com/vaadin/tests/design/DesignFormatterTest.java b/server/tests/src/com/vaadin/tests/design/DesignFormatterTest.java index c7909751a1..1e6d446427 100644 --- a/server/tests/src/com/vaadin/tests/design/DesignFormatterTest.java +++ b/server/tests/src/com/vaadin/tests/design/DesignFormatterTest.java @@ -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");