CsmConfigurationProviderChain implements CsmConfigurationProvider { @Override public CsmConfiguration getConfiguration() { for (CsmConfigurationProvider p : providers) { try { return p.getConfiguration(); } catch (SdkClientException e) { if (log.isDebugEnabled()) { log.debug("Unable to load configuration from " + p.toString() + ": " + e.getMessage()); } } } throw new SdkClientException("Could not resolve client side monitoring configuration from" + " the configured providers in the chain"); } CsmConfigurationProviderChain(CsmConfigurationProvider... providers); @Override CsmConfiguration getConfiguration();  }
@Test(expected = SdkClientException.class) public void testThrowsSdkClientExceptionIfEndOfChainReached() { CsmConfigurationProvider provider = mock(CsmConfigurationProvider.class); when(provider.getConfiguration()).thenThrow(new SdkClientException("")); CsmConfigurationProviderChain chain = new CsmConfigurationProviderChain(provider); chain.getConfiguration(); }
@Test public void testReturnsTheFirstResult() { CsmConfigurationProvider provider1 = mock(CsmConfigurationProvider.class); CsmConfigurationProvider provider2 = mock(CsmConfigurationProvider.class); CsmConfiguration cfg1 = new CsmConfiguration(true, 1234, "foo"); CsmConfiguration cfg2 = new CsmConfiguration(false, 5678, "bar"); when(provider1.getConfiguration()).thenReturn(cfg1); when(provider2.getConfiguration()).thenReturn(cfg2); CsmConfigurationProviderChain chain = new CsmConfigurationProviderChain(provider1, provider2); assertEquals(cfg1, chain.getConfiguration()); }
@Test public void testContinuesUntilAProviderReturnsAConfiguration() { CsmConfigurationProvider provider1 = mock(CsmConfigurationProvider.class); CsmConfigurationProvider provider2 = mock(CsmConfigurationProvider.class); CsmConfigurationProvider provider3 = mock(CsmConfigurationProvider.class); CsmConfiguration cfg = new CsmConfiguration(true, 1234, "foo"); when(provider1.getConfiguration()).thenThrow(new SdkClientException("")); when(provider2.getConfiguration()).thenThrow(new SdkClientException("")); when(provider3.getConfiguration()).thenReturn(cfg); CsmConfigurationProviderChain chain = new CsmConfigurationProviderChain(provider1, provider2, provider3); assertEquals(cfg, chain.getConfiguration()); }