Search in sources :

Example 6 with CloneGroup

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

the class SuffixTreeCloneDetectionAlgorithmTest method huge.

/**
   * See SONAR-3060
   * <p>
   * In case when file contains a lot of duplicated blocks suffix-tree works better than original algorithm,
   * which works more than 5 minutes for this example.
   * </p><p>
   * However should be noted that current implementation with suffix-tree also is not optimal,
   * even if it works for this example couple of seconds,
   * because duplications should be filtered in order to remove fully-covered.
   * But such cases nearly never appear in real-world, so current implementation is acceptable for the moment.
   * </p>
   */
@Test
public void huge() {
    CloneIndex index = createIndex();
    Block[] fileBlocks = new Block[5000];
    for (int i = 0; i < 5000; i++) {
        fileBlocks[i] = newBlock("x", new ByteArray("01"), i);
    }
    List<CloneGroup> result = detect(index, fileBlocks);
    assertEquals(1, result.size());
}
Also used : Block(org.sonar.duplications.block.Block) ByteArray(org.sonar.duplications.block.ByteArray) 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 7 with CloneGroup

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

the class SuffixTreeCloneDetectionAlgorithmTest method noDuplications.

/**
   * Given: file without duplications
   * Expected: {@link Collections#EMPTY_LIST} (no need to construct suffix-tree)
   */
@Test
public void noDuplications() {
    CloneIndex index = createIndex();
    Block[] fileBlocks = newBlocks("a", "1 2 3");
    List<CloneGroup> result = detect(index, fileBlocks);
    assertThat(result, sameInstance(Collections.EMPTY_LIST));
}
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 8 with CloneGroup

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

the class SuffixTreeCloneDetectionAlgorithmTest method myTest2.

/**
   * This test and associated with it suffix-tree demonstrates that without filtering in {@link DuplicationsCollector#endOfGroup()}
   * possible to construct {@link CloneGroup}, which is fully covered by another {@link CloneGroup}.
   * 
   * Given:
   * <pre>
   * x: a 2 3 b 2 3 c 2 3 d 2 3 2 3 2 3
   * </pre>
   * Expected:
   * <pre>
   * x-x (2 3 2 3)
   * x-x-x-x-x-x (2 3)
   * <pre>
   * TODO Godin: however would be better to receive only (2 3)
   */
@Test
public void myTest2() {
    CloneIndex index = createIndex();
    Block[] fileBlocks = newBlocks("x", "a 2 3 b 2 3 c 2 3 d 2 3 2 3 2 3");
    List<CloneGroup> result = detect(index, fileBlocks);
    print(result);
    assertEquals(2, result.size());
    assertThat(result, hasCloneGroup(4, newClonePart("x", 10, 4), newClonePart("x", 12, 4)));
    assertThat(result, hasCloneGroup(2, newClonePart("x", 1, 2), newClonePart("x", 4, 2), newClonePart("x", 7, 2), newClonePart("x", 10, 2), newClonePart("x", 12, 2), newClonePart("x", 14, 2)));
}
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 9 with CloneGroup

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

the class PmdBridgeTest method testDuplicationBetweenTwoFiles.

@Test
public void testDuplicationBetweenTwoFiles() {
    File file1 = new File("test-resources/org/sonar/duplications/cpd/CPDTest/CPDFile1.java");
    File file2 = new File("test-resources/org/sonar/duplications/cpd/CPDTest/CPDFile2.java");
    addToIndex(file1);
    addToIndex(file2);
    List<CloneGroup> duplications = detect(file1);
    assertThat(duplications.size()).isEqualTo(1);
    CloneGroup duplication = duplications.get(0);
    assertThat(duplication.getOriginPart().getResourceId()).isEqualTo(file1.getAbsolutePath());
    ClonePart part1 = new ClonePart(file1.getAbsolutePath(), 1, 18, 41);
    ClonePart part2 = new ClonePart(file2.getAbsolutePath(), 1, 18, 41);
    assertThat(duplication.getCloneParts()).containsOnly(part1, part2);
    assertThat(duplication.getLengthInUnits()).as("length in tokens").isEqualTo(115);
}
Also used : ClonePart(org.sonar.duplications.index.ClonePart) CloneGroup(org.sonar.duplications.index.CloneGroup) File(java.io.File) Test(org.junit.Test)

Example 10 with CloneGroup

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

the class DetectorTestCase method covered.

/**
   * Given:
   * <pre>
   * b: 1 2 1 2
   * a: 1 2 1
   * </pre>
   * Expected:
   * <pre>
   * a-b-b (1 2)
   * a-b (1 2 1)
   * </pre>
   * "a-a-b-b (1)" should not be reported, because fully covered by "a-b (1 2 1)"
   */
@Test
public void covered() {
    CloneIndex index = createIndex(newBlocks("b", "1 2 1 2"));
    Block[] fileBlocks = newBlocks("a", "1 2 1");
    List<CloneGroup> result = detect(index, fileBlocks);
    print(result);
    assertThat(result.size(), is(2));
    assertThat(result, hasCloneGroup(3, newClonePart("a", 0, 3), newClonePart("b", 0, 3)));
    assertThat(result, hasCloneGroup(2, newClonePart("a", 0, 2), newClonePart("b", 0, 2), newClonePart("b", 2, 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