use of org.sonar.api.test.MutableTestPlan in project sonarqube by SonarSource.
the class TestExecutionAndCoveragePublisher method publish.
@Override
public void publish(ScannerReportWriter writer) {
final ScannerReport.Test.Builder testBuilder = ScannerReport.Test.newBuilder();
final ScannerReport.CoverageDetail.Builder builder = ScannerReport.CoverageDetail.newBuilder();
final ScannerReport.CoverageDetail.CoveredFile.Builder coveredBuilder = ScannerReport.CoverageDetail.CoveredFile.newBuilder();
for (final InputComponent c : componentStore.all()) {
DefaultInputComponent component = (DefaultInputComponent) c;
final MutableTestPlan testPlan = testPlanBuilder.loadPerspective(MutableTestPlan.class, component);
if (testPlan == null || Iterables.isEmpty(testPlan.testCases())) {
continue;
}
final Set<String> testNamesWithCoverage = new HashSet<>();
writer.writeTests(component.batchId(), StreamSupport.stream(testPlan.testCases().spliterator(), false).map(testCase -> toProtobufTest(testBuilder, testNamesWithCoverage, testCase)).collect(toList()));
writer.writeCoverageDetails(component.batchId(), testNamesWithCoverage.stream().map(testName -> toProtobufCoverageDetails(builder, coveredBuilder, testPlan, testName)).collect(toList()));
}
}
use of org.sonar.api.test.MutableTestPlan in project sonarqube by SonarSource.
the class GenericTestExecutionReportParserTest method mockMutableTestPlan.
private MutableTestPlan mockMutableTestPlan(MutableTestCase testCase) {
MutableTestPlan testPlan = mock(MutableTestPlan.class);
when(testPlan.addTestCase(anyString())).thenReturn(testCase);
return testPlan;
}
use of org.sonar.api.test.MutableTestPlan in project sonarqube by SonarSource.
the class CoveragePerTestSensor method processTestFile.
private void processTestFile(InputFile inputFile, SensorContext context) {
File testExecutionFile = new File(inputFile.file().getParentFile(), inputFile.file().getName() + TEST_EXTENSION);
if (testExecutionFile.exists()) {
LOG.debug("Processing " + testExecutionFile.getAbsolutePath());
try {
List<String> lines = FileUtils.readLines(testExecutionFile, fs.encoding().name());
int lineNumber = 0;
MutableTestPlan testPlan = perspectives.as(MutableTestPlan.class, inputFile);
for (String line : lines) {
lineNumber++;
if (StringUtils.isBlank(line)) {
continue;
}
if (line.startsWith("#")) {
continue;
}
try {
Iterator<String> split = Splitter.on(";").split(line).iterator();
String name = split.next();
while (split.hasNext()) {
String coveredBlockStr = split.next();
Iterator<String> splitCoveredBlock = Splitter.on(",").split(coveredBlockStr).iterator();
String componentPath = splitCoveredBlock.next();
InputFile coveredFile = context.fileSystem().inputFile(context.fileSystem().predicates().hasPath(componentPath));
MutableTestable testable = perspectives.as(MutableTestable.class, coveredFile);
List<Integer> coveredLines = new ArrayList<>();
while (splitCoveredBlock.hasNext()) {
coveredLines.add(Integer.parseInt(splitCoveredBlock.next()));
}
for (MutableTestCase testCase : testPlan.testCasesByName(name)) {
testCase.setCoverageBlock(testable, coveredLines);
}
}
} catch (Exception e) {
throw new IllegalStateException("Error processing line " + lineNumber + " of file " + testExecutionFile.getAbsolutePath(), e);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
use of org.sonar.api.test.MutableTestPlan in project sonarqube by SonarSource.
the class TestExecutionSensor method processTestFile.
private void processTestFile(InputFile inputFile, SensorContext context) {
File testExecutionFile = new File(inputFile.file().getParentFile(), inputFile.file().getName() + TEST_EXTENSION);
if (testExecutionFile.exists()) {
LOG.debug("Processing " + testExecutionFile.getAbsolutePath());
try {
List<String> lines = FileUtils.readLines(testExecutionFile, fs.encoding().name());
int lineNumber = 0;
MutableTestPlan testPlan = perspectives.as(MutableTestPlan.class, inputFile);
for (String line : lines) {
lineNumber++;
if (StringUtils.isBlank(line)) {
continue;
}
if (line.startsWith("#")) {
continue;
}
try {
Iterator<String> split = Splitter.on(":").split(line).iterator();
String name = split.next();
String durationStr = split.next();
Long duration = StringUtils.isNotBlank(durationStr) ? Long.parseLong(durationStr) : null;
String msg = split.next();
String stack = split.next();
String status = split.next();
String type = split.next();
MutableTestCase testCase = testPlan.addTestCase(name);
testCase.setDurationInMs(duration);
testCase.setMessage(msg);
testCase.setStackTrace(stack);
testCase.setStatus(Status.valueOf(status));
testCase.setType(type);
} catch (Exception e) {
throw new IllegalStateException("Error processing line " + lineNumber + " of file " + testExecutionFile.getAbsolutePath(), e);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
use of org.sonar.api.test.MutableTestPlan in project sonar-java by SonarSource.
the class UnitTestAnalyzer method addCoverage.
private boolean addCoverage(InputFile resource, InputFile testFile, String testName, List<Integer> coveredLines) {
boolean result = false;
Testable testAbleFile = perspectives.as(MutableTestable.class, resource);
if (testAbleFile != null) {
MutableTestPlan testPlan = perspectives.as(MutableTestPlan.class, testFile);
if (testPlan != null) {
for (MutableTestCase testCase : testPlan.testCasesByName(testName)) {
testCase.setCoverageBlock(testAbleFile, coveredLines);
result = true;
}
}
}
return result;
}
Aggregations