Search in sources :

Example 26 with Block

use of org.sonar.duplications.block.Block 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 27 with Block

use of org.sonar.duplications.block.Block in project sonarqube by SonarSource.

the class DetectorTestCase method fileAlreadyInIndex.

/**
 * Given:
 * <pre>
 * a: 1 2 3
 * b: 1 2 4
 * a: 1 2 5
 * </pre>
 * Expected:
 * <pre>
 * a-b (1 2) - instead of "a-a-b", which will be the case if file from index not ignored
 * </pre>
 */
@Test
public void fileAlreadyInIndex() {
    CloneIndex index = createIndex(newBlocks("a", "1 2 3"), newBlocks("b", "1 2 4"));
    // Note about blocks with hashes "3", "4" and "5": those blocks here in order to not face another problem - with EOF (see separate test)
    Block[] fileBlocks = newBlocks("a", "1 2 5");
    List<CloneGroup> result = detect(index, fileBlocks);
    print(result);
    assertThat(result.size(), is(1));
    assertThat(result, hasCloneGroup(2, newClonePart("a", 0, 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 28 with Block

use of org.sonar.duplications.block.Block 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)

Example 29 with Block

use of org.sonar.duplications.block.Block 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 30 with Block

use of org.sonar.duplications.block.Block 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)

Aggregations

Block (org.sonar.duplications.block.Block)56 Test (org.junit.Test)37 ByteArray (org.sonar.duplications.block.ByteArray)29 CloneGroup (org.sonar.duplications.index.CloneGroup)18 CloneIndex (org.sonar.duplications.index.CloneIndex)16 CloneGroupMatcher.hasCloneGroup (org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup)15 ArrayList (java.util.ArrayList)13 MemoryCloneIndex (org.sonar.duplications.index.MemoryCloneIndex)12 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)8 ClonePart (org.sonar.duplications.index.ClonePart)5 IOException (java.io.IOException)4 List (java.util.List)4 InputFile (org.sonar.api.batch.fs.InputFile)4 Statement (org.sonar.duplications.statement.Statement)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2