11package com .tngtech .configbuilder .util ;
22
33import com .tngtech .configbuilder .annotation .typetransformer .*;
4- import com .tngtech .configbuilder .configuration .BuilderConfiguration ;
54import com .tngtech .configbuilder .configuration .ErrorMessageSetup ;
65import com .tngtech .configbuilder .exception .PrimitiveParsingException ;
76import org .junit .Before ;
1413import java .nio .file .Path ;
1514import java .nio .file .Paths ;
1615import java .util .Collection ;
16+ import java .util .EnumSet ;
1717import java .util .List ;
18+ import java .util .Set ;
1819
1920import 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 ;
2023import static org .assertj .core .api .Assertions .assertThat ;
2124import static org .mockito .Mockito .when ;
2225
2326@ RunWith (MockitoJUnitRunner .class )
2427public class FieldValueTransformerComponentTest {
2528
26- public class TestTransformer extends TypeTransformer <String , Integer > {
29+ public static class TestTransformer extends TypeTransformer <String , Integer > {
2730 @ Override
2831 public Integer transform (String argument ) {
2932 return 1472 ;
3033 }
3134 }
3235
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
3838 }
3939
40- private class TestConfigClass {
40+ private static class TestConfigClass {
4141
4242 @ TypeTransformers ({CharacterSeparatedStringToStringListTransformer .class })
4343 private Collection <String > stringCollectionField ;
@@ -46,36 +46,38 @@ private class TestConfigClass {
4646 private Boolean boolField ;
4747 @ TypeTransformers ({TestTransformer .class })
4848 private int otherIntField ;
49- @ TypeTransformers ({AnotherTestTransformer .class })
50- private Integer integerField ;
5149 private Collection <Path > pathCollectionField ;
5250 private Collection <Integer > integerCollectionField ;
5351 private Collection <Object > objectCollectionField ;
52+ private TestEnum enumField ;
53+ private List <TestEnum > enumListField ;
54+ private Set <TestEnum > enumSetField ;
5455 }
5556
5657 @ Mock
5758 private ConfigBuilderFactory configBuilderFactory ;
5859 @ Mock
5960 private ErrorMessageSetup errorMessageSetup ;
60- @ Mock
61- private BuilderConfiguration builderConfiguration ;
61+
62+ private FieldValueTransformer fieldValueTransformer ;
6263
6364 private Field stringCollectionField ;
6465 private Field intField ;
6566 private Field doubleField ;
6667 private Field boolField ;
6768 private Field otherIntField ;
68- private Field integerField ;
6969 private Field pathCollectionField ;
7070 private Field integerCollectionField ;
7171 private Field objectCollectionField ;
72-
73- private FieldValueTransformer fieldValueTransformer ;
72+ private Field enumField ;
73+ private Field enumListField ;
74+ private Field enumSetField ;
7475
7576 @ Before
7677 public void setUp () throws Exception {
7778 when (configBuilderFactory .getInstance (ErrorMessageSetup .class )).thenReturn (errorMessageSetup );
7879 when (configBuilderFactory .getInstance (GenericsAndCastingHelper .class )).thenReturn (new GenericsAndCastingHelper ());
80+ when (configBuilderFactory .getInstance (EnumTypeExtractor .class )).thenReturn (new EnumTypeExtractor ());
7981
8082 when (configBuilderFactory .getInstance (CharacterSeparatedStringToStringListTransformer .class )).thenReturn (new CharacterSeparatedStringToStringListTransformer ());
8183 when (configBuilderFactory .getInstance (CharacterSeparatedStringToStringSetTransformer .class )).thenReturn (new CharacterSeparatedStringToStringSetTransformer ());
@@ -86,23 +88,25 @@ public void setUp() throws Exception {
8688 when (configBuilderFactory .getInstance (StringOrPrimitiveToPrimitiveTransformer .class )).thenReturn (new StringOrPrimitiveToPrimitiveTransformer ());
8789 when (configBuilderFactory .getInstance (TestTransformer .class )).thenReturn (new TestTransformer ());
8890
91+ this .fieldValueTransformer = new FieldValueTransformer (configBuilderFactory );
92+
8993 stringCollectionField = TestConfigClass .class .getDeclaredField ("stringCollectionField" );
9094 intField = TestConfigClass .class .getDeclaredField ("intField" );
9195 boolField = TestConfigClass .class .getDeclaredField ("boolField" );
9296 otherIntField = TestConfigClass .class .getDeclaredField ("otherIntField" );
93- integerField = TestConfigClass .class .getDeclaredField ("integerField" );
9497 pathCollectionField = TestConfigClass .class .getDeclaredField ("pathCollectionField" );
9598 integerCollectionField = TestConfigClass .class .getDeclaredField ("integerCollectionField" );
9699 doubleField = TestConfigClass .class .getDeclaredField ("doubleField" );
97100 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" );
100104 }
101105
102106 @ Test
103107 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" ) );
106110 }
107111
108112 @ Test
@@ -136,25 +140,43 @@ public void testThatTransformersInAnnotationArePrioritized() {
136140
137141 @ Test
138142 public void testTransformingStringToPathCollection () {
139- Collection < Path > actualResult = ( Collection < Path >) fieldValueTransformer .transformFieldValue (pathCollectionField , "/etc,/usr" );
143+ Object actualResult = fieldValueTransformer .transformFieldValue (pathCollectionField , "/etc,/usr" );
140144 assertThat (actualResult ).isEqualTo (newArrayList (Paths .get ("/etc" ), Paths .get ("/usr" )));
141145 }
142146
143147 @ Test
144148 public void testTransformingStringToIntegerCollection () {
145- Collection < Integer > actualResult = ( Collection < Integer >) fieldValueTransformer .transformFieldValue (integerCollectionField , "3,4" );
149+ Object actualResult = fieldValueTransformer .transformFieldValue (integerCollectionField , "3,4" );
146150 assertThat (actualResult ).isEqualTo (newArrayList (3 , 4 ));
147151 }
148152
149153 @ Test
150154 public void testTransformingStringToObjectCollection () {
151- Collection < Object > actualResult = ( Collection < Object >) fieldValueTransformer .transformFieldValue (objectCollectionField , "someString,anotherString" );
155+ Object actualResult = fieldValueTransformer .transformFieldValue (objectCollectionField , "someString,anotherString" );
152156 assertThat (actualResult ).isEqualTo (newArrayList ("someString" , "anotherString" ));
153157 }
154158
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+
155177 @ Test
156178 public void testThatValueTransformerIgnoresNull () {
157- Collection < Path > actualResult = ( Collection < Path >) fieldValueTransformer .transformFieldValue (pathCollectionField , null );
179+ Object actualResult = fieldValueTransformer .transformFieldValue (pathCollectionField , null );
158180 assertThat (actualResult ).isNull ();
159181 }
160- }
182+ }
0 commit comments