Issue #6287 - improve testing & changes from review

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2021-05-19 11:22:33 +10:00
parent d7c42bb49a
commit 779cf4ccbc
4 changed files with 521 additions and 12 deletions

View File

@ -76,7 +76,7 @@ public class WebSocketCoreClient extends ContainerLifeCycle
public void setClassLoader(ClassLoader classLoader)
{
this.classLoader = classLoader;
this.classLoader = Objects.requireNonNull(classLoader);
}
public CompletableFuture<CoreSession> connect(FrameHandler frameHandler, URI wsUri) throws IOException

View File

@ -49,8 +49,9 @@ import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class ClientClassLoaderTest
public class JavaxClientClassLoaderTest
{
private WSServer server;
private HttpClient httpClient;
@ -85,7 +86,7 @@ public class ClientClassLoaderTest
@OnOpen
public void onOpen(Session session)
{
session.getAsyncRemote().sendText("ClassLoader: " + Thread.currentThread().getContextClassLoader());
session.getAsyncRemote().sendText("ContextClassLoader: " + Thread.currentThread().getContextClassLoader());
}
@OnMessage
@ -98,23 +99,21 @@ public class ClientClassLoaderTest
@WebServlet("/servlet")
public static class WebSocketClientServlet extends HttpServlet
{
private WebSocketContainer clientContainer;
private final WebSocketContainer clientContainer = ContainerProvider.getWebSocketContainer();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
{
clientContainer = ContainerProvider.getWebSocketContainer();
URI wsEchoUri = URI.create("ws://localhost:" + req.getServerPort() + "/echo/");
ClientSocket clientSocket = new ClientSocket();
try (Session ignored = clientContainer.connectToServer(clientSocket, wsEchoUri))
{
String recv = clientSocket.textMessages.poll(5, TimeUnit.SECONDS);
assertThat(recv, containsString("ClassLoader: WebAppClassLoader"));
assertNotNull(recv);
resp.setStatus(HttpStatus.OK_200);
resp.getWriter().write("test complete");
resp.getWriter().println(recv);
resp.getWriter().println("ClientClassLoader: " + clientContainer.getClass().getClassLoader());
}
catch (Exception e)
{
@ -178,8 +177,13 @@ public class ClientClassLoaderTest
// After hitting each WebApp we will get 200 response if test succeeds.
ContentResponse response = httpClient.GET(server.getServerUri().resolve("/app/servlet"));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContentAsString(), containsString("test complete"));
}
// The ContextClassLoader in the WebSocketClients onOpen was the WebAppClassloader.
assertThat(response.getContentAsString(), containsString("ContextClassLoader: WebAppClassLoader"));
// Verify that we used Servers version of WebSocketClient.
ClassLoader serverClassLoader = server.getServer().getClass().getClassLoader();
assertThat(response.getContentAsString(), containsString("ClientClassLoader: " + serverClassLoader)); }
@Test
public void websocketProvidedByWebApp() throws Exception
@ -203,6 +207,11 @@ public class ClientClassLoaderTest
// After hitting each WebApp we will get 200 response if test succeeds.
ContentResponse response = httpClient.GET(server.getServerUri().resolve("/app/servlet"));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
assertThat(response.getContentAsString(), containsString("test complete"));
// The ContextClassLoader in the WebSocketClients onOpen was the WebAppClassloader.
assertThat(response.getContentAsString(), containsString("ContextClassLoader: WebAppClassLoader"));
// Verify that we used WebApps version of WebSocketClient.
assertThat(response.getContentAsString(), containsString("ClientClassLoader: WebAppClassLoader"));
}
}

View File

@ -0,0 +1,238 @@
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.tests;
import java.net.URI;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.client.api.Response;
import org.eclipse.jetty.http.BadMessageException;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.websocket.api.Session;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketConnect;
import org.eclipse.jetty.websocket.api.annotations.OnWebSocketMessage;
import org.eclipse.jetty.websocket.api.annotations.WebSocket;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.eclipse.jetty.websocket.client.config.JettyWebSocketClientConfiguration;
import org.eclipse.jetty.websocket.common.WebSocketSession;
import org.eclipse.jetty.websocket.core.WebSocketComponents;
import org.eclipse.jetty.websocket.core.client.CoreClientUpgradeRequest;
import org.eclipse.jetty.websocket.server.JettyWebSocketServlet;
import org.eclipse.jetty.websocket.server.JettyWebSocketServletFactory;
import org.eclipse.jetty.websocket.server.config.JettyWebSocketConfiguration;
import org.eclipse.jetty.xml.XmlConfiguration;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class JettyClientClassLoaderTest
{
private final WebAppTester webAppTester = new WebAppTester();
private final HttpClient httpClient = new HttpClient();
@FunctionalInterface
interface ThrowingRunnable
{
void run() throws Exception;
}
public void start(ThrowingRunnable configuration) throws Exception
{
configuration.run();
webAppTester.start();
httpClient.start();
}
@AfterEach
public void after() throws Exception
{
httpClient.stop();
webAppTester.stop();
}
@WebSocket
public static class ClientSocket
{
LinkedBlockingQueue<String> textMessages = new LinkedBlockingQueue<>();
@OnWebSocketConnect
public void onOpen(Session session) throws Exception
{
session.getRemote().sendString("ContextClassLoader: " + Thread.currentThread().getContextClassLoader());
}
@OnWebSocketMessage
public void onMessage(String message)
{
textMessages.add(message);
}
}
@WebServlet("/servlet")
public static class WebSocketClientServlet extends HttpServlet
{
private final WebSocketClient clientContainer = new WebSocketClient();
@Override
public void init(ServletConfig config) throws ServletException
{
try
{
clientContainer.start();
}
catch (Exception e)
{
throw new ServletException(e);
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
{
URI wsEchoUri = URI.create("ws://localhost:" + req.getServerPort() + "/echo");
ClientSocket clientSocket = new ClientSocket();
try (Session ignored = clientContainer.connect(clientSocket, wsEchoUri).get(5, TimeUnit.SECONDS))
{
String recv = clientSocket.textMessages.poll(5, TimeUnit.SECONDS);
assertNotNull(recv);
resp.setStatus(HttpStatus.OK_200);
resp.getWriter().println(recv);
resp.getWriter().println("ClientClassLoader: " + clientContainer.getClass().getClassLoader());
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
}
@WebServlet("/")
public static class EchoServlet extends JettyWebSocketServlet
{
@Override
protected void configure(JettyWebSocketServletFactory factory)
{
factory.register(EchoSocket.class);
}
}
@WebSocket
public static class EchoSocket
{
@OnWebSocketMessage
public void onMessage(Session session, String message) throws Exception
{
session.getRemote().sendString(message);
}
}
public WebAppTester.WebApp createWebSocketWebapp(String contextName) throws Exception
{
WebAppTester.WebApp app = webAppTester.createWebApp(contextName);
// Copy over the individual jars required for Javax WebSocket.
app.createWebInf();
app.copyLib(WebSocketPolicy.class, "websocket-jetty-api.jar");
app.copyLib(WebSocketClient.class, "websocket-jetty-client.jar");
app.copyLib(WebSocketSession.class, "websocket-jetty-common.jar");
app.copyLib(ContainerLifeCycle.class, "jetty-util.jar");
app.copyLib(CoreClientUpgradeRequest.class, "websocket-core-client.jar");
app.copyLib(WebSocketComponents.class, "websocket-core-common.jar");
app.copyLib(Response.class, "jetty-client.jar");
app.copyLib(ByteBufferPool.class, "jetty-io.jar");
app.copyLib(BadMessageException.class, "jetty-http.jar");
app.copyLib(XmlConfiguration.class, "jetty-xml.jar");
return app;
}
@Test
public void websocketProvidedByServer() throws Exception
{
start(() ->
{
WebAppTester.WebApp app1 = webAppTester.createWebApp("/app");
app1.addConfiguration(new JettyWebSocketClientConfiguration());
app1.createWebInf();
app1.copyClass(WebSocketClientServlet.class);
app1.copyClass(ClientSocket.class);
app1.deploy();
WebAppTester.WebApp app2 = webAppTester.createWebApp("/echo");
app2.addConfiguration(new JettyWebSocketConfiguration());
app2.createWebInf();
app2.copyClass(EchoServlet.class);
app2.copyClass(EchoSocket.class);
app2.deploy();
});
// After hitting each WebApp we will get 200 response if test succeeds.
ContentResponse response = httpClient.GET(webAppTester.getServerUri().resolve("/app/servlet"));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
// The ContextClassLoader in the WebSocketClients onOpen was the WebAppClassloader.
assertThat(response.getContentAsString(), containsString("ContextClassLoader: WebAppClassLoader"));
// Verify that we used Servers version of WebSocketClient.
ClassLoader serverClassLoader = webAppTester.getServer().getClass().getClassLoader();
assertThat(response.getContentAsString(), containsString("ClientClassLoader: " + serverClassLoader));
}
@Test
public void websocketProvidedByWebApp() throws Exception
{
start(() ->
{
WebAppTester.WebApp app1 = createWebSocketWebapp("/app");
app1.createWebInf();
app1.copyClass(WebSocketClientServlet.class);
app1.copyClass(ClientSocket.class);
app1.deploy();
WebAppTester.WebApp app2 = webAppTester.createWebApp("/echo");
app2.addConfiguration(new JettyWebSocketConfiguration());
app2.createWebInf();
app2.copyClass(EchoServlet.class);
app2.copyClass(EchoSocket.class);
app2.deploy();
});
// After hitting each WebApp we will get 200 response if test succeeds.
ContentResponse response = httpClient.GET(webAppTester.getServerUri().resolve("/app/servlet"));
assertThat(response.getStatus(), is(HttpStatus.OK_200));
// The ContextClassLoader in the WebSocketClients onOpen was the WebAppClassloader.
assertThat(response.getContentAsString(), containsString("ContextClassLoader: WebAppClassLoader"));
// Verify that we used WebApps version of WebSocketClient.
assertThat(response.getContentAsString(), containsString("ClientClassLoader: WebAppClassLoader"));
}
}

View File

@ -0,0 +1,262 @@
//
// ========================================================================
// Copyright (c) 1995-2021 Mort Bay Consulting Pty Ltd and others.
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License v. 2.0 which is available at
// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
// ========================================================================
//
package org.eclipse.jetty.websocket.tests;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import org.eclipse.jetty.annotations.AnnotationConfiguration;
import org.eclipse.jetty.plus.webapp.EnvConfiguration;
import org.eclipse.jetty.plus.webapp.PlusConfiguration;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.ServerConnector;
import org.eclipse.jetty.server.handler.ContextHandlerCollection;
import org.eclipse.jetty.toolchain.test.FS;
import org.eclipse.jetty.toolchain.test.IO;
import org.eclipse.jetty.toolchain.test.JAR;
import org.eclipse.jetty.toolchain.test.MavenTestingUtils;
import org.eclipse.jetty.util.TypeUtil;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
import org.eclipse.jetty.util.resource.PathResource;
import org.eclipse.jetty.webapp.Configuration;
import org.eclipse.jetty.webapp.FragmentConfiguration;
import org.eclipse.jetty.webapp.JettyWebXmlConfiguration;
import org.eclipse.jetty.webapp.JmxConfiguration;
import org.eclipse.jetty.webapp.JndiConfiguration;
import org.eclipse.jetty.webapp.MetaInfConfiguration;
import org.eclipse.jetty.webapp.WebAppConfiguration;
import org.eclipse.jetty.webapp.WebAppContext;
import org.eclipse.jetty.webapp.WebInfConfiguration;
import org.eclipse.jetty.webapp.WebXmlConfiguration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
/**
* Utility to build out exploded directory WebApps.
*/
public class WebAppTester extends ContainerLifeCycle
{
private static final Logger LOG = LoggerFactory.getLogger(WebAppTester.class);
private final Path _testDir;
private final Server _server;
private final ServerConnector _serverConnector;
private final ContextHandlerCollection _contexts;
private Configuration[] excludedConfiguration;
public WebAppTester()
{
this(null);
}
public WebAppTester(Path testDir)
{
if (testDir == null)
{
try
{
Path targetTestingPath = MavenTestingUtils.getTargetTestingPath();
FS.ensureDirExists(targetTestingPath);
_testDir = Files.createTempDirectory(targetTestingPath, "contexts");
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
else
{
_testDir = testDir;
FS.ensureDirExists(_testDir);
}
_server = new Server();
_serverConnector = new ServerConnector(_server);
_server.addConnector(_serverConnector);
_contexts = new ContextHandlerCollection();
_server.setHandler(_contexts);
addBean(_server);
}
@Override
protected void doStop() throws Exception
{
// Recursively delete testDir when stopping.
org.eclipse.jetty.util.IO.delete(_testDir.toFile());
}
public Server getServer()
{
return _server;
}
public URI getServerUri()
{
if (!isStarted())
throw new IllegalStateException("Not Started");
return URI.create("http://localhost:" + getPort());
}
public int getPort()
{
return _serverConnector.getLocalPort();
}
public WebApp createWebApp(String contextPath)
{
return new WebApp(contextPath);
}
public class WebApp
{
private final WebAppContext _context;
private final Path _contextDir;
private final Path _webInf;
private final Path _classesDir;
private final Path _libDir;
private WebApp(String contextPath)
{
// Ensure context directory.
String contextDirName = contextPath.replace("/", "");
if (contextDirName.length() == 0)
contextDirName = "ROOT";
_contextDir = _testDir.resolve(contextDirName);
FS.ensureEmpty(_contextDir);
// Ensure WEB-INF directories.
_webInf = _contextDir.resolve("WEB-INF");
FS.ensureDirExists(_webInf);
_classesDir = _webInf.resolve("classes");
FS.ensureDirExists(_classesDir);
_libDir = _webInf.resolve("lib");
FS.ensureDirExists(_libDir);
// Configure the WebAppContext.
_context = new WebAppContext();
_context.setContextPath(contextPath);
_context.setBaseResource(new PathResource(_contextDir));
_context.setConfigurations(new Configuration[]
{
new JmxConfiguration(),
new WebInfConfiguration(),
new WebXmlConfiguration(),
new MetaInfConfiguration(),
new FragmentConfiguration(),
new EnvConfiguration(),
new PlusConfiguration(),
new AnnotationConfiguration(),
new JndiConfiguration(),
new WebAppConfiguration(),
new JettyWebXmlConfiguration()
});
}
public WebAppContext getWebAppContext()
{
return _context;
}
public String getContextPath()
{
return _context.getContextPath();
}
public Path getContextDir()
{
return _contextDir;
}
public void addConfiguration(Configuration... configurations)
{
_context.addConfiguration(configurations);
}
public void createWebInf() throws IOException
{
String emptyWebXml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<web-app\n" +
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" +
" xmlns=\"http://java.sun.com/xml/ns/javaee\"\n" +
" xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd\"\n" +
" metadata-complete=\"false\"\n" +
" version=\"3.0\">\n" +
"</web-app>";
File webXml = _webInf.resolve("web.xml").toFile();
try (FileWriter out = new FileWriter(webXml))
{
out.write(emptyWebXml);
}
}
public void copyWebInf(String testResourceName) throws IOException
{
File testWebXml = MavenTestingUtils.getTestResourceFile(testResourceName);
Path webXml = _webInf.resolve("web.xml");
IO.copy(testWebXml, webXml.toFile());
}
public void copyClass(Class<?> clazz) throws Exception
{
ClassLoader cl = Thread.currentThread().getContextClassLoader();
String endpointPath = TypeUtil.toClassReference(clazz);
URL classUrl = cl.getResource(endpointPath);
assertThat("Class URL for: " + clazz, classUrl, notNullValue());
Path destFile = _classesDir.resolve(endpointPath);
FS.ensureDirExists(destFile.getParent());
File srcFile = new File(classUrl.toURI());
IO.copy(srcFile, destFile.toFile());
}
public void copyLib(Class<?> clazz, String jarFileName) throws URISyntaxException, IOException
{
Path jarFile = _libDir.resolve(jarFileName);
URL codeSourceURL = clazz.getProtectionDomain().getCodeSource().getLocation();
assertThat("Class CodeSource URL is file scheme", codeSourceURL.getProtocol(), is("file"));
File sourceCodeSourceFile = new File(codeSourceURL.toURI());
if (sourceCodeSourceFile.isDirectory())
{
LOG.info("Creating " + jarFile + " from " + sourceCodeSourceFile);
JAR.create(sourceCodeSourceFile, jarFile.toFile());
}
else
{
LOG.info("Copying " + sourceCodeSourceFile + " to " + jarFile);
IO.copy(sourceCodeSourceFile, jarFile.toFile());
}
}
public void deploy()
{
_contexts.addHandler(_context);
_contexts.manage(_context);
_context.setThrowUnavailableOnStartupException(true);
if (LOG.isDebugEnabled())
LOG.debug("{}", _context.dump());
}
}
}