use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonarqube by SonarSource.
the class XooTokenizerTest method testExecution.
@Test
public void testExecution() throws IOException {
File source = new File(baseDir, "src/foo.xoo");
FileUtils.write(source, "token1 token2 token3\ntoken4");
DefaultInputFile inputFile = new TestInputFileBuilder("foo", "src/foo.xoo").setLanguage("xoo").setModuleBaseDir(baseDir.toPath()).build();
fileSystem.add(inputFile);
XooTokenizer tokenizer = new XooTokenizer(fileSystem);
SourceCode sourceCode = mock(SourceCode.class);
when(sourceCode.getFileName()).thenReturn(inputFile.absolutePath());
Tokens cpdTokens = new Tokens();
tokenizer.tokenize(sourceCode, cpdTokens);
// 4 tokens + EOF
assertThat(cpdTokens.getTokens()).hasSize(5);
assertThat(cpdTokens.getTokens().get(3)).isEqualTo(new TokenEntry("token4", "src/foo.xoo", 2));
}
use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonarqube by SonarSource.
the class DefaultTestCase method setCoverageBlock.
@Override
public MutableTestCase setCoverageBlock(InputFile mainFile, List<Integer> lines) {
Preconditions.checkArgument(mainFile.type() == Type.MAIN, "Test file can only cover a main file");
DefaultInputFile coveredFile = (DefaultInputFile) mainFile;
if (coverageBlocksByTestedFile.containsKey(coveredFile)) {
throw new CoverageAlreadyExistsException("The link between " + name() + " and " + coveredFile.key() + " already exists");
}
coverageBlocksByTestedFile.put(coveredFile, new DefaultCoverageBlock(this, coveredFile, lines));
return this;
}
use of org.sonar.api.batch.fs.internal.DefaultInputFile 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.DefaultInputFile 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;
}
use of org.sonar.api.batch.fs.internal.DefaultInputFile in project sonarqube by SonarSource.
the class CoveragePublisher method publish.
@Override
public void publish(ScannerReportWriter writer) {
for (final DefaultInputFile inputFile : componentStore.allFilesToPublish()) {
Map<Integer, LineCoverage.Builder> coveragePerLine = new LinkedHashMap<>();
int lineCount = inputFile.lines();
applyLineMeasure(inputFile.key(), lineCount, CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY, coveragePerLine, (value, builder) -> builder.setHits(Integer.parseInt(value) > 0));
applyLineMeasure(inputFile.key(), lineCount, CoreMetrics.CONDITIONS_BY_LINE_KEY, coveragePerLine, (value, builder) -> builder.setConditions(Integer.parseInt(value)));
applyLineMeasure(inputFile.key(), lineCount, CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY, coveragePerLine, (value, builder) -> builder.setCoveredConditions(Integer.parseInt(value)));
writer.writeComponentCoverage(inputFile.batchId(), Iterables.transform(coveragePerLine.values(), BuildCoverage.INSTANCE));
}
}
Aggregations