Skip to content

Commit c33d3ef

Browse files
author
TheSnoozer
committed
add tests to ensure that properties are generated in a tree-like strucutre
1 parent 1d42ced commit c33d3ef

File tree

1 file changed

+133
-1
lines changed

1 file changed

+133
-1
lines changed

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

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
package pl.project13.core;
1919

20+
import jakarta.json.Json;
21+
import jakarta.json.JsonObject;
22+
import jakarta.json.JsonReader;
2023
import junitparams.JUnitParamsRunner;
2124
import junitparams.Parameters;
2225
import org.apache.commons.io.FileUtils;
@@ -29,11 +32,24 @@
2932

3033
import javax.annotation.Nonnull;
3134
import java.io.File;
35+
import java.io.FileInputStream;
3236
import java.io.IOException;
37+
import java.io.InputStreamReader;
38+
import java.nio.charset.StandardCharsets;
3339
import java.nio.file.Files;
3440
import java.nio.file.Path;
3541
import java.text.SimpleDateFormat;
36-
import java.util.*;
42+
import java.util.Arrays;
43+
import java.util.Collection;
44+
import java.util.Collections;
45+
import java.util.Date;
46+
import java.util.HashMap;
47+
import java.util.HashSet;
48+
import java.util.List;
49+
import java.util.Map;
50+
import java.util.Properties;
51+
import java.util.Set;
52+
import java.util.TimeZone;
3753
import java.util.regex.Pattern;
3854

3955
import static java.util.Arrays.asList;
@@ -479,6 +495,122 @@ public void shouldGenerateCustomPropertiesFileJson(boolean useNativeGit) throws
479495
Assert.assertEquals(p, properties);
480496
}
481497

498+
@Test
499+
@Parameters(method = "useNativeGit")
500+
// https://github.com/git-commit-id/git-commit-id-maven-plugin/pull/123
501+
public void shouldGenerateJsonWithCorrectObjectStructure(boolean useNativeGit) throws Exception {
502+
// given
503+
File dotGitDirectory = createTmpDotGitDirectory(AvailableGitTestRepo.WITH_ONE_COMMIT_WITH_SPECIAL_CHARACTERS);
504+
CommitIdPropertiesOutputFormat commitIdPropertiesOutputFormat = CommitIdPropertiesOutputFormat.JSON;
505+
506+
File targetFilePath = sandbox.resolve("custom-git.json").toFile();
507+
targetFilePath.delete();
508+
509+
GitCommitIdPlugin.Callback cb =
510+
new GitCommitIdTestCallback()
511+
.setDotGitDirectory(dotGitDirectory)
512+
.setUseNativeGit(useNativeGit)
513+
.setShouldGenerateGitPropertiesFile(true)
514+
.setCommitIdGenerationMode(CommitIdGenerationMode.FULL)
515+
.setGenerateGitPropertiesFilename(targetFilePath)
516+
.setPropertiesOutputFormat(commitIdPropertiesOutputFormat)
517+
.build();
518+
Properties properties = new Properties();
519+
520+
// when
521+
GitCommitIdPlugin.runPlugin(cb, properties);
522+
// then
523+
assertThat(targetFilePath).exists();
524+
525+
try (FileInputStream fis = new FileInputStream(targetFilePath)) {
526+
try (InputStreamReader reader = new InputStreamReader(fis, StandardCharsets.UTF_8)) {
527+
try (JsonReader jsonReader = Json.createReader(reader)) {
528+
JsonObject jsonObject = jsonReader.readObject();
529+
buildTree(jsonObject.keySet());
530+
}
531+
}
532+
}
533+
}
534+
535+
class TreeNode {
536+
String value;
537+
private Set<TreeNode> children;
538+
539+
public TreeNode(String value) {
540+
this.value = value;
541+
this.children = new HashSet<>();
542+
}
543+
544+
public void addChild(TreeNode t){
545+
this.children.add(t);
546+
}
547+
548+
@Override
549+
public String toString() {
550+
return "TreeNode{" +
551+
"value='" + value + "'," +
552+
"children=" + children +
553+
"}";
554+
}
555+
}
556+
557+
class TreeLeave extends TreeNode {
558+
public TreeLeave(String value) {
559+
super(value);
560+
}
561+
562+
@Override
563+
public void addChild(TreeNode t) {
564+
throw new IllegalStateException(
565+
"Unexpected TreeLeave: Can not nest " + t.value + " under " + this.value);
566+
}
567+
568+
@Override
569+
public String toString() {
570+
return "TreeLeave{" +
571+
"value='" + value + "'}";
572+
}
573+
}
574+
575+
private TreeNode buildTree(Set<String> nodes) {
576+
TreeNode root = new TreeNode("");
577+
578+
for (String node : nodes) {
579+
TreeNode currentNode = root;
580+
String[] keys = node.split("\\.");
581+
582+
for (int i = 0; i < keys.length; i++) {
583+
String key = keys[i];
584+
585+
TreeNode child = findChild(currentNode, key);
586+
if (i == keys.length - 1) {
587+
if (child == null) {
588+
child = new TreeLeave(key);
589+
} else if (child instanceof TreeNode) {
590+
throw new IllegalStateException(
591+
"Unexpected TreeNode: Can not nest " + key + " under " + child.value);
592+
}
593+
} else {
594+
if (child == null) {
595+
child = new TreeNode(key);
596+
}
597+
}
598+
currentNode.addChild(child);
599+
currentNode = child;
600+
}
601+
}
602+
return root;
603+
}
604+
605+
private TreeNode findChild(TreeNode node, String value) {
606+
for (TreeNode child : node.children) {
607+
if (child.value.equals(value)) {
608+
return child;
609+
}
610+
}
611+
return null;
612+
}
613+
482614
@Test
483615
@Parameters(method = "useNativeGit")
484616
public void shouldGenerateCustomPropertiesFileXml(boolean useNativeGit) throws Exception {

0 commit comments

Comments
 (0)