17
17
18
18
package pl .project13 .core ;
19
19
20
- import org .junit .Before ;
20
+ import junitparams .JUnitParamsRunner ;
21
+ import junitparams .Parameters ;
22
+ import org .junit .Assert ;
23
+ import org .junit .Ignore ;
21
24
import org .junit .Rule ;
22
25
import org .junit .Test ;
23
26
import org .junit .rules .TemporaryFolder ;
27
+ import org .junit .runner .RunWith ;
24
28
import pl .project13 .core .log .LogInterface ;
25
29
import pl .project13 .core .util .BuildFileChangeListener ;
30
+ import pl .project13 .core .util .GenericFileManager ;
26
31
27
32
import java .io .File ;
28
33
import java .io .IOException ;
29
34
import java .nio .file .Files ;
30
35
import java .nio .file .Path ;
36
+ import java .util .Arrays ;
37
+ import java .util .Collection ;
38
+ import java .util .Optional ;
31
39
import java .util .Properties ;
40
+ import java .util .stream .Collectors ;
41
+ import java .util .stream .Stream ;
32
42
33
43
import static java .nio .charset .StandardCharsets .UTF_8 ;
44
+ import static java .util .Arrays .asList ;
34
45
import static org .junit .Assert .assertEquals ;
35
46
import static org .junit .Assert .assertTrue ;
36
47
import static org .mockito .Mockito .mock ;
37
48
49
+ @ RunWith (JUnitParamsRunner .class )
38
50
public class PropertiesFileGeneratorTest {
39
51
@ Rule
40
52
public final TemporaryFolder temporaryFolder = new TemporaryFolder ();
41
53
42
- private PropertiesFileGenerator propertiesFileGenerator ;
43
-
44
- @ Before
45
- public void setUp () {
46
- LogInterface logInterface = mock ( LogInterface . class );
54
+ private LogInterface getLogInterface () {
55
+ return mock ( LogInterface . class );
56
+ }
57
+
58
+ private BuildFileChangeListener getBuildFileChangeListener () {
47
59
BuildFileChangeListener buildFileChangeListener = file -> {
48
60
// Ignore
49
61
};
62
+ return buildFileChangeListener ;
63
+ }
50
64
51
- propertiesFileGenerator = new PropertiesFileGenerator (logInterface , buildFileChangeListener , CommitIdPropertiesOutputFormat .PROPERTIES , "" , "test" );
65
+ private PropertiesFileGenerator getPropertiesFileGenerator () {
66
+ return getPropertiesFileGenerator (CommitIdPropertiesOutputFormat .PROPERTIES );
67
+ }
68
+
69
+ private PropertiesFileGenerator getPropertiesFileGenerator (CommitIdPropertiesOutputFormat propertiesOutputFormat ) {
70
+ return new PropertiesFileGenerator (
71
+ getLogInterface (),
72
+ getBuildFileChangeListener (),
73
+ CommitIdPropertiesOutputFormat .PROPERTIES ,
74
+ "" ,
75
+ "test"
76
+ );
52
77
}
53
78
54
79
/**
@@ -66,7 +91,7 @@ public void generatedPropertiesFileDoesNotEscapeUnicode() throws GitCommitIdExec
66
91
properties .put (GitCommitPropertyConstant .COMMIT_MESSAGE_SHORT , "測試中文" );
67
92
68
93
Path propertiesPath = temporaryFolder .getRoot ().toPath ().resolve ("git.properties" );
69
- propertiesFileGenerator .maybeGeneratePropertiesFile (
94
+ getPropertiesFileGenerator () .maybeGeneratePropertiesFile (
70
95
properties , temporaryFolder .getRoot (), propertiesPath .toFile (), UTF_8 , false );
71
96
72
97
String actualContent = Files .readString (propertiesPath , UTF_8 );
@@ -85,7 +110,7 @@ public void generatedPropertiesFileEscapeUnicode() throws GitCommitIdExecutionEx
85
110
properties .put (GitCommitPropertyConstant .COMMIT_MESSAGE_SHORT , "測試中文" );
86
111
87
112
Path propertiesPath = temporaryFolder .getRoot ().toPath ().resolve ("git.properties" );
88
- propertiesFileGenerator .maybeGeneratePropertiesFile (
113
+ getPropertiesFileGenerator () .maybeGeneratePropertiesFile (
89
114
properties , temporaryFolder .getRoot (), propertiesPath .toFile (), UTF_8 , true );
90
115
91
116
String actualContent = Files .readString (propertiesPath , UTF_8 );
@@ -103,7 +128,7 @@ public void generatedPropertiesFileDoesNotContainDateComment() throws GitCommitI
103
128
properties .put (GitCommitPropertyConstant .BRANCH , "develop" );
104
129
105
130
Path propertiesPath = temporaryFolder .getRoot ().toPath ().resolve ("git.properties" );
106
- propertiesFileGenerator .maybeGeneratePropertiesFile (
131
+ getPropertiesFileGenerator () .maybeGeneratePropertiesFile (
107
132
properties , temporaryFolder .getRoot (), propertiesPath .toFile (), UTF_8 , true );
108
133
109
134
String actualContent = Files .readString (propertiesPath , UTF_8 );
@@ -114,11 +139,12 @@ public void generatedPropertiesFileDoesNotContainDateComment() throws GitCommitI
114
139
}
115
140
116
141
@ Test
117
- public void rereadGeneratedPropertiesFile () throws GitCommitIdExecutionException , IOException {
142
+ public void reReadGeneratedPropertiesFile () throws GitCommitIdExecutionException , IOException {
118
143
Properties properties = new Properties ();
119
144
properties .put (GitCommitPropertyConstant .COMMIT_ID_FULL , "b5993378ffadd1f84dc8da220b9204d157ec0f29" );
120
145
properties .put (GitCommitPropertyConstant .BRANCH , "develop" );
121
-
146
+
147
+ PropertiesFileGenerator propertiesFileGenerator = getPropertiesFileGenerator ();
122
148
Path propertiesPath = temporaryFolder .getRoot ().toPath ().resolve ("git.properties" );
123
149
propertiesFileGenerator .maybeGeneratePropertiesFile (
124
150
properties , temporaryFolder .getRoot (), propertiesPath .toFile (), UTF_8 , true );
@@ -140,7 +166,7 @@ public void worksWithRelativeFileLocation() throws GitCommitIdExecutionException
140
166
properties .put (GitCommitPropertyConstant .COMMIT_ID_FULL , "b5993378ffadd1f84dc8da220b9204d157ec0f29" );
141
167
142
168
Path relativePath = new File ("src/blah/blub/git.properties" ).toPath ();
143
- propertiesFileGenerator .maybeGeneratePropertiesFile (
169
+ getPropertiesFileGenerator () .maybeGeneratePropertiesFile (
144
170
properties , temporaryFolder .getRoot (), relativePath .toFile (), UTF_8 , false );
145
171
146
172
@@ -151,4 +177,50 @@ public void worksWithRelativeFileLocation() throws GitCommitIdExecutionException
151
177
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n " );
152
178
assertEquals (expectedContent , actualContent );
153
179
}
180
+
181
+ public Collection <?> dumpAndReadFormats () {
182
+ Collection <?> collection = Arrays .stream (CommitIdPropertiesOutputFormat .values ()).flatMap (f1 ->
183
+ Arrays .stream (CommitIdPropertiesOutputFormat .values ()).map (f2 -> {
184
+ if (f1 .equals (f2 )) {
185
+ return Optional .empty ();
186
+ } else {
187
+ return Optional .of (new Object []{f1 , f2 });
188
+ }
189
+ }).filter (o -> o .isPresent ()).map (o -> o .get ())
190
+ ).collect (Collectors .toSet ());
191
+ return collection ;
192
+ }
193
+
194
+
195
+ @ Test
196
+ @ Parameters (method = "dumpAndReadFormats" )
197
+ @ Ignore ("Read and write is not consistent..." )
198
+ // https://github.com/git-commit-id/git-commit-id-plugin-core/issues/99
199
+ public void reReadGeneratedPropertiesFileWithDifferentFormats (
200
+ CommitIdPropertiesOutputFormat dumpFormat ,
201
+ CommitIdPropertiesOutputFormat readFormat
202
+ ) throws GitCommitIdExecutionException , IOException {
203
+ Properties dumpedProperties = new Properties ();
204
+ dumpedProperties .put (GitCommitPropertyConstant .COMMIT_ID_FULL , "b5993378ffadd1f84dc8da220b9204d157ec0f29" );
205
+ dumpedProperties .put (GitCommitPropertyConstant .BRANCH , "develop" );
206
+
207
+ Path propertiesPath = temporaryFolder .getRoot ().toPath ().resolve ("git.json" );
208
+ GenericFileManager .dumpProperties (
209
+ getLogInterface (),
210
+ dumpFormat ,
211
+ propertiesPath .toFile (),
212
+ UTF_8 ,
213
+ true ,
214
+ "test" ,
215
+ dumpedProperties
216
+ );
217
+ Properties readProperties = GenericFileManager .readProperties (
218
+ getLogInterface (),
219
+ readFormat ,
220
+ propertiesPath .toFile (),
221
+ UTF_8 ,
222
+ "test"
223
+ );
224
+ Assert .assertEquals (dumpedProperties , readProperties );
225
+ }
154
226
}
0 commit comments