use of org.sonar.api.batch.fs.internal.DefaultInputComponent in project sonarqube by SonarSource.
the class DefaultComponentTreeTest method test.
@Test
public void test() {
DefaultInputComponent root = new DefaultInputModule("root");
DefaultInputComponent mod1 = new DefaultInputModule("mod1");
DefaultInputComponent mod2 = new DefaultInputModule("mod2");
DefaultInputComponent mod3 = new DefaultInputModule("mod3");
DefaultInputComponent mod4 = new DefaultInputModule("mod4");
tree.index(mod1, root);
tree.index(mod2, mod1);
tree.index(mod3, root);
tree.index(mod4, root);
assertThat(tree.getChildren(root)).containsOnly(mod1, mod3, mod4);
assertThat(tree.getChildren(mod4)).isEmpty();
assertThat(tree.getChildren(mod1)).containsOnly(mod2);
assertThat(tree.getParent(mod4)).isEqualTo(root);
assertThat(tree.getParent(mod2)).isEqualTo(mod1);
assertThat(tree.getParent(mod1)).isEqualTo(root);
assertThat(tree.getParent(root)).isNull();
}
use of org.sonar.api.batch.fs.internal.DefaultInputComponent in project sonarqube by SonarSource.
the class CpdExecutor method runCpdAnalysis.
@VisibleForTesting
void runCpdAnalysis(ExecutorService executorService, String componentKey, final Collection<Block> fileBlocks, long timeout) {
DefaultInputComponent component = (DefaultInputComponent) componentStore.getByKey(componentKey);
if (component == null) {
LOG.error("Resource not found in component store: {}. Skipping CPD computation for it", componentKey);
return;
}
InputFile inputFile = (InputFile) component;
LOG.debug("Detection of duplications for {}", inputFile.absolutePath());
progressReport.message(String.format("%d/%d - current file: %s", count, total, inputFile.absolutePath()));
List<CloneGroup> duplications;
Future<List<CloneGroup>> futureResult = executorService.submit(() -> SuffixTreeCloneDetectionAlgorithm.detect(index, fileBlocks));
try {
duplications = futureResult.get(timeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
LOG.warn("Timeout during detection of duplications for " + inputFile.absolutePath());
futureResult.cancel(true);
return;
} catch (Exception e) {
throw new IllegalStateException("Fail during detection of duplication for " + inputFile.absolutePath(), e);
}
List<CloneGroup> filtered;
if (!"java".equalsIgnoreCase(inputFile.language())) {
Predicate<CloneGroup> minimumTokensPredicate = DuplicationPredicates.numberOfUnitsNotLessThan(getMinimumTokens(inputFile.language()));
filtered = from(duplications).filter(minimumTokensPredicate).toList();
} else {
filtered = duplications;
}
saveDuplications(component, filtered);
}
use of org.sonar.api.batch.fs.internal.DefaultInputComponent in project sonarqube by SonarSource.
the class MeasuresPublisher method publish.
@Override
public void publish(ScannerReportWriter writer) {
final ScannerReport.Measure.Builder builder = ScannerReport.Measure.newBuilder();
for (final InputComponent c : componentStore.all()) {
DefaultInputComponent component = (DefaultInputComponent) c;
if (component.isFile()) {
DefaultInputFile file = (DefaultInputFile) component;
// Recompute all coverage measures from line data to take into account the possible merge of several reports
updateCoverageFromLineData(file);
// Recompute test execution measures from MutableTestPlan to take into account the possible merge of several reports
updateTestExecutionFromTestPlan(file);
}
Iterable<DefaultMeasure<?>> scannerMeasures = measureCache.byComponentKey(component.key());
if (scannerMeasures.iterator().hasNext()) {
writer.writeComponentMeasures(component.batchId(), StreamSupport.stream(scannerMeasures.spliterator(), false).map(input -> {
if (input.value() == null) {
throw new IllegalArgumentException(String.format("Measure on metric '%s' and component '%s' has no value, but it's not allowed", input.metric().key(), component.key()));
}
builder.clear();
builder.setMetricKey(input.metric().key());
setValueAccordingToType(builder, input);
return builder.build();
}).collect(Collectors.toList()));
}
}
}
use of org.sonar.api.batch.fs.internal.DefaultInputComponent in project sonarqube by SonarSource.
the class ComponentsPublisher method recursiveWriteComponent.
/**
* Writes the tree of components recursively, deep-first.
* @return true if component was written (not skipped)
*/
private boolean recursiveWriteComponent(DefaultInputComponent component) {
Collection<InputComponent> children = componentTree.getChildren(component).stream().filter(c -> recursiveWriteComponent((DefaultInputComponent) c)).collect(Collectors.toList());
if (shouldSkipComponent(component, children)) {
return false;
}
ScannerReport.Component.Builder builder = ScannerReport.Component.newBuilder();
// non-null fields
builder.setRef(component.batchId());
builder.setType(getType(component));
// Don't set key on directories and files to save space since it can be deduced from path
if (component instanceof InputModule) {
DefaultInputModule inputModule = (DefaultInputModule) component;
// Here we want key without branch
builder.setKey(inputModule.key());
// protocol buffers does not accept null values
String name = getName(inputModule);
if (name != null) {
builder.setName(name);
}
String description = getDescription(inputModule);
if (description != null) {
builder.setDescription(description);
}
writeVersion(inputModule, builder);
}
if (component.isFile()) {
DefaultInputFile file = (DefaultInputFile) component;
builder.setIsTest(file.type() == InputFile.Type.TEST);
builder.setLines(file.lines());
String lang = getLanguageKey(file);
if (lang != null) {
builder.setLanguage(lang);
}
}
String path = getPath(component);
if (path != null) {
builder.setPath(path);
}
for (InputComponent child : children) {
builder.addChildRef(((DefaultInputComponent) child).batchId());
}
writeLinks(component, builder);
writer.writeComponent(builder.build());
return true;
}
Aggregations