2424import pl .project13 .core .log .LogInterface ;
2525import pl .project13 .core .util .BuildFileChangeListener ;
2626
27+ import java .io .File ;
2728import java .io .IOException ;
2829import java .nio .file .Files ;
2930import java .nio .file .Path ;
3031import java .util .Properties ;
3132
3233import static java .nio .charset .StandardCharsets .UTF_8 ;
3334import static org .junit .Assert .assertEquals ;
35+ import static org .junit .Assert .assertTrue ;
3436import static org .mockito .Mockito .mock ;
3537
3638public class PropertiesFileGeneratorTest {
@@ -57,7 +59,8 @@ public void generatedPropertiesFileDoesNotEscapeUnicode() throws GitCommitIdExec
5759 properties .put (GitCommitPropertyConstant .COMMIT_MESSAGE_SHORT , "測試中文" );
5860
5961 Path propertiesPath = temporaryFolder .getRoot ().toPath ().resolve ("git.properties" );
60- propertiesFileGenerator .maybeGeneratePropertiesFile (properties , propertiesPath .toFile (), UTF_8 , false );
62+ propertiesFileGenerator .maybeGeneratePropertiesFile (
63+ properties , temporaryFolder .getRoot (), propertiesPath .toFile (), UTF_8 , false );
6164
6265 byte [] bytes = Files .readAllBytes (propertiesPath );
6366 String actualContent = new String (bytes , UTF_8 );
@@ -76,7 +79,8 @@ public void generatedPropertiesFileEscapeUnicode() throws GitCommitIdExecutionEx
7679 properties .put (GitCommitPropertyConstant .COMMIT_MESSAGE_SHORT , "測試中文" );
7780
7881 Path propertiesPath = temporaryFolder .getRoot ().toPath ().resolve ("git.properties" );
79- propertiesFileGenerator .maybeGeneratePropertiesFile (properties , propertiesPath .toFile (), UTF_8 , true );
82+ propertiesFileGenerator .maybeGeneratePropertiesFile (
83+ properties , temporaryFolder .getRoot (), propertiesPath .toFile (), UTF_8 , true );
8084
8185 byte [] bytes = Files .readAllBytes (propertiesPath );
8286 String actualContent = new String (bytes , UTF_8 );
@@ -94,7 +98,8 @@ public void generatedPropertiesFileDoesNotContainDateComment() throws GitCommitI
9498 properties .put (GitCommitPropertyConstant .BRANCH , "develop" );
9599
96100 Path propertiesPath = temporaryFolder .getRoot ().toPath ().resolve ("git.properties" );
97- propertiesFileGenerator .maybeGeneratePropertiesFile (properties , propertiesPath .toFile (), UTF_8 , true );
101+ propertiesFileGenerator .maybeGeneratePropertiesFile (
102+ properties , temporaryFolder .getRoot (), propertiesPath .toFile (), UTF_8 , true );
98103
99104 byte [] bytes = Files .readAllBytes (propertiesPath );
100105 String actualContent = new String (bytes , UTF_8 );
@@ -111,10 +116,12 @@ public void rereadGeneratedPropertiesFile() throws GitCommitIdExecutionException
111116 properties .put (GitCommitPropertyConstant .BRANCH , "develop" );
112117
113118 Path propertiesPath = temporaryFolder .getRoot ().toPath ().resolve ("git.properties" );
114- propertiesFileGenerator .maybeGeneratePropertiesFile (properties , propertiesPath .toFile (), UTF_8 , true );
119+ propertiesFileGenerator .maybeGeneratePropertiesFile (
120+ properties , temporaryFolder .getRoot (), propertiesPath .toFile (), UTF_8 , true );
115121
116122 // Re-read the generated properties file.
117- propertiesFileGenerator .maybeGeneratePropertiesFile (properties , propertiesPath .toFile (), UTF_8 , true );
123+ propertiesFileGenerator .maybeGeneratePropertiesFile (
124+ properties , temporaryFolder .getRoot (), propertiesPath .toFile (), UTF_8 , true );
118125
119126 byte [] bytes = Files .readAllBytes (propertiesPath );
120127 String actualContent = new String (bytes , UTF_8 );
@@ -123,4 +130,23 @@ public void rereadGeneratedPropertiesFile() throws GitCommitIdExecutionException
123130 + "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n " ;
124131 assertEquals (expectedContent , actualContent );
125132 }
133+
134+ @ Test
135+ public void worksWithRelativeFileLocation () throws GitCommitIdExecutionException , IOException {
136+ Properties properties = new Properties ();
137+ properties .put (GitCommitPropertyConstant .COMMIT_ID_FULL , "b5993378ffadd1f84dc8da220b9204d157ec0f29" );
138+
139+ Path relativePath = new File ("src/blah/blub/git.properties" ).toPath ();
140+ propertiesFileGenerator .maybeGeneratePropertiesFile (
141+ properties , temporaryFolder .getRoot (), relativePath .toFile (), UTF_8 , false );
142+
143+
144+ Path absolutePath = temporaryFolder .getRoot ().toPath ().resolve ("src/blah/blub/git.properties" );
145+ assertTrue (absolutePath .toFile ().exists ());
146+ byte [] bytes = Files .readAllBytes (absolutePath );
147+ String actualContent = new String (bytes , UTF_8 );
148+ String expectedContent = "#Generated by Git-Commit-Id-Plugin\n "
149+ + "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n " ;
150+ assertEquals (expectedContent , actualContent );
151+ }
126152}
0 commit comments