use of org.sonar.api.test.MutableTestCase 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.MutableTestCase 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.MutableTestCase in project sonarqube by SonarSource.
the class TestExecutionAndCoveragePublisher method toProtobufCoverageDetails.
private CoverageDetail toProtobufCoverageDetails(final ScannerReport.CoverageDetail.Builder builder, final ScannerReport.CoverageDetail.CoveredFile.Builder coveredBuilder, final MutableTestPlan testPlan, String testName) {
// Take first test with provided name
MutableTestCase testCase = testPlan.testCasesByName(testName).iterator().next();
builder.clear();
builder.setTestName(testName);
for (CoverageBlock block : testCase.coverageBlocks()) {
coveredBuilder.clear();
DefaultInputComponent c = (DefaultInputComponent) componentStore.getByKey(((DefaultTestable) block.testable()).inputFile().key());
coveredBuilder.setFileRef(c.batchId());
for (int line : block.lines()) {
coveredBuilder.addCoveredLine(line);
}
builder.addCoveredFile(coveredBuilder.build());
}
return builder.build();
}
use of org.sonar.api.test.MutableTestCase in project sonarqube by SonarSource.
the class GenericTestExecutionReportParserTest method mockMutableTestCase.
private MutableTestCase mockMutableTestCase() {
MutableTestCase testCase = mock(MutableTestCase.class);
when(testCase.setDurationInMs(anyLong())).thenReturn(testCase);
when(testCase.setStatus(any(org.sonar.api.test.TestCase.Status.class))).thenReturn(testCase);
when(testCase.setMessage(anyString())).thenReturn(testCase);
when(testCase.setStackTrace(anyString())).thenReturn(testCase);
when(testCase.setType(anyString())).thenReturn(testCase);
return testCase;
}
use of org.sonar.api.test.MutableTestCase in project sonarqube by SonarSource.
the class GenericTestExecutionReportParser method parseTestCase.
private void parseTestCase(SMInputCursor cursor, MutableTestPlan testPlan) throws XMLStreamException {
checkElementName(cursor, "testCase");
MutableTestCase testCase = testPlan.addTestCase(mandatoryAttribute(cursor, NAME_ATTR));
TestCase.Status status = TestCase.Status.OK;
testCase.setDurationInMs(longValue(mandatoryAttribute(cursor, DURATION_ATTR), cursor, DURATION_ATTR, 0));
SMInputCursor child = cursor.descendantElementCursor();
if (child.getNext() != null) {
String elementName = child.getLocalName();
if (SKIPPED.equals(elementName)) {
status = TestCase.Status.SKIPPED;
} else if (FAILURE.equals(elementName)) {
status = TestCase.Status.FAILURE;
} else if (ERROR.equals(elementName)) {
status = TestCase.Status.ERROR;
}
if (TestCase.Status.OK != status) {
testCase.setMessage(mandatoryAttribute(child, MESSAGE_ATTR));
testCase.setStackTrace(child.collectDescendantText());
}
}
testCase.setStatus(status);
}
Aggregations