use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.
the class IntegrateCrossProjectDuplications method computeCpd.
public void computeCpd(Component component, Collection<Block> originBlocks, Collection<Block> duplicationBlocks) {
CloneIndex duplicationIndex = new PackedMemoryCloneIndex();
populateIndex(duplicationIndex, originBlocks);
populateIndex(duplicationIndex, duplicationBlocks);
List<CloneGroup> duplications = SuffixTreeCloneDetectionAlgorithm.detect(duplicationIndex, originBlocks);
Iterable<CloneGroup> filtered = from(duplications).filter(getNumberOfUnitsNotLessThan(component.getFileAttributes().getLanguageKey()));
addDuplications(component, filtered);
}
use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.
the class IntegrateCrossProjectDuplications method addDuplications.
private void addDuplications(Component file, Iterable<CloneGroup> duplications) {
int cloneGroupCount = 0;
for (CloneGroup duplication : duplications) {
cloneGroupCount++;
if (cloneGroupCount > MAX_CLONE_GROUP_PER_FILE) {
LOGGER.warn("Too many duplication groups on file {}. Keeping only the first {} groups.", file.getKey(), MAX_CLONE_GROUP_PER_FILE);
break;
}
addDuplication(file, duplication);
}
}
use of org.sonar.duplications.index.CloneGroup 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)));
}
use of org.sonar.duplications.index.CloneGroup 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)));
}
use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.
the class PmdBridgeTest method testDuplicationInSingleFile.
@Test
public void testDuplicationInSingleFile() {
File file = new File("test-resources/org/sonar/duplications/cpd/CPDTest/CPDFile3.java");
addToIndex(file);
List<CloneGroup> duplications = detect(file);
assertThat(duplications.size()).isEqualTo(1);
CloneGroup duplication = duplications.get(0);
assertThat(duplication.getOriginPart().getResourceId()).isEqualTo(file.getAbsolutePath());
assertThat(duplication.getCloneParts().size()).isEqualTo(2);
assertThat(duplication.getLengthInUnits()).as("length in tokens").isEqualTo(157);
ClonePart part = duplication.getCloneParts().get(0);
assertThat(part.getResourceId()).isEqualTo(file.getAbsolutePath());
assertThat(part.getStartLine()).isEqualTo(30);
assertThat(part.getEndLine()).isEqualTo(44);
}
Aggregations