1
1
package de .kreth .property2java ;
2
2
3
3
import static de .kreth .property2java .TestPropertiesSource .testProperties ;
4
+ import static org .assertj .core .api .Assertions .assertThat ;
4
5
import static org .hamcrest .MatcherAssert .assertThat ;
6
+ import static org .junit .Assert .assertFalse ;
5
7
import static org .junit .jupiter .api .Assertions .assertEquals ;
6
8
import static org .junit .jupiter .api .Assertions .assertNotNull ;
7
9
import static org .junit .jupiter .api .Assertions .assertTrue ;
10
+ import static org .mockito .ArgumentMatchers .any ;
8
11
import static org .mockito .ArgumentMatchers .anyString ;
9
12
import static org .mockito .Mockito .mock ;
10
13
import static org .mockito .Mockito .never ;
17
20
import java .io .StringWriter ;
18
21
import java .io .Writer ;
19
22
import java .util .Arrays ;
23
+ import java .util .EnumSet ;
20
24
import java .util .HashMap ;
21
25
import java .util .List ;
22
26
import java .util .Map ;
27
31
import org .hamcrest .Matchers ;
28
32
import org .junit .jupiter .api .BeforeEach ;
29
33
import org .junit .jupiter .api .Test ;
30
- import org .mockito .Mockito ;
31
-
34
+ import org .junit .jupiter .api .extension .ExtendWith ;
35
+ import org .mockito .ArgumentCaptor ;
36
+ import org .mockito .Mock ;
37
+ import org .mockito .junit .jupiter .MockitoExtension ;
38
+ import org .mockito .junit .jupiter .MockitoSettings ;
39
+ import org .mockito .quality .Strictness ;
40
+
41
+ import freemarker .template .Template ;
42
+ import freemarker .template .TemplateException ;
43
+
44
+ @ ExtendWith (MockitoExtension .class )
45
+ @ MockitoSettings (strictness = Strictness .LENIENT )
32
46
class GeneratorTests {
33
47
34
48
private String path = "application.properties" ;
35
49
50
+ @ Mock
36
51
private Configuration config ;
37
52
38
- private Generator generator ;
39
-
40
53
@ BeforeEach
41
54
void setUp () throws Exception {
42
55
Map <String , Reader > input = new HashMap <>();
43
56
input .put (path , testProperties ());
44
57
45
- config = Mockito .spy (TestImplConfig .class );
46
-
47
58
when (config .getRootPath ()).thenReturn (new File ("." ).toPath ());
48
59
when (config .getFormat ()).thenReturn (Format .WithUnaryOperatorParameter );
49
60
when (config .getInput ()).thenReturn (input );
50
61
when (config .mapFilenameToClassName (anyString ())).thenCallRealMethod ();
51
62
when (config .outputCharset ()).thenCallRealMethod ();
63
+ when (config .getOptions ()).thenReturn (EnumSet .noneOf (GeneratorOptions .class ));
64
+
65
+ }
66
+
67
+ @ Test
68
+ void testAllOptionsConfiguration () throws IOException , GeneratorException , TemplateException {
69
+
70
+ Template template = mock (Template .class );
71
+ Generator generator = new Generator (config , template );
72
+ when (config .getOptions ()).thenReturn (EnumSet .allOf (GeneratorOptions .class ));
73
+
74
+ StringWriter out = new StringWriter ();
75
+ when (config .outWriter (anyString ())).thenReturn (out );
52
76
53
- generator = new Generator (config );
77
+ @ SuppressWarnings ("unchecked" )
78
+ ArgumentCaptor <Map <String , Object >> rootCaptior = ArgumentCaptor .forClass (Map .class );
79
+ generator .start ();
80
+ verify (template ).process (rootCaptior .capture (), any (Writer .class ));
81
+ Map <String , Object > root = rootCaptior .getValue ();
82
+ @ SuppressWarnings ("unchecked" )
83
+ EnumSet <GeneratorOptions > options = (EnumSet <GeneratorOptions >) root .get ("options" );
84
+ assertThat (options ).contains (GeneratorOptions .WithMessageFormatter , GeneratorOptions .WithSubstitutors );
85
+
86
+ @ SuppressWarnings ("unchecked" )
87
+ List <String > imports = (List <String >) root .get ("imports" );
88
+ assertThat (imports ).contains ("java.text.MessageFormat" );
54
89
}
55
90
91
+ @ Test
92
+ void testWithSubstitutorsNoImportsConfiguration () throws IOException , GeneratorException , TemplateException {
93
+
94
+ Template template = mock (Template .class );
95
+ Generator generator = new Generator (config , template );
96
+ when (config .getOptions ()).thenReturn (EnumSet .of (GeneratorOptions .WithSubstitutors ));
97
+
98
+ StringWriter out = new StringWriter ();
99
+ when (config .outWriter (anyString ())).thenReturn (out );
100
+
101
+ @ SuppressWarnings ("unchecked" )
102
+ ArgumentCaptor <Map <String , Object >> rootCaptior = ArgumentCaptor .forClass (Map .class );
103
+ generator .start ();
104
+ verify (template ).process (rootCaptior .capture (), any (Writer .class ));
105
+ Map <String , Object > root = rootCaptior .getValue ();
106
+ @ SuppressWarnings ("unchecked" )
107
+ EnumSet <GeneratorOptions > options = (EnumSet <GeneratorOptions >) root .get ("options" );
108
+ assertThat (options ).contains (GeneratorOptions .WithSubstitutors );
109
+
110
+ assertFalse (root .containsKey ("imports" ));
111
+ }
112
+
56
113
@ Test
57
114
void testClassDefinition () throws IOException , GeneratorException {
58
115
116
+ Generator generator = new Generator (config );
59
117
when (config .getPackage ()).thenReturn ("de.kreth.property2java" );
60
118
when (config .mapFilenameToClassName (anyString ())).thenCallRealMethod ();
61
119
@@ -102,6 +160,7 @@ void testClassDefinition() throws IOException, GeneratorException {
102
160
@ Test
103
161
void testOneInputGeneratesOneOutput () throws IOException , GeneratorException {
104
162
163
+ Generator generator = new Generator (config );
105
164
Writer out = mock (Writer .class );
106
165
Writer nonOut = mock (Writer .class );
107
166
when (config .outWriter (anyString ())).thenReturn (out , nonOut );
@@ -114,6 +173,7 @@ void testOneInputGeneratesOneOutput() throws IOException, GeneratorException {
114
173
@ Test
115
174
void testKeys () throws IOException , GeneratorException {
116
175
176
+ Generator generator = new Generator (config );
117
177
StringWriter out = new StringWriter ();
118
178
when (config .outWriter (anyString ())).thenReturn (out );
119
179
generator .start ();
0 commit comments