Search in sources :

Example 46 with Block

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

the class PackedMemoryCloneIndexTest method should_construct_blocks_with_normalized_hash.

/**
 * When: query by a hash value.
 * Expected: all blocks should have same hash, which presented in the form of the same object.
 */
@Test
public void should_construct_blocks_with_normalized_hash() {
    index.insert(newBlock("a", 1));
    index.insert(newBlock("b", 1));
    index.insert(newBlock("c", 1));
    ByteArray requestedHash = new ByteArray(1L);
    Collection<Block> blocks = index.getBySequenceHash(requestedHash);
    assertThat(blocks.size(), is(3));
    for (Block block : blocks) {
        assertThat(block.getBlockHash(), sameInstance(requestedHash));
    }
}
Also used : ByteArray(org.sonar.duplications.block.ByteArray) Block(org.sonar.duplications.block.Block) Test(org.junit.Test)

Example 47 with Block

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

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

Example 49 with Block

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

the class PmdBlockChunkerTest method shouldBuildBlocks.

@Test
public void shouldBuildBlocks() {
    TokensLine line1 = new TokensLine(0, 9, 1, Character.toString((char) 1));
    TokensLine line2 = new TokensLine(10, 19, 2, Character.toString((char) 2));
    TokensLine line3 = new TokensLine(20, 29, 3, Character.toString((char) 3));
    List<Block> blocks = new PmdBlockChunker(2).chunk("resourceId", Arrays.asList(line1, line2, line3));
    assertThat(blocks.size(), is(2));
    Block block = blocks.get(0);
    // assertThat(block.getLengthInUnits(), is(11));
    assertThat(block.getStartLine(), is(1));
    assertThat(block.getEndLine(), is(2));
    assertThat(block.getBlockHash(), is(new ByteArray(1L * 31 + 2)));
    block = blocks.get(1);
    // assertThat(block.getLengthInUnits(), is(33));
    assertThat(block.getStartLine(), is(2));
    assertThat(block.getEndLine(), is(3));
    assertThat(block.getBlockHash(), is(new ByteArray(2L * 31 + 3)));
}
Also used : TokensLine(org.sonar.api.batch.sensor.cpd.internal.TokensLine) Block(org.sonar.duplications.block.Block) ByteArray(org.sonar.duplications.block.ByteArray) Test(org.junit.Test)

Example 50 with Block

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

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