]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10194 Fix block move detection for issue tracking
authorJulien HENRY <henryju@yahoo.fr>
Tue, 12 Dec 2017 10:37:10 +0000 (11:37 +0100)
committerJulien HENRY <julien.henry@sonarsource.com>
Thu, 14 Dec 2017 08:27:30 +0000 (09:27 +0100)
sonar-core/src/main/java/org/sonar/core/issue/tracking/BlockRecognizer.java
sonar-core/src/test/java/org/sonar/core/issue/tracking/TrackerTest.java

index 06c647da40706c0da8729e6b30f18017574bd64b..c1dec9f236f001eaa517369512871a38fe5585d2 100644 (file)
@@ -84,7 +84,9 @@ class BlockRecognizer<RAW extends Trackable, BASE extends Trackable> {
     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);
index c894c65fcdb6c844ba048ba7a739210f2025227a..0a0fc629fbeaed2d5dbb2a49c2a3ce310dca9f9a 100644 (file)
@@ -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<Issue, Issue> tracking = tracker.track(rawInput, baseInput);
+    assertThat(tracking.baseFor(raw1)).isNull();
+    assertThat(tracking.baseFor(raw2)).isNull();
+    assertThat(tracking.getUnmatchedBases()).hasSize(2);
+  }
+
   /**
    * SONAR-3072
    */