|
17 | 17 |
|
18 | 18 | package pl.project13.core;
|
19 | 19 |
|
| 20 | +import jakarta.json.Json; |
| 21 | +import jakarta.json.JsonObject; |
| 22 | +import jakarta.json.JsonReader; |
20 | 23 | import junitparams.JUnitParamsRunner;
|
21 | 24 | import junitparams.Parameters;
|
22 | 25 | import org.apache.commons.io.FileUtils;
|
|
29 | 32 |
|
30 | 33 | import javax.annotation.Nonnull;
|
31 | 34 | import java.io.File;
|
| 35 | +import java.io.FileInputStream; |
32 | 36 | import java.io.IOException;
|
| 37 | +import java.io.InputStreamReader; |
| 38 | +import java.nio.charset.StandardCharsets; |
33 | 39 | import java.nio.file.Files;
|
34 | 40 | import java.nio.file.Path;
|
35 | 41 | 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; |
37 | 53 | import java.util.regex.Pattern;
|
38 | 54 |
|
39 | 55 | import static java.util.Arrays.asList;
|
@@ -479,6 +495,122 @@ public void shouldGenerateCustomPropertiesFileJson(boolean useNativeGit) throws
|
479 | 495 | Assert.assertEquals(p, properties);
|
480 | 496 | }
|
481 | 497 |
|
| 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 | + |
482 | 614 | @Test
|
483 | 615 | @Parameters(method = "useNativeGit")
|
484 | 616 | public void shouldGenerateCustomPropertiesFileXml(boolean useNativeGit) throws Exception {
|
|
0 commit comments