Skip to content

Commit ec18345

Browse files
author
TheSnoozer
committed
mark several methods as protected and allow allow to read / write properties through the GenericFileManager publicly
1 parent 8fe9ae5 commit ec18345

File tree

6 files changed

+47
-17
lines changed

6 files changed

+47
-17
lines changed

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

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,36 @@
3131
import java.io.IOException;
3232
import java.io.OutputStream;
3333
import java.nio.charset.Charset;
34+
import java.nio.charset.StandardCharsets;
3435
import java.nio.file.Files;
3536
import java.util.Properties;
3637

3738
public class GenericFileManager {
39+
public static Properties readPropertiesAsUtf8(
40+
@Nonnull CommitIdPropertiesOutputFormat propertiesOutputFormat,
41+
@Nonnull File gitPropsFile
42+
) throws GitCommitIdExecutionException {
43+
return readProperties(
44+
propertiesOutputFormat,
45+
gitPropsFile,
46+
StandardCharsets.UTF_8
47+
);
48+
}
49+
50+
public static Properties readProperties(
51+
@Nonnull CommitIdPropertiesOutputFormat propertiesOutputFormat,
52+
@Nonnull File gitPropsFile,
53+
@Nonnull Charset sourceCharset
54+
) throws GitCommitIdExecutionException {
55+
return readProperties(
56+
null,
57+
propertiesOutputFormat,
58+
gitPropsFile,
59+
sourceCharset,
60+
null
61+
);
62+
}
63+
3864
public static Properties readProperties(
3965
@Nullable LogInterface log,
4066
@Nonnull CommitIdPropertiesOutputFormat propertiesOutputFormat,
@@ -47,7 +73,7 @@ public static Properties readProperties(
4773
try {
4874
if (log != null) {
4975
log.info(String.format("Reading existing %s file [%s] (for project %s)...",
50-
propertiesOutputFormat.name().toLowerCase(), gitPropsFile.getAbsolutePath(), projectName));
76+
propertiesOutputFormat.name().toLowerCase(), gitPropsFile.getAbsolutePath(), projectName));
5177
}
5278
switch (propertiesOutputFormat) {
5379
case JSON:
@@ -85,7 +111,7 @@ public static void dumpProperties(
85111
try {
86112
if (log != null) {
87113
log.info(String.format("Writing %s file [%s] (for project %s)...",
88-
propertiesOutputFormat.name().toLowerCase(), gitPropsFile.getAbsolutePath(), projectName));
114+
propertiesOutputFormat.name().toLowerCase(), gitPropsFile.getAbsolutePath(), projectName));
89115
}
90116

91117
Files.createDirectories(gitPropsFile.getParentFile().toPath());

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
import java.util.Properties;
3434

3535
public class JsonManager {
36-
public static void dumpJson(OutputStream outputStream, OrderedProperties sortedLocalProperties, Charset sourceCharset) throws IOException {
36+
protected static void dumpJson(OutputStream outputStream, OrderedProperties sortedLocalProperties, Charset sourceCharset) throws IOException {
3737
JsonGeneratorFactory jgf = Json.createGeneratorFactory(
3838
Collections.singletonMap(JsonGenerator.PRETTY_PRINTING, true));
3939

@@ -50,7 +50,7 @@ public static void dumpJson(OutputStream outputStream, OrderedProperties sortedL
5050

5151
}
5252

53-
public static Properties readJsonProperties(@Nonnull File jsonFile, Charset sourceCharset) throws CannotReadFileException {
53+
protected static Properties readJsonProperties(@Nonnull File jsonFile, Charset sourceCharset) throws CannotReadFileException {
5454
Properties retVal = new Properties();
5555
try (FileInputStream fis = new FileInputStream(jsonFile)) {
5656
try (InputStreamReader reader = new InputStreamReader(fis, sourceCharset)) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ private static boolean isNotEmpty(@Nullable String value) {
4040
return null != value && !" ".equals(value.trim().replaceAll(" ", ""));
4141
}
4242

43-
public static Properties readProperties(@Nonnull File propertiesFile) throws CannotReadFileException {
43+
protected static Properties readProperties(@Nonnull File propertiesFile) throws CannotReadFileException {
4444
return PropertyManager.readProperties(propertiesFile, StandardCharsets.ISO_8859_1);
4545
}
4646

47-
public static Properties readProperties(@Nonnull File propertiesFile, @Nonnull Charset charset) throws CannotReadFileException {
47+
protected static Properties readProperties(@Nonnull File propertiesFile, @Nonnull Charset charset) throws CannotReadFileException {
4848
try (FileInputStream fis = new FileInputStream(propertiesFile);
4949
InputStreamReader reader = new InputStreamReader(fis, charset)) {
5050
final OrderedProperties retVal = new OrderedProperties();
@@ -55,7 +55,7 @@ public static Properties readProperties(@Nonnull File propertiesFile, @Nonnull C
5555
}
5656
}
5757

58-
public static void dumpProperties(OutputStream outputStream, OrderedProperties sortedLocalProperties, boolean escapeUnicode) throws IOException {
58+
protected static void dumpProperties(OutputStream outputStream, OrderedProperties sortedLocalProperties, boolean escapeUnicode) throws IOException {
5959
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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import java.util.Properties;
3131

3232
public class XmlManager {
33-
public static void dumpXml(OutputStream outputStream, OrderedProperties sortedLocalProperties, Charset sourceCharset) throws IOException {
33+
protected static void dumpXml(OutputStream outputStream, OrderedProperties sortedLocalProperties, Charset sourceCharset) throws IOException {
3434
/*
3535
TODO get rid of custom indents and use
3636
https://ewernli.com/2009/06/18/stax-pretty-printer/
@@ -66,7 +66,7 @@ public static void dumpXml(OutputStream outputStream, OrderedProperties sortedLo
6666

6767
}
6868

69-
public static Properties readXmlProperties(@Nonnull File xmlFile, Charset sourceCharset) throws CannotReadFileException {
69+
protected static Properties readXmlProperties(@Nonnull File xmlFile, Charset sourceCharset) throws CannotReadFileException {
7070
Properties retVal = new Properties();
7171

7272
try (FileInputStream fis = new FileInputStream(xmlFile)) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import java.util.Properties;
3838

3939
public class YmlManager {
40-
public static void dumpYml(OutputStream outputStream, OrderedProperties sortedLocalProperties, Charset sourceCharset) throws IOException {
40+
protected static void dumpYml(OutputStream outputStream, OrderedProperties sortedLocalProperties, Charset sourceCharset) throws IOException {
4141
try (Writer outputWriter = new OutputStreamWriter(outputStream, sourceCharset)) {
4242
DumperOptions dumperOptions = new DumperOptions();
4343
dumperOptions.setAllowUnicode(true);
@@ -56,7 +56,7 @@ public static void dumpYml(OutputStream outputStream, OrderedProperties sortedLo
5656
}
5757
}
5858

59-
public static Properties readYmlProperties(@Nonnull File xmlFile, Charset sourceCharset) throws CannotReadFileException {
59+
protected static Properties readYmlProperties(@Nonnull File xmlFile, Charset sourceCharset) throws CannotReadFileException {
6060
Properties retVal = new Properties();
6161

6262
try (FileInputStream fis = new FileInputStream(xmlFile)) {

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.junit.*;
2626
import org.junit.runner.RunWith;
2727
import pl.project13.core.git.GitDescribeConfig;
28+
import pl.project13.core.util.GenericFileManager;
2829
import pl.project13.core.util.JsonManager;
2930
import pl.project13.core.util.XmlManager;
3031
import pl.project13.core.util.YmlManager;
@@ -445,6 +446,7 @@ public void shouldGenerateCustomPropertiesFileProperties(boolean useNativeGit) t
445446
public void shouldGenerateCustomPropertiesFileJson(boolean useNativeGit) throws Exception {
446447
// given
447448
File dotGitDirectory = createTmpDotGitDirectory(AvailableGitTestRepo.WITH_ONE_COMMIT_WITH_SPECIAL_CHARACTERS);
449+
CommitIdPropertiesOutputFormat commitIdPropertiesOutputFormat = CommitIdPropertiesOutputFormat.JSON;
448450

449451
File targetFilePath = sandbox.resolve("custom-git.json").toFile();
450452
targetFilePath.delete();
@@ -455,15 +457,15 @@ public void shouldGenerateCustomPropertiesFileJson(boolean useNativeGit) throws
455457
.setUseNativeGit(useNativeGit)
456458
.setShouldGenerateGitPropertiesFile(true)
457459
.setGenerateGitPropertiesFilename(targetFilePath)
458-
.setPropertiesOutputFormat(CommitIdPropertiesOutputFormat.JSON)
460+
.setPropertiesOutputFormat(commitIdPropertiesOutputFormat)
459461
.build();
460462
Properties properties = new Properties();
461463

462464
// when
463465
GitCommitIdPlugin.runPlugin(cb, properties);
464466
// then
465467
assertThat(targetFilePath).exists();
466-
Properties p = JsonManager.readJsonProperties(targetFilePath, StandardCharsets.UTF_8);
468+
Properties p = GenericFileManager.readPropertiesAsUtf8(commitIdPropertiesOutputFormat, targetFilePath);
467469
assertThat(p.size() > 10);
468470
Assert.assertEquals(p, properties);
469471
}
@@ -473,6 +475,7 @@ public void shouldGenerateCustomPropertiesFileJson(boolean useNativeGit) throws
473475
public void shouldGenerateCustomPropertiesFileXml(boolean useNativeGit) throws Exception {
474476
// given
475477
File dotGitDirectory = createTmpDotGitDirectory(AvailableGitTestRepo.WITH_ONE_COMMIT_WITH_SPECIAL_CHARACTERS);
478+
CommitIdPropertiesOutputFormat commitIdPropertiesOutputFormat = CommitIdPropertiesOutputFormat.XML;
476479

477480
File targetFilePath = sandbox.resolve("custom-git.xml").toFile();
478481
targetFilePath.delete();
@@ -483,15 +486,15 @@ public void shouldGenerateCustomPropertiesFileXml(boolean useNativeGit) throws E
483486
.setUseNativeGit(useNativeGit)
484487
.setShouldGenerateGitPropertiesFile(true)
485488
.setGenerateGitPropertiesFilename(targetFilePath)
486-
.setPropertiesOutputFormat(CommitIdPropertiesOutputFormat.XML)
489+
.setPropertiesOutputFormat(commitIdPropertiesOutputFormat)
487490
.build();
488491
Properties properties = new Properties();
489492

490493
// when
491494
GitCommitIdPlugin.runPlugin(cb, properties);
492495
// then
493496
assertThat(targetFilePath).exists();
494-
Properties p = XmlManager.readXmlProperties(targetFilePath, StandardCharsets.UTF_8);
497+
Properties p = GenericFileManager.readPropertiesAsUtf8(commitIdPropertiesOutputFormat, targetFilePath);
495498
assertThat(p.size() > 10);
496499
Assert.assertEquals(p, properties);
497500
}
@@ -501,6 +504,7 @@ public void shouldGenerateCustomPropertiesFileXml(boolean useNativeGit) throws E
501504
public void shouldGenerateCustomPropertiesFileYml(boolean useNativeGit) throws Exception {
502505
// given
503506
File dotGitDirectory = createTmpDotGitDirectory(AvailableGitTestRepo.WITH_ONE_COMMIT_WITH_SPECIAL_CHARACTERS);
507+
CommitIdPropertiesOutputFormat commitIdPropertiesOutputFormat = CommitIdPropertiesOutputFormat.YML;
504508

505509
File targetFilePath = sandbox.resolve("custom-git.yml").toFile();
506510
targetFilePath.delete();
@@ -511,15 +515,15 @@ public void shouldGenerateCustomPropertiesFileYml(boolean useNativeGit) throws E
511515
.setUseNativeGit(useNativeGit)
512516
.setShouldGenerateGitPropertiesFile(true)
513517
.setGenerateGitPropertiesFilename(targetFilePath)
514-
.setPropertiesOutputFormat(CommitIdPropertiesOutputFormat.YML)
518+
.setPropertiesOutputFormat(commitIdPropertiesOutputFormat)
515519
.build();
516520
Properties properties = new Properties();
517521

518522
// when
519523
GitCommitIdPlugin.runPlugin(cb, properties);
520524
// then
521525
assertThat(targetFilePath).exists();
522-
Properties p = YmlManager.readYmlProperties(targetFilePath, StandardCharsets.UTF_8);
526+
Properties p = GenericFileManager.readPropertiesAsUtf8(commitIdPropertiesOutputFormat, targetFilePath);
523527
assertThat(p.size() > 10);
524528
Assert.assertEquals(p, properties);
525529
}

0 commit comments

Comments
 (0)