use of org.sonar.duplications.index.ClonePart in project sonarqube by SonarSource.
the class CpdExecutor method toReportDuplication.
private Duplication toReportDuplication(InputComponent component, Duplication.Builder dupBuilder, Duplicate.Builder blockBuilder, CloneGroup input) {
dupBuilder.clear();
ClonePart originBlock = input.getOriginPart();
blockBuilder.clear();
dupBuilder.setOriginPosition(ScannerReport.TextRange.newBuilder().setStartLine(originBlock.getStartLine()).setEndLine(originBlock.getEndLine()).build());
int clonePartCount = 0;
for (ClonePart duplicate : input.getCloneParts()) {
if (!duplicate.equals(originBlock)) {
clonePartCount++;
if (clonePartCount > MAX_CLONE_PART_PER_GROUP) {
LOG.warn("Too many duplication references on file " + component + " for block at line " + originBlock.getStartLine() + ". Keep only the first " + MAX_CLONE_PART_PER_GROUP + " references.");
break;
}
blockBuilder.clear();
String componentKey = duplicate.getResourceId();
if (!component.key().equals(componentKey)) {
DefaultInputComponent sameProjectComponent = (DefaultInputComponent) componentStore.getByKey(componentKey);
blockBuilder.setOtherFileRef(sameProjectComponent.batchId());
}
dupBuilder.addDuplicate(blockBuilder.setRange(ScannerReport.TextRange.newBuilder().setStartLine(duplicate.getStartLine()).setEndLine(duplicate.getEndLine()).build()).build());
}
}
return dupBuilder.build();
}
use of org.sonar.duplications.index.ClonePart in project sonarqube by SonarSource.
the class CpdExecutorTest method reportTooManyDuplicates.
@Test
public void reportTooManyDuplicates() throws Exception {
// 1 origin part + 101 duplicates = 102
List<ClonePart> parts = new ArrayList<>(CpdExecutor.MAX_CLONE_PART_PER_GROUP + 2);
for (int i = 0; i < CpdExecutor.MAX_CLONE_PART_PER_GROUP + 2; i++) {
parts.add(new ClonePart(batchComponent1.key(), i, i, i + 1));
}
List<CloneGroup> groups = Arrays.asList(CloneGroup.builder().setLength(0).setOrigin(parts.get(0)).setParts(parts).build());
executor.saveDuplications(batchComponent1, groups);
Duplication[] dups = readDuplications(1);
assertThat(dups[0].getDuplicateList()).hasSize(CpdExecutor.MAX_CLONE_PART_PER_GROUP);
assertThat(logTester.logs(LoggerLevel.WARN)).contains("Too many duplication references on file " + batchComponent1 + " for block at line 0. Keep only the first " + CpdExecutor.MAX_CLONE_PART_PER_GROUP + " references.");
}
use of org.sonar.duplications.index.ClonePart in project sonarqube by SonarSource.
the class IntegrateCrossProjectDuplications method addDuplication.
private void addDuplication(Component file, CloneGroup duplication) {
ClonePart originPart = duplication.getOriginPart();
Iterable<Duplicate> duplicates = convertClonePartsToDuplicates(file, duplication);
if (!Iterables.isEmpty(duplicates)) {
duplicationRepository.add(file, new Duplication(new TextBlock(originPart.getStartLine(), originPart.getEndLine()), duplicates));
}
}
use of org.sonar.duplications.index.ClonePart in project sonarqube by SonarSource.
the class OriginalCloneDetectionAlgorithm method reportClones.
private void reportClones(BlocksGroup beginGroup, BlocksGroup endGroup, int cloneLength) {
List<Block[]> pairs = beginGroup.pairs(endGroup, cloneLength);
ClonePart origin = null;
List<ClonePart> parts = new ArrayList<>();
for (int i = 0; i < pairs.size(); i++) {
Block[] pair = pairs.get(i);
Block firstBlock = pair[0];
Block lastBlock = pair[1];
ClonePart part = new ClonePart(firstBlock.getResourceId(), firstBlock.getIndexInFile(), firstBlock.getStartLine(), lastBlock.getEndLine());
if (originResourceId.equals(part.getResourceId())) {
if (origin == null || part.getUnitStart() < origin.getUnitStart()) {
origin = part;
}
}
parts.add(part);
}
filter.add(CloneGroup.builder().setLength(cloneLength).setOrigin(origin).setParts(parts).build());
}
use of org.sonar.duplications.index.ClonePart in project sonarqube by SonarSource.
the class PmdBridgeTest method testDuplicationInSingleFile.
@Test
public void testDuplicationInSingleFile() {
File file = new File("test-resources/org/sonar/duplications/cpd/CPDTest/CPDFile3.java");
addToIndex(file);
List<CloneGroup> duplications = detect(file);
assertThat(duplications.size()).isEqualTo(1);
CloneGroup duplication = duplications.get(0);
assertThat(duplication.getOriginPart().getResourceId()).isEqualTo(file.getAbsolutePath());
assertThat(duplication.getCloneParts().size()).isEqualTo(2);
assertThat(duplication.getLengthInUnits()).as("length in tokens").isEqualTo(157);
ClonePart part = duplication.getCloneParts().get(0);
assertThat(part.getResourceId()).isEqualTo(file.getAbsolutePath());
assertThat(part.getStartLine()).isEqualTo(30);
assertThat(part.getEndLine()).isEqualTo(44);
}
Aggregations