Skip to content

Commit

Permalink
Write git.json file with UTF-8 encoding.
Browse files Browse the repository at this point in the history
Resolves #130
  • Loading branch information
frankbregulla1111 committed Sep 23, 2014
1 parent 02ce239 commit ca10920
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 64 deletions.
25 changes: 1 addition & 24 deletions src/main/java/pl/project13/maven/git/GitCommitIdMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.io.Closeables;
import com.google.common.io.Files;
Expand Down Expand Up @@ -272,14 +271,6 @@ public class GitCommitIdMojo extends AbstractMojo {
@SuppressWarnings("UnusedDeclaration")
private List<String> excludeProperties = Collections.emptyList();

/**
* Specifies the character encoding for the properties file that is generated by this plugin
*
* @parameter expression="${encoding}" default-value="${project.reporting.outputEncoding}
* @since 2.1.11
*/
private String encoding;

/**
* The properties we store our data in and then expose them
*/
Expand Down Expand Up @@ -499,7 +490,7 @@ void generatePropertiesFile(@NotNull Properties properties, File base, String pr
try {
Files.createParentDirs(gitPropsFile);

outputWriter = new OutputStreamWriter(new FileOutputStream(gitPropsFile), determineEncoding());
outputWriter = new OutputStreamWriter(new FileOutputStream(gitPropsFile), Charset.forName("UTF-8"));
if ("json".equalsIgnoreCase(format)) {
log("Writing json file to [", gitPropsFile.getAbsolutePath(), "] (for module ", project.getName(), ")...");
ObjectMapper mapper = new ObjectMapper();
Expand All @@ -516,20 +507,6 @@ void generatePropertiesFile(@NotNull Properties properties, File base, String pr
}
}

private String determineEncoding() {
if (Strings.isNullOrEmpty(this.encoding) )
{
getLog().warn(
"File encoding has not been set, using platform encoding " + Charset.defaultCharset().name()
+ ", i.e. build is platform dependent!" );
return Charset.defaultCharset().name();
}
else
{
return this.encoding;
}
}

@VisibleForTesting
File craftPropertiesOutputFile(File base, String propertiesFilename){
File returnPath = new File(base, propertiesFilename);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,42 +208,6 @@ public void shouldGenerateCustomPropertiesFileProperties(boolean useNativeGit) t
@Test
@Parameters(method = "useNativeGit")
public void shouldGenerateCustomPropertiesFileJson(boolean useNativeGit) throws Exception {
// given
mavenSandbox.withParentProject("my-pom-project", "pom")
.withChildProject("my-jar-module", "jar")
.withGitRepoInChild(AvailableGitTestRepo.GIT_COMMIT_ID)
.create(CleanUp.CLEANUP_FIRST);

MavenProject targetProject = mavenSandbox.getChildProject();

String targetFilePath = "target/classes/custom-git.properties";
File expectedFile = new File(targetProject.getBasedir(), targetFilePath);

setProjectToExecuteMojoIn(targetProject);
alterMojoSettings("generateGitPropertiesFile", true);
alterMojoSettings("generateGitPropertiesFilename", targetFilePath);
alterMojoSettings("format", "json");
alterMojoSettings("useNativeGit", useNativeGit);

// when
try {
mojo.execute();

// then
assertThat(expectedFile).exists();
String json = Files.toString(expectedFile, Charset.defaultCharset());
ObjectMapper om = new ObjectMapper();
Map<String, String> map = new HashMap<String, String>();
map = om.readValue(expectedFile, map.getClass());
assertThat(map.size() > 10);
} finally {
FileUtils.forceDelete(expectedFile);
}
}

@Test
@Parameters(value = {"UTF-8", "ISO-8859-1"})
public void shouldGenerateCustomPropertiesFileJsonWithCustomEncoding(String encoding) throws Exception {
// given
mavenSandbox.withParentProject("my-pom-project", "pom")
.withChildProject("my-jar-module", "jar")
Expand All @@ -254,21 +218,19 @@ public void shouldGenerateCustomPropertiesFileJsonWithCustomEncoding(String enco

String targetFilePath = "target/classes/custom-git.properties";
File expectedFile = new File(targetProject.getBasedir(), targetFilePath);
Charset charset = Charset.forName(encoding);

setProjectToExecuteMojoIn(targetProject);
alterMojoSettings("generateGitPropertiesFile", true);
alterMojoSettings("generateGitPropertiesFilename", targetFilePath);
alterMojoSettings("format", "json");
alterMojoSettings("useNativeGit", true);
alterMojoSettings("encoding", charset.name());
alterMojoSettings("useNativeGit", useNativeGit);
// when
try {
mojo.execute();

// then
assertThat(expectedFile).exists();
String json = Files.toString(expectedFile, charset);
String json = Files.toString(expectedFile, Charset.forName("UTF-8"));
ObjectMapper om = new ObjectMapper();
Map<String, String> map = new HashMap<String, String>();
map = om.readValue(json, map.getClass());
Expand Down

0 comments on commit ca10920

Please sign in to comment.