Search in sources :

Example 11 with CloneIndex

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

the class DetectorTestCase method example1.

/**
   * Given:
   * <pre>
   * b:     3 4 5 6
   * c:         5 6 7
   * a: 1 2 3 4 5 6 7 8 9
   * </pre>
   * Expected:
   * <pre>
   * a-b (3 4 5 6)
   * a-b-c (5 6)
   * a-c (5 6 7)
   * </pre>
   */
@Test
public void example1() {
    CloneIndex index = createIndex(newBlocks("b", "3 4 5 6"), newBlocks("c", "5 6 7"));
    Block[] fileBlocks = newBlocks("a", "1 2 3 4 5 6 7 8 9");
    List<CloneGroup> result = detect(index, fileBlocks);
    print(result);
    assertThat(result.size(), is(3));
    assertThat(result, hasCloneGroup(4, newClonePart("a", 2, 4), newClonePart("b", 0, 4)));
    assertThat(result, hasCloneGroup(3, newClonePart("a", 4, 3), newClonePart("c", 0, 3)));
    assertThat(result, hasCloneGroup(2, newClonePart("a", 4, 2), newClonePart("b", 2, 2), newClonePart("c", 0, 2)));
}
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 12 with CloneIndex

use of org.sonar.duplications.index.CloneIndex 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 13 with CloneIndex

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

the class IntegrateCrossProjectDuplications method computeCpd.

public void computeCpd(Component component, Collection<Block> originBlocks, Collection<Block> duplicationBlocks) {
    CloneIndex duplicationIndex = new PackedMemoryCloneIndex();
    populateIndex(duplicationIndex, originBlocks);
    populateIndex(duplicationIndex, duplicationBlocks);
    List<CloneGroup> duplications = SuffixTreeCloneDetectionAlgorithm.detect(duplicationIndex, originBlocks);
    Iterable<CloneGroup> filtered = from(duplications).filter(getNumberOfUnitsNotLessThan(component.getFileAttributes().getLanguageKey()));
    addDuplications(component, filtered);
}
Also used : PackedMemoryCloneIndex(org.sonar.duplications.index.PackedMemoryCloneIndex) CloneIndex(org.sonar.duplications.index.CloneIndex) CloneGroup(org.sonar.duplications.index.CloneGroup) PackedMemoryCloneIndex(org.sonar.duplications.index.PackedMemoryCloneIndex)

Example 14 with CloneIndex

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

the class SuffixTreeCloneDetectionAlgorithmTest method myTest.

/**
   * Given:
   * <pre>
   * x: a 2 b 2 c 2 2 2
   * </pre>
   * Expected:
   * <pre>
   * x-x (2 2)
   * x-x-x-x-x (2)
   * <pre>
   * TODO Godin: however would be better to receive only (2)
   */
@Test
public void myTest() {
    CloneIndex index = createIndex();
    Block[] fileBlocks = newBlocks("x", "a 2 b 2 c 2 2 2");
    List<CloneGroup> result = detect(index, fileBlocks);
    print(result);
    assertEquals(2, result.size());
    assertThat(result, hasCloneGroup(2, newClonePart("x", 5, 2), newClonePart("x", 6, 2)));
    assertThat(result, hasCloneGroup(1, newClonePart("x", 1, 1), newClonePart("x", 3, 1), newClonePart("x", 5, 1), newClonePart("x", 6, 1), newClonePart("x", 7, 1)));
}
Also used : Block(org.sonar.duplications.block.Block) 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 15 with CloneIndex

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

the class SuffixTreeCloneDetectionAlgorithmTest method myTest3.

/**
   * This test and associated with it suffix-tree demonstrates that without check of origin in {@link Search}
   * possible to construct {@link CloneGroup} with a wrong origin.
   * 
   * Given:
   * <pre>
   * a: 1 2 3 4
   * b: 4 3 2
   * c: 4 3 1
   * </pre>
   * Expected:
   * <pre>
   * a-c (1)
   * a-b (2)
   * a-b-c (3)
   * a-b-c (4)
   * <pre>
   */
@Test
public void myTest3() {
    CloneIndex index = createIndex(newBlocks("b", "4 3 2"), newBlocks("c", "4 3 1"));
    Block[] fileBlocks = newBlocks("a", "1 2 3 4");
    List<CloneGroup> result = detect(index, fileBlocks);
    print(result);
    assertEquals(4, result.size());
    assertThat(result, hasCloneGroup(1, newClonePart("a", 0, 1), newClonePart("c", 2, 1)));
    assertThat(result, hasCloneGroup(1, newClonePart("a", 1, 1), newClonePart("b", 2, 1)));
    assertThat(result, hasCloneGroup(1, newClonePart("a", 2, 1), newClonePart("b", 1, 1), newClonePart("c", 1, 1)));
    assertThat(result, hasCloneGroup(1, newClonePart("a", 3, 1), newClonePart("b", 0, 1), newClonePart("c", 0, 1)));
}
Also used : Block(org.sonar.duplications.block.Block) CloneIndex(org.sonar.duplications.index.CloneIndex) CloneGroup(org.sonar.duplications.index.CloneGroup) CloneGroupMatcher.hasCloneGroup(org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup) Test(org.junit.Test)

Aggregations

CloneIndex (org.sonar.duplications.index.CloneIndex)17 Test (org.junit.Test)16 Block (org.sonar.duplications.block.Block)16 CloneGroup (org.sonar.duplications.index.CloneGroup)16 CloneGroupMatcher.hasCloneGroup (org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup)15 MemoryCloneIndex (org.sonar.duplications.index.MemoryCloneIndex)11 ByteArray (org.sonar.duplications.block.ByteArray)3 ClonePart (org.sonar.duplications.index.ClonePart)1 PackedMemoryCloneIndex (org.sonar.duplications.index.PackedMemoryCloneIndex)1