use of org.sonar.duplications.index.CloneIndex 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)));
}
use of org.sonar.duplications.index.CloneIndex 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)));
}
Aggregations