1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/*
* 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.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.tests.data.bean.AnotherTestEnum;
import com.vaadin.tests.data.bean.TestEnum;
import com.vaadin.ui.TextField;
public class TestAnyEnumToStringConverter {
public class AnyEnumToStringConverter implements Converter<Enum, String> {
public AnyEnumToStringConverter() {
}
@Override
public String convertToModel(Enum value,
Class<? extends String> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
if (value == null) {
return null;
}
return value.toString();
}
@Override
public Enum convertToPresentation(String value,
Class<? extends Enum> targetType, Locale locale)
throws com.vaadin.data.util.converter.Converter.ConversionException {
if (value == null) {
return null;
}
for (Enum e : targetType.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();
}
@Test
public void nullConversion() {
Assert.assertEquals(null, converter.convertToModel(null, null, null));
}
@Test
public void enumToStringConversion() {
Assert.assertEquals(TestEnum.TWO.toString(),
converter.convertToModel(TestEnum.TWO, String.class, null));
Assert.assertEquals(AnotherTestEnum.TWO.toString(), converter
.convertToModel(AnotherTestEnum.TWO, String.class, null));
}
@Test
public void stringToEnumConversion() {
Assert.assertEquals(TestEnum.TWO, converter.convertToPresentation(
TestEnum.TWO.toString(), TestEnum.class, null));
Assert.assertEquals(AnotherTestEnum.TWO, converter
.convertToPresentation(AnotherTestEnum.TWO.toString(),
AnotherTestEnum.class, 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());
}
}
|