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());
}
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());
}
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());
}
Aggregations