You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2023/07/27 19:31:10 UTC

[cxf] 01/02: CXF-8870: Get rid of EasyMock in cxf-rt-transports-http-undertow (#1341)

This is an automated email from the ASF dual-hosted git repository.

reta pushed a commit to branch 3.6.x-fixes
in repository https://gitbox.apache.org/repos/asf/cxf.git

commit 38b06816ac276542d7f3de3a8f867e6aa739659c
Author: Andriy Redko <dr...@gmail.com>
AuthorDate: Thu Jul 27 12:06:06 2023 -0400

    CXF-8870: Get rid of EasyMock in cxf-rt-transports-http-undertow (#1341)
    
    (cherry picked from commit b40a2e5bf71e70a92bb5c782890d017c1fb2922a)
    
    # Conflicts:
    #       rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPDestinationTest.java
---
 rt/transports/http-undertow/pom.xml                |   5 +-
 .../http_undertow/SimpleErrorPageHandler.java.bak  |  51 -----
 .../http_undertow/UndertowHTTPDestinationTest.java | 220 ++++++++++-----------
 .../UndertowHTTPServerEngineTest.java              |  22 +--
 4 files changed, 112 insertions(+), 186 deletions(-)

diff --git a/rt/transports/http-undertow/pom.xml b/rt/transports/http-undertow/pom.xml
index 947ef8b3f4..ac6c714626 100644
--- a/rt/transports/http-undertow/pom.xml
+++ b/rt/transports/http-undertow/pom.xml
@@ -119,8 +119,9 @@
             <scope>test</scope>
         </dependency>
         <dependency>
-            <groupId>org.easymock</groupId>
-            <artifactId>easymock</artifactId>
+            <groupId>org.mockito</groupId>
+            <artifactId>mockito-core</artifactId>
+            <version>${cxf.mockito.version}</version>
             <scope>test</scope>
         </dependency>
         <dependency>
diff --git a/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/SimpleErrorPageHandler.java.bak b/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/SimpleErrorPageHandler.java.bak
deleted file mode 100644
index e6936f1d41..0000000000
--- a/rt/transports/http-undertow/src/main/java/org/apache/cxf/transport/http_undertow/SimpleErrorPageHandler.java.bak
+++ /dev/null
@@ -1,51 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-package org.apache.cxf.transport.http_undertow;
-
-import io.undertow.io.Sender;
-import io.undertow.server.HttpHandler;
-import io.undertow.server.HttpServerExchange;
-import io.undertow.util.Headers;
-
-
-public class SimpleErrorPageHandler implements HttpHandler {
-
-    private final HttpHandler next;
-
-    public SimpleErrorPageHandler(final HttpHandler next) {
-        this.next = next;
-    }
-
-    @Override
-    public void handleRequest(final HttpServerExchange exchange) throws Exception {
-        try {
-            next.handleRequest(exchange);
-        } catch (Throwable t) {
-            if(exchange.isResponseChannelAvailable()) {
-                if (exchange.getResponseCode() == 500) {
-                    final String errorPage = "<html><head><title>Error</title></head><body>Internal Error 500</body></html>";
-                    exchange.getResponseHeaders().put(Headers.CONTENT_LENGTH, "" + errorPage.length());
-                    exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html");
-                    Sender sender = exchange.getResponseSender();
-                    sender.send(errorPage);
-                }
-            }
-        }
-    }
-}
diff --git a/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPDestinationTest.java b/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPDestinationTest.java
index eed1d9da40..f290d2377b 100644
--- a/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPDestinationTest.java
+++ b/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPDestinationTest.java
@@ -75,7 +75,6 @@ import org.apache.cxf.ws.addressing.JAXWSAConstants;
 import io.undertow.util.HeaderMap;
 import io.undertow.util.HttpString;
 
-import org.easymock.EasyMock;
 import org.junit.After;
 import org.junit.Test;
 
@@ -86,7 +85,12 @@ import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertSame;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
-
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isA;
+import static org.mockito.Mockito.doCallRealMethod;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public class UndertowHTTPDestinationTest {
     protected static final String AUTH_HEADER = "Authorization";
@@ -212,7 +216,7 @@ public class UndertowHTTPDestinationTest {
     @Test
     public void testContinuationsIgnored() throws Exception {
 
-        HttpServletRequest httpRequest = EasyMock.createMock(HttpServletRequest.class);
+        HttpServletRequest httpRequest = mock(HttpServletRequest.class);
 
         ServiceInfo serviceInfo = new ServiceInfo();
         serviceInfo.setName(new QName("bla", "Service"));
@@ -296,6 +300,30 @@ public class UndertowHTTPDestinationTest {
                     defaultBus, BusFactory.getThreadDefaultBus());
     }
 
+    @Test
+    public void testDoServiceWithWsdlGET() throws Exception {
+        destination = setUpDestination(false, false);
+        setUpDoService(false,
+                       false,
+                       false,
+                       "GET",
+                       "?wsdl",
+                       200);
+        destination.doService(request, response);
+
+        assertNotNull("unexpected null message", inMessage);
+        assertEquals("unexpected method",
+                     inMessage.get(Message.HTTP_REQUEST_METHOD),
+                     "GET");
+        assertEquals("unexpected path",
+                     inMessage.get(Message.PATH_INFO),
+                     "/bar/foo");
+        assertEquals("unexpected query",
+                     inMessage.get(Message.QUERY_STRING),
+                     "wsdl");
+
+    }
+
     @Test
     public void testDoServiceWithHttpGET() throws Exception {
         destination = setUpDestination(false, false);
@@ -409,8 +437,7 @@ public class UndertowHTTPDestinationTest {
         endpointInfo.setName(new QName("bla", "Port"));
         endpointInfo.addExtensor(policy);
 
-        engine = EasyMock.createMock(UndertowHTTPServerEngine.class);
-        EasyMock.replay();
+        engine = mock(UndertowHTTPServerEngine.class);
         endpointInfo.setAddress(NOWHERE + "bar/foo");
 
         UndertowHTTPDestination dest =
@@ -473,10 +500,9 @@ public class UndertowHTTPDestinationTest {
         Map<String, Object> context = new HashMap<>();
         assertNull("fails with no context", destination.getId(context));
 
-        AddressingProperties maps = EasyMock.createMock(AddressingProperties.class);
-        maps.getToEndpointReference();
-        EasyMock.expectLastCall().andReturn(refWithId);
-        EasyMock.replay(maps);
+        AddressingProperties maps = mock(AddressingProperties.class);
+        when(maps.getToEndpointReference()).thenReturn(refWithId);
+
         context.put(JAXWSAConstants.ADDRESSING_PROPERTIES_INBOUND, maps);
         String result = destination.getId(context);
         assertNotNull(result);
@@ -536,22 +562,16 @@ public class UndertowHTTPDestinationTest {
             bus = new ExtensionManagerBus();
             bus.setExtension(mgr, ConduitInitiatorManager.class);
         } else {
-            bus = EasyMock.createMock(Bus.class);
-            bus.getExtension(EndpointResolverRegistry.class);
-            EasyMock.expectLastCall().andReturn(null);
-            bus.getExtension(ContinuationProviderFactory.class);
-            EasyMock.expectLastCall().andReturn(null).anyTimes();
-            bus.getExtension(PolicyDataEngine.class);
-            EasyMock.expectLastCall().andReturn(null).anyTimes();
-            bus.hasExtensionByName("org.apache.cxf.ws.policy.PolicyEngine");
-            EasyMock.expectLastCall().andReturn(false);
-            bus.getExtension(ClassLoader.class);
-            EasyMock.expectLastCall().andReturn(this.getClass().getClassLoader());
-            EasyMock.replay(bus);
+            bus = mock(Bus.class);
+            when(bus.getExtension(EndpointResolverRegistry.class)).thenReturn(null);
+            when(bus.getExtension(ContinuationProviderFactory.class)).thenReturn(null);
+            when(bus.getExtension(PolicyDataEngine.class)).thenReturn(null);
+            when(bus.hasExtensionByName("org.apache.cxf.ws.policy.PolicyEngine")).thenReturn(false);
+            when(bus.getExtension(ClassLoader.class)).thenReturn(this.getClass().getClassLoader());
         }
 
 
-        engine = EasyMock.createNiceMock(UndertowHTTPServerEngine.class);
+        engine = mock(UndertowHTTPServerEngine.class);
         ServiceInfo serviceInfo = new ServiceInfo();
         serviceInfo.setName(new QName("bla", "Service"));
         endpointInfo = new EndpointInfo(serviceInfo, "");
@@ -559,12 +579,9 @@ public class UndertowHTTPDestinationTest {
         endpointInfo.setAddress(NOWHERE + "bar/foo");
 
         endpointInfo.addExtensor(policy);
-        engine.addServant(EasyMock.eq(new URL(NOWHERE + "bar/foo")),
-                          EasyMock.isA(UndertowHTTPHandler.class));
-        EasyMock.expectLastCall();
-        engine.getContinuationsEnabled();
-        EasyMock.expectLastCall().andReturn(true);
-        EasyMock.replay(engine);
+        doCallRealMethod().when(engine).addServant(eq(new URL(NOWHERE + "bar/foo")),
+                          isA(UndertowHTTPHandler.class));
+        when(engine.getContinuationsEnabled()).thenReturn(true);
 
         UndertowHTTPDestination dest = new EasyMockUndertowHTTPDestination(bus,
                                                              transportFactory.getRegistry(),
@@ -584,10 +601,7 @@ public class UndertowHTTPDestinationTest {
     }
 
     private void setUpRemoveServant() throws Exception {
-        EasyMock.reset(engine);
-        engine.removeServant(EasyMock.eq(new URL(NOWHERE + "bar/foo")));
-        EasyMock.expectLastCall();
-        EasyMock.replay(engine);
+        doCallRealMethod().when(engine).removeServant(eq(new URL(NOWHERE + "bar/foo")));
     }
 
     private void setUpDoService(boolean setRedirectURL) throws Exception {
@@ -631,48 +645,43 @@ public class UndertowHTTPDestinationTest {
                                 int status
                                 ) throws Exception {
 
-        is = EasyMock.createMock(ServletInputStream.class);
-        os = EasyMock.createMock(ServletOutputStream.class);
-        request = EasyMock.createMock(HttpServletRequest.class);
-        response = EasyMock.createMock(HttpServletResponse.class);
-        request.getMethod();
-        EasyMock.expectLastCall().andReturn(method).atLeastOnce();
-        request.getUserPrincipal();
-        EasyMock.expectLastCall().andReturn(null).anyTimes();
+        is = mock(ServletInputStream.class);
+        os = mock(ServletOutputStream.class);
+        request = mock(HttpServletRequest.class);
+        response = mock(HttpServletResponse.class);
+        when(request.getMethod()).thenReturn(method);
+        when(request.getUserPrincipal()).thenReturn(null);
 
         if (setRedirectURL) {
             policy.setRedirectURL(NOWHERE + "foo/bar");
-            response.sendRedirect(EasyMock.eq(NOWHERE + "foo/bar"));
-            EasyMock.expectLastCall();
-            response.flushBuffer();
-            EasyMock.expectLastCall();
-            EasyMock.expectLastCall();
+            doNothing().when(response).sendRedirect(eq(NOWHERE + "foo/bar"));
+            doNothing().when(response).flushBuffer();
         } else {
             //getQueryString for if statement
-            request.getQueryString();
-            EasyMock.expectLastCall().andReturn(query);
+            when(request.getQueryString()).thenReturn(query);
 
             if ("GET".equals(method) && "?wsdl".equals(query)) {
+                when(request.getHeaderNames()).thenReturn(Collections.emptyEnumeration());
                 verifyGetWSDLQuery();
             } else { // test for the post
-                EasyMock.expect(request.getAttribute(AbstractHTTPDestination.CXF_CONTINUATION_MESSAGE))
-                    .andReturn(null);
-
-
-                EasyMock.expect(request.getInputStream()).andReturn(is);
-                EasyMock.expect(request.getContextPath()).andReturn("/bar");
-                EasyMock.expect(request.getServletPath()).andReturn("");
-                EasyMock.expect(request.getPathInfo()).andReturn("/foo");
-                EasyMock.expect(request.getRequestURI()).andReturn("/foo");
-                EasyMock.expect(request.getRequestURL())
-                    .andReturn(new StringBuffer("http://localhost/foo")).anyTimes();
-                request.setAttribute("org.springframework.web.servlet.HandlerMapping.bestMatchingPattern", "/foo");
-                EasyMock.expectLastCall();
-                EasyMock.expect(request.getCharacterEncoding()).andReturn(StandardCharsets.UTF_8.name());
-                EasyMock.expect(request.getQueryString()).andReturn(query);
-                EasyMock.expect(request.getHeader("Accept")).andReturn("*/*");
-                EasyMock.expect(request.getContentType()).andReturn("text/xml charset=utf8").times(2);
-                EasyMock.expect(request.getAttribute("http.service.redirection")).andReturn(null).anyTimes();
+                when(request.getAttribute(AbstractHTTPDestination.CXF_CONTINUATION_MESSAGE))
+                    .thenReturn(null);
+
+
+                when(request.getInputStream()).thenReturn(is);
+                when(request.getContextPath()).thenReturn("/bar");
+                when(request.getServletPath()).thenReturn("");
+                when(request.getPathInfo()).thenReturn("/foo");
+                when(request.getRequestURI()).thenReturn("/foo");
+                when(request.getRequestURL())
+                    .thenReturn(new StringBuffer("http://localhost/foo"));
+                doNothing().when(request)
+                    .setAttribute("org.springframework.web.servlet.HandlerMapping.bestMatchingPattern", "/foo");
+                when(request.getCharacterEncoding()).thenReturn(StandardCharsets.UTF_8.name());
+                when(request.getQueryString()).thenReturn(query);
+                when(request.getHeader("Accept")).thenReturn("*/*");
+                when(request.getContentType()).thenReturn("text/xml charset=utf8");
+                when(request.getAttribute("http.service.redirection")).thenReturn(null);
 
                 HeaderMap httpFields = new HeaderMap();
                 httpFields.add(new HttpString("content-type"), "text/xml");
@@ -683,55 +692,39 @@ public class UndertowHTTPDestinationTest {
                 for (HttpString header : httpFields.getHeaderNames()) {
                     headers.add(header.toString());
                 }
-                EasyMock.expect(request.getHeaderNames()).andReturn(Collections.enumeration(headers));
-                request.getHeaders("content-type");
-                EasyMock.expectLastCall().andReturn(Collections.enumeration(httpFields.get("content-type")));
-                request.getHeaders(UndertowHTTPDestinationTest.AUTH_HEADER);
-                EasyMock.expectLastCall().andReturn(Collections.enumeration(
-                                                    httpFields.get(UndertowHTTPDestinationTest.AUTH_HEADER)));
-
-                EasyMock.expect(request.getInputStream()).andReturn(is);
-                EasyMock.expectLastCall();
-                response.flushBuffer();
-                EasyMock.expectLastCall();
+                when(request.getHeaderNames()).thenReturn(Collections.enumeration(headers));
+                when(request.getHeaders("content-type")).thenReturn(
+                    Collections.enumeration(httpFields.get("content-type")));
+                when(request.getHeaders(UndertowHTTPDestinationTest.AUTH_HEADER)).thenReturn(Collections.enumeration(
+                    httpFields.get(UndertowHTTPDestinationTest.AUTH_HEADER)));
+
+                when(request.getInputStream()).thenReturn(is);
+                doNothing().when(response).flushBuffer();
+
                 if (sendResponse) {
-                    response.setStatus(status);
-                    EasyMock.expectLastCall();
-                    response.setContentType("text/xml charset=utf8");
-                    EasyMock.expectLastCall();
-                    response.addHeader(EasyMock.isA(String.class), EasyMock.isA(String.class));
-                    EasyMock.expectLastCall().anyTimes();
-                    response.setContentLength(0);
-                    EasyMock.expectLastCall().anyTimes();
-                    response.getOutputStream();
-                    EasyMock.expectLastCall().andReturn(os);
-                    response.getStatus();
-                    EasyMock.expectLastCall().andReturn(status).anyTimes();
-                    response.flushBuffer();
-                    EasyMock.expectLastCall();
+                    doNothing().when(response).setStatus(status);
+                    doNothing().when(response).setContentType("text/xml charset=utf8");
+                    doNothing().when(response).addHeader(isA(String.class), isA(String.class));
+                    doNothing().when(response).setContentLength(0);
+                    when(response.getOutputStream()).thenReturn(os);
+                    when(response.getStatus()).thenReturn(status);
+                    doNothing().when(response).flushBuffer();
                 }
-                request.getAttribute("javax.servlet.request.cipher_suite");
-                EasyMock.expectLastCall().andReturn("anythingwilldoreally");
-                request.getAttribute("javax.net.ssl.session");
-                EasyMock.expectLastCall().andReturn(null);
-                request.getAttribute("javax.servlet.request.X509Certificate");
-                EasyMock.expectLastCall().andReturn(null);
+                when(request.getAttribute("javax.servlet.request.cipher_suite")).thenReturn("anythingwilldoreally");
+                when(request.getAttribute("javax.net.ssl.session")).thenReturn(null);
+                when(request.getAttribute("javax.servlet.request.X509Certificate")).thenReturn(null);
             }
         }
 
         if (decoupled) {
             setupDecoupledBackChannel();
         }
-        EasyMock.replay(response);
-        EasyMock.replay(request);
     }
 
     private void setupDecoupledBackChannel() throws IOException {
-        decoupledBackChannel = EasyMock.createMock(Conduit.class);
-        decoupledBackChannel.setMessageObserver(EasyMock.isA(MessageObserver.class));
-        decoupledBackChannel.prepare(EasyMock.isA(Message.class));
-        EasyMock.expectLastCall();
-        EasyMock.replay(decoupledBackChannel);
+        decoupledBackChannel = mock(Conduit.class);
+        doNothing().when(decoupledBackChannel).setMessageObserver(isA(MessageObserver.class));
+        doNothing().when(decoupledBackChannel).prepare(isA(Message.class));
     }
 
     private void setUpInMessage() {
@@ -759,21 +752,12 @@ public class UndertowHTTPDestinationTest {
     }
 
     private void verifyGetWSDLQuery() throws Exception {
-        EasyMock.reset(bus);
-        request.getRequestURL();
-        EasyMock.expectLastCall().andReturn(new StringBuffer("http://localhost/bar/foo")).times(2);
-        request.getPathInfo();
-        EasyMock.expectLastCall().andReturn("/bar/foo");
-        request.getCharacterEncoding();
-        EasyMock.expectLastCall().andReturn(StandardCharsets.UTF_8.name());
-        request.getQueryString();
-        EasyMock.expectLastCall().andReturn("wsdl");
-        response.setContentType("text/xml");
-        EasyMock.expectLastCall();
-        response.getOutputStream();
-        EasyMock.expectLastCall().andReturn(os).anyTimes();
-        EasyMock.expectLastCall();
-        EasyMock.replay(bus);
+        when(request.getRequestURL()).thenReturn(new StringBuffer("http://localhost/bar/foo"));
+        when(request.getPathInfo()).thenReturn("/bar/foo");
+        when(request.getCharacterEncoding()).thenReturn(StandardCharsets.UTF_8.name());
+        when(request.getQueryString()).thenReturn("wsdl");
+        doNothing().when(response).setContentType("text/xml");
+        when(response.getOutputStream()).thenReturn(os);
     }
 
     private void verifyDoService() throws Exception {
diff --git a/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngineTest.java b/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngineTest.java
index fd1abdbe9c..9daa8f2bd7 100644
--- a/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngineTest.java
+++ b/rt/transports/http-undertow/src/test/java/org/apache/cxf/transport/http_undertow/UndertowHTTPServerEngineTest.java
@@ -42,15 +42,14 @@ import org.apache.cxf.helpers.IOUtils;
 import org.apache.cxf.management.InstrumentationManager;
 import org.apache.cxf.testutil.common.TestUtil;
 
-import org.easymock.EasyMock;
-import org.easymock.IMocksControl;
 import org.junit.Before;
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertTrue;
 import static org.junit.Assert.fail;
-
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public class UndertowHTTPServerEngineTest {
     private static final int PORT1
@@ -62,26 +61,19 @@ public class UndertowHTTPServerEngineTest {
 
 
     private Bus bus;
-    private IMocksControl control;
     private UndertowHTTPServerEngineFactory factory;
 
     @Before
     public void setUp() throws Exception {
-        control = EasyMock.createNiceControl();
-        bus = control.createMock(Bus.class);
+        bus = mock(Bus.class);
 
         Configurer configurer = new ConfigurerImpl();
-        bus.getExtension(Configurer.class);
-        EasyMock.expectLastCall().andReturn(configurer).anyTimes();
-
-        InstrumentationManager iManager = control.createMock(InstrumentationManager.class);
-        iManager.getMBeanServer();
-        EasyMock.expectLastCall().andReturn(ManagementFactory.getPlatformMBeanServer()).anyTimes();
+        when(bus.getExtension(Configurer.class)).thenReturn(configurer);
 
-        bus.getExtension(InstrumentationManager.class);
-        EasyMock.expectLastCall().andReturn(iManager).anyTimes();
+        InstrumentationManager iManager = mock(InstrumentationManager.class);
+        when(iManager.getMBeanServer()).thenReturn(ManagementFactory.getPlatformMBeanServer());
 
-        control.replay();
+        when(bus.getExtension(InstrumentationManager.class)).thenReturn(iManager);
 
         factory = new UndertowHTTPServerEngineFactory();
         factory.setBus(bus);