1616 */
1717package 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+
1924import java .io .IOException ;
2025import java .util .HashMap ;
2126import org .apache .activemq .broker .BrokerService ;
2227import org .apache .activemq .broker .Locker ;
2328import org .apache .activemq .broker .SuppressReplyException ;
2429import org .apache .activemq .util .LeaseLockerIOExceptionHandler ;
25- import org .apache .activemq .util .ServiceStopper ;
2630import 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 ;
3231import org .junit .Test ;
3332import org .slf4j .Logger ;
3433import 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
4135public 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