Search in sources :

Example 16 with GeneratedFile

use of org.sonar.java.model.GeneratedFile in project sonar-java by SonarSource.

the class JasperTest method test_exclude_current_jsp.

@Test
void test_exclude_current_jsp() throws Exception {
    SensorContextTester ctx = jspContext(JSP_SOURCE);
    ctx.setSettings(new MapSettings().setProperty("sonar.exclusions", "**/any.js,**/test_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 17 with GeneratedFile

use of org.sonar.java.model.GeneratedFile in project sonar-java by SonarSource.

the class JasperTest method test_failing_tag_compilation.

@Test
void test_failing_tag_compilation() throws Exception {
    String tagLib = "<%@ taglib prefix=\"t\" tagdir=\"/WEB-INF/tags\" %>";
    SensorContextTester ctx = jspContext(tagLib + "<t:mytag />");
    createJspFile(tagLib + "<% new Missing(); %> ", webInf.resolve("tags/mytag.tag"));
    Map<String, GeneratedFile> generatedFiles = new Jasper().generateFiles(ctx, emptyList()).stream().collect(Collectors.toMap(GeneratedFile::filename, f -> f));
    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:\norg.apache.jasper.JasperException:")));
}
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 18 with GeneratedFile

use of org.sonar.java.model.GeneratedFile in project sonar-java by SonarSource.

the class JavaSensorTest method assertJasperIsInvoked.

private void assertJasperIsInvoked(MapSettings settings) throws IOException {
    Path base = tmp.newFolder().toPath();
    Path generatedFilePath = tmp.newFile("Generated.java").toPath();
    Files.write(generatedFilePath, "class Generated {}".getBytes());
    GeneratedFile generatedFile = new GeneratedFile(generatedFilePath);
    SensorContextTester context = SensorContextTester.create(base);
    context.setSettings(settings);
    context.fileSystem().setWorkDir(tmp.newFolder().toPath());
    SonarComponents sonarComponents = createSonarComponentsMock(context);
    JavaFileScanner javaFileScanner = mock(JavaFileScanner.class);
    JspCodeScanner testCodeVisitor = mock(JspCodeScanner.class);
    when(sonarComponents.jspChecks()).thenReturn(Collections.singletonList(testCodeVisitor));
    when(sonarComponents.mainChecks()).thenReturn(Collections.singletonList(javaFileScanner));
    Jasper jasper = mock(Jasper.class);
    when(jasper.generateFiles(any(), any())).thenReturn(asList(generatedFile));
    JavaSensor jss = new JavaSensor(sonarComponents, context.fileSystem(), mock(JavaResourceLocator.class), new MapSettings().asConfig(), mock(NoSonarFilter.class), null, jasper);
    jss.execute(context);
    ArgumentCaptor<JavaFileScannerContext> scannerContext = ArgumentCaptor.forClass(JavaFileScannerContext.class);
    verify(testCodeVisitor, times(1)).scanFile(scannerContext.capture());
    assertThat(scannerContext.getValue().getInputFile()).isSameAs(generatedFile);
    // normal visitors are not invoked on generated files
    verify(javaFileScanner, never()).scanFile(any());
}
Also used : Path(java.nio.file.Path) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) DefaultJavaResourceLocator(org.sonar.java.DefaultJavaResourceLocator) JavaResourceLocator(org.sonar.plugins.java.api.JavaResourceLocator) GeneratedFile(org.sonar.java.model.GeneratedFile) SonarComponents(org.sonar.java.SonarComponents) Jasper(org.sonar.java.jsp.Jasper) MapSettings(org.sonar.api.config.internal.MapSettings) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext)

Aggregations

GeneratedFile (org.sonar.java.model.GeneratedFile)18 Test (org.junit.jupiter.api.Test)15 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)15 Path (java.nio.file.Path)8 InputFile (org.sonar.api.batch.fs.InputFile)8 MapSettings (org.sonar.api.config.internal.MapSettings)8 DefaultInputFile (org.sonar.api.batch.fs.internal.DefaultInputFile)7 IOException (java.io.IOException)4 DisabledOnOs (org.junit.jupiter.api.condition.DisabledOnOs)4 SensorContext (org.sonar.api.batch.sensor.SensorContext)4 File (java.io.File)3 StandardCharsets (java.nio.charset.StandardCharsets)3 Files (java.nio.file.Files)3 Paths (java.nio.file.Paths)3 Arrays.asList (java.util.Arrays.asList)3 Collection (java.util.Collection)3 Collections.emptyList (java.util.Collections.emptyList)3 Collections.singletonList (java.util.Collections.singletonList)3 List (java.util.List)3 Map (java.util.Map)3