use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.
the class CpdExecutorTest method reportTooManyDuplicates.
@Test
public void reportTooManyDuplicates() throws Exception {
// 1 origin part + 101 duplicates = 102
List<ClonePart> parts = new ArrayList<>(CpdExecutor.MAX_CLONE_PART_PER_GROUP + 2);
for (int i = 0; i < CpdExecutor.MAX_CLONE_PART_PER_GROUP + 2; i++) {
parts.add(new ClonePart(batchComponent1.key(), i, i, i + 1));
}
List<CloneGroup> groups = Arrays.asList(CloneGroup.builder().setLength(0).setOrigin(parts.get(0)).setParts(parts).build());
executor.saveDuplications(batchComponent1, groups);
Duplication[] dups = readDuplications(1);
assertThat(dups[0].getDuplicateList()).hasSize(CpdExecutor.MAX_CLONE_PART_PER_GROUP);
assertThat(logTester.logs(LoggerLevel.WARN)).contains("Too many duplication references on file " + batchComponent1 + " for block at line 0. Keep only the first " + CpdExecutor.MAX_CLONE_PART_PER_GROUP + " references.");
}
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 = duplications.stream().filter(getNumberOfUnitsNotLessThan(component.getFileAttributes().getLanguageKey())).collect(Collectors.toList());
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.getDbKey(), MAX_CLONE_GROUP_PER_FILE);
break;
}
addDuplication(file, duplication);
}
}
use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.
the class DuplicationsCollector method endOfGroup.
/**
* Constructs CloneGroup and saves it.
*/
@Override
public void endOfGroup() {
ClonePart origin = null;
CloneGroup.Builder builder = CloneGroup.builder().setLength(length);
List<ClonePart> parts = new ArrayList<>(count);
for (int[] b : blockNumbers) {
Block firstBlock = text.getBlock(b[0]);
Block lastBlock = text.getBlock(b[1]);
ClonePart part = new ClonePart(firstBlock.getResourceId(), firstBlock.getIndexInFile(), firstBlock.getStartLine(), lastBlock.getEndLine());
// TODO Godin: maybe use FastStringComparator here ?
if (originResourceId.equals(part.getResourceId())) {
// part from origin
if (origin == null) {
origin = part;
// To calculate length important to use the origin, because otherwise block may come from DB without required data
builder.setLengthInUnits(lastBlock.getEndUnit() - firstBlock.getStartUnit() + 1);
} else if (part.getUnitStart() < origin.getUnitStart()) {
origin = part;
}
}
parts.add(part);
}
Collections.sort(parts, ContainsInComparator.CLONEPART_COMPARATOR);
builder.setOrigin(origin).setParts(parts);
filter(builder.build());
reset();
}
use of org.sonar.duplications.index.CloneGroup in project sonarqube by SonarSource.
the class DetectorTestCase method exampleFromPaper.
/**
* Given:
* <pre>
* y: 2 3 4 5
* z: 3 4
* x: 1 2 3 4 5 6
* </pre>
* Expected:
* <pre>
* x-y (2 3 4 5)
* x-y-z (3 4)
* </pre>
*/
@Test
public void exampleFromPaper() {
CloneIndex index = createIndex(newBlocks("y", "2 3 4 5"), newBlocks("z", "3 4"));
Block[] fileBlocks = newBlocks("x", "1 2 3 4 5 6");
List<CloneGroup> result = detect(index, fileBlocks);
print(result);
assertEquals(2, result.size());
assertThat(result, hasCloneGroup(4, newClonePart("x", 1, 4), newClonePart("y", 0, 4)));
assertThat(result, hasCloneGroup(2, newClonePart("x", 2, 2), newClonePart("y", 1, 2), newClonePart("z", 0, 2)));
}
Aggregations