Search in sources :

Example 16 with CloneIndex

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

the class DetectorTestCase method exampleFromPaperWithModifiedResourceIds.

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

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

the class DetectorTestCase method same_lines_but_different_indexes.

/**
   * Given file with two lines, containing following statements:
   * <pre>
   * 0: A,B,A,B
   * 1: A,B,A
   * </pre>
   * with block size 5 each block will span both lines, and hashes will be:
   * <pre>
   * A,B,A,B,A=1
   * B,A,B,A,B=2
   * A,B,A,B,A=1
   * </pre>
   * Expected: one clone with two parts, which contain exactly the same lines
   */
@Test
public void same_lines_but_different_indexes() {
    CloneIndex cloneIndex = createIndex();
    Block.Builder block = Block.builder().setResourceId("a").setLines(0, 1);
    Block[] fileBlocks = new Block[] { block.setBlockHash(new ByteArray("1".getBytes())).setIndexInFile(0).build(), block.setBlockHash(new ByteArray("2".getBytes())).setIndexInFile(1).build(), block.setBlockHash(new ByteArray("1".getBytes())).setIndexInFile(2).build() };
    List<CloneGroup> clones = detect(cloneIndex, fileBlocks);
    print(clones);
    assertThat(clones.size(), is(1));
    Iterator<CloneGroup> clonesIterator = clones.iterator();
    CloneGroup clone = clonesIterator.next();
    assertThat(clone.getCloneUnitLength(), is(1));
    assertThat(clone.getCloneParts().size(), is(2));
    assertThat(clone.getOriginPart(), is(new ClonePart("a", 0, 0, 1)));
    assertThat(clone.getCloneParts(), hasItem(new ClonePart("a", 0, 0, 1)));
    assertThat(clone.getCloneParts(), hasItem(new ClonePart("a", 2, 0, 1)));
}
Also used : ClonePart(org.sonar.duplications.index.ClonePart) Block(org.sonar.duplications.block.Block) ByteArray(org.sonar.duplications.block.ByteArray) 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)

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