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/03 22:47:56 UTC

[cxf] 01/02: CXF-8876: Get rid of EasyMock in cxf-rt-transports-http-jetty (#1318)

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 7fb5cdd08ab862f60bb867086e0d667bf4333f37
Author: Andriy Redko <dr...@gmail.com>
AuthorDate: Mon Jul 3 18:31:14 2023 -0400

    CXF-8876: Get rid of EasyMock in cxf-rt-transports-http-jetty (#1318)
    
    (cherry picked from commit f355ccc3a4bfc7d47485f1b623723e5b2cac96f5)
    
    # Conflicts:
    #       rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPDestinationTest.java
---
 rt/transports/http-jetty/pom.xml                   |   5 +-
 .../http_jetty/JettyHTTPDestinationTest.java       | 197 ++++++++-------------
 .../http_jetty/JettyHTTPServerEngineTest.java      |  21 +--
 3 files changed, 87 insertions(+), 136 deletions(-)

diff --git a/rt/transports/http-jetty/pom.xml b/rt/transports/http-jetty/pom.xml
index 71f232cd2b..38d19f90e0 100644
--- a/rt/transports/http-jetty/pom.xml
+++ b/rt/transports/http-jetty/pom.xml
@@ -149,8 +149,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-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPDestinationTest.java b/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPDestinationTest.java
index dab7139483..34a0911aac 100644
--- a/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPDestinationTest.java
+++ b/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPDestinationTest.java
@@ -74,7 +74,6 @@ import org.apache.cxf.ws.addressing.JAXWSAConstants;
 import org.eclipse.jetty.server.Request;
 import org.eclipse.jetty.server.Response;
 
-import org.easymock.EasyMock;
 import org.junit.After;
 import org.junit.Test;
 
@@ -85,6 +84,11 @@ 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.doNothing;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
 
 public class JettyHTTPDestinationTest {
     protected static final String AUTH_HEADER = "Authorization";
@@ -210,7 +214,7 @@ public class JettyHTTPDestinationTest {
     @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"));
@@ -407,8 +411,7 @@ public class JettyHTTPDestinationTest {
         endpointInfo.setName(new QName("bla", "Port"));
         endpointInfo.addExtensor(policy);
 
-        engine = EasyMock.createMock(JettyHTTPServerEngine.class);
-        EasyMock.replay();
+        engine = mock(JettyHTTPServerEngine.class);
         endpointInfo.setAddress(NOWHERE + "bar/foo");
 
         JettyHTTPDestination dest =
@@ -471,10 +474,8 @@ public class JettyHTTPDestinationTest {
         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);
@@ -534,22 +535,16 @@ public class JettyHTTPDestinationTest {
             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(JettyHTTPServerEngine.class);
+        engine = mock(JettyHTTPServerEngine.class);
         ServiceInfo serviceInfo = new ServiceInfo();
         serviceInfo.setName(new QName("bla", "Service"));
         endpointInfo = new EndpointInfo(serviceInfo, "");
@@ -557,12 +552,9 @@ public class JettyHTTPDestinationTest {
         endpointInfo.setAddress(NOWHERE + "bar/foo");
 
         endpointInfo.addExtensor(policy);
-        engine.addServant(EasyMock.eq(new URL(NOWHERE + "bar/foo")),
-                          EasyMock.isA(JettyHTTPHandler.class));
-        EasyMock.expectLastCall();
-        engine.getContinuationsEnabled();
-        EasyMock.expectLastCall().andReturn(true);
-        EasyMock.replay(engine);
+        doNothing().when(engine).addServant(eq(new URL(NOWHERE + "bar/foo")),
+                          isA(JettyHTTPHandler.class));
+        when(engine.getContinuationsEnabled()).thenReturn(true);
 
         JettyHTTPDestination dest = new EasyMockJettyHTTPDestination(bus,
                                                              transportFactory.getRegistry(),
@@ -582,10 +574,7 @@ public class JettyHTTPDestinationTest {
     }
 
     private void setUpRemoveServant() throws Exception {
-        EasyMock.reset(engine);
-        engine.removeServant(EasyMock.eq(new URL(NOWHERE + "bar/foo")));
-        EasyMock.expectLastCall();
-        EasyMock.replay(engine);
+        doNothing().when(engine).removeServant(eq(new URL(NOWHERE + "bar/foo")));
     }
 
     private void setUpDoService(boolean setRedirectURL) throws Exception {
@@ -629,108 +618,85 @@ public class JettyHTTPDestinationTest {
                                 int status
                                 ) throws Exception {
 
-        is = EasyMock.createMock(ServletInputStream.class);
-        os = EasyMock.createMock(ServletOutputStream.class);
-        request = EasyMock.createMock(Request.class);
-        response = EasyMock.createMock(Response.class);
-        request.getMethod();
-        EasyMock.expectLastCall().andReturn(method).atLeastOnce();
+        is = mock(ServletInputStream.class);
+        os = mock(ServletOutputStream.class);
+        request = mock(Request.class);
+        response = mock(Response.class);
+        when(request.getMethod()).thenReturn(method);
         //request.getConnection();
-        //EasyMock.expectLastCall().andReturn(null).anyTimes();
-        request.getUserPrincipal();
-        EasyMock.expectLastCall().andReturn(null).anyTimes();
+        //whenLastCall().thenReturn(null).anyTimes();
+        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();
-            request.setHandled(true);
-            EasyMock.expectLastCall();
+            doNothing().when(response).sendRedirect(eq(NOWHERE + "foo/bar"));
+            doNothing().when(response).flushBuffer();
+            doNothing().when(request).setHandled(true);
         } else {
             //getQueryString for if statement
-            request.getQueryString();
-            EasyMock.expectLastCall().andReturn(query);
+            when(request.getQueryString()).thenReturn(query);
 
             if ("GET".equals(method) && "?wsdl".equals(query)) {
                 verifyGetWSDLQuery();
             } else { // test for the post
-                EasyMock.expect(request.getAttribute(AbstractHTTPDestination.CXF_CONTINUATION_MESSAGE))
-                    .andReturn(null);
-
-                //EasyMock.expect(request.getMethod()).andReturn(method);
-                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("org.eclipse.jetty.ajax.Continuation")).andReturn(null);
-                EasyMock.expect(request.getAttribute("http.service.redirection")).andReturn(null).anyTimes();
+                when(request.getAttribute(AbstractHTTPDestination.CXF_CONTINUATION_MESSAGE))
+                    .thenReturn(null);
+
+                //when(request.getMethod()).thenReturn(method);
+                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("org.eclipse.jetty.ajax.Continuation")).thenReturn(null);
+                when(request.getAttribute("http.service.redirection")).thenReturn(null);
 
                 List<String> headers = Arrays.asList(new String[] {"content-type",
                                                                    JettyHTTPDestinationTest.AUTH_HEADER});
                 List<String> ct = Arrays.asList(new String[] {"text/xml", "charset=utf8"});
-                EasyMock.expect(request.getHeaderNames()).andReturn(Collections.enumeration(headers));
-                request.getHeaders("content-type");
-                EasyMock.expectLastCall().andReturn(Collections.enumeration(ct));
-                request.getHeaders(JettyHTTPDestinationTest.AUTH_HEADER);
-                EasyMock.expectLastCall()
-                    .andReturn(Collections
+                when(request.getHeaderNames()).thenReturn(Collections.enumeration(headers));
+                when(request.getHeaders("content-type")).thenReturn(Collections.enumeration(ct));
+                when(request.getHeaders(JettyHTTPDestinationTest.AUTH_HEADER))
+                    .thenReturn(Collections
                                .enumeration(Collections
                                             .singletonList(JettyHTTPDestinationTest.BASIC_AUTH)));
 
-                EasyMock.expect(request.getInputStream()).andReturn(is);
-                request.setHandled(true);
-                EasyMock.expectLastCall();
-                response.flushBuffer();
-                EasyMock.expectLastCall();
+                when(request.getInputStream()).thenReturn(is);
+                doNothing().when(request).setHandled(true);
+                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() {
@@ -758,22 +724,13 @@ public class JettyHTTPDestinationTest {
     }
 
     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();
+        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");
+        doNothing().when(response).getOutputStream();
         request.setHandled(true);
-        EasyMock.expectLastCall();
-        EasyMock.replay(bus);
     }
 
     private void verifyDoService() throws Exception {
diff --git a/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineTest.java b/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineTest.java
index f3d55db989..e2f281867c 100644
--- a/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineTest.java
+++ b/rt/transports/http-jetty/src/test/java/org/apache/cxf/transport/http_jetty/JettyHTTPServerEngineTest.java
@@ -54,8 +54,6 @@ import org.eclipse.jetty.server.handler.ContextHandler;
 import org.eclipse.jetty.util.thread.QueuedThreadPool;
 import org.eclipse.jetty.util.thread.ThreadPool;
 
-import org.easymock.EasyMock;
-import org.easymock.IMocksControl;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -64,6 +62,8 @@ import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
 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 JettyHTTPServerEngineTest {
     private static final int PORT1
@@ -77,26 +77,19 @@ public class JettyHTTPServerEngineTest {
 
 
     private Bus bus;
-    private IMocksControl control;
     private JettyHTTPServerEngineFactory 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();
+        when(bus.getExtension(Configurer.class)).thenReturn(configurer);
 
-        InstrumentationManager iManager = control.createMock(InstrumentationManager.class);
-        iManager.getMBeanServer();
-        EasyMock.expectLastCall().andReturn(ManagementFactory.getPlatformMBeanServer()).anyTimes();
+        InstrumentationManager iManager = mock(InstrumentationManager.class);
+        when(iManager.getMBeanServer()).thenReturn(ManagementFactory.getPlatformMBeanServer());
 
-        bus.getExtension(InstrumentationManager.class);
-        EasyMock.expectLastCall().andReturn(iManager).anyTimes();
-
-        control.replay();
+        when(bus.getExtension(InstrumentationManager.class)).thenReturn(iManager);
 
         factory = new JettyHTTPServerEngineFactory();
         factory.setBus(bus);