From c872fdc707462ada981c8fbb5693c971677e6082 Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Thu, 25 Apr 2024 12:20:46 +0200 Subject: [PATCH] Skip subreport messages when copying and merging reports. --- .../java/edu/hm/hafner/analysis/Report.java | 8 +-- .../edu/hm/hafner/analysis/ReportTest.java | 61 ++++++++++++------- 2 files changed, 42 insertions(+), 27 deletions(-) diff --git a/src/main/java/edu/hm/hafner/analysis/Report.java b/src/main/java/edu/hm/hafner/analysis/Report.java index 9ead7a909..cb8642f0c 100644 --- a/src/main/java/edu/hm/hafner/analysis/Report.java +++ b/src/main/java/edu/hm/hafner/analysis/Report.java @@ -336,8 +336,8 @@ public Report addAll(final Report... reports) { } else { reportsToAdd.addAll(report.subReports); - infoMessages.addAll(report.getInfoMessages()); - errorMessages.addAll(report.getErrorMessages()); + infoMessages.addAll(report.infoMessages); + errorMessages.addAll(report.errorMessages); } } @@ -870,8 +870,8 @@ private void copyProperties(final Report source, final Report destination) { destination.name = source.getName(); destination.originReportFile = source.getOriginReportFile(); destination.duplicatesSize += source.duplicatesSize; // not recursively - destination.infoMessages.addAll(source.getInfoMessages()); - destination.errorMessages.addAll(source.getErrorMessages()); + destination.infoMessages.addAll(source.infoMessages); + destination.errorMessages.addAll(source.errorMessages); destination.countersByKey = Stream.concat( destination.countersByKey.entrySet().stream(), source.countersByKey.entrySet().stream()) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, Integer::sum)); diff --git a/src/test/java/edu/hm/hafner/analysis/ReportTest.java b/src/test/java/edu/hm/hafner/analysis/ReportTest.java index 484a341d5..e994a90c4 100644 --- a/src/test/java/edu/hm/hafner/analysis/ReportTest.java +++ b/src/test/java/edu/hm/hafner/analysis/ReportTest.java @@ -11,6 +11,7 @@ import java.util.Set; import java.util.UUID; import java.util.function.BiFunction; +import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -44,34 +45,32 @@ * @author Marcel Binder * @author Ullrich Hafner */ -@SuppressWarnings({"PMD.GodClass", "PMD.ExcessiveImports", "PMD.ExcessiveClassLength", "checkstyle:ClassDataAbstractionCoupling"}) +@SuppressWarnings({"PMD.GodClass", "PMD.ExcessiveImports", "PMD.ExcessiveClassLength", "checkstyle:ClassDataAbstractionCoupling", "checkstyle:ClassFanOutComplexity"}) class ReportTest extends SerializableTest { private static final String SERIALIZATION_NAME = "report.ser"; - private static final Issue HIGH = new IssueBuilder().setMessage("issue-1") + private static final Issue HIGH = build(b -> + b.setMessage("issue-1").setFileName("file-1").setSeverity(Severity.WARNING_HIGH)); + private static final Issue NORMAL_1 = build(b -> + b.setMessage("issue-2") .setFileName("file-1") - .setSeverity(Severity.WARNING_HIGH) - .build(); - private static final Issue NORMAL_1 = new IssueBuilder().setMessage("issue-2") + .setSeverity(Severity.WARNING_NORMAL)); + private static final Issue NORMAL_2 = build(b -> + b.setMessage("issue-3") .setFileName("file-1") - .setSeverity(Severity.WARNING_NORMAL) - .build(); - private static final Issue NORMAL_2 = new IssueBuilder().setMessage("issue-3") - .setFileName("file-1") - .setSeverity(Severity.WARNING_NORMAL) - .build(); - private static final Issue LOW_2_A = new IssueBuilder().setMessage("issue-4") + .setSeverity(Severity.WARNING_NORMAL)); + private static final Issue LOW_2_A = build(b -> + b.setMessage("issue-4") .setFileName("file-2") - .setSeverity(Severity.WARNING_LOW) - .build(); - private static final Issue LOW_2_B = new IssueBuilder().setMessage("issue-5") + .setSeverity(Severity.WARNING_LOW)); + private static final Issue LOW_2_B = build(b -> + b.setMessage("issue-5") .setFileName("file-2") - .setSeverity(Severity.WARNING_LOW) - .build(); - private static final Issue LOW_FILE_3 = new IssueBuilder().setMessage("issue-6") + .setSeverity(Severity.WARNING_LOW)); + private static final Issue LOW_FILE_3 = build(b -> + b.setMessage("issue-6") .setFileName("file-3") - .setSeverity(Severity.WARNING_LOW) - .build(); + .setSeverity(Severity.WARNING_LOW)); private static final int VALUE = 1234; private static final String KEY = "key"; @@ -90,6 +89,14 @@ static void beforeAll() { SLF4JBridgeHandler.install(); } + static Issue build(final Consumer configuration) { + try (var issueBuilder = new IssueBuilder()) { + configuration.accept(issueBuilder); + + return issueBuilder.build(); + } + } + @Test void shouldFindIssuesInModifiedCode() { Report report = new Report(); @@ -986,13 +993,20 @@ void shouldCopyMessagesRecursively() { assertThat(aggregated.getNameOfOrigin(CHECKSTYLE_ID)).isEqualTo(CHECKSTYLE_NAME); assertThat(aggregated.getNameOfOrigin(SPOTBUGS_ID)).isEqualTo(SPOTBUGS_NAME); - assertThat(aggregated.getInfoMessages()).contains( + verifyAggregation(aggregated); + + var copy = aggregated.copy(); + verifyAggregation(copy); + } + + private void verifyAggregation(final Report aggregated) { + assertThat(aggregated.getInfoMessages()).containsOnlyOnce( "Info message from CheckStyle", "Info message from SpotBugs", "Info (Wrapped) message from CheckStyle", "Info (Wrapped) message from SpotBugs", "Info (Aggregated) message"); - assertThat(aggregated.getErrorMessages()).contains( + assertThat(aggregated.getErrorMessages()).containsOnlyOnce( "Error message from CheckStyle", "Error message from SpotBugs", "Error (Wrapped) message from CheckStyle", @@ -1130,7 +1144,8 @@ void shouldObeyEqualsContract() { .forClass(Report.class) .withPrefabValues(Report.class, new Report("left", "Left"), new Report("right", "Right")) .withPrefabValues(TreeString.class, TreeString.valueOf("One"), TreeString.valueOf("Two")) - .withPrefabValues(LineRangeList.class, new LineRangeList(new LineRange(2, 2)), new LineRangeList(new LineRange(1, 1))) + .withPrefabValues(LineRangeList.class, new LineRangeList(new LineRange(2, 2)), + new LineRangeList(new LineRange(1, 1))) .verify(); }