diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-11-17 16:17:38 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2015-11-18 09:07:24 +0100 |
commit | d2494dcd38af169e65436b1ba0b8f5ebb27d12e4 (patch) | |
tree | 0bc062cbe34a00024e6f096341ecccce0f58714a | |
parent | 678d27506f2c945273ce9703933d19ea79723a34 (diff) | |
download | sonarqube-d2494dcd38af169e65436b1ba0b8f5ebb27d12e4.tar.gz sonarqube-d2494dcd38af169e65436b1ba0b8f5ebb27d12e4.zip |
SONAR-6323 Duplications on current file are now ignored
2 files changed, 44 insertions, 7 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/duplication/IntegrateCrossProjectDuplications.java b/server/sonar-server/src/main/java/org/sonar/server/computation/duplication/IntegrateCrossProjectDuplications.java index 6d1ce20b839..efee7c5193d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/computation/duplication/IntegrateCrossProjectDuplications.java +++ b/server/sonar-server/src/main/java/org/sonar/server/computation/duplication/IntegrateCrossProjectDuplications.java @@ -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); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/duplication/IntegrateCrossProjectDuplicationsTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/duplication/IntegrateCrossProjectDuplicationsTest.java index 638d77b8859..6cbcdb319ab 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/computation/duplication/IntegrateCrossProjectDuplicationsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/computation/duplication/IntegrateCrossProjectDuplicationsTest.java @@ -148,6 +148,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); |