summaryrefslogtreecommitdiffstats
path: root/server/tests
diff options
context:
space:
mode:
authorArtur Signell <artur@vaadin.com>2013-05-21 16:51:32 +0300
committerVaadin Code Review <review@vaadin.com>2013-05-22 13:50:40 +0000
commit6bf83a428aa747f6b46d1d4c4ad2e279971d14e7 (patch)
tree2120465ccf24b83acf01ba6c829c4fe59866b74b /server/tests
parent0b635061bb9b30e9f28d49fe9606eb3cab2fd3f1 (diff)
downloadvaadin-framework-6bf83a428aa747f6b46d1d4c4ad2e279971d14e7.tar.gz
vaadin-framework-6bf83a428aa747f6b46d1d4c4ad2e279971d14e7.zip
Modified the logic in setPropertyDatasource which determines if a new converter is needed (#11863)
The previous logic had two flaws * It allowed converter model type to be a sub type of the model type but not vice versa. Similarly for presentation type. * If the user has set a converter it should be used and not be replaced unless it is absolutely sure that it cannot in any possible way handle conversion (e.g. converter from integer to double cannot handle string to list conversion). If there is a slight chance that it can handle conversion, let it be and let the user set another converter when needed. Change-Id: I2e1c0b3aac90be63ddbc780195f8428398e28ada
Diffstat (limited to 'server/tests')
-rw-r--r--server/tests/src/com/vaadin/tests/data/converter/AnotherTestEnum.java16
-rw-r--r--server/tests/src/com/vaadin/tests/data/converter/TestAnyEnumToStringConverter.java128
-rw-r--r--server/tests/src/com/vaadin/tests/data/converter/TestEnum.java16
-rw-r--r--server/tests/src/com/vaadin/tests/data/converter/TestSpecificEnumToStringConverter.java119
4 files changed, 279 insertions, 0 deletions
diff --git a/server/tests/src/com/vaadin/tests/data/converter/AnotherTestEnum.java b/server/tests/src/com/vaadin/tests/data/converter/AnotherTestEnum.java
new file mode 100644
index 0000000000..33a6a87359
--- /dev/null
+++ b/server/tests/src/com/vaadin/tests/data/converter/AnotherTestEnum.java
@@ -0,0 +1,16 @@
+package com.vaadin.tests.data.converter;
+
+public enum AnotherTestEnum {
+ ONE("ONE"), TWO("TWO");
+
+ private String id;
+
+ private AnotherTestEnum(String id) {
+ this.id = id;
+ }
+
+ @Override
+ public String toString() {
+ return id;
+ }
+}
diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestAnyEnumToStringConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestAnyEnumToStringConverter.java
new file mode 100644
index 0000000000..baa81ce656
--- /dev/null
+++ b/server/tests/src/com/vaadin/tests/data/converter/TestAnyEnumToStringConverter.java
@@ -0,0 +1,128 @@
+/*
+ * Copyright 2000-2013 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.data.converter;
+
+import java.util.Locale;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.vaadin.data.util.ObjectProperty;
+import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ReverseConverter;
+import com.vaadin.ui.TextField;
+
+public class TestAnyEnumToStringConverter {
+
+ public class AnyEnumToStringConverter implements Converter<Enum, String> {
+
+ private Class<? extends Enum>[] enumClass;
+
+ public AnyEnumToStringConverter(Class<? extends Enum>... enumClass) {
+ this.enumClass = enumClass;
+ }
+
+ @Override
+ public String convertToModel(Enum value, Locale locale)
+ throws com.vaadin.data.util.converter.Converter.ConversionException {
+ if (value == null) {
+ return null;
+ }
+
+ return value.toString();
+ }
+
+ @Override
+ public Enum convertToPresentation(String value, Locale locale)
+ throws com.vaadin.data.util.converter.Converter.ConversionException {
+ if (value == null) {
+ return null;
+ }
+ for (Class<? extends Enum> candidate : enumClass) {
+ for (Enum e : candidate.getEnumConstants()) {
+ if (e.toString().equals(value)) {
+ return e;
+ }
+ }
+ }
+
+ return null;
+ }
+
+ @Override
+ public Class<String> getModelType() {
+ return String.class;
+ }
+
+ @Override
+ public Class<Enum> getPresentationType() {
+ return Enum.class;
+ }
+
+ }
+
+ private AnyEnumToStringConverter converter;
+
+ @Before
+ public void setup() {
+ converter = new AnyEnumToStringConverter(TestEnum.class,
+ AnotherTestEnum.class);
+ }
+
+ @Test
+ public void nullConversion() {
+ Assert.assertEquals(null, converter.convertToModel(null, null));
+ }
+
+ @Test
+ public void enumToStringConversion() {
+ Assert.assertEquals(TestEnum.TWO.toString(),
+ converter.convertToModel(TestEnum.TWO, null));
+ Assert.assertEquals(AnotherTestEnum.TWO.toString(),
+ converter.convertToModel(AnotherTestEnum.TWO, null));
+ }
+
+ @Test
+ public void stringToEnumConversion() {
+ Assert.assertEquals(TestEnum.TWO,
+ converter.convertToPresentation(TestEnum.TWO.toString(), null));
+ Assert.assertEquals(AnotherTestEnum.TWO, converter
+ .convertToPresentation(AnotherTestEnum.TWO.toString(), null));
+ }
+
+ @Test
+ public void stringToEnumWithField() {
+ TextField tf = new TextField();
+ tf.setConverter(new ReverseConverter(converter));
+ tf.setPropertyDataSource(new ObjectProperty(AnotherTestEnum.TWO));
+ Assert.assertEquals(AnotherTestEnum.TWO.toString(), tf.getValue());
+ tf.setValue(AnotherTestEnum.ONE.toString());
+ Assert.assertEquals(AnotherTestEnum.ONE.toString(), tf.getValue());
+ Assert.assertEquals(AnotherTestEnum.ONE, tf.getConvertedValue());
+ Assert.assertEquals(AnotherTestEnum.ONE, tf.getPropertyDataSource()
+ .getValue());
+
+ tf.setPropertyDataSource(new ObjectProperty(TestEnum.TWO));
+ Assert.assertEquals(TestEnum.TWO.toString(), tf.getValue());
+ tf.setValue(TestEnum.ONE.toString());
+ Assert.assertEquals(TestEnum.ONE.toString(), tf.getValue());
+ Assert.assertEquals(TestEnum.ONE, tf.getConvertedValue());
+ Assert.assertEquals(TestEnum.ONE, tf.getPropertyDataSource().getValue());
+
+ }
+}
diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestEnum.java b/server/tests/src/com/vaadin/tests/data/converter/TestEnum.java
new file mode 100644
index 0000000000..a4b709a843
--- /dev/null
+++ b/server/tests/src/com/vaadin/tests/data/converter/TestEnum.java
@@ -0,0 +1,16 @@
+package com.vaadin.tests.data.converter;
+
+public enum TestEnum {
+ ONE("1"), TWO("2");
+
+ private String id;
+
+ private TestEnum(String id) {
+ this.id = id;
+ }
+
+ @Override
+ public String toString() {
+ return id;
+ }
+}
diff --git a/server/tests/src/com/vaadin/tests/data/converter/TestSpecificEnumToStringConverter.java b/server/tests/src/com/vaadin/tests/data/converter/TestSpecificEnumToStringConverter.java
new file mode 100644
index 0000000000..ef8c57f7f1
--- /dev/null
+++ b/server/tests/src/com/vaadin/tests/data/converter/TestSpecificEnumToStringConverter.java
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2000-2013 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.data.converter;
+
+import java.util.Locale;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.vaadin.data.util.ObjectProperty;
+import com.vaadin.data.util.converter.Converter;
+import com.vaadin.data.util.converter.ReverseConverter;
+import com.vaadin.ui.TextField;
+
+public class TestSpecificEnumToStringConverter {
+
+ public class SpecificEnumToStringConverter implements
+ Converter<Enum, String> {
+
+ private Class<? extends Enum> enumClass;
+
+ public SpecificEnumToStringConverter(Class<? extends Enum> enumClass) {
+ this.enumClass = enumClass;
+ }
+
+ @Override
+ public String convertToModel(Enum value, Locale locale)
+ throws com.vaadin.data.util.converter.Converter.ConversionException {
+ if (value == null) {
+ return null;
+ }
+
+ return value.toString();
+ }
+
+ @Override
+ public Enum convertToPresentation(String value, Locale locale)
+ throws com.vaadin.data.util.converter.Converter.ConversionException {
+ if (value == null) {
+ return null;
+ }
+
+ for (Enum e : enumClass.getEnumConstants()) {
+ if (e.toString().equals(value)) {
+ return e;
+ }
+ }
+
+ return null;
+ }
+
+ @Override
+ public Class<String> getModelType() {
+ return String.class;
+ }
+
+ @Override
+ public Class<Enum> getPresentationType() {
+ return (Class<Enum>) enumClass;
+ }
+
+ }
+
+ SpecificEnumToStringConverter testEnumConverter;
+ SpecificEnumToStringConverter anotherTestEnumConverter;
+
+ @Before
+ public void setup() {
+ testEnumConverter = new SpecificEnumToStringConverter(TestEnum.class);
+ anotherTestEnumConverter = new SpecificEnumToStringConverter(
+ AnotherTestEnum.class);
+ }
+
+ @Test
+ public void nullConversion() {
+ Assert.assertEquals(null, testEnumConverter.convertToModel(null, null));
+ }
+
+ @Test
+ public void enumToStringConversion() {
+ Assert.assertEquals(TestEnum.TWO.toString(),
+ testEnumConverter.convertToModel(TestEnum.TWO, null));
+ }
+
+ @Test
+ public void stringToEnumConversion() {
+ Assert.assertEquals(TestEnum.TWO, testEnumConverter
+ .convertToPresentation(TestEnum.TWO.toString(), null));
+ }
+
+ @Test
+ public void stringToEnumWithField() {
+ TextField tf = new TextField();
+ tf.setConverter(new ReverseConverter(anotherTestEnumConverter));
+ tf.setPropertyDataSource(new ObjectProperty(AnotherTestEnum.TWO));
+ Assert.assertEquals(AnotherTestEnum.TWO.toString(), tf.getValue());
+ tf.setValue(AnotherTestEnum.ONE.toString());
+ Assert.assertEquals(AnotherTestEnum.ONE.toString(), tf.getValue());
+ Assert.assertEquals(AnotherTestEnum.ONE, tf.getConvertedValue());
+ Assert.assertEquals(AnotherTestEnum.ONE, tf.getPropertyDataSource()
+ .getValue());
+
+ }
+}