1717
1818package pl .project13 .core ;
1919
20- import org .junit .Before ;
20+ import junitparams .JUnitParamsRunner ;
21+ import junitparams .Parameters ;
22+ import org .junit .Assert ;
23+ import org .junit .Ignore ;
2124import org .junit .Rule ;
2225import org .junit .Test ;
2326import org .junit .rules .TemporaryFolder ;
27+ import org .junit .runner .RunWith ;
2428import pl .project13 .core .log .LogInterface ;
2529import pl .project13 .core .util .BuildFileChangeListener ;
30+ import pl .project13 .core .util .GenericFileManager ;
2631
2732import java .io .File ;
2833import java .io .IOException ;
2934import java .nio .file .Files ;
3035import java .nio .file .Path ;
36+ import java .util .Arrays ;
37+ import java .util .Collection ;
38+ import java .util .Optional ;
3139import java .util .Properties ;
40+ import java .util .stream .Collectors ;
41+ import java .util .stream .Stream ;
3242
3343import static java .nio .charset .StandardCharsets .UTF_8 ;
44+ import static java .util .Arrays .asList ;
3445import static org .junit .Assert .assertEquals ;
3546import static org .junit .Assert .assertTrue ;
3647import static org .mockito .Mockito .mock ;
3748
49+ @ RunWith (JUnitParamsRunner .class )
3850public class PropertiesFileGeneratorTest {
3951 @ Rule
4052 public final TemporaryFolder temporaryFolder = new TemporaryFolder ();
4153
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 () {
4759 BuildFileChangeListener buildFileChangeListener = file -> {
4860 // Ignore
4961 };
62+ return buildFileChangeListener ;
63+ }
5064
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+ );
5277 }
5378
5479 /**
@@ -66,7 +91,7 @@ public void generatedPropertiesFileDoesNotEscapeUnicode() throws GitCommitIdExec
6691 properties .put (GitCommitPropertyConstant .COMMIT_MESSAGE_SHORT , "測試中文" );
6792
6893 Path propertiesPath = temporaryFolder .getRoot ().toPath ().resolve ("git.properties" );
69- propertiesFileGenerator .maybeGeneratePropertiesFile (
94+ getPropertiesFileGenerator () .maybeGeneratePropertiesFile (
7095 properties , temporaryFolder .getRoot (), propertiesPath .toFile (), UTF_8 , false );
7196
7297 String actualContent = Files .readString (propertiesPath , UTF_8 );
@@ -85,7 +110,7 @@ public void generatedPropertiesFileEscapeUnicode() throws GitCommitIdExecutionEx
85110 properties .put (GitCommitPropertyConstant .COMMIT_MESSAGE_SHORT , "測試中文" );
86111
87112 Path propertiesPath = temporaryFolder .getRoot ().toPath ().resolve ("git.properties" );
88- propertiesFileGenerator .maybeGeneratePropertiesFile (
113+ getPropertiesFileGenerator () .maybeGeneratePropertiesFile (
89114 properties , temporaryFolder .getRoot (), propertiesPath .toFile (), UTF_8 , true );
90115
91116 String actualContent = Files .readString (propertiesPath , UTF_8 );
@@ -103,7 +128,7 @@ public void generatedPropertiesFileDoesNotContainDateComment() throws GitCommitI
103128 properties .put (GitCommitPropertyConstant .BRANCH , "develop" );
104129
105130 Path propertiesPath = temporaryFolder .getRoot ().toPath ().resolve ("git.properties" );
106- propertiesFileGenerator .maybeGeneratePropertiesFile (
131+ getPropertiesFileGenerator () .maybeGeneratePropertiesFile (
107132 properties , temporaryFolder .getRoot (), propertiesPath .toFile (), UTF_8 , true );
108133
109134 String actualContent = Files .readString (propertiesPath , UTF_8 );
@@ -114,11 +139,12 @@ public void generatedPropertiesFileDoesNotContainDateComment() throws GitCommitI
114139 }
115140
116141 @ Test
117- public void rereadGeneratedPropertiesFile () throws GitCommitIdExecutionException , IOException {
142+ public void reReadGeneratedPropertiesFile () throws GitCommitIdExecutionException , IOException {
118143 Properties properties = new Properties ();
119144 properties .put (GitCommitPropertyConstant .COMMIT_ID_FULL , "b5993378ffadd1f84dc8da220b9204d157ec0f29" );
120145 properties .put (GitCommitPropertyConstant .BRANCH , "develop" );
121-
146+
147+ PropertiesFileGenerator propertiesFileGenerator = getPropertiesFileGenerator ();
122148 Path propertiesPath = temporaryFolder .getRoot ().toPath ().resolve ("git.properties" );
123149 propertiesFileGenerator .maybeGeneratePropertiesFile (
124150 properties , temporaryFolder .getRoot (), propertiesPath .toFile (), UTF_8 , true );
@@ -140,7 +166,7 @@ public void worksWithRelativeFileLocation() throws GitCommitIdExecutionException
140166 properties .put (GitCommitPropertyConstant .COMMIT_ID_FULL , "b5993378ffadd1f84dc8da220b9204d157ec0f29" );
141167
142168 Path relativePath = new File ("src/blah/blub/git.properties" ).toPath ();
143- propertiesFileGenerator .maybeGeneratePropertiesFile (
169+ getPropertiesFileGenerator () .maybeGeneratePropertiesFile (
144170 properties , temporaryFolder .getRoot (), relativePath .toFile (), UTF_8 , false );
145171
146172
@@ -151,4 +177,50 @@ public void worksWithRelativeFileLocation() throws GitCommitIdExecutionException
151177 + "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n " );
152178 assertEquals (expectedContent , actualContent );
153179 }
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+ }
154226}
0 commit comments