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)));
}
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)));
}
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()]);
}
Aggregations