Java Code Examples for org.ehcache.impl.config.resilience.DefaultResilienceStrategyProviderConfiguration

Following code examples demonstrate how to use org.ehcache.impl.config.resilience.DefaultResilienceStrategyProviderConfigurationfrom ehcache. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.ehcache.impl.config.resilience.DefaultResilienceStrategyProviderConfigurationand various code implementation of this class.

  public void testDefaultLoaderWriterInstanceReturned() {
    ResilienceStrategy<?, ?> resilienceStrategy = mock(ResilienceStrategy.class);

    DefaultResilienceStrategyProviderConfiguration configuration = new DefaultResilienceStrategyProviderConfiguration();
    configuration.setDefaultLoaderWriterResilienceStrategy(resilienceStrategy);

    DefaultResilienceStrategyProvider provider = new DefaultResilienceStrategyProvider(configuration);

    assertThat(provider.createResilienceStrategy("foo", mock(CacheConfiguration.class), mock(RecoveryStore.class), mock(CacheLoaderWriter.class)), sameInstance(resilienceStrategy));
  } 


  public void testPreconfiguredInstanceConstructed() {
    DefaultResilienceStrategyProviderConfiguration configuration = new DefaultResilienceStrategyProviderConfiguration();
    configuration.addResilienceStrategyFor("foo", TestResilienceStrategy.class, "FooBar");

    DefaultResilienceStrategyProvider provider = new DefaultResilienceStrategyProvider(configuration);

    ResilienceStrategy<?, ?> resilienceStrategy = provider.createResilienceStrategy("foo", mock(CacheConfiguration.class), mock(RecoveryStore.class));
    assertThat(resilienceStrategy, instanceOf(TestResilienceStrategy.class));
    assertThat(((TestResilienceStrategy) resilienceStrategy).message, is("FooBar"));
  } 

  public void testDefaultInstanceConstructed() {
    DefaultResilienceStrategyProviderConfiguration configuration = new DefaultResilienceStrategyProviderConfiguration();
    configuration.setDefaultResilienceStrategy(TestResilienceStrategy.class, "FooBar");

    DefaultResilienceStrategyProvider provider = new DefaultResilienceStrategyProvider(configuration);

    ResilienceStrategy<?, ?> resilienceStrategy = provider.createResilienceStrategy("foo", mock(CacheConfiguration.class), mock(RecoveryStore.class));
    assertThat(resilienceStrategy, instanceOf(TestResilienceStrategy.class));
    assertThat(((TestResilienceStrategy) resilienceStrategy).message, is("FooBar"));
  } 

  public void testDefaultInstanceReturned() {
    ResilienceStrategy<?, ?> resilienceStrategy = mock(ResilienceStrategy.class);

    DefaultResilienceStrategyProviderConfiguration configuration = new DefaultResilienceStrategyProviderConfiguration();
    configuration.setDefaultResilienceStrategy(resilienceStrategy);

    DefaultResilienceStrategyProvider provider = new DefaultResilienceStrategyProvider(configuration);

    assertThat(provider.createResilienceStrategy("foo", mock(CacheConfiguration.class), mock(RecoveryStore.class)), sameInstance(resilienceStrategy));
  } 

  public void testPreconfiguredLoaderWriterInstanceReturned() {
    ResilienceStrategy<?, ?> resilienceStrategy = mock(ResilienceStrategy.class);

    DefaultResilienceStrategyProviderConfiguration configuration = new DefaultResilienceStrategyProviderConfiguration();
    configuration.addResilienceStrategyFor("foo", resilienceStrategy);

    DefaultResilienceStrategyProvider provider = new DefaultResilienceStrategyProvider(configuration);

    assertThat(provider.createResilienceStrategy("foo", mock(CacheConfiguration.class), mock(RecoveryStore.class), mock(CacheLoaderWriter.class)), sameInstance(resilienceStrategy));
  } 

  public void testProvidedLoaderWriterInstanceConstructed() {
    DefaultResilienceStrategyProviderConfiguration configuration = new DefaultResilienceStrategyProviderConfiguration();
    DefaultResilienceStrategyProvider provider = new DefaultResilienceStrategyProvider(configuration);

    CacheConfiguration<?, ?> cacheConfiguration = mock(CacheConfiguration.class);
    when(cacheConfiguration.getServiceConfigurations()).thenReturn(Collections.singleton(new DefaultResilienceStrategyConfiguration(TestResilienceStrategy.class, "FooBar")));

    ResilienceStrategy<?, ?> resilienceStrategy = provider.createResilienceStrategy("foo", cacheConfiguration, mock(RecoveryStore.class), mock(CacheLoaderWriter.class));
    assertThat(resilienceStrategy, instanceOf(TestResilienceStrategy.class));
    assertThat(((TestResilienceStrategy) resilienceStrategy).message, is("FooBar"));
  } 

  public void testProvidedLoaderWriterInstanceReturned() {
    ResilienceStrategy<?, ?> resilienceStrategy = mock(ResilienceStrategy.class);

    DefaultResilienceStrategyProviderConfiguration configuration = new DefaultResilienceStrategyProviderConfiguration();
    DefaultResilienceStrategyProvider provider = new DefaultResilienceStrategyProvider(configuration);

    CacheConfiguration<?, ?> cacheConfiguration = mock(CacheConfiguration.class);
    when(cacheConfiguration.getServiceConfigurations()).thenReturn(Collections.singleton(new DefaultResilienceStrategyConfiguration(resilienceStrategy)));

    assertThat(provider.createResilienceStrategy("foo", cacheConfiguration, mock(RecoveryStore.class), mock(CacheLoaderWriter.class)), sameInstance(resilienceStrategy));
  } 

  public void testPreconfiguredLoaderWriterInstanceConstructed() {
    DefaultResilienceStrategyProviderConfiguration configuration = new DefaultResilienceStrategyProviderConfiguration();
    configuration.addResilienceStrategyFor("foo", TestResilienceStrategy.class, "FooBar");

    DefaultResilienceStrategyProvider provider = new DefaultResilienceStrategyProvider(configuration);

    ResilienceStrategy<?, ?> resilienceStrategy = provider.createResilienceStrategy("foo", mock(CacheConfiguration.class), mock(RecoveryStore.class), mock(CacheLoaderWriter.class));
    assertThat(resilienceStrategy, instanceOf(TestResilienceStrategy.class));
    assertThat(((TestResilienceStrategy) resilienceStrategy).message, is("FooBar"));
  } 

  public void testPreconfiguredInstanceReturned() {
    ResilienceStrategy<?, ?> resilienceStrategy = mock(ResilienceStrategy.class);

    DefaultResilienceStrategyProviderConfiguration configuration = new DefaultResilienceStrategyProviderConfiguration();
    configuration.addResilienceStrategyFor("foo", resilienceStrategy);

    DefaultResilienceStrategyProvider provider = new DefaultResilienceStrategyProvider(configuration);

    assertThat(provider.createResilienceStrategy("foo", mock(CacheConfiguration.class), mock(RecoveryStore.class)), sameInstance(resilienceStrategy));
  } 

  public void testDefaultLoaderWriterInstanceConstructed() {
    DefaultResilienceStrategyProviderConfiguration configuration = new DefaultResilienceStrategyProviderConfiguration();
    configuration.setDefaultLoaderWriterResilienceStrategy(TestResilienceStrategy.class, "FooBar");

    DefaultResilienceStrategyProvider provider = new DefaultResilienceStrategyProvider(configuration);

    ResilienceStrategy<?, ?> resilienceStrategy = provider.createResilienceStrategy("foo", mock(CacheConfiguration.class), mock(RecoveryStore.class), mock(CacheLoaderWriter.class));
    assertThat(resilienceStrategy, instanceOf(TestResilienceStrategy.class));
    assertThat(((TestResilienceStrategy) resilienceStrategy).message, is("FooBar"));
  } 

  public void testProvidedInstanceConstructed() {
    DefaultResilienceStrategyProviderConfiguration configuration = new DefaultResilienceStrategyProviderConfiguration();
    DefaultResilienceStrategyProvider provider = new DefaultResilienceStrategyProvider(configuration);

    CacheConfiguration<?, ?> cacheConfiguration = mock(CacheConfiguration.class);
    when(cacheConfiguration.getServiceConfigurations()).thenReturn(Collections.singleton(new DefaultResilienceStrategyConfiguration(TestResilienceStrategy.class, "FooBar")));

    ResilienceStrategy<?, ?> resilienceStrategy = provider.createResilienceStrategy("foo", cacheConfiguration, mock(RecoveryStore.class));
    assertThat(resilienceStrategy, instanceOf(TestResilienceStrategy.class));
    assertThat(((TestResilienceStrategy) resilienceStrategy).message, is("FooBar"));
  } 

  public void testProvidedInstanceReturned() {
    ResilienceStrategy<?, ?> resilienceStrategy = mock(ResilienceStrategy.class);

    DefaultResilienceStrategyProviderConfiguration configuration = new DefaultResilienceStrategyProviderConfiguration();
    DefaultResilienceStrategyProvider provider = new DefaultResilienceStrategyProvider(configuration);

    CacheConfiguration<?, ?> cacheConfiguration = mock(CacheConfiguration.class);
    when(cacheConfiguration.getServiceConfigurations()).thenReturn(Collections.singleton(new DefaultResilienceStrategyConfiguration(resilienceStrategy)));

    assertThat(provider.createResilienceStrategy("foo", cacheConfiguration, mock(RecoveryStore.class)), sameInstance(resilienceStrategy));
  } 

  protected DefaultResilienceStrategyProvider(DefaultResilienceStrategyProviderConfiguration configuration) {
    this.regularStrategies = new ComponentProvider(configuration.getDefaultConfiguration(), configuration);
    this.loaderWriterStrategies = new ComponentProvider(configuration.getDefaultLoaderWriterConfiguration(), configuration);
  } 

  public void testSpecifiedConfigIsPassed() {
    ResilienceStrategy resilienceStrategy = mock(ResilienceStrategy.class);

    DefaultResilienceStrategyProviderConfiguration configuration = new DefaultResilienceStrategyProviderConfiguration();
    configuration.setDefaultResilienceStrategy(resilienceStrategy);
    ResilienceStrategyProvider provider = new DefaultResilienceStrategyProviderFactory().create(configuration);

    assertThat(provider.createResilienceStrategy("foo", mock(CacheConfiguration.class), mock(RecoveryStore.class)), sameInstance(resilienceStrategy));
  } 

  public ResilienceStrategyProvider create(ServiceCreationConfiguration<ResilienceStrategyProvider> configuration) {
    if (configuration == null) {
      return new DefaultResilienceStrategyProvider();
    } else if (configuration instanceof DefaultResilienceStrategyProviderConfiguration) {
      return new DefaultResilienceStrategyProvider((DefaultResilienceStrategyProviderConfiguration) configuration);
    } else {
      throw new IllegalArgumentException("Expected a configuration of type DefaultResilienceStrategyProviderConfiguration (or none) but got " + configuration
        .getClass()
        .getName());
    }
  } 

Advertisement
Javadoc
ServiceCreationConfiguration for the default ResilienceStrategyProvider.
Advertisement