From cc5d31793b9353cadfa0321ba4fa31decc5f6c5c Mon Sep 17 00:00:00 2001 From: Johannes Koenen Date: Tue, 5 Dec 2023 14:02:04 +0100 Subject: [PATCH] [QD-7821] Ensure that newly generated rule lists are mutable While `Arrays.asList()` creates an `ArrayList`, this is NOT a `java.util.ArrayList` - and is in fact immutable. Update the tests to catch this --- .../baseline/DescriptorWithLocation.java | 4 +--- .../jetbrains/qodana/sarif/BaselineTest.java | 23 +++++++++++-------- .../AbsentBaselineTest/baseline_old.json | 4 ++-- .../testData/AbsentBaselineTest/report.json | 4 ++-- 4 files changed, 19 insertions(+), 16 deletions(-) diff --git a/sarif/src/main/java/com/jetbrains/qodana/sarif/baseline/DescriptorWithLocation.java b/sarif/src/main/java/com/jetbrains/qodana/sarif/baseline/DescriptorWithLocation.java index 9310088..1e4d297 100644 --- a/sarif/src/main/java/com/jetbrains/qodana/sarif/baseline/DescriptorWithLocation.java +++ b/sarif/src/main/java/com/jetbrains/qodana/sarif/baseline/DescriptorWithLocation.java @@ -51,13 +51,11 @@ void addTo(Run run) { getOrCreate(e::getRules, e::setRules, ArrayList::new) .add(descriptor); } else { - // Collections.singletonList() is immutable - //noinspection ArraysAsListWithZeroOrOneArgument getOrCreate(tool::getExtensions, tool::setExtensions, HashSet::new) // we don't want to copy all rules from the source AND not override them .add(location.shallowCopy() .withIsComprehensive(false) - .withRules(Arrays.asList(descriptor)) + .withRules(new ArrayList<>(Collections.singletonList(descriptor))) ); } diff --git a/sarif/src/test/java/com/jetbrains/qodana/sarif/BaselineTest.java b/sarif/src/test/java/com/jetbrains/qodana/sarif/BaselineTest.java index 8e3f5f5..4142a88 100644 --- a/sarif/src/test/java/com/jetbrains/qodana/sarif/BaselineTest.java +++ b/sarif/src/test/java/com/jetbrains/qodana/sarif/BaselineTest.java @@ -11,6 +11,7 @@ import java.util.stream.Collectors; import static com.jetbrains.qodana.sarif.baseline.BaselineCalculation.Options.DEFAULT; +import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; public class BaselineTest { @@ -177,7 +178,7 @@ public void testAbsentResultWithChangedIdAndSameVersion() throws IOException { SarifReport report = readReport("src/test/resources/testData/AbsentBaselineTest/report.json"); SarifReport baseline = readReport("src/test/resources/testData/AbsentBaselineTest/baseline.json"); - doTest(report, baseline, 17, 1, 1, new BaselineCalculation.Options(true)); + doTest(report, baseline, 1, 17, 18, new BaselineCalculation.Options(true)); Set knownDescriptorIds = RuleUtil.allRules(report) .map(ReportingDescriptor::getId) @@ -198,7 +199,7 @@ public void testAbsentResultWithChangedIdAndOldVersion() throws IOException { SarifReport report = readReport("src/test/resources/testData/AbsentBaselineTest/report.json"); SarifReport baseline = readReport("src/test/resources/testData/AbsentBaselineTest/baseline_old.json"); - doTest(report, baseline, 17, 1, 1, new BaselineCalculation.Options(true)); + doTest(report, baseline, 0, 18, 19, new BaselineCalculation.Options(true)); Set knownDescriptorIds = RuleUtil.allRules(report) .map(ReportingDescriptor::getId) @@ -214,7 +215,6 @@ public void testAbsentResultWithChangedIdAndOldVersion() throws IOException { assertEquals(new ArrayList(), withoutDescriptor); } - private void doTest(SarifReport report, SarifReport baseline, int expectedUnchanged, @@ -223,9 +223,12 @@ private void doTest(SarifReport report, BaselineCalculation.Options options ) { BaselineCalculation calculation = BaselineCalculation.compare(report, baseline, options); - assertEquals(expectedUnchanged, calculation.getUnchangedResults(), "Unchanged:"); - assertEquals(expectedAbsent, calculation.getAbsentResults(), "Absent:"); - assertEquals(expectedNew, calculation.getNewResults(), "New:"); + assertAll( + () -> assertEquals(expectedUnchanged, calculation.getUnchangedResults(), "Unchanged:"), + () -> assertEquals(expectedAbsent, calculation.getAbsentResults(), "Absent:"), + () -> assertEquals(expectedNew, calculation.getNewResults(), "New:") + ); + List results = report.getRuns().get(0).getResults(); if (!options.isFillBaselineState()) { @@ -242,9 +245,11 @@ private void doTest(SarifReport report, List resultsUnchanged = grouped.get(Result.BaselineState.UNCHANGED); List resultsAbsent = grouped.get(Result.BaselineState.ABSENT); List resultsNew = grouped.get(Result.BaselineState.NEW); - assertEquals(expectedUnchanged, resultsUnchanged == null ? 0 : resultsUnchanged.size(), "Unchanged:"); - assertEquals(expectedAbsent, resultsAbsent == null ? 0 : resultsAbsent.size(), "Absent:"); - assertEquals(expectedNew, resultsNew == null ? 0 : resultsNew.size(), "New:"); + assertAll( + () -> assertEquals(expectedUnchanged, resultsUnchanged == null ? 0 : resultsUnchanged.size(), "Unchanged:"), + () -> assertEquals(expectedAbsent, resultsAbsent == null ? 0 : resultsAbsent.size(), "Absent:"), + () -> assertEquals(expectedNew, resultsNew == null ? 0 : resultsNew.size(), "New:") + ); } private void doTest(SarifReport report, diff --git a/sarif/src/test/resources/testData/AbsentBaselineTest/baseline_old.json b/sarif/src/test/resources/testData/AbsentBaselineTest/baseline_old.json index ad9fd5b..72019ff 100644 --- a/sarif/src/test/resources/testData/AbsentBaselineTest/baseline_old.json +++ b/sarif/src/test/resources/testData/AbsentBaselineTest/baseline_old.json @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:388443f74080fa67cc772fd3face188b8235434adbf80c5e74da494ad4f1ac4d -size 6098015 +oid sha256:c502da2835aa0440cb7ead5614c37cd0e4e69c2cee2f913cb283ee0352341b84 +size 6098018 diff --git a/sarif/src/test/resources/testData/AbsentBaselineTest/report.json b/sarif/src/test/resources/testData/AbsentBaselineTest/report.json index 4901126..59aa286 100644 --- a/sarif/src/test/resources/testData/AbsentBaselineTest/report.json +++ b/sarif/src/test/resources/testData/AbsentBaselineTest/report.json @@ -1,3 +1,3 @@ version https://git-lfs.github.com/spec/v1 -oid sha256:7f8d243d2fce304d038d0e268f5d93e828d4a3b8b35b0725444df56839bedb9a -size 6094919 +oid sha256:e5b022a814d2342dd3545fd7b6a78a11f96d8f0dbbe027cd0378b464efe2d9c2 +size 6094166