Skip to content

Commit

Permalink
CXF-8856: Get rid of EasyMock in cxf-rt-frontend-jaxrs
Browse files Browse the repository at this point in the history
  • Loading branch information
reta committed Jul 5, 2023
1 parent 6d14fa3 commit de39e6b
Show file tree
Hide file tree
Showing 18 changed files with 215 additions and 308 deletions.
5 changes: 3 additions & 2 deletions rt/frontend/jaxrs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@
import org.apache.cxf.message.Message;
import org.apache.cxf.message.MessageImpl;

import org.easymock.EasyMock;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class SelectMethodCandidatesTest {

Expand Down Expand Up @@ -855,14 +856,12 @@ private OperationResourceInfo findTargetResourceClass(List<ClassResourceInfo> re
}

private static Endpoint mockEndpoint() {
Endpoint e = EasyMock.mock(Endpoint.class);
EasyMock.expect(e.isEmpty()).andReturn(true).anyTimes();
EasyMock.expect(e.size()).andReturn(0).anyTimes();
EasyMock.expect(e.getEndpointInfo()).andReturn(null).anyTimes();
EasyMock.expect(e.get(ServerProviderFactory.class.getName())).andReturn(ServerProviderFactory.getInstance())
.times(3);
EasyMock.expect(e.get("org.apache.cxf.jaxrs.comparator")).andReturn(null);
EasyMock.replay(e);
Endpoint e = mock(Endpoint.class);
when(e.isEmpty()).thenReturn(true);
when(e.size()).thenReturn(0);
when(e.getEndpointInfo()).thenReturn(null);
when(e.get(ServerProviderFactory.class.getName())).thenReturn(ServerProviderFactory.getInstance());
when(e.get("org.apache.cxf.jaxrs.comparator")).thenReturn(null);
return e;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,6 @@
import org.apache.cxf.phase.PhaseInterceptorChain;
import org.apache.cxf.transport.http.AbstractHTTPDestination;

import org.easymock.Capture;
import org.easymock.EasyMock;
import org.easymock.IAnswer;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.containsString;
Expand All @@ -70,6 +67,9 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class MessageContextImplTest {

Expand Down Expand Up @@ -144,7 +144,7 @@ public void testProviders() {
public void testHttpRequest() {
Message m = createMessage();
MessageContext mc = new MessageContextImpl(m);
HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
HttpServletRequest request = mock(HttpServletRequest.class);
m.put(AbstractHTTPDestination.HTTP_REQUEST, request);

assertSame(request.getClass(),
Expand All @@ -157,7 +157,7 @@ public void testHttpRequest() {
public void testHttpResponse() {
Message m = createMessage();
MessageContext mc = new MessageContextImpl(m);
HttpServletResponse request = EasyMock.createMock(HttpServletResponse.class);
HttpServletResponse request = mock(HttpServletResponse.class);
m.put(AbstractHTTPDestination.HTTP_RESPONSE, request);
HttpServletResponseFilter filter = (HttpServletResponseFilter)mc.getHttpServletResponse();
assertSame(request.getClass(), filter.getResponse().getClass());
Expand All @@ -169,7 +169,7 @@ public void testHttpResponse() {
public void testServletContext() {
Message m = createMessage();
MessageContext mc = new MessageContextImpl(m);
ServletContext request = EasyMock.createMock(ServletContext.class);
ServletContext request = mock(ServletContext.class);
m.put(AbstractHTTPDestination.HTTP_CONTEXT, request);
assertSame(request.getClass(), mc.getServletContext().getClass());
assertSame(request.getClass(), mc.getContext(ServletContext.class).getClass());
Expand All @@ -179,7 +179,7 @@ public void testServletContext() {
public void testServletConfig() {
Message m = createMessage();
MessageContext mc = new MessageContextImpl(m);
ServletConfig request = EasyMock.createMock(ServletConfig.class);
ServletConfig request = mock(ServletConfig.class);
m.put(AbstractHTTPDestination.HTTP_CONFIG, request);
assertSame(request.getClass(), mc.getServletConfig().getClass());
assertSame(request.getClass(), mc.getContext(ServletConfig.class).getClass());
Expand All @@ -196,9 +196,8 @@ public void testContextResolver() {
Exchange ex = new ExchangeImpl();
m.setExchange(ex);
ex.setInMessage(m);
Endpoint e = EasyMock.createMock(Endpoint.class);
EasyMock.expect(e.get(ServerProviderFactory.class.getName())).andReturn(factory);
EasyMock.replay(e);
Endpoint e = mock(Endpoint.class);
when(e.get(ServerProviderFactory.class.getName())).thenReturn(factory);
ex.put(Endpoint.class, e);
MessageContext mc = new MessageContextImpl(m);
ContextResolver<JAXBContext> resolver2 =
Expand Down Expand Up @@ -226,16 +225,7 @@ public void testAttachments() throws IOException {
in.getExchange().setOutMessage(out);

final Binding binding = in.getExchange().getEndpoint().getBinding();
final Capture<Message> capture = Capture.newInstance();
EasyMock.expect(binding.createMessage(EasyMock.capture(capture)))
.andAnswer(
new IAnswer<Message>() {
@Override
public Message answer() throws Throwable {
return capture.getValue();
}
}
).anyTimes();
when(binding.createMessage(any(Message.class))).thenAnswer(i -> i.getArguments()[0]);

final String id = UUID.randomUUID().toString();
final MultivaluedMap<String, String> headers = new MultivaluedHashMap<>();
Expand All @@ -256,15 +246,14 @@ private Message createMessage() {
Exchange e = new ExchangeImpl();
m.setExchange(e);
e.setInMessage(m);
Binding binding = EasyMock.mock(Binding.class);
Endpoint endpoint = EasyMock.mock(Endpoint.class);
EasyMock.expect(endpoint.getEndpointInfo()).andReturn(null).anyTimes();
EasyMock.expect(endpoint.get(Application.class.getName())).andReturn(null);
EasyMock.expect(endpoint.size()).andReturn(0).anyTimes();
EasyMock.expect(endpoint.isEmpty()).andReturn(true).anyTimes();
EasyMock.expect(endpoint.get(ServerProviderFactory.class.getName())).andReturn(factory).anyTimes();
EasyMock.expect(endpoint.getBinding()).andReturn(binding).anyTimes();
EasyMock.replay(endpoint);
Binding binding = mock(Binding.class);
Endpoint endpoint = mock(Endpoint.class);
when(endpoint.getEndpointInfo()).thenReturn(null);
when(endpoint.get(Application.class.getName())).thenReturn(null);
when(endpoint.size()).thenReturn(0);
when(endpoint.isEmpty()).thenReturn(true);
when(endpoint.get(ServerProviderFactory.class.getName())).thenReturn(factory);
when(endpoint.getBinding()).thenReturn(binding);
e.put(Endpoint.class, endpoint);
return m;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,29 @@
import org.apache.cxf.message.MessageImpl;
import org.apache.cxf.transport.http.Servlet3ContinuationProvider;

import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class AsyncResponseImplTest {

private IMocksControl control;

@Before
public void setUp() {
control = EasyMock.createNiceControl();
}

/**
* According to the spec, subsequent calls to cancel the same AsyncResponse should
* have the same behavior as the first call.
*/
@Test
public void testCancelBehavesTheSameWhenInvokedMultipleTimes() {
HttpServletRequest req = control.createMock(HttpServletRequest.class);
HttpServletResponse resp = control.createMock(HttpServletResponse.class);
AsyncContext asyncCtx = control.createMock(AsyncContext.class);
HttpServletRequest req = mock(HttpServletRequest.class);
HttpServletResponse resp = mock(HttpServletResponse.class);
AsyncContext asyncCtx = mock(AsyncContext.class);
Message msg = new MessageImpl();
msg.setExchange(new ExchangeImpl());
msg.put(ContinuationProvider.class.getName(), new Servlet3ContinuationProvider(req, resp, msg));

req.startAsync();
EasyMock.expectLastCall().andReturn(asyncCtx);
control.replay();
when(req.startAsync()).thenReturn(asyncCtx);

AsyncResponse impl = new AsyncResponseImpl(msg);

Expand Down Expand Up @@ -91,16 +80,14 @@ public void testCancelBehavesTheSameWhenInvokedMultipleTimes() {
*/
@Test
public void testCancelIntBehavesTheSameWhenInvokedMultipleTimes() {
HttpServletRequest req = control.createMock(HttpServletRequest.class);
HttpServletResponse resp = control.createMock(HttpServletResponse.class);
AsyncContext asyncCtx = control.createMock(AsyncContext.class);
HttpServletRequest req = mock(HttpServletRequest.class);
HttpServletResponse resp = mock(HttpServletResponse.class);
AsyncContext asyncCtx = mock(AsyncContext.class);
Message msg = new MessageImpl();
msg.setExchange(new ExchangeImpl());
msg.put(ContinuationProvider.class.getName(), new Servlet3ContinuationProvider(req, resp, msg));

req.startAsync();
EasyMock.expectLastCall().andReturn(asyncCtx);
control.replay();
when(req.startAsync()).thenReturn(asyncCtx);

AsyncResponse impl = new AsyncResponseImpl(msg);

Expand Down Expand Up @@ -128,16 +115,14 @@ public void testCancelIntBehavesTheSameWhenInvokedMultipleTimes() {
*/
@Test
public void testCancelDateBehavesTheSameWhenInvokedMultipleTimes() {
HttpServletRequest req = control.createMock(HttpServletRequest.class);
HttpServletResponse resp = control.createMock(HttpServletResponse.class);
AsyncContext asyncCtx = control.createMock(AsyncContext.class);
HttpServletRequest req = mock(HttpServletRequest.class);
HttpServletResponse resp = mock(HttpServletResponse.class);
AsyncContext asyncCtx = mock(AsyncContext.class);
Message msg = new MessageImpl();
msg.setExchange(new ExchangeImpl());
msg.put(ContinuationProvider.class.getName(), new Servlet3ContinuationProvider(req, resp, msg));

req.startAsync();
EasyMock.expectLastCall().andReturn(asyncCtx);
control.replay();
when(req.startAsync()).thenReturn(asyncCtx);

AsyncResponse impl = new AsyncResponseImpl(msg);

Expand Down Expand Up @@ -168,14 +153,12 @@ public void testCancelDateBehavesTheSameWhenInvokedMultipleTimes() {
*/
@Test
public void testNullContinutaion() {
HttpServletRequest req = control.createMock(HttpServletRequest.class);
AsyncContext asyncCtx = control.createMock(AsyncContext.class);
HttpServletRequest req = mock(HttpServletRequest.class);
AsyncContext asyncCtx = mock(AsyncContext.class);
Message msg = new MessageImpl();
msg.setExchange(new ExchangeImpl());

req.startAsync();
EasyMock.expectLastCall().andReturn(asyncCtx);
control.replay();
when(req.startAsync()).thenReturn(asyncCtx);

AsyncResponse impl;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,13 @@
import org.apache.cxf.service.model.EndpointInfo;
import org.apache.cxf.transport.servlet.ServletDestination;

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.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class RequestPreprocessorTest {

private IMocksControl control;

@Before
public void setUp() {
control = EasyMock.createNiceControl();
control.makeThreadSafe(true);
}

@Test
public void testMethodQuery() {
Message m = mockMessage("http://localhost:8080", "/bar", "_method=GET", "POST");
Expand Down Expand Up @@ -109,18 +97,17 @@ private Message mockMessage(String baseAddress,
m.put("org.apache.cxf.endpoint.private", false);
Exchange e = new ExchangeImpl();
m.setExchange(e);
control.reset();
Endpoint endp = control.mock(Endpoint.class);

Endpoint endp = mock(Endpoint.class);
e.put(Endpoint.class, endp);
EasyMock.expect(endp.isEmpty()).andReturn(true).anyTimes();
EasyMock.expect(endp.get(ServerProviderFactory.class.getName())).andReturn(ServerProviderFactory.getInstance())
.anyTimes();
ServletDestination d = control.createMock(ServletDestination.class);
when(endp.isEmpty()).thenReturn(true);
when(endp.get(ServerProviderFactory.class.getName())).thenReturn(ServerProviderFactory.getInstance());
ServletDestination d = mock(ServletDestination.class);
e.setDestination(d);
EndpointInfo epr = new EndpointInfo();
epr.setAddress(baseAddress);
EasyMock.expect(d.getEndpointInfo()).andReturn(epr).anyTimes();
EasyMock.expect(endp.getEndpointInfo()).andReturn(epr).anyTimes();
when(d.getEndpointInfo()).thenReturn(epr);
when(endp.getEndpointInfo()).thenReturn(epr);
m.put(Message.REQUEST_URI, pathInfo);
m.put(Message.QUERY_STRING, query);
m.put(Message.HTTP_REQUEST_METHOD, method);
Expand All @@ -129,11 +116,10 @@ private Message mockMessage(String baseAddress,
headers.put("X-HTTP-Method-Override", Collections.singletonList(methodHeader));
}
m.put(Message.PROTOCOL_HEADERS, headers);
BindingInfo bi = control.createMock(BindingInfo.class);
BindingInfo bi = mock(BindingInfo.class);
epr.setBinding(bi);
EasyMock.expect(bi.getProperties()).andReturn(Collections.emptyMap()).anyTimes();
when(bi.getProperties()).thenReturn(Collections.emptyMap());

control.replay();
return m;
}

Expand Down
Loading

0 comments on commit de39e6b

Please sign in to comment.