Search in sources :

Example 1 with DefaultTestCase

use of org.sonar.scanner.deprecated.test.DefaultTestCase in project sonarqube by SonarSource.

the class TestExecutionPublisher method updateTestExecutionFromTestPlan.

private void updateTestExecutionFromTestPlan(final InputFile inputFile, ScannerReportWriter writer) {
    final DefaultTestPlan testPlan = testPlanBuilder.getTestPlanByFile(inputFile);
    if (testPlan == null || !testPlan.testCases().iterator().hasNext()) {
        return;
    }
    long nonSkippedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() != Status.SKIPPED).count();
    appendMeasure(inputFile, writer, new DefaultMeasure<Integer>().forMetric(TESTS).withValue((int) nonSkippedTests));
    long executionTime = StreamSupport.stream(testPlan.testCases().spliterator(), false).map(DefaultTestCase::durationInMs).filter(Objects::nonNull).mapToLong(Long::longValue).sum();
    appendMeasure(inputFile, writer, new DefaultMeasure<Long>().forMetric(TEST_EXECUTION_TIME).withValue(executionTime));
    long errorTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.ERROR).count();
    appendMeasure(inputFile, writer, new DefaultMeasure<Integer>().forMetric(TEST_ERRORS).withValue((int) errorTests));
    long skippedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.SKIPPED).count();
    appendMeasure(inputFile, writer, new DefaultMeasure<Integer>().forMetric(SKIPPED_TESTS).withValue((int) skippedTests));
    long failedTests = StreamSupport.stream(testPlan.testCases().spliterator(), false).filter(t -> t.status() == Status.FAILURE).count();
    appendMeasure(inputFile, writer, new DefaultMeasure<Integer>().forMetric(TEST_FAILURES).withValue((int) failedTests));
}
Also used : DefaultInputComponent(org.sonar.api.batch.fs.internal.DefaultInputComponent) InputFile(org.sonar.api.batch.fs.InputFile) InputComponentStore(org.sonar.scanner.scan.filesystem.InputComponentStore) InputComponent(org.sonar.api.batch.fs.InputComponent) TEST_EXECUTION_TIME(org.sonar.api.measures.CoreMetrics.TEST_EXECUTION_TIME) Status(org.sonar.scanner.deprecated.test.DefaultTestCase.Status) TestPlanBuilder(org.sonar.scanner.deprecated.test.TestPlanBuilder) DefaultTestPlan(org.sonar.scanner.deprecated.test.DefaultTestPlan) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) ScannerReportWriter(org.sonar.scanner.protocol.output.ScannerReportWriter) SKIPPED_TESTS(org.sonar.api.measures.CoreMetrics.SKIPPED_TESTS) TEST_ERRORS(org.sonar.api.measures.CoreMetrics.TEST_ERRORS) TESTS(org.sonar.api.measures.CoreMetrics.TESTS) DefaultSensorStorage.toReportMeasure(org.sonar.scanner.sensor.DefaultSensorStorage.toReportMeasure) Objects(java.util.Objects) DefaultTestCase(org.sonar.scanner.deprecated.test.DefaultTestCase) TEST_FAILURES(org.sonar.api.measures.CoreMetrics.TEST_FAILURES) StreamSupport(java.util.stream.StreamSupport) DefaultMeasure(org.sonar.api.batch.sensor.measure.internal.DefaultMeasure) DefaultTestPlan(org.sonar.scanner.deprecated.test.DefaultTestPlan) Objects(java.util.Objects)

Example 2 with DefaultTestCase

use of org.sonar.scanner.deprecated.test.DefaultTestCase in project sonarqube by SonarSource.

the class GenericTestExecutionReportParser method parseTestCase.

private static void parseTestCase(SMInputCursor cursor, DefaultTestPlan testPlan) throws XMLStreamException {
    checkElementName(cursor, "testCase");
    DefaultTestCase testCase = testPlan.addTestCase(mandatoryAttribute(cursor, NAME_ATTR));
    Status status = 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 = Status.SKIPPED;
        } else if (FAILURE.equals(elementName)) {
            status = Status.FAILURE;
        } else if (ERROR.equals(elementName)) {
            status = Status.ERROR;
        }
        if (Status.OK != status) {
            testCase.setMessage(mandatoryAttribute(child, MESSAGE_ATTR));
        }
    }
    testCase.setStatus(status);
}
Also used : Status(org.sonar.scanner.deprecated.test.DefaultTestCase.Status) SMInputCursor(org.codehaus.staxmate.in.SMInputCursor) DefaultTestCase(org.sonar.scanner.deprecated.test.DefaultTestCase)

Example 3 with DefaultTestCase

use of org.sonar.scanner.deprecated.test.DefaultTestCase in project sonarqube by SonarSource.

the class GenericTestExecutionReportParserTest method mockMutableTestCase.

private DefaultTestCase mockMutableTestCase() {
    DefaultTestCase testCase = mock(DefaultTestCase.class);
    when(testCase.setDurationInMs(anyLong())).thenReturn(testCase);
    when(testCase.setStatus(any(DefaultTestCase.Status.class))).thenReturn(testCase);
    when(testCase.setMessage(anyString())).thenReturn(testCase);
    when(testCase.setType(anyString())).thenReturn(testCase);
    return testCase;
}
Also used : DefaultTestCase(org.sonar.scanner.deprecated.test.DefaultTestCase)

Example 4 with DefaultTestCase

use of org.sonar.scanner.deprecated.test.DefaultTestCase in project sonarqube by SonarSource.

the class GenericTestExecutionReportParserTest method before.

@Before
public void before() {
    context = SensorContextTester.create(new File(""));
    fileWithBranches = setupFile("src/main/java/com/example/ClassWithBranches.java");
    emptyFile = setupFile("src/main/java/com/example/EmptyClass.java");
    testPlanBuilder = mock(TestPlanBuilder.class);
    DefaultTestCase testCase = mockMutableTestCase();
    testPlan = mockMutableTestPlan(testCase);
    when(testPlanBuilder.getTestPlan(any(InputFile.class))).thenReturn(testPlan);
}
Also used : TestPlanBuilder(org.sonar.scanner.deprecated.test.TestPlanBuilder) DefaultTestCase(org.sonar.scanner.deprecated.test.DefaultTestCase) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) File(java.io.File) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Before(org.junit.Before)

Aggregations

DefaultTestCase (org.sonar.scanner.deprecated.test.DefaultTestCase)4 InputFile (org.sonar.api.batch.fs.InputFile)2 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)2 Status (org.sonar.scanner.deprecated.test.DefaultTestCase.Status)2 TestPlanBuilder (org.sonar.scanner.deprecated.test.TestPlanBuilder)2 File (java.io.File)1 Objects (java.util.Objects)1 StreamSupport (java.util.stream.StreamSupport)1 SMInputCursor (org.codehaus.staxmate.in.SMInputCursor)1 Before (org.junit.Before)1 InputComponent (org.sonar.api.batch.fs.InputComponent)1 DefaultInputComponent (org.sonar.api.batch.fs.internal.DefaultInputComponent)1 DefaultMeasure (org.sonar.api.batch.sensor.measure.internal.DefaultMeasure)1 SKIPPED_TESTS (org.sonar.api.measures.CoreMetrics.SKIPPED_TESTS)1 TESTS (org.sonar.api.measures.CoreMetrics.TESTS)1 TEST_ERRORS (org.sonar.api.measures.CoreMetrics.TEST_ERRORS)1 TEST_EXECUTION_TIME (org.sonar.api.measures.CoreMetrics.TEST_EXECUTION_TIME)1 TEST_FAILURES (org.sonar.api.measures.CoreMetrics.TEST_FAILURES)1 DefaultTestPlan (org.sonar.scanner.deprecated.test.DefaultTestPlan)1 ScannerReportWriter (org.sonar.scanner.protocol.output.ScannerReportWriter)1