Skip to content

Commit

Permalink
CXF-8865: Get rid of EasyMock in cxf-rt-management (#1310)
Browse files Browse the repository at this point in the history
  • Loading branch information
reta authored Jun 26, 2023
1 parent 00824eb commit ba376d6
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 244 deletions.
5 changes: 3 additions & 2 deletions rt/management/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@
import org.apache.cxf.management.InstrumentationManager;
import org.apache.cxf.message.Message;

import org.easymock.EasyMock;
import org.junit.After;
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.doNothing;
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 CounterRepositoryTest {
private Bus bus;
Expand All @@ -52,31 +57,33 @@ public void setUp() throws Exception {

serviceCounter = new ObjectName("tandoori:type=counter,service=help");
operationCounter = new ObjectName("tandoori:type=counter,service=help,operation=me");
bus = EasyMock.createMock(Bus.class);
EasyMock.expect(bus.getInInterceptors()).andReturn(inlist).anyTimes();
EasyMock.expect(bus.getOutInterceptors()).andReturn(outlist).anyTimes();
EasyMock.expect(bus.getOutFaultInterceptors()).andReturn(faultlist).anyTimes();
bus.getExtension(InstrumentationManager.class);
EasyMock.expectLastCall().andReturn(null).anyTimes();
bus = mock(Bus.class);
when(bus.getInInterceptors()).thenReturn(inlist);
when(bus.getOutInterceptors()).thenReturn(outlist);
when(bus.getOutFaultInterceptors()).thenReturn(faultlist);
when(bus.getExtension(InstrumentationManager.class)).thenReturn(null);

cr = new CounterRepository();
bus.setExtension(cr, CounterRepository.class);
EasyMock.expectLastCall().once();
doNothing().when(bus).setExtension(cr, CounterRepository.class);

EasyMock.replay(bus);
cr.setBus(bus);
}

@After
public void tearDown() {
verify(bus, times(1)).setExtension(cr, CounterRepository.class);
}

@Test
public void testIncreaseOneWayResponseCounter() throws Exception {

//cr.createCounter(operationCounter, true);
MessageHandlingTimeRecorder mhtr = EasyMock.createMock(MessageHandlingTimeRecorder.class);
EasyMock.expect(mhtr.isOneWay()).andReturn(true).anyTimes();
EasyMock.expect(mhtr.getEndTime()).andReturn((long)100000000).anyTimes();
EasyMock.expect(mhtr.getHandlingTime()).andReturn((long)1000).anyTimes();
EasyMock.expect(mhtr.getFaultMode()).andReturn(null).anyTimes();
EasyMock.replay(mhtr);
MessageHandlingTimeRecorder mhtr = mock(MessageHandlingTimeRecorder.class);
when(mhtr.isOneWay()).thenReturn(true);
when(mhtr.getEndTime()).thenReturn((long)100000000);
when(mhtr.getHandlingTime()).thenReturn((long)1000);
when(mhtr.getFaultMode()).thenReturn(null);

cr.increaseCounter(serviceCounter, mhtr);
cr.increaseCounter(operationCounter, mhtr);
ResponseTimeCounter opCounter = (ResponseTimeCounter) cr.getCounter(operationCounter);
Expand All @@ -86,18 +93,17 @@ public void testIncreaseOneWayResponseCounter() throws Exception {
assertEquals("The Service counter isn't increased", sCounter.getNumInvocations(), 1);

verifyBus();
EasyMock.verify(mhtr);
}

@Test
public void testIncreaseOneWayNoResponseCounter() throws Exception {

//cr.createCounter(operationCounter, true);
MessageHandlingTimeRecorder mhtr = EasyMock.createMock(MessageHandlingTimeRecorder.class);
EasyMock.expect(mhtr.isOneWay()).andReturn(true).anyTimes();
EasyMock.expect(mhtr.getEndTime()).andReturn((long)0).anyTimes();
EasyMock.expect(mhtr.getFaultMode()).andReturn(null).anyTimes();
EasyMock.replay(mhtr);
MessageHandlingTimeRecorder mhtr = mock(MessageHandlingTimeRecorder.class);
when(mhtr.isOneWay()).thenReturn(true);
when(mhtr.getEndTime()).thenReturn((long)0);
when(mhtr.getFaultMode()).thenReturn(null);

cr.increaseCounter(serviceCounter, mhtr);
cr.increaseCounter(operationCounter, mhtr);
ResponseTimeCounter opCounter = (ResponseTimeCounter) cr.getCounter(operationCounter);
Expand All @@ -107,17 +113,16 @@ public void testIncreaseOneWayNoResponseCounter() throws Exception {
assertEquals("The Service counter isn't increased", sCounter.getNumInvocations(), 1);

verifyBus();
EasyMock.verify(mhtr);
}

@Test
public void testIncreaseResponseCounter() throws Exception {

MessageHandlingTimeRecorder mhtr1 = EasyMock.createMock(MessageHandlingTimeRecorder.class);
EasyMock.expect(mhtr1.isOneWay()).andReturn(false).anyTimes();
EasyMock.expect(mhtr1.getHandlingTime()).andReturn((long)1000).anyTimes();
EasyMock.expect(mhtr1.getFaultMode()).andReturn(null).anyTimes();
EasyMock.replay(mhtr1);
MessageHandlingTimeRecorder mhtr1 = mock(MessageHandlingTimeRecorder.class);
when(mhtr1.isOneWay()).thenReturn(false);
when(mhtr1.getHandlingTime()).thenReturn((long)1000);
when(mhtr1.getFaultMode()).thenReturn(null);

cr.createCounter(operationCounter);
cr.increaseCounter(serviceCounter, mhtr1);
cr.increaseCounter(operationCounter, mhtr1);
Expand All @@ -133,11 +138,11 @@ public void testIncreaseResponseCounter() throws Exception {
opCounter.getMinResponseTime(), (long)1000);
assertEquals("The Service counter isn't increased", sCounter.getNumInvocations(), 1);

MessageHandlingTimeRecorder mhtr2 = EasyMock.createMock(MessageHandlingTimeRecorder.class);
EasyMock.expect(mhtr2.isOneWay()).andReturn(false).anyTimes();
EasyMock.expect(mhtr2.getHandlingTime()).andReturn((long)2000).anyTimes();
EasyMock.expect(mhtr2.getFaultMode()).andReturn(null).anyTimes();
EasyMock.replay(mhtr2);
MessageHandlingTimeRecorder mhtr2 = mock(MessageHandlingTimeRecorder.class);
when(mhtr2.isOneWay()).thenReturn(false);
when(mhtr2.getHandlingTime()).thenReturn((long)2000);
when(mhtr2.getFaultMode()).thenReturn(null);

cr.increaseCounter(serviceCounter, mhtr2);
cr.increaseCounter(operationCounter, mhtr2);
assertEquals("The operation counter isn't increased", opCounter.getNumInvocations(), 2);
Expand All @@ -161,14 +166,10 @@ public void testIncreaseResponseCounter() throws Exception {
assertTrue(opCounter.getAvgResponseTime().intValue() == 0);

verifyBus();
EasyMock.verify(mhtr1);
EasyMock.verify(mhtr2);
}


private void verifyBus() {
EasyMock.verify(bus);

// the numbers should match the implementation of CounterRepository
assertEquals(2, inlist.size());
assertEquals(1, outlist.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,14 @@
import org.apache.cxf.service.model.EndpointInfo;
import org.apache.cxf.service.model.OperationInfo;

import org.easymock.EasyMock;
import org.junit.Before;

import static org.mockito.ArgumentMatchers.any;
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 AbstractMessageResponseTestBase {
protected static final QName SERVICE_NAME = new QName("http://org.apache.cxf", "hello");
Expand All @@ -63,10 +68,10 @@ public class AbstractMessageResponseTestBase {

@Before
public void setUp() throws Exception {
message = EasyMock.createMock(Message.class);
exchange = EasyMock.createMock(Exchange.class);
bus = EasyMock.createMock(Bus.class);
cRepository = EasyMock.createMock(CounterRepository.class);
message = mock(Message.class);
exchange = mock(Exchange.class);
bus = mock(Bus.class);
cRepository = mock(CounterRepository.class);
clientServiceCounterOName = new ObjectName(CLIENT_SERVICE_ONAME);
serverServiceCounterOName = new ObjectName(SERVER_SERVICE_ONAME);
clientOperationCounterOName = new ObjectName(CLIENT_SERVICE_ONAME
Expand All @@ -86,59 +91,49 @@ protected void setupCounterRepository(boolean increase, boolean isClient) {
operationCounterOName = serverOperationCounterOName;
}
BusFactory.setDefaultBus(bus);
bus.getExtension(CounterRepository.class);
EasyMock.expectLastCall().andReturn(cRepository).anyTimes();
when(bus.getExtension(CounterRepository.class)).thenReturn(cRepository);
if (increase) {
EasyMock.expect(bus.getId()).andReturn(Bus.DEFAULT_BUS_ID).anyTimes();
cRepository.increaseCounter(EasyMock.eq(serviceCounterOName),
EasyMock.isA(MessageHandlingTimeRecorder.class));
EasyMock.expectLastCall();
cRepository.increaseCounter(EasyMock.eq(operationCounterOName),
EasyMock.isA(MessageHandlingTimeRecorder.class));
EasyMock.expectLastCall();
EasyMock.expect(cRepository.getCounter(EasyMock.isA(ObjectName.class))).andReturn(null);
EasyMock.replay(cRepository);
when(bus.getId()).thenReturn(Bus.DEFAULT_BUS_ID);
doNothing().when(cRepository).increaseCounter(eq(serviceCounterOName),
isA(MessageHandlingTimeRecorder.class));

doNothing().when(cRepository).increaseCounter(eq(operationCounterOName),
isA(MessageHandlingTimeRecorder.class));

when(cRepository.getCounter(any(ObjectName.class))).thenReturn(null);
}

EasyMock.replay(bus);
// increase the number
}

protected void setupExchangeForMessage() {
EasyMock.expect(exchange.getBus()).andReturn(bus).anyTimes();

Service service = EasyMock.createMock(Service.class);
EasyMock.expect(service.getName()).andReturn(SERVICE_NAME).anyTimes();
EasyMock.expect(exchange.getService()).andReturn(service).anyTimes();
EasyMock.replay(service);
when(exchange.getBus()).thenReturn(bus);

Endpoint endpoint = EasyMock.createMock(Endpoint.class);
EndpointInfo endpointInfo = EasyMock.createMock(EndpointInfo.class);
EasyMock.expect(endpointInfo.getName()).andReturn(PORT_NAME).anyTimes();
EasyMock.expect(endpoint.getEndpointInfo()).andReturn(endpointInfo).anyTimes();
EasyMock.expect(endpoint.get("javax.management.ObjectName")).andReturn(null).anyTimes();
EasyMock.expect(endpoint.put(EasyMock.eq("javax.management.ObjectName"), EasyMock.anyObject(ObjectName.class)))
.andReturn(null).anyTimes();
EasyMock.expect(exchange.getEndpoint()).andReturn(endpoint).anyTimes();
EasyMock.replay(endpointInfo);
EasyMock.replay(endpoint);
Service service = mock(Service.class);
when(service.getName()).thenReturn(SERVICE_NAME);
when(exchange.getService()).thenReturn(service);

Endpoint endpoint = mock(Endpoint.class);
EndpointInfo endpointInfo = mock(EndpointInfo.class);
when(endpointInfo.getName()).thenReturn(PORT_NAME);
when(endpoint.getEndpointInfo()).thenReturn(endpointInfo);
when(endpoint.get("javax.management.ObjectName")).thenReturn(null);
when(endpoint.put(eq("javax.management.ObjectName"), any(ObjectName.class))).thenReturn(null);
when(exchange.getEndpoint()).thenReturn(endpoint);

//EasyMock.expect(exchange.getBus()).andReturn(bus);
EasyMock.expect(exchange.get("org.apache.cxf.management.service.counter.name")).andReturn(null).anyTimes();
when(exchange.get("org.apache.cxf.management.service.counter.name")).thenReturn(null);
}

protected void setupOperationForMessage() {
OperationInfo op = EasyMock.createMock(OperationInfo.class);
BindingOperationInfo bop = EasyMock.createMock(BindingOperationInfo.class);
EasyMock.expect(exchange.getBindingOperationInfo()).andReturn(bop);
EasyMock.expect(bop.getOperationInfo()).andReturn(op);
EasyMock.expect(op.getName()).andReturn(OPERATION_NAME);
EasyMock.expect(op.getProperty("javax.management.ObjectName", ObjectName.class)).andReturn(null).anyTimes();
op.setProperty(EasyMock.eq("javax.management.ObjectName"),
EasyMock.anyObject(ObjectName.class));
EasyMock.expectLastCall();
EasyMock.replay(bop, op);
OperationInfo op = mock(OperationInfo.class);
BindingOperationInfo bop = mock(BindingOperationInfo.class);
when(exchange.getBindingOperationInfo()).thenReturn(bop);
when(bop.getOperationInfo()).thenReturn(op);
when(op.getName()).thenReturn(OPERATION_NAME);
when(op.getProperty("javax.management.ObjectName", ObjectName.class)).thenReturn(null);
doNothing().when(op).setProperty(eq("javax.management.ObjectName"),
any(ObjectName.class));
}

}
Loading

0 comments on commit ba376d6

Please sign in to comment.