| 1 |
// |
| 2 |
// ======================================================================== |
| 3 |
// Copyright (c) 1995 Mort Bay Consulting Pty Ltd and others. |
| 4 |
// |
| 5 |
// This program and the accompanying materials are made available under the |
| 6 |
// terms of the Eclipse Public License v. 2.0 which is available at |
| 7 |
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 8 |
// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 |
// |
| 10 |
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 11 |
// ======================================================================== |
| 12 |
// |
| 13 |
|
| 14 |
package org.eclipse.jetty.ee10.jstl; |
| 15 |
|
| 16 |
import java.io.File; |
| 17 |
import java.io.IOException; |
| 18 |
import java.io.InputStream; |
| 19 |
import java.net.HttpURLConnection; |
| 20 |
import java.net.URI; |
| 21 |
import java.nio.charset.StandardCharsets; |
| 22 |
|
| 23 |
import jakarta.servlet.jsp.JspException; |
| 24 |
import org.eclipse.jetty.ee10.annotations.AnnotationConfiguration; |
| 25 |
import org.eclipse.jetty.ee10.webapp.WebAppContext; |
| 26 |
import org.eclipse.jetty.server.Server; |
| 27 |
import org.eclipse.jetty.server.ServerConnector; |
| 28 |
import org.eclipse.jetty.toolchain.test.FS; |
| 29 |
import org.eclipse.jetty.toolchain.test.JAR; |
| 30 |
import org.eclipse.jetty.toolchain.test.MavenTestingUtils; |
| 31 |
import org.eclipse.jetty.util.IO; |
| 32 |
import org.junit.jupiter.api.AfterAll; |
| 33 |
import org.junit.jupiter.api.BeforeAll; |
| 34 |
import org.junit.jupiter.api.Test; |
| 35 |
|
| 36 |
import static org.hamcrest.MatcherAssert.assertThat; |
| 37 |
import static org.hamcrest.Matchers.containsString; |
| 38 |
import static org.hamcrest.Matchers.is; |
| 39 |
import static org.hamcrest.Matchers.not; |
| 40 |
|
| 41 |
public class JstlTest |
| 42 |
{ |
| 43 |
private static Server server; |
| 44 |
private static URI baseUri; |
| 45 |
|
| 46 |
@BeforeAll |
| 47 |
public static void startServer() throws Exception |
| 48 |
{ |
| 49 |
// Setup Server |
| 50 |
server = new Server(); |
| 51 |
ServerConnector connector = new ServerConnector(server); |
| 52 |
connector.setPort(0); |
| 53 |
server.addConnector(connector); |
| 54 |
|
| 55 |
//Base dir for test |
| 56 |
File testDir = MavenTestingUtils.getTargetTestingDir("jstl"); |
| 57 |
File testLibDir = new File(testDir, "WEB-INF/lib"); |
| 58 |
FS.ensureDirExists(testLibDir); |
| 59 |
|
| 60 |
//Make a taglib jar |
| 61 |
File srcTagLibDir = MavenTestingUtils.getProjectDir("src/test/taglibjar"); |
| 62 |
File scratchTagLibDir = MavenTestingUtils.getTargetFile("tests/" + JstlTest.class.getSimpleName() + "-taglib-scratch"); |
| 63 |
IO.copy(srcTagLibDir, scratchTagLibDir); |
| 64 |
File tagLibJar = new File(testLibDir, "testtaglib.jar"); |
| 65 |
JAR.create(scratchTagLibDir, tagLibJar); |
| 66 |
|
| 67 |
//Copy content |
| 68 |
File srcWebAppDir = MavenTestingUtils.getProjectDir("src/test/webapp"); |
| 69 |
IO.copyDir(srcWebAppDir, testDir); |
| 70 |
|
| 71 |
// Configure WebAppCont |
| 72 |
WebAppContext context = new WebAppContext(); |
| 73 |
context.setContextPath("/"); |
| 74 |
|
| 75 |
File scratchDir = MavenTestingUtils.getTargetFile("tests/" + JstlTest.class.getSimpleName() + "-scratch"); |
| 76 |
FS.ensureEmpty(scratchDir); |
| 77 |
JspConfig.init(context, testDir.toURI(), scratchDir); |
| 78 |
|
| 79 |
context.addConfiguration(new AnnotationConfiguration()); |
| 80 |
|
| 81 |
server.setHandler(context); |
| 82 |
|
| 83 |
// Start Server |
| 84 |
server.start(); |
| 85 |
|
| 86 |
// Figure out Base URI |
| 87 |
String host = connector.getHost(); |
| 88 |
if (host == null) |
| 89 |
{ |
| 90 |
host = "localhost"; |
| 91 |
} |
| 92 |
int port = connector.getLocalPort(); |
| 93 |
baseUri = new URI(String.format("http://%s:%d/", host, port)); |
| 94 |
} |
| 95 |
|
| 96 |
@AfterAll |
| 97 |
public static void stopServer() throws Exception |
| 98 |
{ |
| 99 |
if (server != null) |
| 100 |
server.stop(); |
| 101 |
} |
| 102 |
|
| 103 |
@Test |
| 104 |
public void testUrlsBasic() throws IOException |
| 105 |
{ |
| 106 |
HttpURLConnection http = (HttpURLConnection)baseUri.resolve("/urls.jsp").toURL().openConnection(); |
| 107 |
assertThat("http response", http.getResponseCode(), is(200)); |
| 108 |
try (InputStream input = http.getInputStream()) |
| 109 |
{ |
| 110 |
String resp = IO.toString(input, StandardCharsets.UTF_8); |
| 111 |
assertThat("Response should be JSP processed", resp, not(containsString("<c:url"))); |
| 112 |
assertThat("Response", resp, containsString("[c:url value] = /ref.jsp;jsessionid=")); |
| 113 |
assertThat("Response", resp, containsString("[c:url param] = ref.jsp;key=value;jsessionid=")); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
@Test |
| 118 |
public void testCatchBasic() throws IOException |
| 119 |
{ |
| 120 |
HttpURLConnection http = (HttpURLConnection)baseUri.resolve("/catch-basic.jsp").toURL().openConnection(); |
| 121 |
assertThat("http response", http.getResponseCode(), is(200)); |
| 122 |
try (InputStream input = http.getInputStream()) |
| 123 |
{ |
| 124 |
String resp = IO.toString(input, StandardCharsets.UTF_8); |
| 125 |
assertThat("Response should be JSP processed", resp, not(containsString("<c:catch"))); |
| 126 |
assertThat("Response", resp, containsString("[c:catch] exception : " + JspException.class.getName())); |
| 127 |
assertThat("Response", resp, containsString("[c:catch] exception.message : In <parseNumber>")); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
@Test |
| 132 |
public void testCatchTaglib() throws IOException |
| 133 |
{ |
| 134 |
HttpURLConnection http = (HttpURLConnection)baseUri.resolve("/catch-taglib.jsp").toURL().openConnection(); |
| 135 |
assertThat("http response", http.getResponseCode(), is(200)); |
| 136 |
try (InputStream input = http.getInputStream()) |
| 137 |
{ |
| 138 |
String resp = IO.toString(input, StandardCharsets.UTF_8); |
| 139 |
assertThat("Response should be JSP processed", resp, not(containsString("<c:catch>"))); |
| 140 |
assertThat("Response should be JSP processed", resp, not(containsString("<jtest:errorhandler>"))); |
| 141 |
assertThat("Response", resp, not(containsString("[jtest:errorhandler] exception is null"))); |
| 142 |
} |
| 143 |
} |
| 144 |
} |