Search in sources :

Example 16 with CloneGroup

use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.

the class DetectorTestCase method problemWithNestedCloneGroups.

/**
   * Given:
   * <pre>
   * b: 1 2 1 2 1 2 1
   * a: 1 2 1 2 1 2
   * </pre>
   * Expected:
   * <pre>
   * a-b-b (1 2 1 2 1) - note that there is overlapping among parts for "b"
   * a-b (1 2 1 2 1 2)
   * </pre>
   */
@Test
public void problemWithNestedCloneGroups() {
    CloneIndex index = createIndex(newBlocks("b", "1 2 1 2 1 2 1"));
    Block[] fileBlocks = newBlocks("a", "1 2 1 2 1 2");
    List<CloneGroup> result = detect(index, fileBlocks);
    print(result);
    assertThat(result.size(), is(2));
    assertThat(result, hasCloneGroup(6, newClonePart("a", 0, 6), newClonePart("b", 0, 6)));
    assertThat(result, hasCloneGroup(5, newClonePart("a", 0, 5), newClonePart("b", 0, 5), newClonePart("b", 2, 5)));
}
Also used : Block(org.sonar.duplications.block.Block) MemoryCloneIndex(org.sonar.duplications.index.MemoryCloneIndex) CloneIndex(org.sonar.duplications.index.CloneIndex) CloneGroup(org.sonar.duplications.index.CloneGroup) CloneGroupMatcher.hasCloneGroup(org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup) Test(org.junit.Test)

Example 17 with CloneGroup

use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.

the class FilterTest method second_part_of_C2_covers_first_part_of_C1.

/**
   * Given:
   * <pre>
   * c1: a[2-2]
   * c2: a[0-1], a[2-3]
   * </pre>
   * Expected:
   * <pre>
   * c1 in c2
   * c2 not in c1
   * </pre>
   */
@Test
public void second_part_of_C2_covers_first_part_of_C1() {
    CloneGroup c1 = newCloneGroup(1, newClonePart("a", 2));
    CloneGroup c2 = newCloneGroup(2, newClonePart("a", 0), newClonePart("a", 2));
    assertThat(Filter.containsIn(c1, c2), is(true));
    assertThat(Filter.containsIn(c2, c1), is(false));
}
Also used : CloneGroup(org.sonar.duplications.index.CloneGroup) Test(org.junit.Test)

Example 18 with CloneGroup

use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.

the class FilterTest method start_index_in_C1_less_than_in_C2.

/**
   * Given:
   * <pre>
   * c1: a[1-1]
   * c2: a[2-2]
   * </pre>
   * Expected: c1 not in c2, c2 not in c1
   */
@Test
public void start_index_in_C1_less_than_in_C2() {
    CloneGroup c1 = newCloneGroup(1, newClonePart("a", 1));
    CloneGroup c2 = newCloneGroup(1, newClonePart("a", 2));
    assertThat(Filter.containsIn(c1, c2), is(false));
}
Also used : CloneGroup(org.sonar.duplications.index.CloneGroup) Test(org.junit.Test)

Example 19 with CloneGroup

use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.

the class FilterTest method reflexive_and_antisymmetric.

/**
   * Given:
   * <pre>
   * c1: a[1-1]
   * c2: a[1-1]
   * </pre>
   * Expected:
   * reflexive - c1 in c1,
   * antisymmetric - c1 in c2, c2 in c1, because c1 = c2
   */
@Test
public void reflexive_and_antisymmetric() {
    CloneGroup c1 = newCloneGroup(1, newClonePart("a", 1));
    CloneGroup c2 = newCloneGroup(1, newClonePart("a", 1));
    assertThat(Filter.containsIn(c1, c1), is(true));
    assertThat(Filter.containsIn(c1, c2), is(true));
    assertThat(Filter.containsIn(c2, c1), is(true));
}
Also used : CloneGroup(org.sonar.duplications.index.CloneGroup) Test(org.junit.Test)

Example 20 with CloneGroup

use of org.sonar.duplications.index.CloneGroup 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)

Aggregations

CloneGroup (org.sonar.duplications.index.CloneGroup)33 Test (org.junit.Test)28 Block (org.sonar.duplications.block.Block)16 CloneIndex (org.sonar.duplications.index.CloneIndex)16 CloneGroupMatcher.hasCloneGroup (org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup)15 MemoryCloneIndex (org.sonar.duplications.index.MemoryCloneIndex)10 ClonePart (org.sonar.duplications.index.ClonePart)9 ArrayList (java.util.ArrayList)3 File (java.io.File)2 ByteArray (org.sonar.duplications.block.ByteArray)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 List (java.util.List)1 TimeoutException (java.util.concurrent.TimeoutException)1 InputFile (org.sonar.api.batch.fs.InputFile)1 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)1 PackedMemoryCloneIndex (org.sonar.duplications.index.PackedMemoryCloneIndex)1 Duplication (org.sonar.scanner.protocol.output.ScannerReport.Duplication)1