From f4986d38ab663245a2fdd05aff4608ad0ee9882a Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Tue, 20 Jun 2023 21:34:53 -0400 Subject: [PATCH] Bulletproof AbstractProxyServlet#destory() to make it easier to write unit tests for custom subclasses of AbstractProxyServlet --- .../jetty/proxy/AbstractProxyServlet.java | 7 +++- .../jetty/proxy/AbstractProxyServletTest.java | 39 +++++++++++++++++++ .../proxy/AsyncMiddleManServletTest.java | 15 +++++-- 3 files changed, 56 insertions(+), 5 deletions(-) create mode 100644 jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractProxyServletTest.java diff --git a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java index 34a2529e799c..4694967b5523 100644 --- a/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java +++ b/jetty-proxy/src/main/java/org/eclipse/jetty/proxy/AbstractProxyServlet.java @@ -145,11 +145,14 @@ public void destroy() { try { - _client.stop(); + if (_client != null) + _client.stop(); } catch (Exception x) { - if (_log.isDebugEnabled()) + if (_log == null) + x.printStackTrace(); + else if (_log.isDebugEnabled()) _log.debug("Failed to stop client", x); } } diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractProxyServletTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractProxyServletTest.java new file mode 100644 index 000000000000..f9c5f5af220a --- /dev/null +++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AbstractProxyServletTest.java @@ -0,0 +1,39 @@ +// +// ======================================================================== +// Copyright (c) 1995 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.proxy; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.eclipse.jetty.client.api.Response.CompleteListener; +import org.junit.jupiter.api.Test; + +public class AbstractProxyServletTest +{ + + @Test + public void testNewDestroy() throws Exception + { + new AbstractProxyServlet() + { + private static final long serialVersionUID = 1L; + + @Override + protected CompleteListener newProxyResponseListener(HttpServletRequest clientRequest, HttpServletResponse proxyResponse) + { + return null; + } + }.destroy(); + } +} diff --git a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AsyncMiddleManServletTest.java b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AsyncMiddleManServletTest.java index 365ece15cdde..9770739dad88 100644 --- a/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AsyncMiddleManServletTest.java +++ b/jetty-proxy/src/test/java/org/eclipse/jetty/proxy/AsyncMiddleManServletTest.java @@ -159,9 +159,18 @@ private void startClient() throws Exception @AfterEach public void dispose() throws Exception { - client.stop(); - proxy.stop(); - server.stop(); + if (client != null) + client.stop(); + if (proxy != null) + proxy.stop(); + if (server != null) + server.stop(); + } + + @Test + public void testNewDestroy() throws Exception + { + new AsyncMiddleManServlet().destroy(); } @Test