From 00843c8ed90c4ad5e62aa28e71ef9a091cab0ee1 Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 12 Sep 2018 14:35:01 -0500 Subject: [PATCH 1/9] Issue #2901 - Moving Http Upgrader to Http Conversation + Solves for Authentication and Redirect Signed-off-by: Joakim Erdfelt --- .../main/java/org/eclipse/jetty/client/HttpRequest.java | 2 +- .../eclipse/jetty/client/http/HttpChannelOverHTTP.java | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java index 7ae3fbb56d2..e2ff05cd6b8 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/HttpRequest.java @@ -108,7 +108,7 @@ public class HttpRequest implements Request headers.put(userAgentField); } - protected HttpConversation getConversation() + public HttpConversation getConversation() { return conversation; } diff --git a/jetty-client/src/main/java/org/eclipse/jetty/client/http/HttpChannelOverHTTP.java b/jetty-client/src/main/java/org/eclipse/jetty/client/http/HttpChannelOverHTTP.java index a73a88ca5a1..cec9cd9110f 100644 --- a/jetty-client/src/main/java/org/eclipse/jetty/client/http/HttpChannelOverHTTP.java +++ b/jetty-client/src/main/java/org/eclipse/jetty/client/http/HttpChannelOverHTTP.java @@ -110,16 +110,16 @@ public class HttpChannelOverHTTP extends HttpChannel // Upgrade Response HttpRequest request = exchange.getRequest(); - if (request instanceof HttpConnectionUpgrader) + HttpConnectionUpgrader upgrader = (HttpConnectionUpgrader) request.getConversation().getAttribute(HttpConnectionUpgrader.class.getName()); + if (upgrader != null) { - HttpConnectionUpgrader listener = (HttpConnectionUpgrader)request; try { - listener.upgrade(response,getHttpConnection()); + upgrader.upgrade(response, getHttpConnection()); } catch (Throwable x) { - return new Result(result,x); + return new Result(result, x); } } } From ae169aa6244ad1afc8c4665a839f9f4b1c9e6e6b Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Wed, 12 Sep 2018 14:40:03 -0500 Subject: [PATCH 2/9] Fixing #2875 - WebSocketClient upgrade with redirect support + Using HttpClient fixes from #2901 to allow upgrader to be held for entire conversation regardless of authentication or redirect behaviors. Signed-off-by: Joakim Erdfelt --- .../client/WebSocketUpgradeRequest.java | 6 +- .../websocket/common/WebSocketSession.java | 2 +- .../common/test/BlockheadClientRequest.java | 1 + .../server/RedirectWebSocketClientTest.java | 155 ++++++++++++++++++ 4 files changed, 161 insertions(+), 3 deletions(-) create mode 100644 jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/RedirectWebSocketClientTest.java diff --git a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketUpgradeRequest.java b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketUpgradeRequest.java index 97fe0d446c3..143944f78db 100644 --- a/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketUpgradeRequest.java +++ b/jetty-websocket/websocket-client/src/main/java/org/eclipse/jetty/websocket/client/WebSocketUpgradeRequest.java @@ -415,6 +415,8 @@ public class WebSocketUpgradeRequest extends HttpRequest implements CompleteList this.localEndpoint = this.wsClient.getEventDriverFactory().wrap(localEndpoint); this.fut = new CompletableFuture(); + + getConversation().setAttribute(HttpConnectionUpgrader.class.getName(), this); } private final String genRandomKey() @@ -504,7 +506,7 @@ public class WebSocketUpgradeRequest extends HttpRequest implements CompleteList } Throwable failure = result.getFailure(); - if ((failure instanceof java.net.ConnectException) || (failure instanceof UpgradeException)) + if ((failure instanceof java.io.IOException) || (failure instanceof UpgradeException)) { // handle as-is handleException(failure); @@ -519,7 +521,7 @@ public class WebSocketUpgradeRequest extends HttpRequest implements CompleteList if (responseStatusCode != HttpStatus.SWITCHING_PROTOCOLS_101) { // Failed to upgrade (other reason) - handleException(new UpgradeException(requestURI,responseStatusCode,responseLine)); + handleException(new UpgradeException(requestURI,responseStatusCode,"Failed to upgrade to websocket: Unexpected HTTP Response Status Code: " + responseLine)); } } diff --git a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java index da6c237152f..ee4c7f8b92e 100644 --- a/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java +++ b/jetty-websocket/websocket-common/src/main/java/org/eclipse/jetty/websocket/common/WebSocketSession.java @@ -620,7 +620,7 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Rem if (LOG.isDebugEnabled()) { - LOG.debug("open -> {}",dump()); + LOG.debug("[{}] open -> {}",getPolicy().getBehavior(),dump()); } if(openFuture != null) diff --git a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/BlockheadClientRequest.java b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/BlockheadClientRequest.java index 2f81c5bc2ab..c082ea7c05a 100644 --- a/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/BlockheadClientRequest.java +++ b/jetty-websocket/websocket-common/src/test/java/org/eclipse/jetty/websocket/common/test/BlockheadClientRequest.java @@ -64,6 +64,7 @@ public class BlockheadClientRequest extends HttpRequest implements Response.Comp super(client, new HttpConversation(), uri); this.client = client; this.fut = new CompletableFuture<>(); + getConversation().setAttribute(HttpConnectionUpgrader.class.getName(), this); } public void setInitialBytes(ByteBuffer initialBytes) diff --git a/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/RedirectWebSocketClientTest.java b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/RedirectWebSocketClientTest.java new file mode 100644 index 00000000000..e4d6ccbd0c4 --- /dev/null +++ b/jetty-websocket/websocket-server/src/test/java/org/eclipse/jetty/websocket/server/RedirectWebSocketClientTest.java @@ -0,0 +1,155 @@ +// +// ======================================================================== +// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.websocket.server; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.notNullValue; + +import java.net.URI; +import java.util.concurrent.Future; + +import org.eclipse.jetty.client.HttpClient; +import org.eclipse.jetty.http.HttpVersion; +import org.eclipse.jetty.server.HttpConfiguration; +import org.eclipse.jetty.server.HttpConnectionFactory; +import org.eclipse.jetty.server.SecureRequestCustomizer; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.ServerConnector; +import org.eclipse.jetty.server.SslConnectionFactory; +import org.eclipse.jetty.server.handler.DefaultHandler; +import org.eclipse.jetty.server.handler.HandlerList; +import org.eclipse.jetty.server.handler.SecuredRedirectHandler; +import org.eclipse.jetty.servlet.ServletContextHandler; +import org.eclipse.jetty.toolchain.test.MavenTestingUtils; +import org.eclipse.jetty.util.ssl.SslContextFactory; +import org.eclipse.jetty.websocket.api.Session; +import org.eclipse.jetty.websocket.api.annotations.WebSocket; +import org.eclipse.jetty.websocket.client.ClientUpgradeRequest; +import org.eclipse.jetty.websocket.client.WebSocketClient; +import org.eclipse.jetty.websocket.server.helper.EchoServlet; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +public class RedirectWebSocketClientTest +{ + public static Server server; + public static URI serverWsUri; + public static URI serverWssUri; + + @BeforeAll + public static void startServer() throws Exception + { + server = new Server(); + + HttpConfiguration http_config = new HttpConfiguration(); + http_config.setSecureScheme("https"); + http_config.setSecurePort(0); + http_config.setOutputBufferSize(32768); + http_config.setRequestHeaderSize(8192); + http_config.setResponseHeaderSize(8192); + http_config.setSendServerVersion(true); + http_config.setSendDateHeader(false); + + SslContextFactory sslContextFactory = newSslContextFactory(); + + // SSL HTTP Configuration + HttpConfiguration https_config = new HttpConfiguration(http_config); + https_config.addCustomizer(new SecureRequestCustomizer()); + + // SSL Connector + ServerConnector sslConnector = new ServerConnector(server, new SslConnectionFactory(sslContextFactory, HttpVersion.HTTP_1_1.asString()), new HttpConnectionFactory(https_config)); + sslConnector.setPort(0); + server.addConnector(sslConnector); + + // Normal Connector + ServerConnector connector = new ServerConnector(server); + connector.setPort(0); + server.addConnector(connector); + + ServletContextHandler contextHandler = new ServletContextHandler(); + contextHandler.setContextPath("/"); + contextHandler.addServlet(EchoServlet.class, "/echo"); + + HandlerList handlers = new HandlerList(); + + handlers.addHandler(new SecuredRedirectHandler()); + handlers.addHandler(contextHandler); + handlers.addHandler(new DefaultHandler()); + + server.setHandler(handlers); + + server.start(); + + serverWsUri = URI.create("ws://localhost:" + connector.getLocalPort() + "/"); + serverWssUri = URI.create("wss://localhost:" + sslConnector.getLocalPort() + "/"); + + // adjust HttpConfiguration in connector + HttpConnectionFactory connectionFactory = connector.getConnectionFactory(HttpConnectionFactory.class); + connectionFactory.getHttpConfiguration().setSecurePort(sslConnector.getLocalPort()); + } + + @AfterAll + public static void stopServer() throws Exception + { + server.stop(); + } + + private static SslContextFactory newSslContextFactory() + { + SslContextFactory ssl = new SslContextFactory(); + ssl.setKeyStorePath(MavenTestingUtils.getTestResourceFile("keystore").getAbsolutePath()); + ssl.setKeyStorePassword("storepwd"); + ssl.setKeyManagerPassword("keypwd"); + return ssl; + } + + @Test + public void testRedirect() throws Exception + { + SslContextFactory ssl = newSslContextFactory(); + ssl.setTrustAll(false); + ssl.setEndpointIdentificationAlgorithm(null); + HttpClient httpClient = new HttpClient(ssl); + + WebSocketClient client = new WebSocketClient(httpClient); + client.addBean(httpClient, true); + client.start(); + + try + { + URI wsUri = serverWsUri.resolve("/echo"); + + ClientUpgradeRequest request = new ClientUpgradeRequest(); + Future sessionFuture = client.connect(new EmptyWebSocket(), wsUri, request); + Session session = sessionFuture.get(); + assertThat(session, is(notNullValue())); + } + finally + { + client.stop(); + } + } + + @WebSocket + public static class EmptyWebSocket { + + } +} From 3384ff90ea45ba7a42bf122ffe82b9e04c1e2785 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Wed, 19 Sep 2018 12:46:39 +0200 Subject: [PATCH 3/9] Fixes #2913 - ClassNotFoundException: sun.reflect.Reflection with JDK 11 Replaced usage of sun.reflect.Reflection with a SecurityManager subclass, so that it works in all JDKs. Signed-off-by: Simone Bordet --- .../jetty/server/handler/ContextHandler.java | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java index d3482ef0af2..d547539d906 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java @@ -21,7 +21,6 @@ package org.eclipse.jetty.server.handler; import java.io.File; import java.io.IOException; import java.io.InputStream; -import java.lang.reflect.Method; import java.net.MalformedURLException; import java.net.URI; import java.net.URL; @@ -2585,19 +2584,18 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu // no security manager just return the classloader if (!_usingSecurityManager) + { return _classLoader; + } else { // check to see if the classloader of the caller is the same as the context - // classloader, or a parent of it + // classloader, or a parent of it, as required by the javadoc specification. try { - Class reflect = Loader.loadClass("sun.reflect.Reflection"); - Method getCallerClass = reflect.getMethod("getCallerClass",Integer.TYPE); - Class caller = (Class)getCallerClass.invoke(null,2); - + Class caller = new Caller().getCallerClass(2); boolean ok = false; - ClassLoader callerLoader = caller.getClassLoader(); + ClassLoader callerLoader = caller == null ? null : caller.getClassLoader(); while (!ok && callerLoader != null) { if (callerLoader == _classLoader) @@ -3083,4 +3081,17 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu */ void exitScope(Context context, Request request); } + + private static class Caller extends SecurityManager + { + public Class getCallerClass(int depth) + { + if (depth < 0) + return null; + Class[] classContext = getClassContext(); + if (classContext.length <= depth) + return null; + return classContext[depth]; + } + } } From 9a9a52b9609e7753e1d8f773b5e0cdf502a2ed56 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Wed, 19 Sep 2018 13:44:12 +0200 Subject: [PATCH 4/9] Fixes #2913 - ClassNotFoundException: sun.reflect.Reflection with JDK 11 Updated after review. Signed-off-by: Simone Bordet --- .../jetty/server/handler/ContextHandler.java | 31 +++++++++---------- 1 file changed, 14 insertions(+), 17 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java index d547539d906..c24540e9564 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java @@ -26,6 +26,7 @@ import java.net.URI; import java.net.URL; import java.net.URLClassLoader; import java.security.AccessController; +import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -2591,27 +2592,23 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu { // check to see if the classloader of the caller is the same as the context // classloader, or a parent of it, as required by the javadoc specification. - try - { - Class caller = new Caller().getCallerClass(2); - boolean ok = false; - ClassLoader callerLoader = caller == null ? null : caller.getClassLoader(); - while (!ok && callerLoader != null) - { - if (callerLoader == _classLoader) - ok = true; - else - callerLoader = callerLoader.getParent(); - } - if (ok) - return _classLoader; - } - catch (Exception e) + // Wrap so that only Jetty code requires the "createSecurityManager" permission. + Caller caller = AccessController.doPrivileged((PrivilegedAction)Caller::new); + Class callerClass = caller.getCallerClass(2); + boolean ok = false; + ClassLoader callerLoader = callerClass == null ? null : callerClass.getClassLoader(); + while (!ok && callerLoader != null) { - LOG.warn("Unable to check classloader of caller",e); + if (callerLoader == _classLoader) + ok = true; + else + callerLoader = callerLoader.getParent(); } + if (ok) + return _classLoader; + AccessController.checkPermission(new RuntimePermission("getClassLoader")); return _classLoader; } From bae502681c57d797818816a834486745f25e3a28 Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Wed, 19 Sep 2018 13:50:33 +0200 Subject: [PATCH 5/9] Fixes #2913 - ClassNotFoundException: sun.reflect.Reflection with JDK 11 Another update after review. Signed-off-by: Simone Bordet --- .../org/eclipse/jetty/server/handler/ContextHandler.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java index c24540e9564..c201e7df17c 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java @@ -2594,10 +2594,9 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu // classloader, or a parent of it, as required by the javadoc specification. // Wrap so that only Jetty code requires the "createSecurityManager" permission. - Caller caller = AccessController.doPrivileged((PrivilegedAction)Caller::new); - Class callerClass = caller.getCallerClass(2); boolean ok = false; - ClassLoader callerLoader = callerClass == null ? null : callerClass.getClassLoader(); + Caller caller = AccessController.doPrivileged((PrivilegedAction)Caller::new); + ClassLoader callerLoader = caller.getCallerClassLoader(2); while (!ok && callerLoader != null) { if (callerLoader == _classLoader) @@ -3081,14 +3080,14 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu private static class Caller extends SecurityManager { - public Class getCallerClass(int depth) + public ClassLoader getCallerClassLoader(int depth) { if (depth < 0) return null; Class[] classContext = getClassContext(); if (classContext.length <= depth) return null; - return classContext[depth]; + return classContext[depth].getClassLoader(); } } } From 3a8a20557dee6dee5d517cfa35bd9b7ed308d70d Mon Sep 17 00:00:00 2001 From: Simone Bordet Date: Wed, 19 Sep 2018 17:07:49 +0200 Subject: [PATCH 6/9] Fixes #2913 - ClassNotFoundException: sun.reflect.Reflection with JDK 11 Another update after review. Signed-off-by: Simone Bordet --- .../eclipse/jetty/server/handler/ContextHandler.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java index c201e7df17c..e3fa2e32d81 100644 --- a/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java +++ b/jetty-server/src/main/java/org/eclipse/jetty/server/handler/ContextHandler.java @@ -2593,21 +2593,17 @@ public class ContextHandler extends ScopedHandler implements Attributes, Gracefu // check to see if the classloader of the caller is the same as the context // classloader, or a parent of it, as required by the javadoc specification. - // Wrap so that only Jetty code requires the "createSecurityManager" permission. - boolean ok = false; + // Wrap in a PrivilegedAction so that only Jetty code will require the + // "createSecurityManager" permission, not also application code that calls this method. Caller caller = AccessController.doPrivileged((PrivilegedAction)Caller::new); ClassLoader callerLoader = caller.getCallerClassLoader(2); - while (!ok && callerLoader != null) + while (callerLoader != null) { if (callerLoader == _classLoader) - ok = true; + return _classLoader; else callerLoader = callerLoader.getParent(); } - - if (ok) - return _classLoader; - AccessController.checkPermission(new RuntimePermission("getClassLoader")); return _classLoader; } From f340b404a8d9849a4aeb72f04903f57d50b812ee Mon Sep 17 00:00:00 2001 From: Olivier Lamy Date: Thu, 20 Sep 2018 15:59:42 +1000 Subject: [PATCH 7/9] get rid of maven warning due to duplicate plugin declaration (#2920) Signed-off-by: olivier lamy --- jetty-distribution/pom.xml | 132 ++++++++++++++++++------------------- 1 file changed, 64 insertions(+), 68 deletions(-) diff --git a/jetty-distribution/pom.xml b/jetty-distribution/pom.xml index c3fe2a86ae1..40286e5ab3c 100644 --- a/jetty-distribution/pom.xml +++ b/jetty-distribution/pom.xml @@ -47,6 +47,7 @@ + org.apache.maven.plugins maven-dependency-plugin + org.ops4j.pax.exam diff --git a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/CookiePatternRuleTest.java b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/CookiePatternRuleTest.java index 651fd354971..edfa3de5b24 100644 --- a/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/CookiePatternRuleTest.java +++ b/jetty-rewrite/src/test/java/org/eclipse/jetty/rewrite/handler/CookiePatternRuleTest.java @@ -42,7 +42,7 @@ import java.io.StringReader; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; -import static org.junit.Assert.fail; +import static org.junit.jupiter.api.Assertions.fail; public class CookiePatternRuleTest diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/CustomResourcesMonitorTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/CustomResourcesMonitorTest.java index 57b4a5fe9a4..0048f34fcdb 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/CustomResourcesMonitorTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/CustomResourcesMonitorTest.java @@ -27,7 +27,6 @@ import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; - import java.io.IOException; import java.io.InputStream; import java.net.Socket; @@ -38,7 +37,8 @@ import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; -import static org.junit.Assert.*; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.*; public class CustomResourcesMonitorTest { @@ -111,28 +111,14 @@ public class CustomResourcesMonitorTest InputStream input1 = socket1.getInputStream(); assertTrue(_fileOnDirectoryMonitor.isLowOnResources()); - try - { - input1.read(); - fail(); - } - catch (SocketTimeoutException expected) - { - } + assertThrows(SocketTimeoutException.class, () -> input1.read()); // Wait a couple of lowResources idleTimeouts. Thread.sleep(2 * lowResourcesIdleTimeout); // Verify the new socket is still open. assertTrue(_fileOnDirectoryMonitor.isLowOnResources()); - try - { - input1.read(); - fail(); - } - catch (SocketTimeoutException expected) - { - } + assertThrows(SocketTimeoutException.class, () -> input1.read()); Files.delete( tmpFile ); diff --git a/jetty-server/src/test/java/org/eclipse/jetty/server/LowResourcesMonitorTest.java b/jetty-server/src/test/java/org/eclipse/jetty/server/LowResourcesMonitorTest.java index 9d7bf40fcba..6cebfcdada6 100644 --- a/jetty-server/src/test/java/org/eclipse/jetty/server/LowResourcesMonitorTest.java +++ b/jetty-server/src/test/java/org/eclipse/jetty/server/LowResourcesMonitorTest.java @@ -23,6 +23,7 @@ import org.eclipse.jetty.util.thread.QueuedThreadPool; import org.eclipse.jetty.util.thread.TimerScheduler; import org.hamcrest.Matchers; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; @@ -38,7 +39,6 @@ import java.util.concurrent.CountDownLatch; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.not; -import static org.junit.Assert.fail; import static org.junit.jupiter.api.Assertions.*; public class LowResourcesMonitorTest @@ -248,28 +248,15 @@ public class LowResourcesMonitorTest InputStream input1 = socket1.getInputStream(); assertTrue(_lowResourcesMonitor.isLowOnResources()); - try - { - input1.read(); - fail(); - } - catch (SocketTimeoutException expected) - { - } + assertThrows( SocketTimeoutException.class, () -> input1.read()); // Wait a couple of lowResources idleTimeouts. Thread.sleep(2 * lowResourcesIdleTimeout); // Verify the new socket is still open. assertTrue(_lowResourcesMonitor.isLowOnResources()); - try - { - input1.read(); - fail(); - } - catch (SocketTimeoutException expected) - { - } + assertThrows( SocketTimeoutException.class, () -> input1.read()); + // Let the maxLowResourcesTime elapse. Thread.sleep(maxLowResourcesTime); diff --git a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/GzipHandlerTest.java b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/GzipHandlerTest.java index c720e0f3e15..48a39b7d2e7 100644 --- a/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/GzipHandlerTest.java +++ b/jetty-servlet/src/test/java/org/eclipse/jetty/servlet/GzipHandlerTest.java @@ -18,15 +18,26 @@ package org.eclipse.jetty.servlet; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.contains; -import static org.hamcrest.Matchers.containsString; -import static org.hamcrest.Matchers.equalToIgnoringCase; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.not; -import static org.hamcrest.Matchers.nullValue; -import static org.junit.jupiter.api.Assertions.assertEquals; +import org.eclipse.jetty.http.HttpTester; +import org.eclipse.jetty.server.LocalConnector; +import org.eclipse.jetty.server.Server; +import org.eclipse.jetty.server.handler.gzip.GzipHandler; +import org.eclipse.jetty.util.IO; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import javax.servlet.DispatcherType; +import javax.servlet.Filter; +import javax.servlet.FilterChain; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -40,22 +51,9 @@ import java.util.Enumeration; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; -import javax.servlet.*; -import javax.servlet.http.HttpServlet; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; - -import org.eclipse.jetty.http.HttpTester; -import org.eclipse.jetty.server.Dispatcher; -import org.eclipse.jetty.server.LocalConnector; -import org.eclipse.jetty.server.Server; -import org.eclipse.jetty.server.handler.gzip.GzipHandler; -import org.eclipse.jetty.util.IO; -import org.hamcrest.Matchers; -import org.junit.Assert; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; +import static org.junit.jupiter.api.Assertions.assertEquals; @SuppressWarnings("serial") public class GzipHandlerTest @@ -616,9 +614,9 @@ public class GzipHandlerTest public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (request.getParameter("X-Content-Encoding")!=null) - Assert.assertEquals(-1,request.getContentLength()); + assertEquals(-1,request.getContentLength()); else if (request.getContentLength()>=0) - Assert.assertThat(request.getParameter("X-Content-Encoding"),Matchers.nullValue()); + assertThat(request.getParameter("X-Content-Encoding"),Matchers.nullValue()); chain.doFilter(request,response); } diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/TypeUtilTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/TypeUtilTest.java index dc103c1b1d4..6349a364fae 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/TypeUtilTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/TypeUtilTest.java @@ -21,10 +21,10 @@ package org.eclipse.jetty.util; import static org.hamcrest.CoreMatchers.containsString; import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assume.assumeTrue; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assumptions.assumeTrue; import java.nio.file.Path; import java.nio.file.Paths; diff --git a/jetty-util/src/test/java/org/eclipse/jetty/util/statistic/RateStatisticTest.java b/jetty-util/src/test/java/org/eclipse/jetty/util/statistic/RateStatisticTest.java index 0a768d4f990..9ef3e5578d2 100644 --- a/jetty-util/src/test/java/org/eclipse/jetty/util/statistic/RateStatisticTest.java +++ b/jetty-util/src/test/java/org/eclipse/jetty/util/statistic/RateStatisticTest.java @@ -24,7 +24,8 @@ import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; import static org.hamcrest.Matchers.equalTo; -import static org.junit.Assert.assertThat; +import static org.hamcrest.MatcherAssert.assertThat; + /* ------------------------------------------------------------ */ diff --git a/pom.xml b/pom.xml index 08f256d856f..4df812973df 100644 --- a/pom.xml +++ b/pom.xml @@ -37,6 +37,8 @@ 2.4.5.Final 1.0.5 + + false 2.22.0 @@ -49,7 +51,7 @@ - 5.0 + 5.1 @@ -647,6 +649,8 @@ ${project.build.directory} ${unix.socket.tmp} + true + ${jetty.testtracker.log} diff --git a/tests/test-integration/src/test/java/org/eclipse/jetty/test/support/JettyDistro.java b/tests/test-integration/src/test/java/org/eclipse/jetty/test/support/JettyDistro.java index 3cc69f47f27..27994a3e1d3 100644 --- a/tests/test-integration/src/test/java/org/eclipse/jetty/test/support/JettyDistro.java +++ b/tests/test-integration/src/test/java/org/eclipse/jetty/test/support/JettyDistro.java @@ -258,7 +258,7 @@ public class JettyDistro */ public JettyDistro(Class clazz, String artifact) throws IOException { - this.jettyHomeDir = MavenTestingUtils.getTargetTestingDir(clazz,"jettyHome"); + this.jettyHomeDir = MavenTestingUtils.getTargetTestingPath(clazz,"jettyHome").toFile(); if (artifact != null) { this.artifactName = artifact; diff --git a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/ClusteredOrphanedSessionTest.java b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/ClusteredOrphanedSessionTest.java index 0e54b028bc3..fb192d31b60 100644 --- a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/ClusteredOrphanedSessionTest.java +++ b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/ClusteredOrphanedSessionTest.java @@ -22,6 +22,7 @@ package org.eclipse.jetty.gcloud.session; import org.eclipse.jetty.server.session.AbstractClusteredOrphanedSessionTest; import org.eclipse.jetty.server.session.SessionDataStoreFactory; import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; /** @@ -32,10 +33,25 @@ import org.junit.jupiter.api.Test; public class ClusteredOrphanedSessionTest extends AbstractClusteredOrphanedSessionTest { + public static GCloudSessionTestSupport __testSupport; + + @BeforeAll + public static void setUp () throws Exception + { + __testSupport = new GCloudSessionTestSupport(); + __testSupport.setUp(); + } + + @AfterAll + public static void tearDown () throws Exception + { + __testSupport.tearDown(); + } + @AfterAll public static void teardown () throws Exception { - GCloudTestSuite.__testSupport.deleteSessions(); + __testSupport.deleteSessions(); } @@ -46,7 +62,7 @@ public class ClusteredOrphanedSessionTest extends AbstractClusteredOrphanedSessi @Override public SessionDataStoreFactory createSessionDataStoreFactory() { - return GCloudSessionTestSupport.newSessionDataStoreFactory(GCloudTestSuite.__testSupport.getDatastore()); + return GCloudSessionTestSupport.newSessionDataStoreFactory(__testSupport.getDatastore()); } diff --git a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/ClusteredSessionScavengingTest.java b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/ClusteredSessionScavengingTest.java index ab430f9c03a..31393388e63 100644 --- a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/ClusteredSessionScavengingTest.java +++ b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/ClusteredSessionScavengingTest.java @@ -22,6 +22,7 @@ package org.eclipse.jetty.gcloud.session; import org.eclipse.jetty.server.session.AbstractClusteredSessionScavengingTest; import org.eclipse.jetty.server.session.SessionDataStoreFactory; import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; /** * ClusteredSessionScavengingTest @@ -30,11 +31,26 @@ import org.junit.jupiter.api.AfterAll; */ public class ClusteredSessionScavengingTest extends AbstractClusteredSessionScavengingTest { - + + public static GCloudSessionTestSupport __testSupport; + + @BeforeAll + public static void setUp () throws Exception + { + __testSupport = new GCloudSessionTestSupport(); + __testSupport.setUp(); + } + + @AfterAll + public static void tearDown () throws Exception + { + __testSupport.tearDown(); + } + @AfterAll public static void teardown () throws Exception { - GCloudTestSuite.__testSupport.deleteSessions(); + __testSupport.deleteSessions(); } @@ -44,7 +60,7 @@ public class ClusteredSessionScavengingTest extends AbstractClusteredSessionScav @Override public SessionDataStoreFactory createSessionDataStoreFactory() { - return GCloudSessionTestSupport.newSessionDataStoreFactory(GCloudTestSuite.__testSupport.getDatastore()); + return GCloudSessionTestSupport.newSessionDataStoreFactory(__testSupport.getDatastore()); } diff --git a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/GCloudSessionDataStoreTest.java b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/GCloudSessionDataStoreTest.java index b652e034d7b..d10de993b63 100644 --- a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/GCloudSessionDataStoreTest.java +++ b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/GCloudSessionDataStoreTest.java @@ -22,7 +22,9 @@ package org.eclipse.jetty.gcloud.session; import org.eclipse.jetty.server.session.AbstractSessionDataStoreTest; import org.eclipse.jetty.server.session.SessionData; import org.eclipse.jetty.server.session.SessionDataStoreFactory; +import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; /** * GCloudSessionDataStoreTest @@ -32,24 +34,39 @@ import org.junit.jupiter.api.AfterEach; public class GCloudSessionDataStoreTest extends AbstractSessionDataStoreTest { + public static GCloudSessionTestSupport __testSupport; + + @BeforeAll + public static void setUp () throws Exception + { + __testSupport = new GCloudSessionTestSupport(); + __testSupport.setUp(); + } + + @AfterAll + public static void tearDown () throws Exception + { + __testSupport.tearDown(); + } + @AfterEach public void teardown () throws Exception { - GCloudTestSuite.__testSupport.deleteSessions(); + __testSupport.deleteSessions(); } @Override public SessionDataStoreFactory createSessionDataStoreFactory() { - return GCloudSessionTestSupport.newSessionDataStoreFactory(GCloudTestSuite.__testSupport.getDatastore()); + return GCloudSessionTestSupport.newSessionDataStoreFactory(__testSupport.getDatastore()); } @Override public void persistSession(SessionData data) throws Exception { - GCloudTestSuite.__testSupport.createSession(data.getId(), data.getContextPath(), data.getVhost(), data.getLastNode(), data.getCreated(), + __testSupport.createSession(data.getId(), data.getContextPath(), data.getVhost(), data.getLastNode(), data.getCreated(), data.getAccessed(), data.getLastAccessed(), data.getMaxInactiveMs(), data.getExpiry(), data.getCookieSet(), data.getLastSaved(), data.getAllAttributes()); @@ -60,7 +77,7 @@ public class GCloudSessionDataStoreTest extends AbstractSessionDataStoreTest public void persistUnreadableSession(SessionData data) throws Exception { - GCloudTestSuite.__testSupport.createSession(data.getId(), data.getContextPath(), data.getVhost(), data.getLastNode(), data.getCreated(), + __testSupport.createSession(data.getId(), data.getContextPath(), data.getVhost(), data.getLastNode(), data.getCreated(), data.getAccessed(), data.getLastAccessed(), data.getMaxInactiveMs(), data.getExpiry(), data.getCookieSet(), data.getLastSaved(), null); } @@ -69,7 +86,7 @@ public class GCloudSessionDataStoreTest extends AbstractSessionDataStoreTest @Override public boolean checkSessionExists(SessionData data) throws Exception { - return GCloudTestSuite.__testSupport.checkSessionExists(data.getId()); + return __testSupport.checkSessionExists(data.getId()); } @@ -79,7 +96,7 @@ public class GCloudSessionDataStoreTest extends AbstractSessionDataStoreTest @Override public boolean checkSessionPersisted(SessionData data) throws Exception { - return GCloudTestSuite.__testSupport.checkSessionPersisted(data); + return __testSupport.checkSessionPersisted(data); } } diff --git a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/GCloudTestSuite.java b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/GCloudTestSuite.java deleted file mode 100644 index a77dd76aceb..00000000000 --- a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/GCloudTestSuite.java +++ /dev/null @@ -1,55 +0,0 @@ -// -// ======================================================================== -// Copyright (c) 1995-2018 Mort Bay Consulting Pty. Ltd. -// ------------------------------------------------------------------------ -// All rights reserved. This program and the accompanying materials -// are made available under the terms of the Eclipse Public License v1.0 -// and Apache License v2.0 which accompanies this distribution. -// -// The Eclipse Public License is available at -// http://www.eclipse.org/legal/epl-v10.html -// -// The Apache License v2.0 is available at -// http://www.opensource.org/licenses/apache2.0.php -// -// You may elect to redistribute this code under either of these licenses. -// ======================================================================== -// - -package org.eclipse.jetty.gcloud.session; - -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.runner.RunWith; -import org.junit.runners.Suite; - -/** - * GCloudTestSuite - * - * Sets up the gcloud emulator once before running all tests. - * - */ -@RunWith(Suite.class) -@Suite.SuiteClasses({ - GCloudSessionDataStoreTest.class, - InvalidationSessionTest.class, - ClusteredSessionScavengingTest.class, - ClusteredOrphanedSessionTest.class -}) -public class GCloudTestSuite -{ - public static GCloudSessionTestSupport __testSupport; - - @BeforeAll - public static void setUp () throws Exception - { - __testSupport = new GCloudSessionTestSupport(); - __testSupport.setUp(); - } - - @AfterAll - public static void tearDown () throws Exception - { - __testSupport.tearDown(); - } -} diff --git a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/InvalidationSessionTest.java b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/InvalidationSessionTest.java index 476c8dd9e18..3e7809aa82a 100644 --- a/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/InvalidationSessionTest.java +++ b/tests/test-sessions/test-gcloud-sessions/src/test/java/org/eclipse/jetty/gcloud/session/InvalidationSessionTest.java @@ -23,6 +23,7 @@ package org.eclipse.jetty.gcloud.session; import org.eclipse.jetty.server.session.AbstractClusteredInvalidationSessionTest; import org.eclipse.jetty.server.session.SessionDataStoreFactory; import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; /** * InvalidationSessionTest @@ -31,11 +32,26 @@ import org.junit.jupiter.api.AfterAll; */ public class InvalidationSessionTest extends AbstractClusteredInvalidationSessionTest { - + + public static GCloudSessionTestSupport __testSupport; + + @BeforeAll + public static void setUp () throws Exception + { + __testSupport = new GCloudSessionTestSupport(); + __testSupport.setUp(); + } + + @AfterAll + public static void tearDown () throws Exception + { + __testSupport.tearDown(); + } + @AfterAll public static void teardown () throws Exception { - GCloudTestSuite.__testSupport.deleteSessions(); + __testSupport.deleteSessions(); } /** @@ -44,6 +60,6 @@ public class InvalidationSessionTest extends AbstractClusteredInvalidationSessio @Override public SessionDataStoreFactory createSessionDataStoreFactory() { - return GCloudSessionTestSupport.newSessionDataStoreFactory(GCloudTestSuite.__testSupport.getDatastore()); + return GCloudSessionTestSupport.newSessionDataStoreFactory(__testSupport.getDatastore()); } }