diff --git a/its/plugin/projects/servlet-jsp/src/main/webapp/views/greeting.jsp b/its/plugin/projects/servlet-jsp/src/main/webapp/views/greeting.jsp index 05f70bb6afe..fbadaf75bd3 100644 --- a/its/plugin/projects/servlet-jsp/src/main/webapp/views/greeting.jsp +++ b/its/plugin/projects/servlet-jsp/src/main/webapp/views/greeting.jsp @@ -22,6 +22,9 @@

${e:forHtml(param.name)}

+ + true + diff --git a/java-jsp/pom.xml b/java-jsp/pom.xml index 97ce5f2fbb9..256a0bea203 100644 --- a/java-jsp/pom.xml +++ b/java-jsp/pom.xml @@ -83,6 +83,16 @@ spring-webmvc 5.2.3.RELEASE + + javax + javaee-web-api + 6.0 + + + javax.servlet + jstl + 1.2 + ${project.build.directory}/test-jars diff --git a/java-jsp/src/main/java/org/sonar/java/jsp/Jasper.java b/java-jsp/src/main/java/org/sonar/java/jsp/Jasper.java index 1bdab1d18b3..e00004c1fd4 100644 --- a/java-jsp/src/main/java/org/sonar/java/jsp/Jasper.java +++ b/java-jsp/src/main/java/org/sonar/java/jsp/Jasper.java @@ -21,6 +21,8 @@ import java.io.File; import java.io.IOException; +import java.io.PrintWriter; +import java.io.StringWriter; import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; @@ -87,9 +89,11 @@ public Collection generateFiles(SensorContext sensorContext, List try { Path generatedFile = transpileJsp(jsp.path(), uriRoot, classLoader, servletContext, options, runtimeContext); generatedJavaFiles.put(generatedFile, new GeneratedFile(generatedFile)); - } catch (Exception e) { + } catch (Exception | LinkageError e) { errorTranspiling = true; - LOG.debug("Error transpiling " + jsp, e); + StringWriter w = new StringWriter(); + e.printStackTrace(new PrintWriter(w)); + LOG.debug("Error transpiling {}. Error:\n{}", jsp, w.toString()); } } if (errorTranspiling) { diff --git a/java-jsp/src/test/java/org/sonar/java/jsp/JasperTest.java b/java-jsp/src/test/java/org/sonar/java/jsp/JasperTest.java index e1d69e026e4..a5a4a6bfa44 100644 --- a/java-jsp/src/test/java/org/sonar/java/jsp/JasperTest.java +++ b/java-jsp/src/test/java/org/sonar/java/jsp/JasperTest.java @@ -46,6 +46,7 @@ import org.sonar.api.utils.log.LoggerLevel; import org.sonar.java.model.GeneratedFile; +import static java.util.Arrays.asList; import static java.util.Collections.emptyList; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; @@ -76,6 +77,8 @@ class JasperTest { public LogTester logTester = new LogTester(); private Path jspFile; private final File springJar = Paths.get("target/test-jars/spring-webmvc-5.2.3.RELEASE.jar").toFile(); + private final File jstlJar = Paths.get("target/test-jars/jstl-1.2.jar").toFile(); + private final File jee6Jar = Paths.get("target/test-jars/javaee-web-api-6.0.jar").toFile(); @BeforeEach void setUp() throws Exception { @@ -127,6 +130,24 @@ void test_with_classpath() throws Exception { 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://java.sun.com/jsp/jstl/core\" prefix = \"c\" %>\n" + + "\n" + + "\n" + + "

Hello World!

\n" + + "what-if\n" + + "\n" + + ""); + Collection 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")); @@ -144,7 +165,9 @@ void test_exception_handling() throws Exception { SensorContextTester ctx = jspContext("<%="); Collection inputFiles = new Jasper().generateFiles(ctx, emptyList()); assertThat(inputFiles).isEmpty(); - assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Error transpiling src/main/webapp/WEB-INF/jsp/test.jsp"); + 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."); } @@ -235,7 +258,10 @@ void test_failing_tag_compilation() throws Exception { stream().collect(Collectors.toMap(GeneratedFile::filename, f -> f)); assertThat(generatedFiles).isEmpty(); - assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Error transpiling src/main/webapp/WEB-INF/jsp/test.jsp"); + 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:"))); + } private SensorContextTester jspContext(String jspSource) throws IOException {