Search in sources :

Example 11 with CloneGroup

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

the class DetectorTestCase method clonesInFileItself.

/**
   * Test for problem, which was described in original paper - same clone would be reported twice.
   * Given:
   * <pre>
   * a: 1 2 3 1 2 4
   * </pre>
   * Expected only one clone:
   * <pre>
   * a-a (1 2)
   * </pre>
   */
@Test
public void clonesInFileItself() {
    CloneIndex index = createIndex();
    Block[] fileBlocks = newBlocks("a", "1 2 3 1 2 4");
    List<CloneGroup> result = detect(index, fileBlocks);
    print(result);
    assertThat(result.size(), is(1));
    assertThat(result, hasCloneGroup(2, newClonePart("a", 0, 2), newClonePart("a", 3, 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 CloneGroup

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

the class DetectorTestCase method example2.

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

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

the class DetectorTestCase method exampleFromPaper.

/**
   * Given:
   * <pre>
   * y:   2 3 4 5
   * z:     3 4
   * x: 1 2 3 4 5 6
   * </pre>
   * Expected:
   * <pre>
   * x-y (2 3 4 5)
   * x-y-z (3 4)
   * </pre>
   */
@Test
public void exampleFromPaper() {
    CloneIndex index = createIndex(newBlocks("y", "2 3 4 5"), newBlocks("z", "3 4"));
    Block[] fileBlocks = newBlocks("x", "1 2 3 4 5 6");
    List<CloneGroup> result = detect(index, fileBlocks);
    print(result);
    assertEquals(2, result.size());
    assertThat(result, hasCloneGroup(4, newClonePart("x", 1, 4), newClonePart("y", 0, 4)));
    assertThat(result, hasCloneGroup(2, newClonePart("x", 2, 2), newClonePart("y", 1, 2), newClonePart("z", 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 14 with CloneGroup

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

the class DetectorTestCase method problemWithEndOfFile.

/**
   * Given:
   * <pre>
   * b: 1 2 3 4
   * a: 1 2 3
   * </pre>
   * Expected clone which ends at the end of file "a":
   * <pre>
   * a-b (1 2 3)
   * </pre>
   */
@Test
public void problemWithEndOfFile() {
    CloneIndex cloneIndex = createIndex(newBlocks("b", "1 2 3 4"));
    Block[] fileBlocks = newBlocks("a", "1 2 3");
    List<CloneGroup> clones = detect(cloneIndex, fileBlocks);
    print(clones);
    assertThat(clones.size(), is(1));
    assertThat(clones, hasCloneGroup(3, newClonePart("a", 0, 3), newClonePart("b", 0, 3)));
}
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 15 with CloneGroup

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

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