use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class VisitorsBridgeForTestsTest method test_semantic_disabled.
@Test
public void test_semantic_disabled() {
SensorContextTester context = SensorContextTester.create(new File("")).setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
SonarComponents sonarComponents = new SonarComponents(null, context.fileSystem(), null, null, null, null);
sonarComponents.setSensorContext(context);
Tree parse = JavaParser.createParser().parse("class A{}");
VisitorsBridgeForTests visitorsBridgeForTests = new VisitorsBridgeForTests(Collections.singletonList(new DummyVisitor()), sonarComponents);
visitorsBridgeForTests.setCurrentFile(new File("dummy.java"));
visitorsBridgeForTests.visitFile(parse);
assertThat(visitorsBridgeForTests.lastCreatedTestContext().getSemanticModel()).isNull();
parse = JavaParser.createParser().parse("class A{}");
visitorsBridgeForTests = new VisitorsBridgeForTests(new DummyVisitor(), sonarComponents);
visitorsBridgeForTests.setCurrentFile(new File("dummy.java"));
visitorsBridgeForTests.visitFile(parse);
assertThat(visitorsBridgeForTests.lastCreatedTestContext().getSemanticModel()).isNotNull();
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SurefireJavaParserTest method shouldMergeInnerClasses.
@Test
public void shouldMergeInnerClasses() throws URISyntaxException {
SensorContextTester context = mockContext();
parser.collect(context, getDirs("innerClasses"), true);
assertThat(context.measure(":org.apache.commons.collections.bidimap.AbstractTestBidiMap", CoreMetrics.TESTS).value()).isEqualTo(7);
assertThat(context.measure(":org.apache.commons.collections.bidimap.AbstractTestBidiMap", CoreMetrics.TEST_ERRORS).value()).isEqualTo(1);
assertThat(context.measures(":org.apache.commons.collections.bidimap.AbstractTestBidiMap$TestBidiMapEntrySet")).isEmpty();
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SurefireJavaParserTest method should_handle_parameterized_tests.
@Test
public void should_handle_parameterized_tests() throws URISyntaxException {
SensorContextTester context = mockContext();
when(javaResourceLocator.findResourceByClassName(anyString())).thenAnswer(invocation -> {
String className = (String) invocation.getArguments()[0];
if (className.equals("org.foo.Junit4ParameterizedTest") || className.startsWith("org.foo.Junit5_0ParameterizedTest") || className.startsWith("org.foo.Junit5_1ParameterizedTest")) {
return new TestInputFileBuilder("", className).build();
}
return null;
});
parser.collect(context, getDirs("junitParameterizedTests"), true);
// class names are wrong in JUnit 4.X parameterized tests, with class name being the name of the test
assertThat(context.measure(":org.foo.Junit4ParameterizedTest", CoreMetrics.TESTS).value()).isEqualTo(7);
assertThat(context.measure(":org.foo.Junit4ParameterizedTest", CoreMetrics.TEST_EXECUTION_TIME).value()).isEqualTo(1);
// class names and test names are wrong in JUnit 5.0, resulting in repeated/parameterized tests sharing the same name,
// with class name being the name of the test (cf. https://github.com/junit-team/junit5/issues/1182)
assertThat(context.measure(":org.foo.Junit5_0ParameterizedTest", CoreMetrics.TESTS).value()).isEqualTo(13);
assertThat(context.measure(":org.foo.Junit5_0ParameterizedTest", CoreMetrics.TEST_EXECUTION_TIME).value()).isEqualTo(48);
// test file with expected fix from JUnit 5.1 (TODO: to be confirmed once 5.1 released)
assertThat(context.measure(":org.foo.Junit5_1ParameterizedTest", CoreMetrics.TESTS).value()).isEqualTo(13);
assertThat(context.measure(":org.foo.Junit5_1ParameterizedTest", CoreMetrics.TEST_EXECUTION_TIME).value()).isEqualTo(48);
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SurefireJavaParserTest method should_not_count_negative_tests.
@Test
public void should_not_count_negative_tests() throws URISyntaxException {
SensorContextTester context = mockContext();
parser.collect(context, getDirs("negativeTestTime"), true);
// Test times : -1.120, 0.644, 0.015 -> computed time : 0.659, ignore negative time.
assertThat(context.measure(":java.Foo", CoreMetrics.SKIPPED_TESTS).value()).isEqualTo(0);
assertThat(context.measure(":java.Foo", CoreMetrics.TESTS).value()).isEqualTo(6);
assertThat(context.measure(":java.Foo", CoreMetrics.TEST_ERRORS).value()).isEqualTo(0);
assertThat(context.measure(":java.Foo", CoreMetrics.TEST_FAILURES).value()).isEqualTo(0);
assertThat(context.measure(":java.Foo", CoreMetrics.TEST_EXECUTION_TIME).value()).isEqualTo(659);
}
use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.
the class SurefireJavaParserTest method shouldUseTestSuiteReportIfAlone.
// SONAR-2841: if there's only a test suite report, then it should be read.
@Test
public void shouldUseTestSuiteReportIfAlone() throws URISyntaxException {
SensorContextTester context = mockContext();
parser.collect(context, getDirs("onlyTestSuiteReport"), true);
assertThat(context.measures(":org.sonar.SecondTest")).hasSize(5);
assertThat(context.measures(":org.sonar.JavaNCSSCollectorTest")).hasSize(5);
}
Aggregations