diff options
author | Leif Åstrand <leif@vaadin.com> | 2015-03-23 08:20:23 +0200 |
---|---|---|
committer | Leif Åstrand <leif@vaadin.com> | 2015-03-23 08:20:23 +0200 |
commit | 20d5456c3d5fb825783d7311ebd797c9ae8690f9 (patch) | |
tree | 9401f94b7d84209bdc991c15995512c56e50d03f /server | |
parent | 16c67cfab9b3dd2dbf324caa612fa3a2d15550d0 (diff) | |
parent | 04272699b8f429a2bc3b8f0a41e20a604213895e (diff) | |
download | vaadin-framework-20d5456c3d5fb825783d7311ebd797c9ae8690f9.tar.gz vaadin-framework-20d5456c3d5fb825783d7311ebd797c9ae8690f9.zip |
Merge remote-tracking branch 'origin/master' into grid-7.5
Change-Id: I53218c269f0701cddd8279bf25c493950144f4cb
Diffstat (limited to 'server')
6 files changed, 249 insertions, 21 deletions
diff --git a/server/src/com/vaadin/server/communication/PushHandler.java b/server/src/com/vaadin/server/communication/PushHandler.java index c570d22086..ea937d279e 100644 --- a/server/src/com/vaadin/server/communication/PushHandler.java +++ b/server/src/com/vaadin/server/communication/PushHandler.java @@ -356,9 +356,13 @@ public class PushHandler extends AtmosphereResourceEventListenerAdapter { "Could not get session. This should never happen", e); return; } catch (SessionExpiredException e) { + // This happens at least if the server is restarted without + // preserving the session. After restart the client reconnects, gets + // a session expired notification and then closes the connection and + // ends up here getLogger() - .log(Level.SEVERE, - "Session expired before push was disconnected. This should never happen", + .log(Level.FINER, + "Session expired before push disconnect event was received", e); return; } @@ -383,9 +387,9 @@ public class PushHandler extends AtmosphereResourceEventListenerAdapter { if (ui == null) { getLogger() - .log(Level.SEVERE, + .log(Level.FINE, "Could not get UI. This should never happen," - + " except when reloading in Firefox -" + + " except when reloading in Firefox and Chrome -" + " see http://dev.vaadin.com/ticket/14251."); return; } else { diff --git a/server/src/com/vaadin/ui/Slider.java b/server/src/com/vaadin/ui/Slider.java index fab6e33cae..99e1e8d5e9 100644 --- a/server/src/com/vaadin/ui/Slider.java +++ b/server/src/com/vaadin/ui/Slider.java @@ -343,4 +343,14 @@ public class Slider extends AbstractField<Double> { return Double.class; } + @Override + public void clear() { + super.setValue(Double.valueOf(getState().minValue)); + } + + @Override + public boolean isEmpty() { + // Slider is never really "empty" + return false; + } } diff --git a/server/src/com/vaadin/ui/declarative/DesignFormatter.java b/server/src/com/vaadin/ui/declarative/DesignFormatter.java index d2fbf2c765..728b3d1077 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; @@ -28,6 +29,7 @@ import java.util.TimeZone; import java.util.concurrent.ConcurrentHashMap; import com.vaadin.data.util.converter.Converter; +import com.vaadin.data.util.converter.StringToBigDecimalConverter; import com.vaadin.data.util.converter.StringToDoubleConverter; import com.vaadin.data.util.converter.StringToFloatConverter; import com.vaadin.event.ShortcutAction; @@ -67,8 +69,8 @@ public class DesignFormatter implements Serializable { */ protected void mapDefaultTypes() { // numbers use standard toString/valueOf approach - for (Class<?> c : new Class<?>[] { Integer.class, Byte.class, - Short.class, Long.class, BigDecimal.class }) { + for (Class<?> c : new Class<?>[] { Byte.class, Short.class, + Integer.class, Long.class }) { DesignToStringConverter<?> conv = new DesignToStringConverter(c); converterMap.put(c, conv); try { @@ -110,21 +112,37 @@ 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); + + final DecimalFormat bigDecimalFmt = new DecimalFormat("0.###", symbols); + bigDecimalFmt.setGroupingUsed(false); + bigDecimalFmt.setParseBigDecimal(true); + converterMap.put(BigDecimal.class, new StringToBigDecimalConverter() { + @Override + protected NumberFormat getFormat(Locale locale) { + return bigDecimalFmt; + }; }); // strings do nothing @@ -169,7 +187,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<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) { diff --git a/server/tests/src/com/vaadin/tests/design/DesignFormatterTest.java b/server/tests/src/com/vaadin/tests/design/DesignFormatterTest.java index c7909751a1..05b2484767 100644 --- a/server/tests/src/com/vaadin/tests/design/DesignFormatterTest.java +++ b/server/tests/src/com/vaadin/tests/design/DesignFormatterTest.java @@ -18,6 +18,7 @@ package com.vaadin.tests.design; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; +import java.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashSet; @@ -53,17 +54,127 @@ 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, - FileResource.class, ExternalResource.class, - ThemeResource.class, Resource.class, TimeZone.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, BigDecimal.class, String.class, + ShortcutAction.class, Date.class, FileResource.class, + ExternalResource.class, ThemeResource.class, Resource.class, + TimeZone.class }) { assertTrue("not supported " + type.getSimpleName(), formatter.canConvert(type)); } } @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 testBigDecimal() { + BigDecimal bd = new BigDecimal("123456789123456789.123456789123456789"); + assertEquals("123456789123456789.123", formatter.format(bd)); + assertEquals(bd, formatter.parse( + "123456789123456789.123456789123456789", BigDecimal.class)); + } + + @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"); String formatted = formatter.format(date); diff --git a/server/tests/src/com/vaadin/tests/server/component/FieldDefaultValues.java b/server/tests/src/com/vaadin/tests/server/component/FieldDefaultValues.java new file mode 100644 index 0000000000..5c9993e6dd --- /dev/null +++ b/server/tests/src/com/vaadin/tests/server/component/FieldDefaultValues.java @@ -0,0 +1,84 @@ +/* + * Copyright 2000-2014 Vaadin Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may not + * use this file except in compliance with the License. You may obtain a copy of + * the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations under + * the License. + */ +package com.vaadin.tests.server.component; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.tests.VaadinClasses; +import com.vaadin.ui.Field; +import com.vaadin.ui.PasswordField; +import com.vaadin.ui.ProgressBar; +import com.vaadin.ui.RichTextArea; +import com.vaadin.ui.Slider; +import com.vaadin.ui.TextArea; + +public class FieldDefaultValues { + + @Test + public void testFieldsHaveDefaultValueAfterClear() throws Exception { + for (Field<?> field : createFields()) { + Object originalValue = field.getValue(); + + // Some fields are not initialized to the "empty" value. #17089 + if (field instanceof PasswordField || field instanceof ProgressBar + || field instanceof RichTextArea + || field instanceof TextArea) { + originalValue = null; + } + + field.clear(); + + Object clearedValue = field.getValue(); + + Assert.assertEquals("Expected to get default value after clearing " + + field.getClass().getName(), originalValue, clearedValue); + } + } + + @Test + public void testFieldsAreEmptyAfterClear() throws Exception { + for (Field<?> field : createFields()) { + field.clear(); + + if (field instanceof Slider) { + Assert.assertFalse( + "Slider should not be empty even after being cleared", + field.isEmpty()); + + } else { + Assert.assertTrue(field.getClass().getName() + + " should be empty after being cleared", + field.isEmpty()); + } + } + } + + @SuppressWarnings("rawtypes") + private static List<Field<?>> createFields() throws InstantiationException, + IllegalAccessException { + List<Field<?>> fieldInstances = new ArrayList<Field<?>>(); + + for (Class<? extends Field> fieldType : VaadinClasses.getFields()) { + fieldInstances.add(fieldType.newInstance()); + } + return fieldInstances; + } + +} |