Jasper { public Collection<GeneratedFile> generateFiles(SensorContext sensorContext, List<File> javaClasspath) { 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); 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 { Path generatedFile = transpileJsp(jsp.path(), uriRoot, classLoader, servletContext, options, runtimeContext); 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); } }  Collection<GeneratedFile> generateFiles(SensorContext sensorContext, List<File> javaClasspath);  }
@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."); }
@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:"))); }
@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>\");"); }
@Test void test_with_classpath() throws Exception { SensorContextTester ctx = jspContext(SPRING_TLD + "<html>\n" + "<body>\n" + "<h2>Hello World!</h2>\n" + "<spring:url value=\"/url/path\" />\n" + "</body>\n" + "</html>"); Collection<GeneratedFile> generatedFiles = new Jasper().generateFiles(ctx, singletonList(springJar)); assertThat(generatedFiles).hasSize(1); InputFile generatedFile = generatedFiles.iterator().next(); List<String> generatedCode = Files.readAllLines(generatedFile.path()); assertThat(generatedCode).contains(" org.springframework.web.servlet.tags.UrlTag _jspx_th_spring_005furl_005f0 = new org.springframework.web.servlet.tags.UrlTag();"); }
@Test void test_with_classpath_jee6_jstl() throws Exception { SensorContextTester ctx = jspContext( "<%@ taglib uri = \"http: "<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"))); }
@Test void test_compilation_without_webinf() throws Exception { SensorContext ctx = jspContext(JSP_SOURCE, tempFolder.resolve("test.jsp")); 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>\");"); assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("WEB-INF directory not found, will use basedir as context root"); }
@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."); }
@Test void test_source_map() throws Exception { SensorContextTester ctx = jspContext(JSP_SOURCE); Collection<GeneratedFile> generatedFiles = new Jasper().generateFiles(ctx, emptyList()); assertThat(generatedFiles).hasSize(1); GeneratedFile generatedFile = generatedFiles.iterator().next(); assertThat(generatedFile.sourceMap()).isNotNull(); }
@DisabledOnOs(WINDOWS) @Test void test_jasper_with_symlink() throws Exception { Path output = tempFolder.resolve("out").toAbsolutePath(); Files.createDirectories(output); Path link = tempFolder.resolve("link"); Files.createSymbolicLink(link, output); Path path = link.resolve("WEB-INF/test.jsp"); SensorContextTester ctx = jspContext(JSP_SOURCE, path); 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>\");"); }
@Test void test_compile_custom_tag() throws Exception { String tagLib = "<%@ taglib prefix=\"t\" tagdir=\"/WEB-INF/tags\" %>"; SensorContextTester ctx = jspContext(tagLib + "<t:mytag />"); createJspFile(tagLib + SPRING_TLD + "<spring:url value=\"/url/path\" />\n" + "<h2>Hello World!</h2>", webInf.resolve("tags/mytag.tag")); Collection<GeneratedFile> generatedFiles = new Jasper().generateFiles(ctx, singletonList(springJar)); assertThat(generatedFiles).hasSize(1); GeneratedFile testJspFile = generatedFiles.iterator().next(); List<String> testJsp = Files.readAllLines(testJspFile.path()); assertThat(testJsp).contains( " org.apache.jsp.tag.web.mytag_tag _jspx_th_t_005fmytag_005f0 = new org.apache.jsp.tag.web.mytag_tag();"); }