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.",
}
}
- 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);
}
}
);
}
+ @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);