Search in sources :

Example 21 with ByteArray

use of org.sonar.duplications.block.ByteArray 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 : Block(org.sonar.duplications.block.Block) ByteArray(org.sonar.duplications.block.ByteArray) Test(org.junit.Test)

Example 22 with ByteArray

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

Example 23 with ByteArray

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

the class DetectorTestCase method newBlocks.

protected static Block[] newBlocks(String resourceId, String hashes) {
    List<Block> result = Lists.newArrayList();
    int indexInFile = 0;
    for (int i = 0; i < hashes.length(); i += 2) {
        Block block = newBlock(resourceId, new ByteArray("0" + hashes.charAt(i)), indexInFile);
        result.add(block);
        indexInFile++;
    }
    return result.toArray(new Block[result.size()]);
}
Also used : Block(org.sonar.duplications.block.Block) ByteArray(org.sonar.duplications.block.ByteArray)

Aggregations

ByteArray (org.sonar.duplications.block.ByteArray)23 Block (org.sonar.duplications.block.Block)20 Test (org.junit.Test)17 ArrayList (java.util.ArrayList)6 List (java.util.List)3 CloneGroup (org.sonar.duplications.index.CloneGroup)3 CloneIndex (org.sonar.duplications.index.CloneIndex)3 HashMap (java.util.HashMap)2 ComponentDto (org.sonar.db.component.ComponentDto)2 SnapshotDto (org.sonar.db.component.SnapshotDto)2 DuplicationUnitDto (org.sonar.db.duplication.DuplicationUnitDto)2 CloneGroupMatcher.hasCloneGroup (org.sonar.duplications.detector.CloneGroupMatcher.hasCloneGroup)2 ClonePart (org.sonar.duplications.index.ClonePart)2 MemoryCloneIndex (org.sonar.duplications.index.MemoryCloneIndex)2 ScannerReport (org.sonar.scanner.protocol.output.ScannerReport)2 File (java.io.File)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1