Search in sources :

Example 1 with TextBlock

use of org.sonar.ce.task.projectanalysis.duplication.TextBlock in project sonarqube by SonarSource.

the class DuplicationLineReader method extractAllDuplicatedTextBlocks.

/**
 * Duplicated blocks in the current file are either {@link Duplication#getOriginal()} or {@link Duplication#getDuplicates()}
 * when the {@link Duplicate} is a {@link InnerDuplicate}.
 * <p>
 * The returned list is mutable on purpose because it will be sorted.
 * </p>
 *
 * @see {@link #createIndexOfDuplicatedTextBlocks(Iterable)}
 */
private static List<TextBlock> extractAllDuplicatedTextBlocks(Iterable<Duplication> duplications) {
    List<TextBlock> duplicatedBlock = new ArrayList<>(size(duplications));
    for (Duplication duplication : duplications) {
        duplicatedBlock.add(duplication.getOriginal());
        Arrays.stream(duplication.getDuplicates()).filter(InnerDuplicate.class::isInstance).forEach(duplicate -> duplicatedBlock.add(duplicate.getTextBlock()));
    }
    return duplicatedBlock;
}
Also used : Duplication(org.sonar.ce.task.projectanalysis.duplication.Duplication) ArrayList(java.util.ArrayList) TextBlock(org.sonar.ce.task.projectanalysis.duplication.TextBlock)

Example 2 with TextBlock

use of org.sonar.ce.task.projectanalysis.duplication.TextBlock in project sonarqube by SonarSource.

the class DuplicationLineReaderTest method read_duplication_with_duplicates_on_other_file_from_other_project.

@Test
public void read_duplication_with_duplicates_on_other_file_from_other_project() {
    DuplicationLineReader reader = duplicationLineReader(duplication(1, 2, new CrossProjectDuplicate("other-component-key-from-another-project", new TextBlock(3, 4))));
    assertThat(reader.read(line1)).isEmpty();
    assertThat(reader.read(line2)).isEmpty();
    assertThat(reader.read(line3)).isEmpty();
    assertThat(reader.read(line4)).isEmpty();
    assertThat(line1.getDuplicationList()).containsExactly(1);
    assertThat(line2.getDuplicationList()).containsExactly(1);
    assertThat(line3.getDuplicationList()).isEmpty();
    assertThat(line4.getDuplicationList()).isEmpty();
}
Also used : CrossProjectDuplicate(org.sonar.ce.task.projectanalysis.duplication.CrossProjectDuplicate) TextBlock(org.sonar.ce.task.projectanalysis.duplication.TextBlock) Test(org.junit.Test)

Example 3 with TextBlock

use of org.sonar.ce.task.projectanalysis.duplication.TextBlock in project sonarqube by SonarSource.

the class NewSizeMeasuresStepTest method compute_duplicated_lines_counts_lines_from_original_and_InnerDuplicate_only_once.

@Test
public void compute_duplicated_lines_counts_lines_from_original_and_InnerDuplicate_only_once() {
    TextBlock original = new TextBlock(1, 10);
    duplicationRepository.addDuplication(FILE_1_REF, original, new TextBlock(10, 11), new TextBlock(11, 12));
    duplicationRepository.addDuplication(FILE_1_REF, new TextBlock(2, 2), new TextBlock(4, 4));
    setNewLines(FILE_1);
    underTest.execute(new TestComputationStepContext());
    assertRawMeasureValueOnPeriod(FILE_1_REF, NEW_DUPLICATED_LINES_KEY, 11d);
}
Also used : TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) TextBlock(org.sonar.ce.task.projectanalysis.duplication.TextBlock) Test(org.junit.Test)

Example 4 with TextBlock

use of org.sonar.ce.task.projectanalysis.duplication.TextBlock in project sonarqube by SonarSource.

the class NewSizeMeasuresStepTest method compute_duplicated_blocks_one_for_original_one_for_each_InnerDuplicate.

@Test
public void compute_duplicated_blocks_one_for_original_one_for_each_InnerDuplicate() {
    TextBlock original = new TextBlock(1, 1);
    duplicationRepository.addDuplication(FILE_1_REF, original, new TextBlock(2, 2), new TextBlock(4, 4), new TextBlock(3, 4));
    setNewLines(FILE_1);
    underTest.execute(new TestComputationStepContext());
    assertRawMeasureValueOnPeriod(FILE_1_REF, NEW_BLOCKS_DUPLICATED_KEY, 4);
}
Also used : TestComputationStepContext(org.sonar.ce.task.step.TestComputationStepContext) TextBlock(org.sonar.ce.task.projectanalysis.duplication.TextBlock) Test(org.junit.Test)

Example 5 with TextBlock

use of org.sonar.ce.task.projectanalysis.duplication.TextBlock in project sonarqube by SonarSource.

the class NewSizeMeasuresStepTest method addDuplicatedBlock.

/**
 * Adds duplication blocks of a single line (each line is specific to its block).
 * This is a very simple use case, convenient for unit tests but more realistic and complex use cases must be tested separately.
 */
private void addDuplicatedBlock(int fileRef, int blockCount) {
    checkArgument(blockCount > 1, "BlockCount can not be less than 2");
    TextBlock original = new TextBlock(1, 1);
    TextBlock[] duplicates = new TextBlock[blockCount - 1];
    for (int i = 2; i < blockCount + 1; i++) {
        duplicates[i - 2] = new TextBlock(i, i);
    }
    duplicationRepository.addDuplication(fileRef, original, duplicates);
}
Also used : TextBlock(org.sonar.ce.task.projectanalysis.duplication.TextBlock)

Aggregations

TextBlock (org.sonar.ce.task.projectanalysis.duplication.TextBlock)17 Test (org.junit.Test)15 TestComputationStepContext (org.sonar.ce.task.step.TestComputationStepContext)13 ArrayList (java.util.ArrayList)1 CrossProjectDuplicate (org.sonar.ce.task.projectanalysis.duplication.CrossProjectDuplicate)1 Duplication (org.sonar.ce.task.projectanalysis.duplication.Duplication)1 InProjectDuplicate (org.sonar.ce.task.projectanalysis.duplication.InProjectDuplicate)1