/** * @return true if the internal client exists and is connected, false otherwise */ public synchronized boolean hasValidClient() { // The dismissed client always return false for this statement. return this.messagingClient.isConnected(); }
@Override public Map<String,String> getConfiguration() { final Map<String,String> result; synchronized( this ) { result = this.messagingClient.getConfiguration(); } return result; }
/** * Resets the internal client. */ protected synchronized IMessagingClient resetInternalClient() { IMessagingClient oldClient = this.messagingClient; this.messagingClient = new JmxWrapperForMessagingClient( null ); return oldClient; }
@Test public void testSimpleWrappedMethods() throws Exception { // We do not run in OSGi, but it should not raise any error IMessagingClient messagingClient = Mockito.mock( IMessagingClient.class ); JmxWrapperForMessagingClient client = new JmxWrapperForMessagingClient( messagingClient ); // Simple wrapped methods client.closeConnection(); client.deleteMessagingServerArtifacts( Mockito.mock( Application.class )); Assert.assertEquals( 0, client.getConfiguration().size()); client.openConnection(); client.subscribe( Mockito.mock( MessagingContext.class )); client.unsubscribe( Mockito.mock( MessagingContext.class )); Mockito.verify( messagingClient ).closeConnection();; Mockito.verify( messagingClient ).deleteMessagingServerArtifacts( Mockito.any( Application.class ));; Mockito.verify( messagingClient ).getConfiguration(); Mockito.verify( messagingClient ).openConnection(); Mockito.verify( messagingClient ).subscribe( Mockito.any( MessagingContext.class )); Mockito.verify( messagingClient ).unsubscribe( Mockito.any( MessagingContext.class )); Mockito.verifyNoMoreInteractions( messagingClient ); }
@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 ); }
@Test public void testSetOwnerProperties() { IMessagingClient messagingClient = Mockito.mock( IMessagingClient.class ); JmxWrapperForMessagingClient client = new JmxWrapperForMessagingClient( messagingClient ); Assert.assertNull( client.getId()); client.setOwnerProperties( RecipientKind.DM, "domain", "app", "/root" ); Mockito.verify( messagingClient ).setOwnerProperties( RecipientKind.DM, "domain", "app", "/root" ); Assert.assertNotNull( client.getId()); Assert.assertEquals( MessagingUtils.buildId( RecipientKind.DM, "domain", "app", "/root" ), client.getId()); }
@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 ); }
@Override public void addMessagingClientFactory( final IMessagingClientFactory factory ) { this.logger.fine( "A new messaging factory was added: " + factory.getType()); synchronized( this ) { if( this.messagingClient.isDismissClient() && factory.getType().equals( this.messagingType )) { // This is the messaging factory we were expecting... // We can try to switch to this incoming factory right now! switchMessagingType( this.messagingType ); } } }
/** * 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(); } } }
@Override public void removeMessagingClientFactory( IMessagingClientFactory factory ) { String factoryType = factory != null ? factory.getType() : null; IMessagingClient oldClient = null; synchronized( this ) { if( this.messagingClient != null && this.messagingClient.getMessagingType().equals( factoryType )) { // This is the messaging factory we were using... // We must release our messaging client right now. oldClient = resetInternalClient(); } } terminateClient( oldClient, "The previous client could not be terminated correctly.", this.logger ); this.logger.fine( "A messaging factory was removed: " + factoryType ); }
@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 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 ); }
/** * Resets the internal client. */ protected synchronized IMessagingClient resetInternalClient() { IMessagingClient oldClient = this.messagingClient; this.messagingClient = new JmxWrapperForMessagingClient( null ); return oldClient; }
@Override public Map<String,String> getConfiguration() { final Map<String,String> result; synchronized( this ) { result = this.messagingClient.getConfiguration(); } return result; }
@Override public void addMessagingClientFactory( final IMessagingClientFactory factory ) { this.logger.fine( "A new messaging factory was added: " + factory.getType()); synchronized( this ) { if( this.messagingClient.isDismissClient() && factory.getType().equals( this.messagingType )) { // This is the messaging factory we were expecting... // We can try to switch to this incoming factory right now! switchMessagingType( this.messagingType ); } } }
/** * 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(); } } }
/** * @return true if the internal client exists and is connected, false otherwise */ public synchronized boolean hasValidClient() { // The dismissed client always return false for this statement. return this.messagingClient.isConnected(); }
@Override public void removeMessagingClientFactory( IMessagingClientFactory factory ) { String factoryType = factory != null ? factory.getType() : null; IMessagingClient oldClient = null; synchronized( this ) { if( this.messagingClient != null && this.messagingClient.getMessagingType().equals( factoryType )) { // This is the messaging factory we were using... // We must release our messaging client right now. oldClient = resetInternalClient(); } } terminateClient( oldClient, "The previous client could not be terminated correctly.", this.logger ); this.logger.fine( "A messaging factory was removed: " + factoryType ); }
@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 testConstructor_mockOsgi() { ServiceRegistration<MessagingApiMBean> serviceReg = Mockito.mock( ServiceRegistration.class ); BundleContext bundleCtx = Mockito.mock( BundleContext.class ); Mockito.when( bundleCtx.registerService( Mockito.eq( MessagingApiMBean.class ), Mockito.any( JmxWrapperForMessagingClient.class ), Mockito.any( Dictionary.class ))).thenReturn( serviceReg ); OsgiHelper osgiHelper = Mockito.mock( OsgiHelper.class ); Mockito.when( osgiHelper.findBundleContext()).thenReturn( bundleCtx ); IMessagingClient messagingClient = Mockito.mock( IMessagingClient.class ); JmxWrapperForMessagingClient client = new JmxWrapperForMessagingClient( messagingClient, osgiHelper ); Assert.assertNotNull( client.serviceReg ); Assert.assertEquals( serviceReg, client.serviceReg ); }