Skip to content

Commit cb3375a

Browse files
authored
Merge pull request #11 from gary258796/master
Add parameter in function maybeGeneratePropertiesFile in order to choose to escape unicode or not.
2 parents fdc2005 + 84f75bb commit cb3375a

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

src/main/java/pl/project13/core/PropertiesFileGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public PropertiesFileGenerator(LogInterface log, BuildFileChangeListener buildFi
4646
this.projectName = projectName;
4747
}
4848

49-
public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, File base, String propertiesFilename, Charset sourceCharset) throws GitCommitIdExecutionException {
49+
public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, File base, String propertiesFilename, Charset sourceCharset, boolean escapeUnicode) throws GitCommitIdExecutionException {
5050
try {
5151
final File gitPropsFile = craftPropertiesOutputFile(base, propertiesFilename);
5252
final boolean isJsonFormat = "json".equalsIgnoreCase(format);
@@ -91,7 +91,7 @@ public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, Fil
9191
} else {
9292
log.info(String.format("Writing properties file to [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
9393
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
94-
PropertyManager.dumpProperties(outputStream, sortedLocalProperties);
94+
PropertyManager.dumpProperties(outputStream, sortedLocalProperties, escapeUnicode);
9595
}
9696
} catch (final IOException ex) {
9797
throw new RuntimeException("Cannot create custom git properties file: " + gitPropsFile, ex);

src/main/java/pl/project13/core/util/PropertyManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ public static Properties readProperties(@Nonnull File propertiesFile, @Nonnull C
5555
}
5656
}
5757

58-
public static void dumpProperties(OutputStream outputStream, OrderedProperties sortedLocalProperties) throws IOException {
59-
try (Writer outputWriter = new OutputStreamWriter(outputStream, StandardCharsets.ISO_8859_1)) {
58+
public static void dumpProperties(OutputStream outputStream, OrderedProperties sortedLocalProperties, boolean escapeUnicode) throws IOException {
59+
try (Writer outputWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
6060
// use the OrderedProperties.store(Writer, ...)-method to avoid illegal reflective access warning
6161
// see: https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/523
6262
outputWriter.write("#Generated by Git-Commit-Id-Plugin");
6363
outputWriter.write(System.getProperty("line.separator"));
6464
for (Map.Entry<String, String> e : sortedLocalProperties.entrySet()) {
65-
String key = saveConvert(e.getKey(), true, true);
66-
String val = saveConvert(e.getValue(), false, true);
65+
String key = saveConvert(e.getKey(), true, escapeUnicode);
66+
String val = saveConvert(e.getValue(), false, escapeUnicode);
6767
outputWriter.write(key + "=" + val);
6868
outputWriter.write(System.getProperty("line.separator"));
6969
}

src/test/java/pl/project13/core/PropertiesFileGeneratorTest.java

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,44 @@ public void setUp() {
4848

4949
propertiesFileGenerator = new PropertiesFileGenerator(logInterface, buildFileChangeListener, "properties", "", "test");
5050
}
51+
52+
@Test
53+
public void generatedPropertiesFileDoesNotEscapeUnicode() throws GitCommitIdExecutionException, IOException {
54+
Properties properties = new Properties();
55+
properties.put(GitCommitPropertyConstant.COMMIT_ID_FULL, "b5993378ffadd1f84dc8da220b9204d157ec0f29");
56+
properties.put(GitCommitPropertyConstant.BRANCH, "develop");
57+
properties.put(GitCommitPropertyConstant.COMMIT_MESSAGE_SHORT, "測試中文");
58+
59+
Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
60+
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8, false);
61+
62+
byte[] bytes = Files.readAllBytes(propertiesPath);
63+
String actualContent = new String(bytes, UTF_8);
64+
String expectedContent = "#Generated by Git-Commit-Id-Plugin\n"
65+
+ "branch=develop\n"
66+
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n"
67+
+ "commit.message.short=測試中文\n";
68+
assertEquals(expectedContent, actualContent);
69+
}
70+
71+
@Test
72+
public void generatedPropertiesFileEscapeUnicode() throws GitCommitIdExecutionException, IOException {
73+
Properties properties = new Properties();
74+
properties.put(GitCommitPropertyConstant.COMMIT_ID_FULL, "b5993378ffadd1f84dc8da220b9204d157ec0f29");
75+
properties.put(GitCommitPropertyConstant.BRANCH, "develop");
76+
properties.put(GitCommitPropertyConstant.COMMIT_MESSAGE_SHORT, "測試中文");
77+
78+
Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
79+
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8, true);
80+
81+
byte[] bytes = Files.readAllBytes(propertiesPath);
82+
String actualContent = new String(bytes, UTF_8);
83+
String expectedContent = "#Generated by Git-Commit-Id-Plugin\n"
84+
+ "branch=develop\n"
85+
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n"
86+
+ "commit.message.short=\\u6E2C\\u8A66\\u4E2D\\u6587\n";
87+
assertEquals(expectedContent, actualContent);
88+
}
5189

5290
@Test
5391
public void generatedPropertiesFileDoesNotContainDateComment() throws GitCommitIdExecutionException, IOException {
@@ -56,7 +94,7 @@ public void generatedPropertiesFileDoesNotContainDateComment() throws GitCommitI
5694
properties.put(GitCommitPropertyConstant.BRANCH, "develop");
5795

5896
Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
59-
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8);
97+
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8, true);
6098

6199
byte[] bytes = Files.readAllBytes(propertiesPath);
62100
String actualContent = new String(bytes, UTF_8);
@@ -73,10 +111,10 @@ public void rereadGeneratedPropertiesFile() throws GitCommitIdExecutionException
73111
properties.put(GitCommitPropertyConstant.BRANCH, "develop");
74112

75113
Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
76-
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8);
114+
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8, true);
77115

78116
// Re-read the generated properties file.
79-
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8);
117+
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8, true);
80118

81119
byte[] bytes = Files.readAllBytes(propertiesPath);
82120
String actualContent = new String(bytes, UTF_8);
@@ -85,4 +123,4 @@ public void rereadGeneratedPropertiesFile() throws GitCommitIdExecutionException
85123
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n";
86124
assertEquals(expectedContent, actualContent);
87125
}
88-
}
126+
}

0 commit comments

Comments
 (0)