@Test
  public void testGettingToEhcacheConfiguration() {
    // tag::mutableConfigurationExample[]
    MutableConfiguration<Long, String> configuration = new MutableConfiguration<Long, String>();
    configuration.setTypes(Long.class, String.class);
    Cache<Long, String> cache = cacheManager.createCache("someCache", configuration); // <1>

    CompleteConfiguration<Long, String> completeConfiguration =
        cache.getConfiguration(CompleteConfiguration.class); // <2>

    Eh107Configuration<Long, String> eh107Configuration =
        cache.getConfiguration(Eh107Configuration.class); // <3>

    CacheRuntimeConfiguration<Long, String> runtimeConfiguration =
        eh107Configuration.unwrap(CacheRuntimeConfiguration.class); // <4>
    // end::mutableConfigurationExample[]
    assertThat(completeConfiguration, notNullValue());
    assertThat(runtimeConfiguration, notNullValue());

    // Check uses default JSR-107 expiry
    long nanoTime = System.nanoTime();
    LOGGER.info("Seeding random with {}", nanoTime);
    Random random = new Random(nanoTime);
    assertThat(
        runtimeConfiguration
            .getExpiry()
            .getExpiryForCreation(random.nextLong(), Long.toOctalString(random.nextLong())),
        equalTo(org.ehcache.expiry.Duration.FOREVER));
    assertThat(
        runtimeConfiguration
            .getExpiry()
            .getExpiryForAccess(random.nextLong(), Long.toOctalString(random.nextLong())),
        nullValue());
    assertThat(
        runtimeConfiguration
            .getExpiry()
            .getExpiryForUpdate(
                random.nextLong(),
                Long.toOctalString(random.nextLong()),
                Long.toOctalString(random.nextLong())),
        nullValue());
  }
コード例 #2
0
  @Test
  public void testWithoutEhcacheExplicitDependencyAndNoCodeChanges() throws Exception {
    CacheManager manager =
        cachingProvider.getCacheManager(
            getClass()
                .getResource("/org/ehcache/docs/ehcache-jsr107-template-override.xml")
                .toURI(),
            getClass().getClassLoader());

    // tag::jsr107SupplementWithTemplatesExample[]
    MutableConfiguration<Long, String> mutableConfiguration =
        new MutableConfiguration<Long, String>();
    mutableConfiguration.setTypes(Long.class, String.class); // <1>

    Cache<Long, String> anyCache = manager.createCache("anyCache", mutableConfiguration); // <2>

    CacheRuntimeConfiguration<Long, String> ehcacheConfig =
        (CacheRuntimeConfiguration<Long, String>)
            anyCache
                .getConfiguration(Eh107Configuration.class)
                .unwrap(CacheRuntimeConfiguration.class); // <3>
    ehcacheConfig.getResourcePools().getPoolForResource(ResourceType.Core.HEAP).getSize(); // <4>

    Cache<Long, String> anotherCache = manager.createCache("byRefCache", mutableConfiguration);
    assertFalse(anotherCache.getConfiguration(Configuration.class).isStoreByValue()); // <5>

    MutableConfiguration<String, String> otherConfiguration =
        new MutableConfiguration<String, String>();
    otherConfiguration.setTypes(String.class, String.class);
    otherConfiguration.setExpiryPolicyFactory(
        CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE)); // <6>

    Cache<String, String> foosCache = manager.createCache("foos", otherConfiguration); // <7>
    CacheRuntimeConfiguration<Long, String> foosEhcacheConfig =
        (CacheRuntimeConfiguration<Long, String>)
            foosCache
                .getConfiguration(Eh107Configuration.class)
                .unwrap(CacheRuntimeConfiguration.class);
    foosEhcacheConfig.getExpiry().getExpiryForCreation(42L, "Answer!").getAmount(); // <8>

    CompleteConfiguration<String, String> foosConfig =
        foosCache.getConfiguration(CompleteConfiguration.class);

    try {
      final Factory<ExpiryPolicy> expiryPolicyFactory = foosConfig.getExpiryPolicyFactory();
      ExpiryPolicy expiryPolicy = expiryPolicyFactory.create(); // <9>
      throw new AssertionError("Expected UnsupportedOperationException");
    } catch (UnsupportedOperationException e) {
      // Expected
    }
    // end::jsr107SupplementWithTemplatesExample[]
    assertThat(
        ehcacheConfig.getResourcePools().getPoolForResource(ResourceType.Core.HEAP).getSize(),
        is(20L));
    assertThat(
        foosEhcacheConfig.getExpiry().getExpiryForCreation(42L, "Answer!"),
        is(new org.ehcache.expiry.Duration(2, TimeUnit.MINUTES)));
  }