Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CXF-8857: Get rid of EasyMock in cxf-rt-frontend-jaxws #1327

Merged
merged 1 commit into from
Jul 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions rt/frontend/jaxws/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,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 @@ -54,7 +54,6 @@
import org.apache.cxf.service.invoker.MethodDispatcher;
import org.apache.cxf.service.model.BindingOperationInfo;

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

import static org.junit.Assert.assertEquals;
Expand All @@ -64,23 +63,23 @@
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class JAXWSMethodInvokerTest {
private static final QName TEST_HEADER_NAME = new QName("testHeader");
Factory factory = EasyMock.createMock(Factory.class);
Object target = EasyMock.createMock(Hello.class);
Factory factory = mock(Factory.class);
Object target = mock(Hello.class);

@Test
public void testFactoryBeans() throws Throwable {
Exchange ex = EasyMock.createMock(Exchange.class);
EasyMock.reset(factory);
factory.create(ex);
EasyMock.expectLastCall().andReturn(target);
EasyMock.replay(factory);
Exchange ex = mock(Exchange.class);
when(factory.create(ex)).thenReturn(target);
JAXWSMethodInvoker jaxwsMethodInvoker = new JAXWSMethodInvoker(factory);
Object object = jaxwsMethodInvoker.getServiceObject(ex);
assertEquals("the target object and service object should be equal ", object, target);
EasyMock.verify(factory);
}


Expand All @@ -105,6 +104,8 @@ public void testSuspendedException() throws Throwable {
assertSame(suspendedEx, serviceObject.getSuspendedException());
assertSame(originalException, suspendedEx.getRuntimeException());
}

verify(factory, atLeastOnce()).release(ex, serviceObject);
}

@Test
Expand All @@ -124,6 +125,8 @@ public void testFaultAvoidHeadersCopy() throws Throwable {
Message outMsg = ex.getOutMessage();
assertNull(outMsg);
}

verify(factory, atLeastOnce()).release(ex, serviceObject);
}

@Test
Expand All @@ -150,6 +153,8 @@ public void testFaultHeadersCopy() throws Throwable {
assertEquals(1, headers.size());
assertEquals(TEST_HEADER_NAME, headers.get(0).getName());
}

verify(factory, atLeastOnce()).release(ex, serviceObject);
}

@Test
Expand Down Expand Up @@ -209,36 +214,27 @@ public void testProviderInterpretNullAsOneway() throws Throwable {
.invoke(ex, new MessageContentsList(new Object[]{new StreamSource()}));
assertNull(obj);
assertTrue(ex.isOneWay());

verify(factory, atLeastOnce()).release(ex, serviceObject);
}

private JAXWSMethodInvoker prepareJAXWSMethodInvoker(Exchange ex, Object serviceObject,
Method serviceMethod) throws Throwable {
EasyMock.reset(factory);
factory.create(ex);
EasyMock.expectLastCall().andReturn(serviceObject).anyTimes();
when(factory.create(ex)).thenReturn(serviceObject);
factory.release(ex, serviceObject);
EasyMock.expectLastCall().anyTimes();
EasyMock.replay(factory);

BindingOperationInfo boi = new BindingOperationInfo();
ex.put(BindingOperationInfo.class, boi);

Service serviceClass = EasyMock.createMock(Service.class);
serviceClass.size();
EasyMock.expectLastCall().andReturn(0).anyTimes();
serviceClass.isEmpty();
EasyMock.expectLastCall().andReturn(true).anyTimes();
Service serviceClass = mock(Service.class);
when(serviceClass.size()).thenReturn(0);
when(serviceClass.isEmpty()).thenReturn(true);
ex.put(Service.class, serviceClass);

MethodDispatcher md = EasyMock.createMock(MethodDispatcher.class);
serviceClass.get(MethodDispatcher.class.getName());
EasyMock.expectLastCall().andReturn(md).anyTimes();
MethodDispatcher md = mock(MethodDispatcher.class);
when(serviceClass.get(MethodDispatcher.class.getName())).thenReturn(md);

md.getMethod(boi);
EasyMock.expectLastCall().andReturn(serviceMethod).anyTimes();

EasyMock.replay(md);
EasyMock.replay(serviceClass);
when(md.getMethod(boi)).thenReturn(serviceMethod);

// initialize the contextCache
ex.getInMessage().getContextualProperty("dummy");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,66 +29,55 @@
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.Message;

import org.easymock.IMocksControl;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.easymock.EasyMock.createNiceControl;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class AbstractProtocolHandlerInterceptorTest {

private IMocksControl control;
private Binding binding;
private HandlerChainInvoker invoker;
private IIOPMessage message;
private Exchange exchange;

@Before
public void setUp() {
control = createNiceControl();
invoker = control.createMock(HandlerChainInvoker.class);
message = control.createMock(IIOPMessage.class);
exchange = control.createMock(Exchange.class);
binding = control.createMock(Binding.class);
invoker = mock(HandlerChainInvoker.class);
message = mock(IIOPMessage.class);
exchange = mock(Exchange.class);
binding = mock(Binding.class);

@SuppressWarnings("rawtypes")
List<Handler> list = new ArrayList<>();
list.add(null);
expect(binding.getHandlerChain()).andReturn(list).anyTimes();
}

@After
public void tearDown() {
control.verify();
when(binding.getHandlerChain()).thenReturn(list);
}

@Test
public void testInterceptSuccess() {
expect(message.getExchange()).andReturn(exchange).anyTimes();
expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker).anyTimes();
expect(invoker.invokeProtocolHandlers(eq(false),
isA(MessageContext.class))).andReturn(true);
expect(exchange.getOutMessage()).andReturn(message);
control.replay();
when(message.getExchange()).thenReturn(exchange);
when(exchange.get(HandlerChainInvoker.class)).thenReturn(invoker);
when(invoker.invokeProtocolHandlers(eq(false),
isA(MessageContext.class))).thenReturn(true);
when(exchange.getOutMessage()).thenReturn(message);
IIOPHandlerInterceptor pi = new IIOPHandlerInterceptor(binding);
assertEquals("unexpected phase", "user-protocol", pi.getPhase());
pi.handleMessage(message);
}

@Test
public void testInterceptFailure() {
expect(message.getExchange()).andReturn(exchange).anyTimes();
expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker).anyTimes();
expect(exchange.getOutMessage()).andReturn(message);
expect(
when(message.getExchange()).thenReturn(exchange);
when(exchange.get(HandlerChainInvoker.class)).thenReturn(invoker);
when(exchange.getOutMessage()).thenReturn(message);
when(
invoker.invokeProtocolHandlers(eq(false),
isA(MessageContext.class))).andReturn(false);
control.replay();
isA(MessageContext.class))).thenReturn(false);
IIOPHandlerInterceptor pi = new IIOPHandlerInterceptor(binding);
pi.handleMessage(message);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.apache.cxf.jaxws.handler.types.PortComponentHandlerType;
import org.apache.cxf.jaxws.handler.types.XsdStringType;

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

import static org.junit.Assert.assertEquals;
Expand All @@ -45,16 +44,17 @@
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;

@SuppressWarnings("rawtypes")
public class HandlerChainBuilderTest {

Handler[] allHandlers = {EasyMock.createMock(LogicalHandler.class), EasyMock.createMock(Handler.class),
EasyMock.createMock(Handler.class), EasyMock.createMock(LogicalHandler.class)};
Handler[] allHandlers = {mock(LogicalHandler.class), mock(Handler.class),
mock(Handler.class), mock(LogicalHandler.class)};
Handler[] logicalHandlers = {allHandlers[0], allHandlers[3]};
Handler[] protocolHandlers = {allHandlers[1], allHandlers[2]};

HandlerChainBuilder builder = new HandlerChainBuilder(EasyMock.createNiceMock(Bus.class));
HandlerChainBuilder builder = new HandlerChainBuilder(mock(Bus.class));


@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,32 @@
import org.apache.cxf.transport.MessageObserver;
import org.apache.handlers.types.AddNumbersResponse;

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

import static org.easymock.EasyMock.createNiceControl;
import static org.easymock.EasyMock.eq;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.isA;
import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class LogicalHandlerInterceptorTest {

private IMocksControl control;
private Binding binding;
private HandlerChainInvoker invoker;
private Message message;
private Exchange exchange;

@Before
public void setUp() {
control = createNiceControl();
binding = control.createMock(Binding.class);
invoker = control.createMock(HandlerChainInvoker.class);
message = control.createMock(Message.class);
exchange = control.createMock(Exchange.class);
binding = mock(Binding.class);
invoker = mock(HandlerChainInvoker.class);
message = mock(Message.class);
exchange = mock(Exchange.class);
}

@After
Expand All @@ -88,21 +87,19 @@ public boolean handleMessage(LogicalMessageContext arg0) {
});
@SuppressWarnings("rawtypes")
List<Handler> hList = CastUtils.cast(list);
expect(binding.getHandlerChain()).andReturn(hList).anyTimes();
expect(invoker.getLogicalHandlers()).andReturn(list);
expect(message.getExchange()).andReturn(exchange).anyTimes();
expect(message.get(Message.REQUESTOR_ROLE)).andReturn(Boolean.TRUE).anyTimes();
expect(message.keySet()).andReturn(new TreeSet<String>()).anyTimes();
expect(exchange.get(HandlerChainInvoker.class)).andReturn(invoker);
expect(exchange.getOutMessage()).andReturn(message);
expect(invoker.invokeLogicalHandlers(eq(true), isA(LogicalMessageContext.class)))
.andReturn(true);

control.replay();
when(binding.getHandlerChain()).thenReturn(hList);
when(invoker.getLogicalHandlers()).thenReturn(list);
when(message.getExchange()).thenReturn(exchange);
when(message.get(Message.REQUESTOR_ROLE)).thenReturn(Boolean.TRUE);
when(message.keySet()).thenReturn(new TreeSet<String>());
when(exchange.get(HandlerChainInvoker.class)).thenReturn(invoker);
when(exchange.getOutMessage()).thenReturn(message);
when(invoker.invokeLogicalHandlers(eq(true), isA(LogicalMessageContext.class)))
.thenReturn(true);

LogicalHandlerInInterceptor li = new LogicalHandlerInInterceptor(binding);
assertEquals("unexpected phase", "pre-protocol-frontend", li.getPhase());
assertEquals("unwhened phase", "pre-protocol-frontend", li.getPhase());
li.handleMessage(message);
control.verify();
}

//JAX-WS spec: If handler returns false, for a request-response MEP, if the message
Expand Down Expand Up @@ -132,28 +129,24 @@ public boolean handleMessage(LogicalMessageContext messageContext) {
});
HandlerChainInvoker invoker1 = new HandlerChainInvoker(list);

IMocksControl control1 = createNiceControl();
Binding binding1 = control1.createMock(Binding.class);
Binding binding1 = mock(Binding.class);
@SuppressWarnings("rawtypes")
List<Handler> hList = CastUtils.cast(list);
expect(binding1.getHandlerChain()).andReturn(hList).anyTimes();
Exchange exchange1 = control1.createMock(Exchange.class);
expect(exchange1.get(HandlerChainInvoker.class)).andReturn(invoker1).anyTimes();
when(binding1.getHandlerChain()).thenReturn(hList);
Exchange exchange1 = mock(Exchange.class);
when(exchange1.get(HandlerChainInvoker.class)).thenReturn(invoker1);
Message outMessage = new MessageImpl();
outMessage.setExchange(exchange1);
InterceptorChain chain = control1.createMock(InterceptorChain.class);
InterceptorChain chain = spy(InterceptorChain.class);
outMessage.setInterceptorChain(chain);
chain.abort();
EasyMock.expectLastCall();
MessageObserver observer = control1.createMock(MessageObserver.class);
expect(exchange1.get(MessageObserver.class)).andReturn(observer).anyTimes();
MessageObserver observer = spy(MessageObserver.class);
when(exchange1.get(MessageObserver.class)).thenReturn(observer);
observer.onMessage(isA(Message.class));
EasyMock.expectLastCall();

control1.replay();

LogicalHandlerInInterceptor li = new LogicalHandlerInInterceptor(binding1);
li.handleMessage(outMessage);
control1.verify();
verify(chain, atLeastOnce()).abort();
verify(observer, atLeastOnce()).onMessage(isA(Message.class));
}
}
Loading