Search in sources :

Example 51 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class JasperTest method test_exclude_all_jsp.

@Test
void test_exclude_all_jsp() throws Exception {
    SensorContextTester ctx = jspContext(JSP_SOURCE);
    ctx.setSettings(new MapSettings().setProperty("sonar.exclusions", "**/*_jsp.java"));
    Collection<GeneratedFile> generatedFiles = new Jasper().generateFiles(ctx, emptyList());
    assertThat(generatedFiles).isEmpty();
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) MapSettings(org.sonar.api.config.internal.MapSettings) GeneratedFile(org.sonar.java.model.GeneratedFile) Test(org.junit.jupiter.api.Test)

Example 52 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class JasperTest method should_log_warning_when_jasper_fails.

@Test
void should_log_warning_when_jasper_fails() throws Exception {
    SensorContextTester ctx = jspContext(JSP_SOURCE);
    Jasper jasper = spy(new Jasper());
    // we make Jasper#getJasperOptions blowup
    doThrow(new IllegalStateException()).when(jasper).getJasperOptions(any(), any());
    Collection<GeneratedFile> generatedFiles = jasper.generateFiles(ctx, emptyList());
    assertThat(generatedFiles).isEmpty();
    assertThat(logTester.logs(LoggerLevel.WARN)).contains("Failed to transpile JSP files.");
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) GeneratedFile(org.sonar.java.model.GeneratedFile) Test(org.junit.jupiter.api.Test)

Example 53 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class JasperTest method test_compile_custom_tag.

@Test
void test_compile_custom_tag() throws Exception {
    String tagLib = "<%@ taglib prefix=\"t\" tagdir=\"/WEB-INF/tags\" %>";
    SensorContextTester ctx = jspContext(tagLib + "<t:mytag />");
    // spring tag library is used to complete coverage in Jasper classloader - it has resource which is on the project's
    // classpath
    createJspFile(tagLib + SPRING_TLD + "<spring:url value=\"/url/path\" />\n" + "<h2>Hello World!</h2>", webInf.resolve("tags/mytag.tag"));
    Collection<GeneratedFile> generatedFiles = new Jasper().generateFiles(ctx, singletonList(springJar));
    assertThat(generatedFiles).hasSize(1);
    GeneratedFile testJspFile = generatedFiles.iterator().next();
    List<String> testJsp = Files.readAllLines(testJspFile.path());
    assertThat(testJsp).contains("    org.apache.jsp.tag.web.mytag_tag _jspx_th_t_005fmytag_005f0 = new org.apache.jsp.tag.web.mytag_tag();");
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) GeneratedFile(org.sonar.java.model.GeneratedFile) Test(org.junit.jupiter.api.Test)

Example 54 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class JasperTest method jspContext.

private SensorContextTester jspContext(String jspSource, Path path) throws IOException {
    jspFile = createJspFile(jspSource, path);
    SensorContextTester ctx = SensorContextTester.create(tempFolder);
    DefaultInputFile inputFile = TestInputFileBuilder.create("", tempFolder.toFile(), jspFile.toFile()).setLanguage("jsp").setContents(jspSource).build();
    ctx.fileSystem().add(inputFile);
    ctx.fileSystem().setWorkDir(workDir);
    return ctx;
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile)

Example 55 with SensorContextTester

use of org.sonar.api.batch.sensor.internal.SensorContextTester in project sonar-java by SonarSource.

the class VisitorsBridgeForTestsTest method test_report_with_analysis_message.

@Test
void test_report_with_analysis_message() {
    SensorContextTester context = SensorContextTester.create(new File("")).setRuntime(SonarRuntimeImpl.forSonarLint(Version.create(6, 7)));
    SonarComponents sonarComponents = new SonarComponents(null, context.fileSystem(), null, null, null);
    sonarComponents.setSensorContext(context);
    Tree parse = JParserTestUtils.parse("class A{}");
    DummyVisitor javaCheck = new DummyVisitor();
    VisitorsBridgeForTests visitorsBridgeForTests = new VisitorsBridgeForTests(Collections.singletonList(javaCheck), sonarComponents, new JavaVersionImpl());
    visitorsBridgeForTests.setCurrentFile(TestUtils.emptyInputFile("dummy.java"));
    visitorsBridgeForTests.visitFile(parse, false);
    JavaFileScannerContextForTests lastContext = visitorsBridgeForTests.lastCreatedTestContext();
    assertThat(lastContext.getIssues()).isEmpty();
    AnalyzerMessage message = lastContext.createAnalyzerMessage(javaCheck, parse, "test");
    lastContext.addIssue(-1, javaCheck, "test");
    lastContext.addIssue(-1, javaCheck, "test", 15);
    lastContext.addIssueOnFile(javaCheck, "test");
    lastContext.addIssueOnProject(javaCheck, "test");
    lastContext.reportIssue(message);
    assertThat(message.getMessage()).isEqualTo("test");
    assertThat(lastContext.getIssues()).hasSize(5);
}
Also used : SonarComponents(org.sonar.java.SonarComponents) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) JavaVersionImpl(org.sonar.java.model.JavaVersionImpl) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) InputFile(org.sonar.api.batch.fs.InputFile) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)224 File (java.io.File)96 Test (org.junit.jupiter.api.Test)94 InputFile (org.sonar.api.batch.fs.InputFile)75 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)63 MapSettings (org.sonar.api.config.internal.MapSettings)60 Test (org.junit.Test)59 TestInputFileBuilder (org.sonar.api.batch.fs.internal.TestInputFileBuilder)49 Test (org.testng.annotations.Test)42 DefaultFileSystem (org.sonar.api.batch.fs.internal.DefaultFileSystem)40 OpenEdgePluginTest (org.sonar.plugins.openedge.OpenEdgePluginTest)37 Path (java.nio.file.Path)35 OpenEdgeSettings (org.sonar.plugins.openedge.foundation.OpenEdgeSettings)30 ClasspathForTest (org.sonar.java.classpath.ClasspathForTest)25 SonarComponents (org.sonar.java.SonarComponents)22 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)20 GeneratedFile (org.sonar.java.model.GeneratedFile)18 ArrayList (java.util.ArrayList)15 ExternalIssue (org.sonar.api.batch.sensor.issue.ExternalIssue)15 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)14