Search in sources :

Example 1 with Jasper

use of org.sonar.java.jsp.Jasper in project sonar-java by SonarSource.

the class JavaSensorTest method should_invoke_visitors_on_generated_code.

@Test
void should_invoke_visitors_on_generated_code() throws Exception {
    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.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) Test(org.junit.jupiter.api.Test) ClasspathForTest(org.sonar.java.classpath.ClasspathForTest)

Example 2 with Jasper

use of org.sonar.java.jsp.Jasper in project sonar-java by SonarSource.

the class JavaSensorTest method should_not_invoke_jasper_jsp_compilation_in_autoscan_for_security_reasons.

@Test
void should_not_invoke_jasper_jsp_compilation_in_autoscan_for_security_reasons() throws Exception {
    Path base = tmp.newFolder().toPath();
    MapSettings settings = new MapSettings();
    settings.setProperty("sonar.internal.analysis.autoscan", "true");
    SensorContextTester context = SensorContextTester.create(base);
    context.setSettings(settings);
    context.fileSystem().setWorkDir(tmp.newFolder().toPath());
    SonarComponents sonarComponents = createSonarComponentsMock(context);
    JspCodeScanner jspCodeVisitor = mock(JspCodeScanner.class);
    when(sonarComponents.mainChecks()).thenReturn(Collections.emptyList());
    when(sonarComponents.testChecks()).thenReturn(Collections.emptyList());
    when(sonarComponents.jspChecks()).thenReturn(Collections.singletonList(jspCodeVisitor));
    Jasper jasper = mock(Jasper.class);
    JavaSensor jss = new JavaSensor(sonarComponents, context.fileSystem(), mock(JavaResourceLocator.class), context.config(), mock(NoSonarFilter.class), null, jasper);
    jss.execute(context);
    verify(jasper, never()).generateFiles(any(), any());
    verify(jspCodeVisitor, never()).scanFile(any());
}
Also used : Path(java.nio.file.Path) SonarComponents(org.sonar.java.SonarComponents) Jasper(org.sonar.java.jsp.Jasper) MapSettings(org.sonar.api.config.internal.MapSettings) SensorContextTester(org.sonar.api.batch.sensor.internal.SensorContextTester) NoSonarFilter(org.sonar.api.issue.NoSonarFilter) DefaultJavaResourceLocator(org.sonar.java.DefaultJavaResourceLocator) JavaResourceLocator(org.sonar.plugins.java.api.JavaResourceLocator) Test(org.junit.jupiter.api.Test) ClasspathForTest(org.sonar.java.classpath.ClasspathForTest)

Example 3 with Jasper

use of org.sonar.java.jsp.Jasper 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

Path (java.nio.file.Path)3 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)3 MapSettings (org.sonar.api.config.internal.MapSettings)3 NoSonarFilter (org.sonar.api.issue.NoSonarFilter)3 DefaultJavaResourceLocator (org.sonar.java.DefaultJavaResourceLocator)3 SonarComponents (org.sonar.java.SonarComponents)3 Jasper (org.sonar.java.jsp.Jasper)3 JavaResourceLocator (org.sonar.plugins.java.api.JavaResourceLocator)3 Test (org.junit.jupiter.api.Test)2 ClasspathForTest (org.sonar.java.classpath.ClasspathForTest)2 GeneratedFile (org.sonar.java.model.GeneratedFile)2 JavaFileScanner (org.sonar.plugins.java.api.JavaFileScanner)2 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)2