Search in sources :

Example 6 with ClonePart

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();
}
Also used : ClonePart(org.sonar.duplications.index.ClonePart) DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent)

Example 7 with ClonePart

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.");
}
Also used : Duplication(org.sonar.scanner.protocol.output.ScannerReport.Duplication) ClonePart(org.sonar.duplications.index.ClonePart) ArrayList(java.util.ArrayList) CloneGroup(org.sonar.duplications.index.CloneGroup) Test(org.junit.Test)

Example 8 with ClonePart

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));
    }
}
Also used : ClonePart(org.sonar.duplications.index.ClonePart)

Example 9 with ClonePart

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());
}
Also used : ClonePart(org.sonar.duplications.index.ClonePart) ArrayList(java.util.ArrayList) Block(org.sonar.duplications.block.Block)

Example 10 with ClonePart

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);
}
Also used : ClonePart(org.sonar.duplications.index.ClonePart) CloneGroup(org.sonar.duplications.index.CloneGroup) File(java.io.File) Test(org.junit.Test)

Aggregations

ClonePart (org.sonar.duplications.index.ClonePart)13 CloneGroup (org.sonar.duplications.index.CloneGroup)9 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)4 Block (org.sonar.duplications.block.Block)3 File (java.io.File)2 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)1 ByteArray (org.sonar.duplications.block.ByteArray)1 CloneGroupMatcher.hasCloneGroup (org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup)1 CloneIndex (org.sonar.duplications.index.CloneIndex)1 MemoryCloneIndex (org.sonar.duplications.index.MemoryCloneIndex)1 Duplication (org.sonar.scanner.protocol.output.ScannerReport.Duplication)1