diff options
author | Artur Signell <artur@vaadin.com> | 2013-08-09 20:35:54 +0300 |
---|---|---|
committer | Vaadin Code Review <review@vaadin.com> | 2013-10-30 13:58:43 +0000 |
commit | fbf069d745f9903da2c215a46027306417c1e3ed (patch) | |
tree | 1d306b1e5b5ee4ab1522ff961415a789d262369a /server/tests/src/com/vaadin | |
parent | e2157f47967e1470704cb0b2ab023941f26171f9 (diff) | |
download | vaadin-framework-fbf069d745f9903da2c215a46027306417c1e3ed.tar.gz vaadin-framework-fbf069d745f9903da2c215a46027306417c1e3ed.zip |
Add StringToLongConverter for automatic handling of long values (#12225)
Backported from master
Change-Id: Ibb91d28aab3731486c84d09547c7af641f39610a
Merge: no
Diffstat (limited to 'server/tests/src/com/vaadin')
-rw-r--r-- | server/tests/src/com/vaadin/tests/data/converter/TestStringToLongConverter.java | 68 | ||||
-rw-r--r-- | server/tests/src/com/vaadin/tests/server/component/abstractfield/DefaultConverterFactory.java | 42 |
2 files changed, 110 insertions, 0 deletions
diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestStringToLongConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestStringToLongConverter.java new file mode 100644 index 0000000000..18e2ed06c0 --- /dev/null +++ b/server/tests/src/com/vaadin/tests/data/converter/TestStringToLongConverter.java @@ -0,0 +1,68 @@ +package com.vaadin.tests.data.converter; + +import java.util.Locale; + +import junit.framework.Assert; +import junit.framework.TestCase; + +import com.vaadin.data.util.converter.Converter; +import com.vaadin.data.util.converter.ReverseConverter; +import com.vaadin.data.util.converter.StringToLongConverter; + +public class TestStringToLongConverter extends TestCase { + + StringToLongConverter converter = new StringToLongConverter(); + Converter<Long, String> reverseConverter = new ReverseConverter<Long, String>( + converter); + + public void testNullConversion() { + assertEquals(null, converter.convertToModel(null, Long.class, null)); + } + + public void testReverseNullConversion() { + assertEquals(null, + reverseConverter.convertToModel(null, String.class, null)); + } + + public void testEmptyStringConversion() { + assertEquals(null, converter.convertToModel("", Long.class, null)); + } + + public void testValueConversion() { + assertEquals(Long.valueOf(10), + converter.convertToModel("10", Long.class, null)); + } + + public void testReverseValueConversion() { + assertEquals(reverseConverter.convertToModel(10L, String.class, null), + "10"); + } + + public void testExtremeLongValueConversion() { + long l = converter.convertToModel("9223372036854775807", Long.class, + null); + Assert.assertEquals(Long.MAX_VALUE, l); + l = converter.convertToModel("-9223372036854775808", Long.class, null); + assertEquals(Long.MIN_VALUE, l); + } + + public void testExtremeReverseLongValueConversion() { + String str = reverseConverter.convertToModel(Long.MAX_VALUE, + String.class, Locale.ENGLISH); + Assert.assertEquals("9,223,372,036,854,775,807", str); + str = reverseConverter.convertToModel(Long.MIN_VALUE, String.class, + null); + Assert.assertEquals("-9,223,372,036,854,775,808", str); + } + + public void testOutOfBoundsValueConversion() { + // Long.MAX_VALUE+1 is converted to Long.MAX_VALUE + long l = converter.convertToModel("9223372036854775808", Long.class, + null); + Assert.assertEquals(Long.MAX_VALUE, l); + // Long.MIN_VALUE-1 is converted to Long.MIN_VALUE + l = converter.convertToModel("-9223372036854775809", Long.class, null); + assertEquals(Long.MIN_VALUE, l); + + } +} diff --git a/server/tests/src/com/vaadin/tests/server/component/abstractfield/DefaultConverterFactory.java b/server/tests/src/com/vaadin/tests/server/component/abstractfield/DefaultConverterFactory.java index bac024725f..99397e9e8f 100644 --- a/server/tests/src/com/vaadin/tests/server/component/abstractfield/DefaultConverterFactory.java +++ b/server/tests/src/com/vaadin/tests/server/component/abstractfield/DefaultConverterFactory.java @@ -43,6 +43,33 @@ public class DefaultConverterFactory extends TestCase { } + public static class LongBean { + long l1; + Long l2; + + public LongBean(long l1, Long l2) { + this.l1 = l1; + this.l2 = l2; + } + + public long getL1() { + return l1; + } + + public void setL1(long l1) { + this.l1 = l1; + } + + public Long getL2() { + return l2; + } + + public void setL2(Long l2) { + this.l2 = l2; + } + + } + Person paulaBean = new Person("Paula", "Brilliant", "paula@brilliant.com", 34, Sex.FEMALE, new Address("Paula street 1", 12345, "P-town", Country.FINLAND)); @@ -68,6 +95,21 @@ public class DefaultConverterFactory extends TestCase { assertEquals(24f, tf.getPropertyDataSource().getValue()); } + public void testLongConversion() { + VaadinSession sess = new AlwaysLockedVaadinSession(null); + VaadinSession.setCurrent(sess); + + TextField tf = new TextField(); + tf.setLocale(new Locale("en", "US")); + tf.setPropertyDataSource(new MethodProperty<Integer>(new LongBean(12, + 1982739187238L), "l2")); + assertEquals("1,982,739,187,238", tf.getValue()); + tf.setValue("1982739187239"); + assertEquals("1,982,739,187,239", tf.getValue()); + assertEquals(1982739187239L, tf.getConvertedValue()); + assertEquals(1982739187239L, tf.getPropertyDataSource().getValue()); + } + public void testDefaultNumberConversion() { VaadinSession app = new AlwaysLockedVaadinSession(null); VaadinSession.setCurrent(app); |