From: Michal Duda Date: Thu, 15 Nov 2018 10:09:12 +0000 (+0100) Subject: SONAR-11493 fix for non-deterministic issue matching X-Git-Tag: 7.6~112 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=9e553274fbe228c06b92527ace1446d08ee244c3;p=sonarqube.git SONAR-11493 fix for non-deterministic issue matching Sometimes two issues with the same rule and on the same line or on the same module/project could have been mixed up during issue matching. --- diff --git a/sonar-core/src/main/java/org/sonar/core/issue/tracking/Tracker.java b/sonar-core/src/main/java/org/sonar/core/issue/tracking/Tracker.java index be73239e5a5..7ace40bd967 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/tracking/Tracker.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/tracking/Tracker.java @@ -35,19 +35,22 @@ public class Tracker extends Abst public NonClosedTracking trackNonClosed(Input rawInput, Input baseInput) { NonClosedTracking tracking = NonClosedTracking.of(rawInput, baseInput); - // 1. match issues with same rule, same line and same line hash, but not necessarily with same message + // 1. match by rule, line, line hash and message + match(tracking, LineAndLineHashAndMessage::new); + + // 2. match issues with same rule, same line and same line hash, but not necessarily with same message match(tracking, LineAndLineHashKey::new); - // 2. detect code moves by comparing blocks of codes + // 3. detect code moves by comparing blocks of codes detectCodeMoves(rawInput, baseInput, tracking); - // 3. match issues with same rule, same message and same line hash + // 4. match issues with same rule, same message and same line hash match(tracking, LineHashAndMessageKey::new); - // 4. match issues with same rule, same line and same message + // 5. match issues with same rule, same line and same message match(tracking, LineAndMessageKey::new); - // 5. match issues with same rule and same line hash but different line and different message. + // 6. match issues with same rule and same line hash but different line and different message. // See SONAR-2812 match(tracking, LineHashKey::new); diff --git a/sonar-core/src/test/java/org/sonar/core/issue/tracking/TrackerTest.java b/sonar-core/src/test/java/org/sonar/core/issue/tracking/TrackerTest.java index 1b5d60bee6e..ac612407280 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/tracking/TrackerTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/tracking/TrackerTest.java @@ -41,6 +41,7 @@ public class TrackerTest { public static final RuleKey RULE_UNUSED_PRIVATE_METHOD = RuleKey.of("java", "UnusedPrivateMethod"); public static final RuleKey RULE_NOT_DESIGNED_FOR_EXTENSION = RuleKey.of("java", "NotDesignedForExtension"); public static final RuleKey RULE_USE_DIAMOND = RuleKey.of("java", "UseDiamond"); + public static final RuleKey RULE_MISSING_PACKAGE_INFO = RuleKey.of("java", "MissingPackageInfo"); @Rule public ExpectedException thrown = ExpectedException.none(); @@ -432,6 +433,22 @@ public class TrackerTest { assertThat(tracking.baseFor(raw1)).isEqualTo(base1); } + @Test + public void match_issues_with_same_rule_key_on_project_level() { + FakeInput baseInput = new FakeInput(); + Issue base1 = baseInput.createIssue(RULE_MISSING_PACKAGE_INFO, "[com.test:abc] Missing package-info.java in package."); + Issue base2 = baseInput.createIssue(RULE_MISSING_PACKAGE_INFO, "[com.test:abc/def] Missing package-info.java in package."); + + FakeInput rawInput = new FakeInput(); + Issue raw1 = rawInput.createIssue(RULE_MISSING_PACKAGE_INFO, "[com.test:abc/def] Missing package-info.java in package."); + Issue raw2 = rawInput.createIssue(RULE_MISSING_PACKAGE_INFO, "[com.test:abc] Missing package-info.java in package."); + + Tracking tracking = tracker.trackNonClosed(rawInput, baseInput); + assertThat(tracking.getUnmatchedBases()).hasSize(0); + assertThat(tracking.baseFor(raw1)).isEqualTo(base2); + assertThat(tracking.baseFor(raw2)).isEqualTo(base1); + } + private static class Issue implements Trackable { private final RuleKey ruleKey; private final Integer line;