From 7f6004f914450d45aa6d4b3a5e60c21b3e6af53f Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Tue, 12 Dec 2017 11:37:10 +0100 Subject: [PATCH] SONAR-10194 Fix block move detection for issue tracking --- .../core/issue/tracking/BlockRecognizer.java | 4 +- .../core/issue/tracking/TrackerTest.java | 50 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/sonar-core/src/main/java/org/sonar/core/issue/tracking/BlockRecognizer.java b/sonar-core/src/main/java/org/sonar/core/issue/tracking/BlockRecognizer.java index 06c647da407..c1dec9f236f 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/tracking/BlockRecognizer.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/tracking/BlockRecognizer.java @@ -84,7 +84,9 @@ class BlockRecognizer { for (Integer baseLine : basesByLine.keySet()) { for (Integer rawLine : rawsByLine.keySet()) { int weight = lengthOfMaximalBlock(baseInput.getLineHashSequence(), baseLine, rawInput.getLineHashSequence(), rawLine); - possibleLinePairs.add(new LinePair(baseLine, rawLine, weight)); + if (weight > 0) { + possibleLinePairs.add(new LinePair(baseLine, rawLine, weight)); + } } } Collections.sort(possibleLinePairs, LinePairComparator.INSTANCE); 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 c894c65fcdb..0a0fc629fbe 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 @@ -251,6 +251,56 @@ public class TrackerTest { assertThat(tracking.getUnmatchedBases()).isEmpty(); } + // SONAR-10194 + @Test + public void no_match_if_only_same_rulekey() { + FakeInput baseInput = FakeInput.createForSourceLines( + "package aa;", + "", + "/**", + " * Hello world", + " *", + " */", + "public class App {", + "", + " public static void main(String[] args) {", + "", + " int magicNumber = 42;", + "", + " String s = new String(\"Very long line that does not meet our maximum 120 character line length criteria and should be wrapped to avoid SonarQube issues.\");\r\n" + + + " }", + "}"); + Issue base1 = baseInput.createIssueOnLine(11, RuleKey.of("squid", "S109"), "Assign this magic number 42 to a well-named constant, and use the constant instead."); + Issue base2 = baseInput.createIssueOnLine(13, RuleKey.of("squid", "S00103"), "Split this 163 characters long line (which is greater than 120 authorized)."); + + FakeInput rawInput = FakeInput.createForSourceLines( + "package aa;", + "", + "/**", + " * Hello world", + " *", + " */", + "public class App {", + "", + " public static void main(String[] args) {", + " ", + " System.out.println(\"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque vel diam purus. Curabitur ut nisi lacus....\");", + " ", + " int a = 0;", + " ", + " int x = a + 123;", + " }", + "}"); + Issue raw1 = rawInput.createIssueOnLine(11, RuleKey.of("squid", "S00103"), "Split this 139 characters long line (which is greater than 120 authorized)."); + Issue raw2 = rawInput.createIssueOnLine(15, RuleKey.of("squid", "S109"), "Assign this magic number 123 to a well-named constant, and use the constant instead."); + + Tracking tracking = tracker.track(rawInput, baseInput); + assertThat(tracking.baseFor(raw1)).isNull(); + assertThat(tracking.baseFor(raw2)).isNull(); + assertThat(tracking.getUnmatchedBases()).hasSize(2); + } + /** * SONAR-3072 */ -- 2.39.5