Skip to content

Commit 54f2e88

Browse files
kenliao94mattrpav
authored andcommitted
refactored the test to use Mockito
1 parent fb78188 commit 54f2e88

File tree

1 file changed

+18
-63
lines changed

1 file changed

+18
-63
lines changed

activemq-unit-tests/src/test/java/org/apache/activemq/store/jdbc/JDBCIOExceptionHandlerMockeryTest.java

Lines changed: 18 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -16,81 +16,44 @@
1616
*/
1717
package org.apache.activemq.store.jdbc;
1818

19+
import static org.junit.Assert.assertTrue;
20+
import static org.junit.Assert.fail;
21+
import static org.mockito.Mockito.mock;
22+
import static org.mockito.Mockito.when;
23+
1924
import java.io.IOException;
2025
import java.util.HashMap;
2126
import org.apache.activemq.broker.BrokerService;
2227
import org.apache.activemq.broker.Locker;
2328
import org.apache.activemq.broker.SuppressReplyException;
2429
import org.apache.activemq.util.LeaseLockerIOExceptionHandler;
25-
import org.apache.activemq.util.ServiceStopper;
2630
import org.apache.activemq.util.Wait;
27-
import org.jmock.Expectations;
28-
import org.jmock.Mockery;
29-
import org.jmock.States;
30-
import org.jmock.lib.legacy.ClassImposteriser;
31-
import org.junit.Ignore;
3231
import org.junit.Test;
3332
import org.slf4j.Logger;
3433
import org.slf4j.LoggerFactory;
3534

36-
37-
import static org.junit.Assert.assertTrue;
38-
import static org.junit.Assert.fail;
39-
40-
@Ignore // AMQ-9239 FIXME: mock / byte-buddy opens
4135
public class JDBCIOExceptionHandlerMockeryTest {
42-
4336
private static final Logger LOG = LoggerFactory.getLogger(JDBCIOExceptionHandlerMockeryTest.class);
4437
private HashMap<Thread, Throwable> exceptions = new HashMap<Thread, Throwable>();
4538

4639
@Test
4740
public void testShutdownWithoutTransportRestart() throws Exception {
48-
49-
Mockery context = new Mockery() {{
50-
setImposteriser(ClassImposteriser.INSTANCE);
51-
}};
52-
53-
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
54-
@Override
55-
public void uncaughtException(Thread t, Throwable e) {
56-
LOG.error("unexpected exception {} on thread {}", e, t);
57-
exceptions.put(t, e);
58-
}
41+
Thread.setDefaultUncaughtExceptionHandler((t, e) -> {
42+
LOG.error("unexpected exception {} on thread {}", e, t);
43+
exceptions.put(t, e);
5944
});
6045

61-
final BrokerService brokerService = context.mock(BrokerService.class);
62-
final JDBCPersistenceAdapter jdbcPersistenceAdapter = context.mock(JDBCPersistenceAdapter.class);
63-
final Locker locker = context.mock(Locker.class);
64-
65-
final States jdbcConn = context.states("jdbc").startsAs("down");
66-
final States broker = context.states("broker").startsAs("started");
67-
68-
// simulate jdbc up between hasLock and checkpoint, so hasLock fails to verify
69-
context.checking(new Expectations() {{
70-
allowing(brokerService).isStarted();
71-
will(returnValue(true));
72-
allowing(brokerService).isRestartAllowed();
73-
will(returnValue(false));
74-
allowing(brokerService).setSystemExitOnShutdown(with(false));
75-
allowing(brokerService).stopAllConnectors(with(any(ServiceStopper.class)));
76-
allowing(brokerService).getPersistenceAdapter();
77-
will(returnValue(jdbcPersistenceAdapter));
78-
allowing(jdbcPersistenceAdapter).allowIOResumption();
79-
allowing(jdbcPersistenceAdapter).getLocker();
80-
will(returnValue(locker));
81-
allowing(locker).keepAlive();
82-
when(jdbcConn.is("down"));
83-
will(returnValue(true));
84-
allowing(locker).keepAlive();
85-
when(jdbcConn.is("up"));
86-
will(returnValue(false));
87-
88-
allowing(jdbcPersistenceAdapter).checkpoint(with(true));
89-
then(jdbcConn.is("up"));
90-
allowing(brokerService).stop();
91-
then(broker.is("stopped"));
46+
// Create mocks
47+
BrokerService brokerService = mock(BrokerService.class);
48+
JDBCPersistenceAdapter jdbcPersistenceAdapter = mock(JDBCPersistenceAdapter.class);
49+
Locker locker = mock(Locker.class);
9250

93-
}});
51+
// Setup mock behaviors
52+
when(brokerService.isStarted()).thenReturn(true);
53+
when(brokerService.isRestartAllowed()).thenReturn(false);
54+
when(brokerService.getPersistenceAdapter()).thenReturn(jdbcPersistenceAdapter);
55+
when(jdbcPersistenceAdapter.getLocker()).thenReturn(locker);
56+
when(locker.keepAlive()).thenReturn(true); // Connection is down
9457

9558
LeaseLockerIOExceptionHandler underTest = new LeaseLockerIOExceptionHandler();
9659
underTest.setBrokerService(brokerService);
@@ -101,14 +64,6 @@ public void uncaughtException(Thread t, Throwable e) {
10164
} catch (SuppressReplyException expected) {
10265
}
10366

104-
assertTrue("broker stopped state triggered", Wait.waitFor(new Wait.Condition() {
105-
@Override
106-
public boolean isSatisified() throws Exception {
107-
LOG.info("broker state {}", broker);
108-
return broker.is("stopped").isActive();
109-
}
110-
}));
111-
context.assertIsSatisfied();
11267

11368
assertTrue("no exceptions: " + exceptions, exceptions.isEmpty());
11469
}

0 commit comments

Comments
 (0)