Search in sources :

Example 11 with Block

use of org.sonar.duplications.block.Block in project sonarqube by SonarSource.

the class IntegrateCrossProjectDuplicationsTest method add_duplications_from_two_blocks.

@Test
public void add_duplications_from_two_blocks() {
    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, 43).setUnit(0, 5).build(), new Block.Builder().setResourceId(ORIGIN_FILE_KEY).setBlockHash(new ByteArray("2b5747f0e4c59124")).setIndexInFile(1).setLines(32, 45).setUnit(5, 20).build());
    Collection<Block> duplicatedBlocks = asList(new Block.Builder().setResourceId(OTHER_FILE_KEY).setBlockHash(new ByteArray("a8998353e96320ec")).setIndexInFile(0).setLines(40, 53).build(), new Block.Builder().setResourceId(OTHER_FILE_KEY).setBlockHash(new ByteArray("2b5747f0e4c59124")).setIndexInFile(1).setLines(42, 55).build());
    underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
    assertThat(duplicationRepository.getDuplications(ORIGIN_FILE)).containsExactly(crossProjectDuplication(new TextBlock(30, 45), OTHER_FILE_KEY, new TextBlock(40, 55)));
}
Also used : Block(org.sonar.duplications.block.Block) ByteArray(org.sonar.duplications.block.ByteArray) Test(org.junit.Test)

Example 12 with Block

use of org.sonar.duplications.block.Block in project sonarqube by SonarSource.

the class IntegrateCrossProjectDuplicationsTest method add_duplications_from_a_single_block.

@Test
public void add_duplications_from_a_single_block() {
    settings.setProperty("sonar.cpd.xoo.minimumTokens", 10);
    Collection<Block> originBlocks = singletonList(// This block contains 11 tokens -> a duplication will be created
    new Block.Builder().setResourceId(ORIGIN_FILE_KEY).setBlockHash(new ByteArray("a8998353e96320ec")).setIndexInFile(0).setLines(30, 45).setUnit(0, 10).build());
    Collection<Block> duplicatedBlocks = singletonList(new Block.Builder().setResourceId(OTHER_FILE_KEY).setBlockHash(new ByteArray("a8998353e96320ec")).setIndexInFile(0).setLines(40, 55).build());
    underTest.computeCpd(ORIGIN_FILE, originBlocks, duplicatedBlocks);
    assertThat(duplicationRepository.getDuplications(ORIGIN_FILE)).containsExactly(crossProjectDuplication(new TextBlock(30, 45), OTHER_FILE_KEY, new TextBlock(40, 55)));
}
Also used : Block(org.sonar.duplications.block.Block) ByteArray(org.sonar.duplications.block.ByteArray) Test(org.junit.Test)

Example 13 with Block

use of org.sonar.duplications.block.Block in project sonarqube by SonarSource.

the class LoadCrossProjectDuplicationsRepositoryStepTest method call_compute_cpd_on_many_duplication.

@Test
public void call_compute_cpd_on_many_duplication() {
    when(crossProjectDuplicationStatusHolder.isEnabled()).thenReturn(true);
    analysisMetadataHolder.setBaseAnalysis(baseProjectAnalysis);
    ComponentDto otherProject = createProject("OTHER_PROJECT_KEY");
    SnapshotDto otherProjectSnapshot = createProjectSnapshot(otherProject);
    ComponentDto otherFile = createFile("OTHER_FILE_KEY", otherProject);
    ScannerReport.CpdTextBlock originBlock1 = ScannerReport.CpdTextBlock.newBuilder().setHash("a8998353e96320ec").setStartLine(30).setEndLine(45).setStartTokenIndex(0).setEndTokenIndex(10).build();
    ScannerReport.CpdTextBlock originBlock2 = ScannerReport.CpdTextBlock.newBuilder().setHash("b1234353e96320ff").setStartLine(10).setEndLine(25).setStartTokenIndex(5).setEndTokenIndex(15).build();
    batchReportReader.putDuplicationBlocks(FILE_REF, asList(originBlock1, originBlock2));
    DuplicationUnitDto duplicate1 = new DuplicationUnitDto().setHash(originBlock1.getHash()).setStartLine(40).setEndLine(55).setIndexInFile(0).setAnalysisUuid(otherProjectSnapshot.getUuid()).setComponentUuid(otherFile.uuid());
    DuplicationUnitDto duplicate2 = new DuplicationUnitDto().setHash(originBlock2.getHash()).setStartLine(20).setEndLine(35).setIndexInFile(1).setAnalysisUuid(otherProjectSnapshot.getUuid()).setComponentUuid(otherFile.uuid());
    dbClient.duplicationDao().insert(dbSession, duplicate1);
    dbClient.duplicationDao().insert(dbSession, duplicate2);
    dbSession.commit();
    underTest.execute(new TestComputationStepContext());
    Class<ArrayList<Block>> listClass = (Class<ArrayList<Block>>) (Class) ArrayList.class;
    ArgumentCaptor<ArrayList<Block>> originBlocks = ArgumentCaptor.forClass(listClass);
    ArgumentCaptor<ArrayList<Block>> duplicationBlocks = ArgumentCaptor.forClass(listClass);
    verify(integrateCrossProjectDuplications).computeCpd(eq(CURRENT_FILE), originBlocks.capture(), duplicationBlocks.capture());
    Map<Integer, Block> originBlocksByIndex = blocksByIndexInFile(originBlocks.getValue());
    assertThat(originBlocksByIndex.get(0)).isEqualTo(new Block.Builder().setResourceId(CURRENT_FILE_KEY).setBlockHash(new ByteArray(originBlock1.getHash())).setIndexInFile(0).setLines(originBlock1.getStartLine(), originBlock1.getEndLine()).setUnit(originBlock1.getStartTokenIndex(), originBlock1.getEndTokenIndex()).build());
    assertThat(originBlocksByIndex.get(1)).isEqualTo(new Block.Builder().setResourceId(CURRENT_FILE_KEY).setBlockHash(new ByteArray(originBlock2.getHash())).setIndexInFile(1).setLines(originBlock2.getStartLine(), originBlock2.getEndLine()).setUnit(originBlock2.getStartTokenIndex(), originBlock2.getEndTokenIndex()).build());
    Map<Integer, Block> duplicationBlocksByIndex = blocksByIndexInFile(duplicationBlocks.getValue());
    assertThat(duplicationBlocksByIndex.get(0)).isEqualTo(new Block.Builder().setResourceId(otherFile.getDbKey()).setBlockHash(new ByteArray(originBlock1.getHash())).setIndexInFile(duplicate1.getIndexInFile()).setLines(duplicate1.getStartLine(), duplicate1.getEndLine()).build());
    assertThat(duplicationBlocksByIndex.get(1)).isEqualTo(new Block.Builder().setResourceId(otherFile.getDbKey()).setBlockHash(new ByteArray(originBlock2.getHash())).setIndexInFile(duplicate2.getIndexInFile()).setLines(duplicate2.getStartLine(), duplicate2.getEndLine()).build());
}
Also used : DuplicationUnitDto(org.sonar.db.duplication.DuplicationUnitDto) SnapshotDto(org.sonar.db.component.SnapshotDto) ComponentDto(org.sonar.db.component.ComponentDto) ScannerReport(org.sonar.scanner.protocol.output.ScannerReport) ArrayList(java.util.ArrayList) TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) Block(org.sonar.duplications.block.Block) ByteArray(org.sonar.duplications.block.ByteArray) Test(org.junit.Test)

Example 14 with Block

use of org.sonar.duplications.block.Block in project sonarqube by SonarSource.

the class BlocksGroup method intersect.

/**
 * Intersection of two groups is a group, which contains blocks from second group that have corresponding block from first group
 * with same resource id and with corrected index.
 */
private static BlocksGroup intersect(BlocksGroup group1, BlocksGroup group2) {
    BlocksGroup intersection = new BlocksGroup();
    List<Block> list1 = group1.blocks;
    List<Block> list2 = group2.blocks;
    int i = 0;
    int j = 0;
    while (i < list1.size() && j < list2.size()) {
        Block block1 = list1.get(i);
        Block block2 = list2.get(j);
        int c = RESOURCE_ID_COMPARATOR.compare(block1.getResourceId(), block2.getResourceId());
        if (c > 0) {
            j++;
            continue;
        }
        if (c < 0) {
            i++;
            continue;
        }
        if (c == 0) {
            c = block1.getIndexInFile() + 1 - block2.getIndexInFile();
        }
        if (c == 0) {
            // list1[i] == list2[j]
            i++;
            j++;
            intersection.blocks.add(block2);
        }
        if (c > 0) {
            // list1[i] > list2[j]
            j++;
        }
        if (c < 0) {
            // list1[i] < list2[j]
            i++;
        }
    }
    return intersection;
}
Also used : Block(org.sonar.duplications.block.Block)

Example 15 with Block

use of org.sonar.duplications.block.Block in project sonarqube by SonarSource.

the class BlocksGroup method pairs.

private static List<Block[]> pairs(BlocksGroup beginGroup, BlocksGroup endGroup, int len) {
    List<Block[]> result = new ArrayList<>();
    List<Block> beginBlocks = beginGroup.blocks;
    List<Block> endBlocks = endGroup.blocks;
    int i = 0;
    int j = 0;
    while (i < beginBlocks.size() && j < endBlocks.size()) {
        Block beginBlock = beginBlocks.get(i);
        Block endBlock = endBlocks.get(j);
        int c = RESOURCE_ID_COMPARATOR.compare(beginBlock.getResourceId(), endBlock.getResourceId());
        if (c == 0) {
            c = beginBlock.getIndexInFile() + len - 1 - endBlock.getIndexInFile();
        }
        if (c == 0) {
            result.add(new Block[] { beginBlock, endBlock });
            i++;
            j++;
        }
        if (c > 0) {
            j++;
        }
        if (c < 0) {
            i++;
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) Block(org.sonar.duplications.block.Block)

Aggregations

Block (org.sonar.duplications.block.Block)56 Test (org.junit.Test)37 ByteArray (org.sonar.duplications.block.ByteArray)29 CloneGroup (org.sonar.duplications.index.CloneGroup)18 CloneIndex (org.sonar.duplications.index.CloneIndex)16 CloneGroupMatcher.hasCloneGroup (org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup)15 ArrayList (java.util.ArrayList)13 MemoryCloneIndex (org.sonar.duplications.index.MemoryCloneIndex)12 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)8 ClonePart (org.sonar.duplications.index.ClonePart)5 IOException (java.io.IOException)4 List (java.util.List)4 InputFile (org.sonar.api.batch.fs.InputFile)4 Statement (org.sonar.duplications.statement.Statement)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2