Search in sources :

Example 46 with SensorContextTester

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

the class JasperTest method test_exception_handling.

@Test
void test_exception_handling() throws Exception {
    SensorContextTester ctx = jspContext("<%=");
    Collection<GeneratedFile> inputFiles = new Jasper().generateFiles(ctx, emptyList());
    assertThat(inputFiles).isEmpty();
    assertThat(logTester.logs(LoggerLevel.DEBUG)).matches(logs -> logs.stream().anyMatch(line -> line.startsWith("Error transpiling src/main/webapp/WEB-INF/jsp/test.jsp.")));
    assertThat(logTester.logs(LoggerLevel.WARN)).contains("Some JSP pages failed to transpile. Enable debug log for details.");
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) InputFile(org.sonar.api.batch.fs.InputFile) GeneratedFile(org.sonar.java.model.GeneratedFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MapSettings(org.sonar.api.config.internal.MapSettings) Mockito.spy(org.mockito.Mockito.spy) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) Collections.singletonList(java.util.Collections.singletonList) Mockito.doThrow(org.mockito.Mockito.doThrow) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) LogTesterJUnit5(org.sonar.api.utils.log.LogTesterJUnit5) Path(java.nio.file.Path) WINDOWS(org.junit.jupiter.api.condition.OS.WINDOWS) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) SensorContext(org.sonar.api.batch.sensor.SensorContext) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Paths(java.nio.file.Paths) TempDir(org.junit.jupiter.api.io.TempDir) PathUtils(org.apache.commons.io.file.PathUtils) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) GeneratedFile(org.sonar.java.model.GeneratedFile) Test(org.junit.jupiter.api.Test)

Example 47 with SensorContextTester

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

the class JasperTest method test_empty.

@Test
void test_empty() throws Exception {
    SensorContextTester ctx = SensorContextTester.create(tempFolder);
    ctx.fileSystem().setWorkDir(workDir);
    Collection<GeneratedFile> generatedFiles = new Jasper().generateFiles(ctx, emptyList());
    assertThat(generatedFiles).isEmpty();
    assertThat(logTester.logs()).containsOnly("Found 0 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 48 with SensorContextTester

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

the class JasperTest method test_compilation.

@Test
void test_compilation() throws Exception {
    SensorContextTester ctx = jspContext(JSP_SOURCE);
    Collection<GeneratedFile> generatedFiles = new Jasper().generateFiles(ctx, emptyList());
    assertThat(generatedFiles).hasSize(1);
    InputFile generatedFile = generatedFiles.iterator().next();
    List<String> generatedCode = Files.readAllLines(generatedFile.path());
    assertThat(generatedCode).contains("      out.write(\"<html>\\n<body>\\n<h2>Hello World!</h2>\\n</body>\\n</html>\");");
}
Also used : SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) GeneratedFile(org.sonar.java.model.GeneratedFile) InputFile(org.sonar.api.batch.fs.InputFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Test(org.junit.jupiter.api.Test)

Example 49 with SensorContextTester

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

the class JasperTest method test_with_classpath_jee6_jstl.

@Test
void test_with_classpath_jee6_jstl() throws Exception {
    SensorContextTester ctx = jspContext("<%@ taglib uri = \"http://java.sun.com/jsp/jstl/core\" prefix = \"c\" %>\n" + "<html>\n" + "<body>\n" + "<h2>Hello World!</h2>\n" + "<c:if test=\"true\">what-if</c:if>\n" + "</body>\n" + "</html>");
    Collection<GeneratedFile> generatedFiles = new Jasper().generateFiles(ctx, asList(jee6Jar, jstlJar));
    assertThat(generatedFiles).isEmpty();
    assertThat(logTester.logs(LoggerLevel.DEBUG)).matches(logs -> logs.stream().anyMatch(line -> line.startsWith("Error transpiling src/main/webapp/WEB-INF/jsp/test.jsp. Error:\njava.lang.ClassFormatError")));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) InputFile(org.sonar.api.batch.fs.InputFile) GeneratedFile(org.sonar.java.model.GeneratedFile) DefaultInputFile(org.sonar.api.batch.fs.internal.DefaultInputFile) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) MapSettings(org.sonar.api.config.internal.MapSettings) Mockito.spy(org.mockito.Mockito.spy) DisabledOnOs(org.junit.jupiter.api.condition.DisabledOnOs) Collections.singletonList(java.util.Collections.singletonList) Mockito.doThrow(org.mockito.Mockito.doThrow) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) RegisterExtension(org.junit.jupiter.api.extension.RegisterExtension) Arrays.asList(java.util.Arrays.asList) Map(java.util.Map) LogTesterJUnit5(org.sonar.api.utils.log.LogTesterJUnit5) Path(java.nio.file.Path) WINDOWS(org.junit.jupiter.api.condition.OS.WINDOWS) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) Collections.emptyList(java.util.Collections.emptyList) Collection(java.util.Collection) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) SensorContext(org.sonar.api.batch.sensor.SensorContext) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) TestInputFileBuilder(org.sonar.api.batch.fs.internal.TestInputFileBuilder) Test(org.junit.jupiter.api.Test) List(java.util.List) AfterEach(org.junit.jupiter.api.AfterEach) Paths(java.nio.file.Paths) TempDir(org.junit.jupiter.api.io.TempDir) PathUtils(org.apache.commons.io.file.PathUtils) LoggerLevel(org.sonar.api.utils.log.LoggerLevel) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) GeneratedFile(org.sonar.java.model.GeneratedFile) Test(org.junit.jupiter.api.Test)

Example 50 with SensorContextTester

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

the class JasperTest method test_exclude_unrelated_files.

@Test
void test_exclude_unrelated_files() throws Exception {
    SensorContextTester ctx = jspContext(JSP_SOURCE);
    ctx.setSettings(new MapSettings().setProperty("sonar.exclusions", "**/*something.xml"));
    Collection<GeneratedFile> generatedFiles = new Jasper().generateFiles(ctx, emptyList());
    assertThat(generatedFiles).hasSize(1);
}
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)

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