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 9b166d741d55af9688f76cbca94f0cd6c835a9cb Author: Andriy Redko <[email protected]> AuthorDate: Fri Jun 30 14:53:28 2023 -0400 CXF-8890: Get rid of EasyMock in cxf-rt-transports-http (#1317) (cherry picked from commit 7aefb697f7b391170bb48faecf2638e6f72ab9b1) --- rt/transports/http/pom.xml | 5 - .../http/DestinationRegistryImplTest.java | 27 +-- .../transport/http/HTTPConduitURLEasyMockTest.java | 200 +++++++-------------- .../org/apache/cxf/transport/http/HeadersTest.java | 42 ++--- .../cxf/transport/http/policy/PolicyUtilsTest.java | 54 ++---- .../cxf/transport/servlet/BaseUrlHelperTest.java | 22 +-- .../transport/servlet/ServletControllerTest.java | 117 ++++++------ 7 files changed, 165 insertions(+), 302 deletions(-) diff --git a/rt/transports/http/pom.xml b/rt/transports/http/pom.xml index 0c3c37345b..1cc5c3b019 100644 --- a/rt/transports/http/pom.xml +++ b/rt/transports/http/pom.xml @@ -68,11 +68,6 @@ <artifactId>junit</artifactId> <scope>test</scope> </dependency> - <dependency> - <groupId>org.easymock</groupId> - <artifactId>easymock</artifactId> - <scope>test</scope> - </dependency> <dependency> <groupId>org.mockito</groupId> <artifactId>mockito-core</artifactId> diff --git a/rt/transports/http/src/test/java/org/apache/cxf/transport/http/DestinationRegistryImplTest.java b/rt/transports/http/src/test/java/org/apache/cxf/transport/http/DestinationRegistryImplTest.java index 10e926784d..7a52c129df 100755 --- a/rt/transports/http/src/test/java/org/apache/cxf/transport/http/DestinationRegistryImplTest.java +++ b/rt/transports/http/src/test/java/org/apache/cxf/transport/http/DestinationRegistryImplTest.java @@ -26,8 +26,6 @@ import javax.xml.namespace.QName; import org.apache.cxf.service.model.EndpointInfo; import org.apache.cxf.transport.MessageObserver; -import org.easymock.EasyMock; -import org.easymock.IMocksControl; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -36,6 +34,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * @@ -52,20 +52,17 @@ public class DestinationRegistryImplTest { private static final int[] MATCHED_PATH_INDEXES = {0, 0, 1, -1, 3, 0, 0, 4, -1, 5, 5, 5}; - private IMocksControl control; private DestinationRegistry registry; private MessageObserver observer; @Before public void setUp() { - control = EasyMock.createNiceControl(); registry = new DestinationRegistryImpl(); - observer = control.createMock(MessageObserver.class); + observer = mock(MessageObserver.class); } @After public void tearDown() { - control = null; registry = null; } @@ -93,20 +90,18 @@ public class DestinationRegistryImplTest { for (int j = 0; j < REGISTERED_PATHS.length; j++) { AbstractHTTPDestination target = registry.getDestinationForPath(REGISTERED_PATHS[j]); if (mi == j) { - EasyMock.expect(target.getMessageObserver()).andReturn(observer); + when(target.getMessageObserver()).thenReturn(observer); EndpointInfo endpoint = new EndpointInfo(); endpoint.setAddress(REGISTERED_PATHS[mi]); endpoint.setName(QNAME); - EasyMock.expect(target.getEndpointInfo()).andReturn(endpoint); + when(target.getEndpointInfo()).thenReturn(endpoint); } else { - EasyMock.expect(target.getMessageObserver()).andReturn(observer).anyTimes(); + when(target.getMessageObserver()).thenReturn(observer); } } - control.replay(); - AbstractHTTPDestination destination = registry.checkRestfulRequest(REQUEST_PATHS[i]); if (0 <= mi) { @@ -117,25 +112,19 @@ public class DestinationRegistryImplTest { } else { assertNull(destination); } - - control.verify(); - - control.reset(); } } private void setUpDestinations() { for (int i = 0; i < REGISTERED_PATHS.length; i++) { - AbstractHTTPDestination destination = control.createMock(AbstractHTTPDestination.class); + AbstractHTTPDestination destination = mock(AbstractHTTPDestination.class); EndpointInfo endpoint = new EndpointInfo(); endpoint.setAddress(REGISTERED_PATHS[i]); endpoint.setName(QNAME); - EasyMock.expect(destination.getEndpointInfo()).andReturn(endpoint); + when(destination.getEndpointInfo()).thenReturn(endpoint); - control.replay(); registry.addDestination(destination); - control.reset(); } } diff --git a/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitURLEasyMockTest.java b/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitURLEasyMockTest.java index c63aff92eb..3850ce338b 100644 --- a/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitURLEasyMockTest.java +++ b/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HTTPConduitURLEasyMockTest.java @@ -49,8 +49,6 @@ import org.apache.cxf.transport.MessageObserver; import org.apache.cxf.transport.https.HttpsURLConnectionFactory; import org.apache.cxf.ws.addressing.EndpointReferenceType; -import org.easymock.EasyMock; -import org.easymock.IMocksControl; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; @@ -62,6 +60,11 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.nullable; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** */ @@ -75,7 +78,6 @@ public class HTTPConduitURLEasyMockTest { private static final String NOWHERE = "http://nada.nothing.nowhere.null/"; private static final String PAYLOAD = "message payload"; - private IMocksControl control; private EndpointInfo endpointInfo; private HttpsURLConnectionFactory connectionFactory; private HttpURLConnection connection; @@ -145,19 +147,16 @@ public class HTTPConduitURLEasyMockTest { @Test public void testSend() throws Exception { - control = EasyMock.createNiceControl(); HTTPConduit conduit = setUpConduit(true, false, "POST"); Message message = createMessage(); message.put(HTTPConduit.SET_HTTP_RESPONSE_MESSAGE, Boolean.FALSE); conduit.prepare(message); verifySentMessage(conduit, message, "POST"); assertNull(inMessage.get(HTTPConduit.HTTP_RESPONSE_MESSAGE)); - finalVerify(); } @Test public void testSendWithHeaders() throws Exception { - control = EasyMock.createNiceControl(); HTTPConduit conduit = setUpConduit(true, false, "POST"); Message message = createMessage(); message.put(HTTPConduit.SET_HTTP_RESPONSE_MESSAGE, Boolean.TRUE); @@ -165,7 +164,6 @@ public class HTTPConduitURLEasyMockTest { conduit.prepare(message); verifySentMessage(conduit, message, true, "POST", false); assertEquals(HTTP_RESPONSE_MESSAGE, inMessage.get(HTTPConduit.HTTP_RESPONSE_MESSAGE)); - finalVerify(); } private Message createMessage() { @@ -178,7 +176,6 @@ public class HTTPConduitURLEasyMockTest { @Test @org.junit.Ignore public void testSendWithHeadersCheckErrorStream() throws Exception { - control = EasyMock.createNiceControl(); HTTPConduit conduit = setUpConduit(true, false, "POST"); Message message = new MessageImpl(); message.put(HTTPConduit.SET_HTTP_RESPONSE_MESSAGE, Boolean.TRUE); @@ -187,36 +184,30 @@ public class HTTPConduitURLEasyMockTest { conduit.prepare(message); verifySentMessage(conduit, message, true, "POST", true); assertEquals(HTTP_RESPONSE_MESSAGE, inMessage.get(HTTPConduit.HTTP_RESPONSE_MESSAGE)); - finalVerify(); } @Test public void testSendHttpConnection() throws Exception { - control = EasyMock.createNiceControl(); HTTPConduit conduit = setUpConduit(true, false, "POST"); Message message = createMessage(); message.put(HTTPConduit.SET_HTTP_RESPONSE_MESSAGE, Boolean.TRUE); conduit.prepare(message); verifySentMessage(conduit, message, "POST"); assertEquals(HTTP_RESPONSE_MESSAGE, inMessage.get(HTTPConduit.HTTP_RESPONSE_MESSAGE)); - finalVerify(); } @Test public void testSendHttpConnectionAutoRedirect() throws Exception { - control = EasyMock.createNiceControl(); HTTPConduit conduit = setUpConduit(true, true, "POST"); Message message = createMessage(); message.put(HTTPConduit.SET_HTTP_RESPONSE_MESSAGE, Boolean.TRUE); conduit.prepare(message); verifySentMessage(conduit, message, "POST"); assertEquals(HTTP_RESPONSE_MESSAGE, inMessage.get(HTTPConduit.HTTP_RESPONSE_MESSAGE)); - finalVerify(); } @Test public void testSendHttpGetConnectionAutoRedirect() throws Exception { - control = EasyMock.createNiceControl(); HTTPConduit conduit = setUpConduit(true, true, "GET"); Message message = new MessageImpl(); message.put(HTTPConduit.SET_HTTP_RESPONSE_MESSAGE, Boolean.TRUE); @@ -225,12 +216,10 @@ public class HTTPConduitURLEasyMockTest { verifySentMessage(conduit, message, "GET"); assertEquals(HTTP_RESPONSE_MESSAGE, inMessage.get(HTTPConduit.HTTP_RESPONSE_MESSAGE)); conduit.close(message); - finalVerify(); } @Test public void testSendHttpGetConnection() throws Exception { - control = EasyMock.createNiceControl(); HTTPConduit conduit = setUpConduit(true, false, "GET"); Message message = new MessageImpl(); message.put(Message.HTTP_REQUEST_METHOD, "GET"); @@ -239,13 +228,11 @@ public class HTTPConduitURLEasyMockTest { verifySentMessage(conduit, message, "GET"); assertEquals(HTTP_RESPONSE_MESSAGE, inMessage.get(HTTPConduit.HTTP_RESPONSE_MESSAGE)); conduit.close(message); - finalVerify(); } @Test public void testSendOnewayChunkedEmptyPartialResponseProcessResponse() throws Exception { - control = EasyMock.createNiceControl(); HTTPConduit conduit = setUpConduit(true, false, "POST"); Message message = createMessage(); conduit.prepare(message); @@ -256,13 +243,11 @@ public class HTTPConduitURLEasyMockTest { ResponseDelimiter.CHUNKED, true, // empty response "POST"); - finalVerify(); } @Test public void testSendOnewayDoNotProcessResponse() throws Exception { - control = EasyMock.createNiceControl(); HTTPConduit conduit = setUpConduit(true, false, "POST"); Message message = createMessage(); conduit.prepare(message); @@ -272,13 +257,11 @@ public class HTTPConduitURLEasyMockTest { ResponseDelimiter.CHUNKED, true, // empty response "POST"); - finalVerify(); } @Test public void testSendTwowayDecoupledEmptyPartialResponse() throws Exception { - control = EasyMock.createNiceControl(); HTTPConduit conduit = setUpConduit(true, false, "POST"); Message message = createMessage(); conduit.prepare(message); @@ -288,12 +271,10 @@ public class HTTPConduitURLEasyMockTest { ResponseDelimiter.EOF, true, // empty response "POST"); - finalVerify(); } @Test public void testSendHttpConnectionAllowedRedirectVerbs() throws Exception { - control = EasyMock.createNiceControl(); HTTPConduit conduit = setUpConduit(true, false, "POST"); Message message = createMessage(); message.put("http.redirect.allowed.verbs", "POST"); @@ -302,7 +283,6 @@ public class HTTPConduitURLEasyMockTest { conduit.getClient().setAutoRedirect(true); verifySentMessage(conduit, message, "POST"); assertEquals(HTTP_RESPONSE_MESSAGE, inMessage.get(HTTPConduit.HTTP_RESPONSE_MESSAGE)); - finalVerify(); } private void setUpHeaders(Message message) { @@ -325,14 +305,11 @@ public class HTTPConduitURLEasyMockTest { } private void setUpExchange(Message message, boolean oneway) { - Exchange exchange = control.createMock(Exchange.class); + Exchange exchange = mock(Exchange.class); message.setExchange(exchange); - exchange.isOneWay(); - EasyMock.expectLastCall().andReturn(oneway).anyTimes(); - exchange.isSynchronous(); - EasyMock.expectLastCall().andReturn(true).anyTimes(); - exchange.isEmpty(); - EasyMock.expectLastCall().andReturn(true).anyTimes(); + when(exchange.isOneWay()).thenReturn(oneway); + when(exchange.isSynchronous()).thenReturn(true); + when(exchange.isEmpty()).thenReturn(true); } private HTTPConduit setUpConduit( @@ -342,50 +319,37 @@ public class HTTPConduitURLEasyMockTest { endpointInfo = new EndpointInfo(); endpointInfo.setAddress(NOWHERE + "bar/foo"); connectionFactory = - control.createMock(HttpsURLConnectionFactory.class); + mock(HttpsURLConnectionFactory.class); if (send) { - //proxy = control.createMock(Proxy.class); + //proxy = mock(Proxy.class); proxy = Proxy.NO_PROXY; connection = - control.createMock(HttpURLConnection.class); - connection.getURL(); - EasyMock.expectLastCall().andReturn(new URL(NOWHERE + "bar/foo")).anyTimes(); + mock(HttpURLConnection.class); + when(connection.getURL()).thenReturn(new URL(NOWHERE + "bar/foo")); - connectionFactory.createConnection((TLSClientParameters)EasyMock.isNull(), - EasyMock.eq(proxy), - EasyMock.eq(new URL(NOWHERE + "bar/foo"))); - EasyMock.expectLastCall().andReturn(connection); + when(connectionFactory.createConnection(nullable(TLSClientParameters.class), + eq(proxy), + eq(new URL(NOWHERE + "bar/foo")))).thenReturn(connection); - connection.setDoOutput(true); - EasyMock.expectLastCall(); - - connection.setRequestMethod(method); - EasyMock.expectLastCall(); + doNothing().when(connection).setDoOutput(true); + doNothing().when(connection).setRequestMethod(method); if (!autoRedirect && "POST".equals(method)) { - connection.setChunkedStreamingMode(-1); - EasyMock.expectLastCall(); + doNothing().when(connection).setChunkedStreamingMode(-1); } - connection.getRequestMethod(); - EasyMock.expectLastCall().andReturn(method).anyTimes(); + when(connection.getRequestMethod()).thenReturn(method); - connection.setInstanceFollowRedirects(false); - EasyMock.expectLastCall().times(1); + doNothing().when(connection).setInstanceFollowRedirects(false); - connection.setConnectTimeout(303030); - EasyMock.expectLastCall(); - connection.setReadTimeout(404040); - EasyMock.expectLastCall(); - connection.setUseCaches(false); - EasyMock.expectLastCall(); + doNothing().when(connection).setConnectTimeout(303030); + doNothing().when(connection).setReadTimeout(404040); + doNothing().when(connection).setUseCaches(false); } ExtensionManagerBus bus = new ExtensionManagerBus(); - control.replay(); - HTTPConduit conduit = new HTTPTestConduit(bus, endpointInfo, null, @@ -468,31 +432,22 @@ public class HTTPConduitURLEasyMockTest { boolean emptyResponse, String method) throws IOException { - control.verify(); - control.reset(); OutputStream wrappedOS = verifyRequestHeaders(message, expectHeaders, method); if (!"GET".equals(method)) { - os.write(PAYLOAD.getBytes(), 0, PAYLOAD.length()); - EasyMock.expectLastCall(); - - os.flush(); - EasyMock.expectLastCall(); - os.flush(); - EasyMock.expectLastCall(); - os.close(); - EasyMock.expectLastCall(); + doNothing().when(os).write(PAYLOAD.getBytes(), 0, PAYLOAD.length()); + + doNothing().when(os).flush(); + doNothing().when(os).flush(); + doNothing().when(os).close(); } setUpExchange(message, style == ResponseStyle.NONE || style == ResponseStyle.ONEWAY_NONE); - connection.getRequestMethod(); - EasyMock.expectLastCall().andReturn(method).anyTimes(); + when(connection.getRequestMethod()).thenReturn(method); verifyHandleResponse(style, delimiter, emptyResponse, conduit); - control.replay(); - wrappedOS.flush(); wrappedOS.flush(); wrappedOS.close(); @@ -516,8 +471,6 @@ public class HTTPConduitURLEasyMockTest { } } } - - finalVerify(); } private OutputStream verifyRequestHeaders(Message message, boolean expectHeaders, String method) @@ -528,31 +481,23 @@ public class HTTPConduitURLEasyMockTest { assertTrue("expected output stream format", message.getContentFormats().contains(OutputStream.class)); - connection.getRequestMethod(); - EasyMock.expectLastCall().andReturn(method).anyTimes(); + when(connection.getRequestMethod()).thenReturn(method); if (!"GET".equals(method)) { - os = EasyMock.createMock(OutputStream.class); - connection.getOutputStream(); - EasyMock.expectLastCall().andReturn(os); + os = mock(OutputStream.class); + when(connection.getOutputStream()).thenReturn(os); } message.put(HTTPConduit.KEY_HTTP_CONNECTION, connection); if (expectHeaders) { - connection.setRequestProperty(EasyMock.eq("Authorization"), - EasyMock.eq("Basic Qko6dmFsdWU=")); - EasyMock.expectLastCall(); - connection.setRequestProperty(EasyMock.eq("Content-Type"), - EasyMock.eq("text/xml;charset=utf8")); - EasyMock.expectLastCall(); - connection.setRequestProperty(EasyMock.eq("Accept"), - EasyMock.eq("text/xml;charset=utf8,text/plain")); - EasyMock.expectLastCall(); + doNothing().when(connection).setRequestProperty(eq("Authorization"), + eq("Basic Qko6dmFsdWU=")); + doNothing().when(connection).setRequestProperty(eq("Content-Type"), + eq("text/xml;charset=utf8")); + doNothing().when(connection).setRequestProperty(eq("Accept"), + eq("text/xml;charset=utf8,text/plain")); } - connection.getRequestProperties(); - EasyMock.expectLastCall().andReturn(new HashMap<String, List<String>>()).anyTimes(); - - control.replay(); + when(connection.getRequestProperties()).thenReturn(new HashMap<String, List<String>>()); AbstractThresholdOutputStream wrappedOS = (AbstractThresholdOutputStream) message.getContent(OutputStream.class); @@ -561,82 +506,66 @@ public class HTTPConduitURLEasyMockTest { wrappedOS.write(PAYLOAD.getBytes()); wrappedOS.unBuffer(); - control.verify(); - control.reset(); - return wrappedOS; } + @SuppressWarnings("unchecked") private void verifyHandleResponse(ResponseStyle style, ResponseDelimiter delimiter, boolean emptyResponse, HTTPConduit conduit) throws IOException { - connection.getHeaderFields(); - EasyMock.expectLastCall().andReturn(Collections.EMPTY_MAP).anyTimes(); + when(connection.getHeaderFields()).thenReturn(Collections.EMPTY_MAP); - connection.getResponseMessage(); - EasyMock.expectLastCall().andReturn(HTTP_RESPONSE_MESSAGE).anyTimes(); + when(connection.getResponseMessage()).thenReturn(HTTP_RESPONSE_MESSAGE); int responseCode = getResponseCode(style); if (conduit.getClient().isAutoRedirect()) { - connection.getResponseCode(); - EasyMock.expectLastCall().andReturn(301).once().andReturn(responseCode).anyTimes(); - connection.getURL(); - EasyMock.expectLastCall().andReturn(new URL(NOWHERE + "bar/foo/redirect")).anyTimes(); + when(connection.getResponseCode()).thenReturn(301).thenReturn(responseCode); + when(connection.getURL()).thenReturn(new URL(NOWHERE + "bar/foo/redirect")); } else { - connection.getResponseCode(); - EasyMock.expectLastCall().andReturn(responseCode).anyTimes(); + when(connection.getResponseCode()).thenReturn(responseCode); } switch (style) { case NONE: case DECOUPLED: - is = control.createMock(InputStream.class); - connection.getInputStream(); - EasyMock.expectLastCall().andReturn(is).anyTimes(); + is = mock(InputStream.class); + when(connection.getInputStream()).thenReturn(is); connection.getContentLength(); if (delimiter == ResponseDelimiter.CHUNKED || delimiter == ResponseDelimiter.EOF) { - EasyMock.expectLastCall().andReturn(-1).anyTimes(); + when(connection.getContentLength()).thenReturn(-1); if (delimiter == ResponseDelimiter.CHUNKED) { - connection.getHeaderField("Transfer-Encoding"); - EasyMock.expectLastCall().andReturn("chunked"); + when(connection.getHeaderField("Transfer-Encoding")).thenReturn("chunked"); } else if (delimiter == ResponseDelimiter.EOF) { - connection.getHeaderField("Connection"); - EasyMock.expectLastCall().andReturn("close"); + when(connection.getHeaderField("Connection")).thenReturn("close"); } - is.read(); if (emptyResponse) { - EasyMock.expectLastCall().andReturn(-1).anyTimes(); + when(is.read()).thenReturn(-1); } else { - EasyMock.expectLastCall().andReturn((int)'<'); + when(is.read()).thenReturn((int)'<'); } } else { - EasyMock.expectLastCall().andReturn(123).anyTimes(); + when(connection.getContentLength()).thenReturn(123); } if (emptyResponse) { - is.close(); - EasyMock.expectLastCall(); + doNothing().when(is).close(); } break; case BACK_CHANNEL: - is = EasyMock.createMock(InputStream.class); - connection.getInputStream(); - EasyMock.expectLastCall().andReturn(is).anyTimes(); + is = mock(InputStream.class); + when(connection.getInputStream()).thenReturn(is); break; case BACK_CHANNEL_ERROR: - is = EasyMock.createMock(InputStream.class); - connection.getInputStream(); - EasyMock.expectLastCall().andReturn(is).anyTimes(); - connection.getErrorStream(); - EasyMock.expectLastCall().andReturn(null); + is = mock(InputStream.class); + when(connection.getInputStream()).thenReturn(is); + when(connection.getErrorStream()).thenReturn(null); break; case ONEWAY_NONE: - connection.getInputStream(); - EasyMock.expectLastCall().andReturn(new ByteArrayInputStream(new byte[0])).anyTimes(); + when(connection.getInputStream()).thenReturn(new ByteArrayInputStream(new byte[0])); break; default: @@ -644,13 +573,6 @@ public class HTTPConduitURLEasyMockTest { } } - private void finalVerify() { - if (control != null) { - control.verify(); - control = null; - } - } - private int getResponseCode(ResponseStyle style) { int code; if (style == ResponseStyle.BACK_CHANNEL) { diff --git a/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HeadersTest.java b/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HeadersTest.java index 7facde2c72..9da35eef5b 100755 --- a/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HeadersTest.java +++ b/rt/transports/http/src/test/java/org/apache/cxf/transport/http/HeadersTest.java @@ -35,14 +35,14 @@ import org.apache.cxf.helpers.CastUtils; import org.apache.cxf.message.Message; import org.apache.cxf.message.MessageImpl; -import org.easymock.EasyMock; -import org.easymock.IMocksControl; import org.junit.Test; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * @@ -51,7 +51,6 @@ public class HeadersTest { @Test public void setHeadersTest() throws Exception { - IMocksControl control = EasyMock.createNiceControl(); String[] headerNames = {"Content-Type", "authorization", "soapAction"}; String[] headerValues = {"text/xml", "Basic Zm9vOmJhcg==", "foo"}; Map<String, List<String>> inmap = new HashMap<>(); @@ -59,15 +58,13 @@ public class HeadersTest { inmap.put(headerNames[i], Arrays.asList(headerValues[i])); } - HttpServletRequest req = control.createMock(HttpServletRequest.class); - EasyMock.expect(req.getHeaderNames()).andReturn(Collections.enumeration(inmap.keySet())); + HttpServletRequest req = mock(HttpServletRequest.class); + when(req.getHeaderNames()).thenReturn(Collections.enumeration(inmap.keySet())); for (int i = 0; i < headerNames.length; i++) { - EasyMock.expect(req.getHeaders(headerNames[i])). - andReturn(Collections.enumeration(inmap.get(headerNames[i]))); + when(req.getHeaders(headerNames[i])). + thenReturn(Collections.enumeration(inmap.get(headerNames[i]))); } - EasyMock.expect(req.getContentType()).andReturn(headerValues[0]).anyTimes(); - - control.replay(); + when(req.getContentType()).thenReturn(headerValues[0]); Message message = new MessageImpl(); message.put(AbstractHTTPDestination.HTTP_REQUEST, req); @@ -94,8 +91,6 @@ public class HeadersTest { assertEquals("unexpected header", protocolHeaders.get("soapaction").get(0), headerValues[2]); assertEquals("unexpected header", protocolHeaders.get("SOAPACTION").get(0), headerValues[2]); assertEquals("unexpected header", protocolHeaders.get("soapAction").get(0), headerValues[2]); - - control.verify(); } @Test @@ -170,8 +165,6 @@ public class HeadersTest { @Test public void nullContentTypeTest() { - IMocksControl control = EasyMock.createNiceControl(); - Message message = new MessageImpl(); // first check - content-type==null in message, nothing specified in request @@ -183,10 +176,10 @@ public class HeadersTest { // second check - null specified in request, valid content-type specified in message // expect that determineContentType returns the content-type specified in the message - HttpServletRequest req = control.createMock(HttpServletRequest.class); - EasyMock.expect(req.getHeaderNames()).andReturn(Collections.emptyEnumeration()); - EasyMock.expect(req.getContentType()).andReturn(null).anyTimes(); - control.replay(); + HttpServletRequest req = mock(HttpServletRequest.class); + when(req.getHeaderNames()).thenReturn(Collections.emptyEnumeration()); + when(req.getContentType()).thenReturn(null); + message = new MessageImpl(); message.put(Message.CONTENT_TYPE, "application/json"); headers = new Headers(message); @@ -194,23 +187,18 @@ public class HeadersTest { assertEquals("Unexpected content-type determined - expected application/json", "application/json", headers.determineContentType()); - control.verify(); - // third check - content-type==null in message, null in request // expect that determineContentType returns the default value of text/xml - control = EasyMock.createNiceControl(); - req = control.createMock(HttpServletRequest.class); - EasyMock.expect(req.getHeaderNames()).andReturn(Collections.emptyEnumeration()); - EasyMock.expect(req.getContentType()).andReturn(null).anyTimes(); - control.replay(); + req = mock(HttpServletRequest.class); + when(req.getHeaderNames()).thenReturn(Collections.emptyEnumeration()); + when(req.getContentType()).thenReturn(null); + message = new MessageImpl(); message.put(Message.CONTENT_TYPE, null); headers = new Headers(message); headers.copyFromRequest(req); assertEquals("Unexpected content-type determined - expected text/xml", "text/xml", headers.determineContentType()); - - control.verify(); } @Test diff --git a/rt/transports/http/src/test/java/org/apache/cxf/transport/http/policy/PolicyUtilsTest.java b/rt/transports/http/src/test/java/org/apache/cxf/transport/http/policy/PolicyUtilsTest.java index 2c10023dd7..5af0b07ea5 100644 --- a/rt/transports/http/src/test/java/org/apache/cxf/transport/http/policy/PolicyUtilsTest.java +++ b/rt/transports/http/src/test/java/org/apache/cxf/transport/http/policy/PolicyUtilsTest.java @@ -37,26 +37,16 @@ import org.apache.cxf.ws.policy.PolicyAssertion; import org.apache.cxf.ws.policy.PolicyDataEngineImpl; import org.apache.cxf.ws.policy.builder.jaxb.JaxbAssertion; -import org.easymock.EasyMock; -import org.easymock.IMocksControl; -import org.junit.Before; import org.junit.Test; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * */ public class PolicyUtilsTest { - - private IMocksControl control; - - @Before - public void setUp() { - control = EasyMock.createNiceControl(); - } - - @Test public void testAssertClientPolicyNoop() { testAssertPolicyNoop(true); @@ -69,24 +59,20 @@ public class PolicyUtilsTest { void testAssertPolicyNoop(boolean isRequestor) { PolicyDataEngine pde = new PolicyDataEngineImpl(null); - Message message = control.createMock(Message.class); - EasyMock.expect(message.get(AssertionInfoMap.class)).andReturn(null); - control.replay(); + Message message = mock(Message.class); + when(message.get(AssertionInfoMap.class)).thenReturn(null); pde.assertMessage(message, null, new ClientPolicyCalculator()); - control.verify(); - control.reset(); Collection<PolicyAssertion> as = new ArrayList<>(); AssertionInfoMap aim = new AssertionInfoMap(as); - EasyMock.expect(message.get(AssertionInfoMap.class)).andReturn(aim); - control.replay(); + when(message.get(AssertionInfoMap.class)).thenReturn(aim); + if (isRequestor) { pde.assertMessage(message, null, new ClientPolicyCalculator()); } else { pde.assertMessage(message, null, new ServerPolicyCalculator()); } - control.verify(); } @@ -108,7 +94,7 @@ public class PolicyUtilsTest { } void testAssertClientPolicy(boolean outbound) { - Message message = control.createMock(Message.class); + Message message = mock(Message.class); HTTPClientPolicy ep = new HTTPClientPolicy(); HTTPClientPolicy cmp = new HTTPClientPolicy(); @@ -127,21 +113,19 @@ public class PolicyUtilsTest { ais.add(cmai); ais.add(icmai); aim.put(new ClientPolicyCalculator().getDataClassName(), ais); - EasyMock.expect(message.get(AssertionInfoMap.class)).andReturn(aim); - Exchange ex = control.createMock(Exchange.class); - EasyMock.expect(message.getExchange()).andReturn(ex).atLeastOnce(); - EasyMock.expect(ex.getOutMessage()).andReturn(outbound ? message : null).atLeastOnce(); + when(message.get(AssertionInfoMap.class)).thenReturn(aim); + Exchange ex = mock(Exchange.class); + when(message.getExchange()).thenReturn(ex); + when(ex.getOutMessage()).thenReturn(outbound ? message : null); if (!outbound) { - EasyMock.expect(ex.getOutFaultMessage()).andReturn(null).atLeastOnce(); + when(ex.getOutFaultMessage()).thenReturn(null); } - control.replay(); PolicyDataEngine pde = new PolicyDataEngineImpl(null); pde.assertMessage(message, ep, new ClientPolicyCalculator()); assertTrue(eai.isAsserted()); assertTrue(cmai.isAsserted()); assertTrue(icmai.isAsserted()); - control.verify(); } @Test @@ -162,7 +146,7 @@ public class PolicyUtilsTest { } void testAssertServerPolicy(boolean outbound) { - Message message = control.createMock(Message.class); + Message message = mock(Message.class); HTTPServerPolicy ep = new HTTPServerPolicy(); HTTPServerPolicy mp = new HTTPServerPolicy(); HTTPServerPolicy cmp = new HTTPServerPolicy(); @@ -183,21 +167,19 @@ public class PolicyUtilsTest { AssertionInfoMap aim = new AssertionInfoMap(CastUtils.cast(Collections.EMPTY_LIST, PolicyAssertion.class)); aim.put(new ServerPolicyCalculator().getDataClassName(), ais); - EasyMock.expect(message.get(AssertionInfoMap.class)).andReturn(aim).atLeastOnce(); - Exchange ex = control.createMock(Exchange.class); - EasyMock.expect(message.getExchange()).andReturn(ex).atLeastOnce(); - EasyMock.expect(ex.getOutMessage()).andReturn(outbound ? message : null).atLeastOnce(); + when(message.get(AssertionInfoMap.class)).thenReturn(aim); + Exchange ex = mock(Exchange.class); + when(message.getExchange()).thenReturn(ex); + when(ex.getOutMessage()).thenReturn(outbound ? message : null); if (!outbound) { - EasyMock.expect(ex.getOutFaultMessage()).andReturn(null).atLeastOnce(); + when(ex.getOutFaultMessage()).thenReturn(null); } - control.replay(); new PolicyDataEngineImpl(null).assertMessage(message, ep, new ServerPolicyCalculator()); assertTrue(eai.isAsserted()); assertTrue(mai.isAsserted()); assertTrue(outbound ? cmai.isAsserted() : !cmai.isAsserted()); assertTrue(outbound ? icmai.isAsserted() : !icmai.isAsserted()); - control.verify(); } } \ No newline at end of file diff --git a/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/BaseUrlHelperTest.java b/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/BaseUrlHelperTest.java index fd936c1691..2a895e3018 100644 --- a/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/BaseUrlHelperTest.java +++ b/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/BaseUrlHelperTest.java @@ -20,32 +20,28 @@ package org.apache.cxf.transport.servlet; import javax.servlet.http.HttpServletRequest; -import org.easymock.EasyMock; import org.junit.Assert; import org.junit.Test; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + public class BaseUrlHelperTest { private String testGetBaseURL(String requestUrl, String contextPath, String servletPath, String pathInfo) { - HttpServletRequest req = EasyMock.createMock(HttpServletRequest.class); - req.getRequestURL(); - EasyMock.expectLastCall().andReturn(new StringBuffer(requestUrl)); + HttpServletRequest req = mock(HttpServletRequest.class); + when(req.getRequestURL()).thenReturn(new StringBuffer(requestUrl)); - req.getContextPath(); - EasyMock.expectLastCall().andReturn(contextPath).anyTimes(); - req.getServletPath(); - EasyMock.expectLastCall().andReturn(servletPath).anyTimes(); + when(req.getContextPath()).thenReturn(contextPath); + when(req.getServletPath()).thenReturn(servletPath); - req.getPathInfo(); - EasyMock.expectLastCall().andReturn(pathInfo).times(2); + when(req.getPathInfo()).thenReturn(pathInfo); String basePath = contextPath + servletPath; if (basePath.length() == 0) { - req.getRequestURI(); - EasyMock.expectLastCall().andReturn(pathInfo); + when(req.getRequestURI()).thenReturn(pathInfo); } - EasyMock.replay(req); return BaseUrlHelper.getBaseURL(req); } diff --git a/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/ServletControllerTest.java b/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/ServletControllerTest.java index a1c9713d91..4e5ce5f10a 100644 --- a/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/ServletControllerTest.java +++ b/rt/transports/http/src/test/java/org/apache/cxf/transport/servlet/ServletControllerTest.java @@ -32,37 +32,39 @@ import org.apache.cxf.transport.http.AbstractHTTPDestination; import org.apache.cxf.transport.http.DestinationRegistry; import org.apache.cxf.transport.servlet.servicelist.ServiceListGeneratorServlet; -import org.easymock.EasyMock; import org.junit.Test; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.isA; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; public class ServletControllerTest { - private HttpServletRequest req = EasyMock.createMock(HttpServletRequest.class); - private HttpServletResponse res = EasyMock.createMock(HttpServletResponse.class); - private DestinationRegistry registry = EasyMock.createMock(DestinationRegistry.class); - private HttpServlet serviceListGenerator = EasyMock.createMock(HttpServlet.class); + private HttpServletRequest req = mock(HttpServletRequest.class); + private HttpServletResponse res = mock(HttpServletResponse.class); + private DestinationRegistry registry = mock(DestinationRegistry.class); + private HttpServlet serviceListGenerator = mock(HttpServlet.class); private void setReq(String pathInfo, String requestUri, String styleSheet, String formatted) { - EasyMock.expect(req.getPathInfo()).andReturn(pathInfo).anyTimes(); - EasyMock.expect(req.getContextPath()).andReturn("").anyTimes(); - EasyMock.expect(req.getServletPath()).andReturn("").anyTimes(); - req.setAttribute(Message.BASE_PATH, "http://localhost:8080"); - EasyMock.expectLastCall().anyTimes(); - EasyMock.expect(req.getRequestURI()).andReturn(requestUri).times(2); - EasyMock.expect(req.getParameter("stylesheet")).andReturn(styleSheet); - EasyMock.expect(req.getParameter("formatted")).andReturn(formatted); - EasyMock.expect(req.getRequestURL()).andReturn(new StringBuffer("http://localhost:8080").append(requestUri)); - EasyMock.expect(registry.getDestinationsPaths()).andReturn(Collections.emptySet()).atLeastOnce(); - EasyMock.expect(registry.getDestinationForPath("", true)).andReturn(null).anyTimes(); + when(req.getPathInfo()).thenReturn(pathInfo); + when(req.getContextPath()).thenReturn(""); + when(req.getServletPath()).thenReturn(""); + when(req.getRequestURI()).thenReturn(requestUri); + when(req.getParameter("stylesheet")).thenReturn(styleSheet); + when(req.getParameter("formatted")).thenReturn(formatted); + when(req.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080").append(requestUri)); + when(registry.getDestinationsPaths()).thenReturn(Collections.emptySet()); + when(registry.getDestinationForPath("", true)).thenReturn(null); } private void expectServiceListGeneratorCalled() throws ServletException, IOException { - serviceListGenerator.service(EasyMock.isA(HttpServletRequest.class), - EasyMock.isA(HttpServletResponse.class)); - EasyMock.expectLastCall(); + verify(serviceListGenerator, atLeastOnce()).service(isA(HttpServletRequest.class), + isA(HttpServletResponse.class)); } private void expectServiceListGeneratorNotCalled() throws ServletException, IOException { @@ -71,90 +73,79 @@ public class ServletControllerTest { @Test public void testGenerateServiceListing() throws Exception { setReq(null, "/services", null, "true"); - expectServiceListGeneratorCalled(); - EasyMock.replay(req, registry, serviceListGenerator); TestServletController sc = new TestServletController(registry, serviceListGenerator); sc.invoke(req, res); assertFalse(sc.invokeDestinationCalled()); + verify(req, atLeastOnce()).setAttribute(Message.BASE_PATH, "http://localhost:8080"); + verify(req, times(1)).getRequestURI(); + expectServiceListGeneratorCalled(); } @Test public void testGenerateUnformattedServiceListing() throws Exception { - req.getPathInfo(); - EasyMock.expectLastCall().andReturn(null).anyTimes(); - req.getContextPath(); - EasyMock.expectLastCall().andReturn("").anyTimes(); - req.getServletPath(); - EasyMock.expectLastCall().andReturn("").anyTimes(); - req.getRequestURI(); - EasyMock.expectLastCall().andReturn("/services").times(2); - - req.getParameter("stylesheet"); - EasyMock.expectLastCall().andReturn(null); - req.getParameter("formatted"); - EasyMock.expectLastCall().andReturn("false"); - req.getRequestURL(); - EasyMock.expectLastCall().andReturn(new StringBuffer("http://localhost:8080/services")); - req.setAttribute(Message.BASE_PATH, "http://localhost:8080"); - EasyMock.expectLastCall().anyTimes(); - registry.getDestinationsPaths(); - EasyMock.expectLastCall().andReturn(Collections.emptySet()).atLeastOnce(); - registry.getDestinationForPath("", true); - EasyMock.expectLastCall().andReturn(null).anyTimes(); + when(req.getPathInfo()).thenReturn(null); + when(req.getContextPath()).thenReturn(""); + when(req.getServletPath()).thenReturn(""); + when(req.getRequestURI()).thenReturn("/services"); - expectServiceListGeneratorCalled(); - EasyMock.replay(req, registry, serviceListGenerator); + when(req.getParameter("stylesheet")).thenReturn(null); + when(req.getParameter("formatted")).thenReturn("false"); + when(req.getRequestURL()).thenReturn(new StringBuffer("http://localhost:8080/services")); + when(registry.getDestinationsPaths()).thenReturn(Collections.emptySet()); + when(registry.getDestinationForPath("", true)).thenReturn(null); TestServletController sc = new TestServletController(registry, serviceListGenerator); sc.invoke(req, res); assertFalse(sc.invokeDestinationCalled()); + + verify(req, atLeastOnce()).setAttribute(Message.BASE_PATH, "http://localhost:8080"); + verify(req, times(1)).getRequestURI(); + expectServiceListGeneratorCalled(); } @Test public void testHideServiceListing() throws Exception { - req.getPathInfo(); - EasyMock.expectLastCall().andReturn(null); - - registry.getDestinationForPath("", true); - EasyMock.expectLastCall().andReturn(null).atLeastOnce(); - AbstractHTTPDestination dest = EasyMock.createMock(AbstractHTTPDestination.class); - registry.checkRestfulRequest(""); - EasyMock.expectLastCall().andReturn(dest).atLeastOnce(); - dest.getBus(); - EasyMock.expectLastCall().andReturn(null).anyTimes(); - dest.getMessageObserver(); - EasyMock.expectLastCall().andReturn(EasyMock.createMock(MessageObserver.class)).atLeastOnce(); + when(req.getPathInfo()).thenReturn(null); - expectServiceListGeneratorNotCalled(); + when(registry.getDestinationForPath("", true)).thenReturn(null); + AbstractHTTPDestination dest = mock(AbstractHTTPDestination.class); + when(registry.checkRestfulRequest("")).thenReturn(dest); + when(dest.getBus()).thenReturn(null); + when(dest.getMessageObserver()).thenReturn(mock(MessageObserver.class)); - EasyMock.replay(req, registry, serviceListGenerator, dest); TestServletController sc = new TestServletController(registry, serviceListGenerator); sc.setHideServiceList(true); sc.invoke(req, res); assertTrue(sc.invokeDestinationCalled()); + + expectServiceListGeneratorNotCalled(); } @Test public void testDifferentServiceListPath() throws Exception { setReq(null, "/listing", null, "true"); - expectServiceListGeneratorCalled(); - EasyMock.replay(req, registry, serviceListGenerator); TestServletController sc = new TestServletController(registry, serviceListGenerator); sc.setServiceListRelativePath("/listing"); sc.invoke(req, res); assertFalse(sc.invokeDestinationCalled()); + + verify(req, atLeastOnce()).setAttribute(Message.BASE_PATH, "http://localhost:8080"); + verify(req, times(1)).getRequestURI(); + expectServiceListGeneratorCalled(); } @Test public void testHealthcheck() throws Exception { setReq(null, "/services", null, "true"); - EasyMock.expect(req.getAttribute(ServletController.AUTH_SERVICE_LIST)).andReturn(null); - EasyMock.expect(req.getMethod()).andReturn("HEAD"); + when(req.getAttribute(ServletController.AUTH_SERVICE_LIST)).thenReturn(null); + when(req.getMethod()).thenReturn("HEAD"); - EasyMock.replay(req, registry); TestServletController sc = new TestServletController(registry, new ServiceListGeneratorServlet(registry, null)); sc.invoke(req, res); assertFalse(sc.invokeDestinationCalled()); + + verify(req, atLeastOnce()).setAttribute(Message.BASE_PATH, "http://localhost:8080"); + verify(req, times(1)).getRequestURI(); }
