Skip to content

Commit e102b46

Browse files
authored
Merge pull request #116 from git-commit-id/99
#99: try to improve error messages when the json properties can't be read
2 parents 4383ad3 + f2112fe commit e102b46

File tree

4 files changed

+99
-15
lines changed

4 files changed

+99
-15
lines changed

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@
3333
import java.nio.charset.Charset;
3434
import java.nio.charset.StandardCharsets;
3535
import java.nio.file.Files;
36+
import java.util.Arrays;
37+
import java.util.HashSet;
3638
import java.util.Properties;
39+
import java.util.Set;
3740

3841
public class GenericFileManager {
3942
public static Properties readPropertiesAsUtf8(
@@ -61,6 +64,7 @@ public static Properties readProperties(
6164
);
6265
}
6366

67+
@Nonnull
6468
public static Properties readProperties(
6569
@Nullable LogInterface log,
6670
@Nonnull CommitIdPropertiesOutputFormat propertiesOutputFormat,
@@ -139,7 +143,9 @@ public static void dumpProperties(
139143
}
140144
}
141145
} catch (final IOException ex) {
142-
throw new GitCommitIdExecutionException("Cannot create custom git properties file: " + gitPropsFile, ex);
146+
throw new GitCommitIdExecutionException(
147+
String.format("Failed to write %s file [%s] (for project %s)...",
148+
propertiesOutputFormat.name().toLowerCase(), gitPropsFile.getAbsolutePath(), projectName));
143149
}
144150
}
145151
}

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

+3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ protected static Properties readJsonProperties(@Nonnull File jsonFile, Charset s
6464
});
6565
}
6666
}
67+
} catch (jakarta.json.stream.JsonParsingException e) {
68+
// We are likely trying to read a properties that that was not encoded with json-syntax
69+
throw new CannotReadFileException(e);
6770
} catch (IOException e) {
6871
throw new CannotReadFileException(e);
6972
}

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,13 @@ protected static Properties readYmlProperties(@Nonnull File xmlFile, Charset sou
6767
loaderOptions.setProcessComments(false);
6868
Yaml yaml = new Yaml(loaderOptions);
6969
Map<String, Object> data = yaml.load(reader);
70-
for (Map.Entry<String, Object> e: data.entrySet()) {
70+
for (Map.Entry<String, Object> e : data.entrySet()) {
7171
retVal.put(e.getKey(), e.getValue());
7272
}
7373
}
74+
} catch (ClassCastException e) {
75+
// We are likely trying to read a properties that that was not encoded with yml-syntax
76+
throw new CannotReadFileException(e);
7477
} catch (IOException e) {
7578
throw new CannotReadFileException(e);
7679
}

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

+85-13
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,63 @@
1717

1818
package pl.project13.core;
1919

20-
import org.junit.Before;
20+
import junitparams.JUnitParamsRunner;
21+
import junitparams.Parameters;
22+
import org.junit.Assert;
23+
import org.junit.Ignore;
2124
import org.junit.Rule;
2225
import org.junit.Test;
2326
import org.junit.rules.TemporaryFolder;
27+
import org.junit.runner.RunWith;
2428
import pl.project13.core.log.LogInterface;
2529
import pl.project13.core.util.BuildFileChangeListener;
30+
import pl.project13.core.util.GenericFileManager;
2631

2732
import java.io.File;
2833
import java.io.IOException;
2934
import java.nio.file.Files;
3035
import java.nio.file.Path;
36+
import java.util.Arrays;
37+
import java.util.Collection;
38+
import java.util.Optional;
3139
import java.util.Properties;
40+
import java.util.stream.Collectors;
41+
import java.util.stream.Stream;
3242

3343
import static java.nio.charset.StandardCharsets.UTF_8;
44+
import static java.util.Arrays.asList;
3445
import static org.junit.Assert.assertEquals;
3546
import static org.junit.Assert.assertTrue;
3647
import static org.mockito.Mockito.mock;
3748

49+
@RunWith(JUnitParamsRunner.class)
3850
public class PropertiesFileGeneratorTest {
3951
@Rule
4052
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
4153

42-
private PropertiesFileGenerator propertiesFileGenerator;
43-
44-
@Before
45-
public void setUp() {
46-
LogInterface logInterface = mock(LogInterface.class);
54+
private LogInterface getLogInterface() {
55+
return mock(LogInterface.class);
56+
}
57+
58+
private BuildFileChangeListener getBuildFileChangeListener() {
4759
BuildFileChangeListener buildFileChangeListener = file -> {
4860
// Ignore
4961
};
62+
return buildFileChangeListener;
63+
}
5064

51-
propertiesFileGenerator = new PropertiesFileGenerator(logInterface, buildFileChangeListener, CommitIdPropertiesOutputFormat.PROPERTIES, "", "test");
65+
private PropertiesFileGenerator getPropertiesFileGenerator() {
66+
return getPropertiesFileGenerator(CommitIdPropertiesOutputFormat.PROPERTIES);
67+
}
68+
69+
private PropertiesFileGenerator getPropertiesFileGenerator(CommitIdPropertiesOutputFormat propertiesOutputFormat) {
70+
return new PropertiesFileGenerator(
71+
getLogInterface(),
72+
getBuildFileChangeListener(),
73+
CommitIdPropertiesOutputFormat.PROPERTIES,
74+
"",
75+
"test"
76+
);
5277
}
5378

5479
/**
@@ -66,7 +91,7 @@ public void generatedPropertiesFileDoesNotEscapeUnicode() throws GitCommitIdExec
6691
properties.put(GitCommitPropertyConstant.COMMIT_MESSAGE_SHORT, "測試中文");
6792

6893
Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
69-
propertiesFileGenerator.maybeGeneratePropertiesFile(
94+
getPropertiesFileGenerator().maybeGeneratePropertiesFile(
7095
properties, temporaryFolder.getRoot(), propertiesPath.toFile(), UTF_8, false);
7196

7297
String actualContent = Files.readString(propertiesPath, UTF_8);
@@ -85,7 +110,7 @@ public void generatedPropertiesFileEscapeUnicode() throws GitCommitIdExecutionEx
85110
properties.put(GitCommitPropertyConstant.COMMIT_MESSAGE_SHORT, "測試中文");
86111

87112
Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
88-
propertiesFileGenerator.maybeGeneratePropertiesFile(
113+
getPropertiesFileGenerator().maybeGeneratePropertiesFile(
89114
properties, temporaryFolder.getRoot(), propertiesPath.toFile(), UTF_8, true);
90115

91116
String actualContent = Files.readString(propertiesPath, UTF_8);
@@ -103,7 +128,7 @@ public void generatedPropertiesFileDoesNotContainDateComment() throws GitCommitI
103128
properties.put(GitCommitPropertyConstant.BRANCH, "develop");
104129

105130
Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
106-
propertiesFileGenerator.maybeGeneratePropertiesFile(
131+
getPropertiesFileGenerator().maybeGeneratePropertiesFile(
107132
properties, temporaryFolder.getRoot(), propertiesPath.toFile(), UTF_8, true);
108133

109134
String actualContent = Files.readString(propertiesPath, UTF_8);
@@ -114,11 +139,12 @@ public void generatedPropertiesFileDoesNotContainDateComment() throws GitCommitI
114139
}
115140

116141
@Test
117-
public void rereadGeneratedPropertiesFile() throws GitCommitIdExecutionException, IOException {
142+
public void reReadGeneratedPropertiesFile() throws GitCommitIdExecutionException, IOException {
118143
Properties properties = new Properties();
119144
properties.put(GitCommitPropertyConstant.COMMIT_ID_FULL, "b5993378ffadd1f84dc8da220b9204d157ec0f29");
120145
properties.put(GitCommitPropertyConstant.BRANCH, "develop");
121-
146+
147+
PropertiesFileGenerator propertiesFileGenerator = getPropertiesFileGenerator();
122148
Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.properties");
123149
propertiesFileGenerator.maybeGeneratePropertiesFile(
124150
properties, temporaryFolder.getRoot(), propertiesPath.toFile(), UTF_8, true);
@@ -140,7 +166,7 @@ public void worksWithRelativeFileLocation() throws GitCommitIdExecutionException
140166
properties.put(GitCommitPropertyConstant.COMMIT_ID_FULL, "b5993378ffadd1f84dc8da220b9204d157ec0f29");
141167

142168
Path relativePath = new File("src/blah/blub/git.properties").toPath();
143-
propertiesFileGenerator.maybeGeneratePropertiesFile(
169+
getPropertiesFileGenerator().maybeGeneratePropertiesFile(
144170
properties, temporaryFolder.getRoot(), relativePath.toFile(), UTF_8, false);
145171

146172

@@ -151,4 +177,50 @@ public void worksWithRelativeFileLocation() throws GitCommitIdExecutionException
151177
+ "commit.id.full=b5993378ffadd1f84dc8da220b9204d157ec0f29\n");
152178
assertEquals(expectedContent, actualContent);
153179
}
180+
181+
public Collection<?> dumpAndReadFormats() {
182+
Collection<?> collection = Arrays.stream(CommitIdPropertiesOutputFormat.values()).flatMap(f1 ->
183+
Arrays.stream(CommitIdPropertiesOutputFormat.values()).map(f2 -> {
184+
if (f1.equals(f2)) {
185+
return Optional.empty();
186+
} else {
187+
return Optional.of(new Object[]{f1, f2});
188+
}
189+
}).filter(o -> o.isPresent()).map(o -> o.get())
190+
).collect(Collectors.toSet());
191+
return collection;
192+
}
193+
194+
195+
@Test
196+
@Parameters(method = "dumpAndReadFormats")
197+
@Ignore("Read and write is not consistent...")
198+
// https://github.com/git-commit-id/git-commit-id-plugin-core/issues/99
199+
public void reReadGeneratedPropertiesFileWithDifferentFormats(
200+
CommitIdPropertiesOutputFormat dumpFormat,
201+
CommitIdPropertiesOutputFormat readFormat
202+
) throws GitCommitIdExecutionException, IOException {
203+
Properties dumpedProperties = new Properties();
204+
dumpedProperties.put(GitCommitPropertyConstant.COMMIT_ID_FULL, "b5993378ffadd1f84dc8da220b9204d157ec0f29");
205+
dumpedProperties.put(GitCommitPropertyConstant.BRANCH, "develop");
206+
207+
Path propertiesPath = temporaryFolder.getRoot().toPath().resolve("git.json");
208+
GenericFileManager.dumpProperties(
209+
getLogInterface(),
210+
dumpFormat,
211+
propertiesPath.toFile(),
212+
UTF_8,
213+
true,
214+
"test",
215+
dumpedProperties
216+
);
217+
Properties readProperties = GenericFileManager.readProperties(
218+
getLogInterface(),
219+
readFormat,
220+
propertiesPath.toFile(),
221+
UTF_8,
222+
"test"
223+
);
224+
Assert.assertEquals(dumpedProperties, readProperties);
225+
}
154226
}

0 commit comments

Comments
 (0)