]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6323 Duplications on current file are now ignored 636/head
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 17 Nov 2015 15:17:38 +0000 (16:17 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 18 Nov 2015 08:07:24 +0000 (09:07 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/duplication/IntegrateCrossProjectDuplications.java
server/sonar-server/src/test/java/org/sonar/server/computation/duplication/IntegrateCrossProjectDuplicationsTest.java

index 6d1ce20b839eb66fad9a15012a7f66cee3649745..efee7c5193d8f61f92eeec935dffd562f935fe57 100644 (file)
@@ -93,7 +93,8 @@ public class IntegrateCrossProjectDuplications {
     ClonePart originPart = duplication.getOriginPart();
     TextBlock originTextBlock = new TextBlock(originPart.getStartLine(), originPart.getEndLine());
     int clonePartCount = 0;
-    for (ClonePart part : from(duplication.getCloneParts()).filter(new DoesNotMatchOriginalPart(originPart))) {
+    for (ClonePart part : from(duplication.getCloneParts())
+      .filter(new DoesNotMatchSameComponentKey(originPart.getResourceId()))) {
       clonePartCount++;
       if (clonePartCount > MAX_CLONE_PART_PER_GROUP) {
         LOGGER.warn("Too many duplication references on file {} for block at line {}. Keeping only the first {} references.",
@@ -139,16 +140,16 @@ public class IntegrateCrossProjectDuplications {
     }
   }
 
-  private static class DoesNotMatchOriginalPart implements Predicate<ClonePart> {
-    private final ClonePart originPart;
+  private static class DoesNotMatchSameComponentKey implements Predicate<ClonePart> {
+    private final String componentKey;
 
-    private DoesNotMatchOriginalPart(ClonePart originPart) {
-      this.originPart = originPart;
+    private DoesNotMatchSameComponentKey(String componentKey) {
+      this.componentKey = componentKey;
     }
 
     @Override
-    public boolean apply(ClonePart part) {
-      return !part.equals(originPart);
+    public boolean apply(@Nonnull ClonePart part) {
+      return !part.getResourceId().equals(componentKey);
     }
   }
 
index 638d77b88593736af0653c2871e65feb66a04daa..6cbcdb319abb19437996e2dbe9dd7ae3b0dcfcbf 100644 (file)
@@ -147,6 +147,42 @@ public class IntegrateCrossProjectDuplicationsTest {
       );
   }
 
+  @Test
+  public void add_no_duplication_from_current_file() {
+    settings.setProperty("sonar.cpd.xoo.minimumTokens", 10);
+
+    Collection<Block> originBlocks = asList(
+      new Block.Builder()
+        .setResourceId(ORIGIN_FILE_KEY)
+        .setBlockHash(new ByteArray("a8998353e96320ec"))
+        .setIndexInFile(0)
+        .setLines(30, 45)
+        .setUnit(0, 10)
+        .build(),
+      // Duplication is on the same file
+      new Block.Builder()
+        .setResourceId(ORIGIN_FILE_KEY)
+        .setBlockHash(new ByteArray("a8998353e96320ec"))
+        .setIndexInFile(0)
+        .setLines(46, 60)
+        .setUnit(0, 10)
+        .build()
+      );
+
+    Collection<Block> duplicatedBlocks = singletonList(
+      new Block.Builder()
+        .setResourceId(OTHER_FILE_KEY)
+        .setBlockHash(new ByteArray("a8998353e96320ed"))
+        .setIndexInFile(0)
+        .setLines(40, 55)
+        .build()
+      );
+
+    underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
+
+    verifyNoMoreInteractions(duplicationRepository);
+  }
+
   @Test
   public void add_no_duplication_when_not_enough_tokens() {
     settings.setProperty("sonar.cpd.xoo.minimumTokens", 10);