/** * Closes the connection of a messaging client and terminates it properly. * @param client the client (never null) * @param errorMessage the error message to log in case of problem * @param logger a logger */ static void terminateClient( IMessagingClient client, String errorMessage, Logger logger ) { try { logger.fine( "The reconfigurable client is requesting its internal connection to be closed." ); if( client != null ) client.closeConnection(); } catch( Exception e ) { logger.warning( errorMessage + " " + e.getMessage()); Utils.logException( logger, e ); } finally { // "unregisterService" was not merged with "closeConnection" // on purpose. What is specific to JMX is restricted to this class // and this bundle. Sub-classes may use "closeConnection" without // any side effect on the JMX part. if( client instanceof JmxWrapperForMessagingClient ) ((JmxWrapperForMessagingClient) client).unregisterService(); } } }
/** * Closes the connection of a messaging client and terminates it properly. * @param client the client (never null) * @param errorMessage the error message to log in case of problem * @param logger a logger */ static void terminateClient( IMessagingClient client, String errorMessage, Logger logger ) { try { logger.fine( "The reconfigurable client is requesting its internal connection to be closed." ); if( client != null ) client.closeConnection(); } catch( Exception e ) { logger.warning( errorMessage + " " + e.getMessage()); Utils.logException( logger, e ); } finally { // "unregisterService" was not merged with "closeConnection" // on purpose. What is specific to JMX is restricted to this class // and this bundle. Sub-classes may use "closeConnection" without // any side effect on the JMX part. if( client instanceof JmxWrapperForMessagingClient ) ((JmxWrapperForMessagingClient) client).unregisterService(); } } }
@Test public void testTerminateClient_2() throws Exception { Logger logger = Logger.getLogger( getClass().getName()); JmxWrapperForMessagingClient client = Mockito.mock( JmxWrapperForMessagingClient.class ); ReconfigurableClient.terminateClient( client, "", logger ); Mockito.verify( client ).closeConnection(); Mockito.verify( client ).unregisterService();; Mockito.verifyNoMoreInteractions( client ); }
@Test public void testTerminateClient_3() throws Exception { Logger logger = Logger.getLogger( getClass().getName()); JmxWrapperForMessagingClient client = Mockito.mock( JmxWrapperForMessagingClient.class ); Mockito.doThrow( new IOException( "for test" )).when( client ).closeConnection(); ReconfigurableClient.terminateClient( client, "", logger ); Mockito.verify( client ).closeConnection(); Mockito.verify( client ).unregisterService();; Mockito.verifyNoMoreInteractions( client ); }
@Test @SuppressWarnings( "unchecked" ) public void testUnregister_withRegistration_withException() { IMessagingClient messagingClient = Mockito.mock( IMessagingClient.class ); JmxWrapperForMessagingClient client = new JmxWrapperForMessagingClient( messagingClient ); ServiceRegistration<MessagingApiMBean> serviceReg = Mockito.mock( ServiceRegistration.class ); Mockito.doThrow( new RuntimeException( "for test" )).when( serviceReg ).unregister(); client.serviceReg = serviceReg; client.unregisterService(); Assert.assertNull( client.serviceReg ); Mockito.verify( serviceReg, Mockito.only()).unregister(); }
@Test @SuppressWarnings( "unchecked" ) public void testUnregister_withRegistration() { IMessagingClient messagingClient = Mockito.mock( IMessagingClient.class ); JmxWrapperForMessagingClient client = new JmxWrapperForMessagingClient( messagingClient ); ServiceRegistration<MessagingApiMBean> serviceReg = Mockito.mock( ServiceRegistration.class ); client.serviceReg = serviceReg; client.unregisterService(); Assert.assertNull( client.serviceReg ); Mockito.verify( serviceReg, Mockito.only()).unregister(); }
@Test public void testUnregister_noRegistration() { IMessagingClient messagingClient = Mockito.mock( IMessagingClient.class ); JmxWrapperForMessagingClient client = new JmxWrapperForMessagingClient( messagingClient ); Assert.assertNull( client.serviceReg ); client.unregisterService(); Assert.assertNull( client.serviceReg ); }