Skip to content

Commit

Permalink
Merge pull request #11 from gary258796/master
Browse files Browse the repository at this point in the history
Add parameter in function maybeGeneratePropertiesFile in order to choose to escape unicode or not.
  • Loading branch information
TheSnoozer authored Oct 7, 2022
2 parents fdc2005 + 84f75bb commit cb3375a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/main/java/pl/project13/core/PropertiesFileGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public PropertiesFileGenerator(LogInterface log, BuildFileChangeListener buildFi
this.projectName = projectName;
}

public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, File base, String propertiesFilename, Charset sourceCharset) throws GitCommitIdExecutionException {
public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, File base, String propertiesFilename, Charset sourceCharset, boolean escapeUnicode) throws GitCommitIdExecutionException {
try {
final File gitPropsFile = craftPropertiesOutputFile(base, propertiesFilename);
final boolean isJsonFormat = "json".equalsIgnoreCase(format);
Expand Down Expand Up @@ -91,7 +91,7 @@ public void maybeGeneratePropertiesFile(@Nonnull Properties localProperties, Fil
} else {
log.info(String.format("Writing properties file to [%s] (for module %s)...", gitPropsFile.getAbsolutePath(), projectName));
// using outputStream directly instead of outputWriter this way the UTF-8 characters appears in unicode escaped form
PropertyManager.dumpProperties(outputStream, sortedLocalProperties);
PropertyManager.dumpProperties(outputStream, sortedLocalProperties, escapeUnicode);
}
} catch (final IOException ex) {
throw new RuntimeException("Cannot create custom git properties file: " + gitPropsFile, ex);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/pl/project13/core/util/PropertyManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ public static Properties readProperties(@Nonnull File propertiesFile, @Nonnull C
}
}

public static void dumpProperties(OutputStream outputStream, OrderedProperties sortedLocalProperties) throws IOException {
try (Writer outputWriter = new OutputStreamWriter(outputStream, StandardCharsets.ISO_8859_1)) {
public static void dumpProperties(OutputStream outputStream, OrderedProperties sortedLocalProperties, boolean escapeUnicode) throws IOException {
try (Writer outputWriter = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8)) {
// use the OrderedProperties.store(Writer, ...)-method to avoid illegal reflective access warning
// see: https://github.com/git-commit-id/git-commit-id-maven-plugin/issues/523
outputWriter.write("#Generated by Git-Commit-Id-Plugin");
outputWriter.write(System.getProperty("line.separator"));
for (Map.Entry<String, String> e : sortedLocalProperties.entrySet()) {
String key = saveConvert(e.getKey(), true, true);
String val = saveConvert(e.getValue(), false, true);
String key = saveConvert(e.getKey(), true, escapeUnicode);
String val = saveConvert(e.getValue(), false, escapeUnicode);
outputWriter.write(key + "=" + val);
outputWriter.write(System.getProperty("line.separator"));
}
Expand Down
46 changes: 42 additions & 4 deletions src/test/java/pl/project13/core/PropertiesFileGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,44 @@ public void setUp() {

propertiesFileGenerator = new PropertiesFileGenerator(logInterface, buildFileChangeListener, "properties", "", "test");
}

@Test
public void generatedPropertiesFileDoesNotEscapeUnicode() throws GitCommitIdExecutionException, IOException {
Properties properties = new Properties();
properties.put(GitCommitPropertyConstant.COMMIT_ID_FULL, "b5993378ffadd1f84dc8da220b9204d157ec0f29");
properties.put(GitCommitPropertyConstant.BRANCH, "develop");
properties.put(GitCommitPropertyConstant.COMMIT_MESSAGE_SHORT, "測試中文");

Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8, false);

byte[] bytes = Files.readAllBytes(propertiesPath);
String actualContent = new String(bytes, UTF_8);
String expectedContent = "#Generated by Git-Commit-Id-Plugin\n"
+ "branch=develop\n"
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n"
+ "commit.message.short=測試中文\n";
assertEquals(expectedContent, actualContent);
}

@Test
public void generatedPropertiesFileEscapeUnicode() throws GitCommitIdExecutionException, IOException {
Properties properties = new Properties();
properties.put(GitCommitPropertyConstant.COMMIT_ID_FULL, "b5993378ffadd1f84dc8da220b9204d157ec0f29");
properties.put(GitCommitPropertyConstant.BRANCH, "develop");
properties.put(GitCommitPropertyConstant.COMMIT_MESSAGE_SHORT, "測試中文");

Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8, true);

byte[] bytes = Files.readAllBytes(propertiesPath);
String actualContent = new String(bytes, UTF_8);
String expectedContent = "#Generated by Git-Commit-Id-Plugin\n"
+ "branch=develop\n"
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n"
+ "commit.message.short=\\u6E2C\\u8A66\\u4E2D\\u6587\n";
assertEquals(expectedContent, actualContent);
}

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

Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8);
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8, true);

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

Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8);
propertiesFileGenerator.maybeGeneratePropertiesFile(properties, temporaryFolder.getRoot(), propertiesPath.getFileName().toString(), UTF_8, true);

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

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

0 comments on commit cb3375a

Please sign in to comment.