1
1
package com .tngtech .configbuilder .util ;
2
2
3
3
import com .tngtech .configbuilder .annotation .typetransformer .*;
4
- import com .tngtech .configbuilder .configuration .BuilderConfiguration ;
5
4
import com .tngtech .configbuilder .configuration .ErrorMessageSetup ;
6
5
import com .tngtech .configbuilder .exception .PrimitiveParsingException ;
7
6
import org .junit .Before ;
14
13
import java .nio .file .Path ;
15
14
import java .nio .file .Paths ;
16
15
import java .util .Collection ;
16
+ import java .util .EnumSet ;
17
17
import java .util .List ;
18
+ import java .util .Set ;
18
19
19
20
import static com .google .common .collect .Lists .newArrayList ;
21
+ import static com .tngtech .configbuilder .util .FieldValueTransformerComponentTest .TestEnum .BAR ;
22
+ import static com .tngtech .configbuilder .util .FieldValueTransformerComponentTest .TestEnum .FOO ;
20
23
import static org .assertj .core .api .Assertions .assertThat ;
21
24
import static org .mockito .Mockito .when ;
22
25
23
26
@ RunWith (MockitoJUnitRunner .class )
24
27
public class FieldValueTransformerComponentTest {
25
28
26
- public class TestTransformer extends TypeTransformer <String , Integer > {
29
+ public static class TestTransformer extends TypeTransformer <String , Integer > {
27
30
@ Override
28
31
public Integer transform (String argument ) {
29
32
return 1472 ;
30
33
}
31
34
}
32
35
33
- public static class AnotherTestTransformer extends TypeTransformer <String , Integer > {
34
- @ Override
35
- public Integer transform (String argument ) {
36
- return 1472 ;
37
- }
36
+ enum TestEnum {
37
+ FOO , BAR
38
38
}
39
39
40
- private class TestConfigClass {
40
+ private static class TestConfigClass {
41
41
42
42
@ TypeTransformers ({CharacterSeparatedStringToStringListTransformer .class })
43
43
private Collection <String > stringCollectionField ;
@@ -46,36 +46,38 @@ private class TestConfigClass {
46
46
private Boolean boolField ;
47
47
@ TypeTransformers ({TestTransformer .class })
48
48
private int otherIntField ;
49
- @ TypeTransformers ({AnotherTestTransformer .class })
50
- private Integer integerField ;
51
49
private Collection <Path > pathCollectionField ;
52
50
private Collection <Integer > integerCollectionField ;
53
51
private Collection <Object > objectCollectionField ;
52
+ private TestEnum enumField ;
53
+ private List <TestEnum > enumListField ;
54
+ private Set <TestEnum > enumSetField ;
54
55
}
55
56
56
57
@ Mock
57
58
private ConfigBuilderFactory configBuilderFactory ;
58
59
@ Mock
59
60
private ErrorMessageSetup errorMessageSetup ;
60
- @ Mock
61
- private BuilderConfiguration builderConfiguration ;
61
+
62
+ private FieldValueTransformer fieldValueTransformer ;
62
63
63
64
private Field stringCollectionField ;
64
65
private Field intField ;
65
66
private Field doubleField ;
66
67
private Field boolField ;
67
68
private Field otherIntField ;
68
- private Field integerField ;
69
69
private Field pathCollectionField ;
70
70
private Field integerCollectionField ;
71
71
private Field objectCollectionField ;
72
-
73
- private FieldValueTransformer fieldValueTransformer ;
72
+ private Field enumField ;
73
+ private Field enumListField ;
74
+ private Field enumSetField ;
74
75
75
76
@ Before
76
77
public void setUp () throws Exception {
77
78
when (configBuilderFactory .getInstance (ErrorMessageSetup .class )).thenReturn (errorMessageSetup );
78
79
when (configBuilderFactory .getInstance (GenericsAndCastingHelper .class )).thenReturn (new GenericsAndCastingHelper ());
80
+ when (configBuilderFactory .getInstance (EnumTypeExtractor .class )).thenReturn (new EnumTypeExtractor ());
79
81
80
82
when (configBuilderFactory .getInstance (CharacterSeparatedStringToStringListTransformer .class )).thenReturn (new CharacterSeparatedStringToStringListTransformer ());
81
83
when (configBuilderFactory .getInstance (CharacterSeparatedStringToStringSetTransformer .class )).thenReturn (new CharacterSeparatedStringToStringSetTransformer ());
@@ -86,23 +88,25 @@ public void setUp() throws Exception {
86
88
when (configBuilderFactory .getInstance (StringOrPrimitiveToPrimitiveTransformer .class )).thenReturn (new StringOrPrimitiveToPrimitiveTransformer ());
87
89
when (configBuilderFactory .getInstance (TestTransformer .class )).thenReturn (new TestTransformer ());
88
90
91
+ this .fieldValueTransformer = new FieldValueTransformer (configBuilderFactory );
92
+
89
93
stringCollectionField = TestConfigClass .class .getDeclaredField ("stringCollectionField" );
90
94
intField = TestConfigClass .class .getDeclaredField ("intField" );
91
95
boolField = TestConfigClass .class .getDeclaredField ("boolField" );
92
96
otherIntField = TestConfigClass .class .getDeclaredField ("otherIntField" );
93
- integerField = TestConfigClass .class .getDeclaredField ("integerField" );
94
97
pathCollectionField = TestConfigClass .class .getDeclaredField ("pathCollectionField" );
95
98
integerCollectionField = TestConfigClass .class .getDeclaredField ("integerCollectionField" );
96
99
doubleField = TestConfigClass .class .getDeclaredField ("doubleField" );
97
100
objectCollectionField = TestConfigClass .class .getDeclaredField ("objectCollectionField" );
98
-
99
- this .fieldValueTransformer = new FieldValueTransformer (configBuilderFactory );
101
+ enumField = TestConfigClass .class .getDeclaredField ("enumField" );
102
+ enumListField = TestConfigClass .class .getDeclaredField ("enumListField" );
103
+ enumSetField = TestConfigClass .class .getDeclaredField ("enumSetField" );
100
104
}
101
105
102
106
@ Test
103
107
public void testTransformingStringToStringCollection () {
104
- List < String > actualResult = ( List < String >) fieldValueTransformer .transformFieldValue (stringCollectionField , "Alpha,Beta,Gamma" );
105
- assertThat (actualResult ).containsExactly ( "Alpha" , "Beta" , "Gamma" );
108
+ Object actualResult = fieldValueTransformer .transformFieldValue (stringCollectionField , "Alpha,Beta,Gamma" );
109
+ assertThat (actualResult ).isEqualTo ( newArrayList ( "Alpha" , "Beta" , "Gamma" ) );
106
110
}
107
111
108
112
@ Test
@@ -136,25 +140,43 @@ public void testThatTransformersInAnnotationArePrioritized() {
136
140
137
141
@ Test
138
142
public void testTransformingStringToPathCollection () {
139
- Collection < Path > actualResult = ( Collection < Path >) fieldValueTransformer .transformFieldValue (pathCollectionField , "/etc,/usr" );
143
+ Object actualResult = fieldValueTransformer .transformFieldValue (pathCollectionField , "/etc,/usr" );
140
144
assertThat (actualResult ).isEqualTo (newArrayList (Paths .get ("/etc" ), Paths .get ("/usr" )));
141
145
}
142
146
143
147
@ Test
144
148
public void testTransformingStringToIntegerCollection () {
145
- Collection < Integer > actualResult = ( Collection < Integer >) fieldValueTransformer .transformFieldValue (integerCollectionField , "3,4" );
149
+ Object actualResult = fieldValueTransformer .transformFieldValue (integerCollectionField , "3,4" );
146
150
assertThat (actualResult ).isEqualTo (newArrayList (3 , 4 ));
147
151
}
148
152
149
153
@ Test
150
154
public void testTransformingStringToObjectCollection () {
151
- Collection < Object > actualResult = ( Collection < Object >) fieldValueTransformer .transformFieldValue (objectCollectionField , "someString,anotherString" );
155
+ Object actualResult = fieldValueTransformer .transformFieldValue (objectCollectionField , "someString,anotherString" );
152
156
assertThat (actualResult ).isEqualTo (newArrayList ("someString" , "anotherString" ));
153
157
}
154
158
159
+ @ Test
160
+ public void testTransformingStringToEnum () {
161
+ Object actualResult = fieldValueTransformer .transformFieldValue (enumField , "FOO" );
162
+ assertThat (actualResult ).isEqualTo (FOO );
163
+ }
164
+
165
+ @ Test
166
+ public void testTransformingStringToEnumList () {
167
+ Object actualResult = fieldValueTransformer .transformFieldValue (enumListField , "FOO, BAR, FOO" );
168
+ assertThat (actualResult ).isEqualTo (newArrayList (FOO , BAR , FOO ));
169
+ }
170
+
171
+ @ Test
172
+ public void testTransformingStringToEnumSet () {
173
+ Object actualResult = fieldValueTransformer .transformFieldValue (enumSetField , "BAR, FOO" );
174
+ assertThat (actualResult ).isEqualTo (EnumSet .of (BAR , FOO ));
175
+ }
176
+
155
177
@ Test
156
178
public void testThatValueTransformerIgnoresNull () {
157
- Collection < Path > actualResult = ( Collection < Path >) fieldValueTransformer .transformFieldValue (pathCollectionField , null );
179
+ Object actualResult = fieldValueTransformer .transformFieldValue (pathCollectionField , null );
158
180
assertThat (actualResult ).isNull ();
159
181
}
160
- }
182
+ }
0 commit comments