From 6bf83a428aa747f6b46d1d4c4ad2e279971d14e7 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Tue, 21 May 2013 16:51:32 +0300 Subject: 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 --- .../tests/data/converter/AnotherTestEnum.java | 16 +++ .../converter/TestAnyEnumToStringConverter.java | 128 +++++++++++++++++++++ .../com/vaadin/tests/data/converter/TestEnum.java | 16 +++ .../TestSpecificEnumToStringConverter.java | 119 +++++++++++++++++++ 4 files changed, 279 insertions(+) create mode 100644 server/tests/src/com/vaadin/tests/data/converter/AnotherTestEnum.java create mode 100644 server/tests/src/com/vaadin/tests/data/converter/TestAnyEnumToStringConverter.java create mode 100644 server/tests/src/com/vaadin/tests/data/converter/TestEnum.java create mode 100644 server/tests/src/com/vaadin/tests/data/converter/TestSpecificEnumToStringConverter.java (limited to 'server/tests') 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 { + + private Class[] enumClass; + + public AnyEnumToStringConverter(Class... 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 candidate : enumClass) { + for (Enum e : candidate.getEnumConstants()) { + if (e.toString().equals(value)) { + return e; + } + } + } + + return null; + } + + @Override + public Class getModelType() { + return String.class; + } + + @Override + public Class 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 { + + private Class enumClass; + + public SpecificEnumToStringConverter(Class 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 getModelType() { + return String.class; + } + + @Override + public Class getPresentationType() { + return (Class) 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()); + + } +} -- cgit v1.2.3