use of org.sonar.java.model.GeneratedFile 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.model.GeneratedFile in project sonar-java by SonarSource.
the class Jasper method generateFiles.
public Collection<GeneratedFile> generateFiles(SensorContext sensorContext, List<File> javaClasspath) {
List<String> sonarExclusions = Arrays.asList(sensorContext.config().getStringArray(SONAR_EXCLUSIONS_PROPERTY));
if (isAllJavaJspExcluded(sonarExclusions)) {
return Collections.emptyList();
}
Predicate<String> javaExclusionFilter = createExclusionFilter(sonarExclusions);
List<InputFile> jspFiles = jspFiles(sensorContext.fileSystem());
LOG.debug("Found {} JSP files.", jspFiles.size());
if (jspFiles.isEmpty()) {
return Collections.emptyList();
}
Path uriRoot = findWebInfParentDirectory(sensorContext.fileSystem()).orElse(sensorContext.fileSystem().baseDir().getAbsoluteFile().toPath());
LOG.debug("Context root set to {}", uriRoot);
Path outputDir = outputDir(sensorContext);
// Jasper internally calls Thread#getContextClassLoader to instantiate some classes. ContextClassLoader is set by scanner
// and doesn't contain plugin jar, so we need to configure ContextClassLoader with the class loader of the plugin to be able
// to run Jasper. Original classloader is restored in finally.
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
try {
ClassLoader classLoader = initClassLoader(javaClasspath);
Thread.currentThread().setContextClassLoader(classLoader);
JspFactory.setDefaultFactory(new JspFactoryImpl());
JspCServletContext servletContext = new ServletContext(uriRoot.toUri().toURL(), classLoader);
JasperOptions options = getJasperOptions(outputDir, servletContext);
JspRuntimeContext runtimeContext = new JspRuntimeContext(servletContext, options);
boolean errorTranspiling = false;
Map<Path, GeneratedFile> generatedJavaFiles = new HashMap<>();
for (InputFile jsp : jspFiles) {
try {
transpileJsp(jsp.path(), uriRoot, classLoader, servletContext, options, runtimeContext, javaExclusionFilter).ifPresent(generatedFile -> generatedJavaFiles.put(generatedFile, new GeneratedFile(generatedFile)));
} catch (Exception | LinkageError e) {
errorTranspiling = true;
StringWriter w = new StringWriter();
e.printStackTrace(new PrintWriter(w));
LOG.debug("Error transpiling {}. Error:\n{}", jsp, w.toString());
}
}
if (errorTranspiling) {
LOG.warn("Some JSP pages failed to transpile. Enable debug log for details.");
}
runtimeContext.getSmaps().values().forEach(smap -> processSourceMap(uriRoot, generatedJavaFiles, smap, sensorContext.fileSystem()));
return generatedJavaFiles.values();
} catch (Exception e) {
LOG.warn("Failed to transpile JSP files.", e);
return Collections.emptyList();
} finally {
Thread.currentThread().setContextClassLoader(originalClassLoader);
}
}
use of org.sonar.java.model.GeneratedFile 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.");
}
use of org.sonar.java.model.GeneratedFile 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.");
}
use of org.sonar.java.model.GeneratedFile 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>\");");
}
Aggregations