Java Code Examples for javax.cache.configuration.MutableConfiguration#setTypes()

The following examples show how to use javax.cache.configuration.MutableConfiguration#setTypes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CacheMBStatisticsBeanTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void testExpiryOnCreation() throws Exception {

    // close cache since need to configure cache with ExpireOnCreationPolicy
    CacheManager mgr = cache.getCacheManager();
    mgr.destroyCache(cache.getName());

    MutableConfiguration<Long, String> config = new MutableConfiguration<Long, String>();
    config.setStatisticsEnabled(true);
    config.setTypes(Long.class, String.class);
    config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(ExpireOnCreationPolicy.class));

    cache = mgr.createCache(getTestCacheName(), config);
    cache.put(1L, "hello");
    assertEquals(0L, lookupManagementAttribute(cache, CacheStatistics, "CachePuts"));

    Map<Long, String> map = new HashMap<Long, String>();
    map.put(2L, "goodbye");
    map.put(3L, "world");
    cache.putAll(map);
    assertEquals(0L, lookupManagementAttribute(cache, CacheStatistics, "CachePuts"));
}
 
Example 2
Source File: ConfigStatsManagementActivationTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void basicJsr107StillWorks() throws Exception {
  CacheManager cacheManager = provider.getCacheManager();

  MutableConfiguration<Long, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(Long.class, String.class);
  configuration.setManagementEnabled(true);
  configuration.setStatisticsEnabled(true);

  Cache<Long, String> cache = cacheManager.createCache("cache", configuration);
  @SuppressWarnings("unchecked")
  Eh107Configuration<Long, String> eh107Configuration = cache.getConfiguration(Eh107Configuration.class);

  assertThat(eh107Configuration.isManagementEnabled(), is(true));
  assertThat(eh107Configuration.isStatisticsEnabled(), is(true));

  assertThat(isMbeanRegistered("cache", MBEAN_MANAGEMENT_TYPE), is(true));
  assertThat(isMbeanRegistered("cache", MBEAN_STATISTICS_TYPE), is(true));
}
 
Example 3
Source File: ConfigStatsManagementActivationTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testManagementEnabledOverriddenFromTemplate() throws Exception {
  CacheManager cacheManager = provider.getCacheManager(getClass().getResource("/ehcache-107-mbeans-template-config.xml")
          .toURI(),
      provider.getDefaultClassLoader());

  MutableConfiguration<Long, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(Long.class, String.class);
  configuration.setManagementEnabled(true);
  configuration.setStatisticsEnabled(true);

  Cache<Long, String> cache = cacheManager.createCache("disables-mbeans", configuration);

  @SuppressWarnings("unchecked")
  Eh107Configuration<Long, String> eh107Configuration = cache.getConfiguration(Eh107Configuration.class);
  assertThat(eh107Configuration.isManagementEnabled(), is(false));
  assertThat(eh107Configuration.isStatisticsEnabled(), is(false));

  assertThat(isMbeanRegistered("disables-mbeans", MBEAN_MANAGEMENT_TYPE), is(false));
  assertThat(isMbeanRegistered("disables-mbeans", MBEAN_STATISTICS_TYPE), is(false));
}
 
Example 4
Source File: CacheWriterTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * Configure write-through before each test.
 */
@Before
public void onBeforeEachTest() throws IOException {
  // establish and open a CacheWriterServer to handle cache
  // cache loading requests from a CacheWriterClient
  cacheWriter = new RecordingCacheWriter<>();
  cacheWriterServer = new CacheWriterServer<>(10000, cacheWriter);
  cacheWriterServer.open();

  // establish the CacheManager for the tests
  cacheManager = Caching.getCachingProvider().getCacheManager();

  // establish a CacheWriterClient that a Cache can use for writing/deleting entries
  // (via the CacheWriterServer)
  CacheWriterClient<Integer, String> theCacheWriter = new CacheWriterClient<>(cacheWriterServer.getInetAddress(),
      cacheWriterServer.getPort());

  MutableConfiguration<Integer, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(Integer.class, String.class);
  configuration.setCacheWriterFactory(FactoryBuilder.factoryOf(theCacheWriter));
  configuration.setWriteThrough(true);

  getCacheManager().createCache("cache-writer-test", configuration);
  cache = getCacheManager().getCache("cache-writer-test", Integer.class, String.class);
}
 
Example 5
Source File: CacheLoaderTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * Establish the {@link CacheManager} and {@link Cache} for a test.
 */
@Before
public void onBeforeEachTest() throws IOException {
  //establish and open a CacheLoaderServer to handle cache
  //cache loading requests from a CacheLoaderClient
  cacheLoaderServer = new CacheLoaderServer<String, String>(10000);
  cacheLoaderServer.open();

  //establish the CacheManager for the tests
  cacheManager = Caching.getCachingProvider().getCacheManager();

  //establish a CacheLoaderClient that a Cache can use for loading entries
  //(via the CacheLoaderServer)
  CacheLoaderClient<String, String> cacheLoader =
      new CacheLoaderClient<>(cacheLoaderServer.getInetAddress(), cacheLoaderServer.getPort());

  //establish a Cache Configuration that uses a CacheLoader and Read-Through
  MutableConfiguration<String, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(String.class, String.class);
  configuration.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
  configuration.setReadThrough(true);

  //configure the cache
  cacheManager.createCache("cache-loader-test", configuration);
  cache = cacheManager.getCache("cache-loader-test", String.class, String.class);
}
 
Example 6
Source File: CacheLoaderWithoutReadThroughTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * Establish the {@link javax.cache.CacheManager} and {@link javax.cache.Cache} for a test.
 */
@Before
public void onBeforeEachTest() throws IOException {
  //establish and open a CacheLoaderServer to handle cache
  //cache loading requests from a CacheLoaderClient
  cacheLoaderServer = new CacheLoaderServer<String, String>(10000);
  cacheLoaderServer.open();

  //establish the CacheManager for the tests
  cacheManager = Caching.getCachingProvider().getCacheManager();

  //establish a CacheLoaderClient that a Cache can use for loading entries
  //(via the CacheLoaderServer)
  CacheLoaderClient<String, String> cacheLoader =
      new CacheLoaderClient<>(cacheLoaderServer.getInetAddress(), cacheLoaderServer.getPort());

  //establish a Cache Configuration that uses a CacheLoader (no Read-Through)
  MutableConfiguration<String, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(String.class, String.class);
  configuration.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
  configuration.setReadThrough(false);

  //configure the cache
  cacheManager.createCache("cache-loader-test", configuration);
  cache = cacheManager.getCache("cache-loader-test", String.class, String.class);
}
 
Example 7
Source File: CacheLoaderWriterTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
/**
 * Establish the {@link javax.cache.CacheManager} and {@link Cache} for a test.
 */
@Before
public void onBeforeEachTest() throws IOException {
  //establish and open a CacheLoaderServer to handle cache
  //cache loading requests from a CacheLoaderClient
  recordingCacheLoader = new RecordingCacheLoader<String>();
  cacheLoaderServer = new CacheLoaderServer<String, String>(10000, recordingCacheLoader);
  cacheLoaderServer.open();

  // establish and open a CacheWriterServer to handle cache
  // cache loading requests from a CacheWriterClient
  recordingCacheWriter = new RecordingCacheWriter<>();
  cacheWriterServer = new CacheWriterServer<>(10001, recordingCacheWriter);
  cacheWriterServer.open();

  //establish the CacheManager for the tests
  cacheManager = Caching.getCachingProvider().getCacheManager();

  //establish a CacheLoaderClient that a Cache can use for loading entries
  //(via the CacheLoaderServer)
  CacheLoaderClient<String, String> cacheLoader =
      new CacheLoaderClient<>(cacheLoaderServer.getInetAddress(), cacheLoaderServer.getPort());

  // establish a CacheWriterClient that a Cache can use for writing/deleting entries
  // (via the CacheWriterServer)
  CacheWriterClient<String, String> cacheWriter = new CacheWriterClient<>(cacheWriterServer.getInetAddress(),
      cacheWriterServer.getPort());

  //establish a Cache Configuration that uses a CacheLoader and Read-Through
  MutableConfiguration<String, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(String.class, String.class);
  configuration.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
  configuration.setReadThrough(true);
  configuration.setCacheWriterFactory(FactoryBuilder.factoryOf(cacheWriter));
  configuration.setWriteThrough(true);

  //configure the cache
  cacheManager.createCache("cache-loader-writer-test", configuration);
  cache = cacheManager.getCache("cache-loader-writer-test", String.class, String.class);
}
 
Example 8
Source File: TypesTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
/**
 * What happens when you:
 *
 * 1) declare using generics with a super class
 * 2) declare using configuration with a sub class
 *
 * Set generics to Identifier and Dog
 * Bypass generics with a raw MutableConfiguration but set runtime to Identifier and Hound
 *
 * The configuration checking gets done on put.
 */
@Test
public void genericsEnforcementAndStricterTypeEnforcement() {

  //configure the cache
  MutableConfiguration config = new MutableConfiguration<>();
  config.setTypes(Identifier.class, Hound.class);
  Cache<Identifier, Dog> cache = cacheManager.createCache(cacheName, config);

  //Types are restricted and types are enforced
  //Cannot put in wrong types
  //cache.put(1, "something");

  //can put in
  cache.put(pistachio.getName(), pistachio);
  //can put in with generics but possibly not with configuration as not a hound
  try {
    cache.put(tonto.getName(), tonto);
  } catch (ClassCastException e) {
    //expected but not mandatory. The RI throws these.
  }

  //cannot get out wrong key types
  //assertNotNull(cache.get(1));
  assertNotNull(cache.get(pistachio.getName()));
  //not necessarily
  //assertNotNull(cache.get(tonto.getName()));

  //cannot remove wrong key types
  //assertTrue(cache.remove(1));
  assertTrue(cache.remove(pistachio.getName()));
  //not necessarily
  //assertTrue(cache.remove(tonto.getName()));
}
 
Example 9
Source File: LoaderWriterConfigTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
private MutableConfiguration<Long, String> getConfiguration(final boolean readThrough, final CacheLoader<Long, String> cacheLoader,
                                                            final boolean writeThrough, final CacheWriter<Long, String> cacheWriter) {
  MutableConfiguration<Long, String> config = new MutableConfiguration<>();
  config.setTypes(Long.class, String.class);
  config.setReadThrough(readThrough);
  config.setCacheLoaderFactory(() -> cacheLoader);
  config.setWriteThrough(writeThrough);
  config.setCacheWriterFactory(() -> cacheWriter);
  return config;
}
 
Example 10
Source File: StatisticsTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  CachingProvider provider = Caching.getCachingProvider();
  cacheManager = provider.getCacheManager(getClass().getResource("/ehcache-107-stats.xml").toURI(), ClassLoader.getSystemClassLoader());
  MutableConfiguration<String, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(String.class, String.class);
  heapCache = cacheManager.createCache("heap", configuration);
  heapStatistics = (Eh107CacheStatisticsMXBean) ((Eh107Cache<String, String>) heapCache).getStatisticsMBean();
  offheapCache = cacheManager.createCache("offheap", configuration);
  offheapStatistics = (Eh107CacheStatisticsMXBean) ((Eh107Cache<String, String>) offheapCache).getStatisticsMBean();
  diskCache = cacheManager.createCache("disk", configuration);
  diskStatistics = (Eh107CacheStatisticsMXBean) ((Eh107Cache<String, String>) diskCache).getStatisticsMBean();
}
 
Example 11
Source File: TypesTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
/**
 * Same as above but using the shorthand Caching to acquire.
 * Should work the same.
 */
@Test
public void genericsEnforcementAndStricterTypeEnforcementFromCaching() {

  //configure the cache
  MutableConfiguration config = new MutableConfiguration<>();
  config.setTypes(Identifier.class, Hound.class);
  Cache<Identifier, Dog> cache = cacheManager.createCache(cacheName, config);

  //Types are restricted and types are enforced
  //Cannot put in wrong types
  //cache.put(1, "something");

  //can put in
  cache.put(pistachio.getName(), pistachio);
  //can put in with generics but possibly not with configuration as not a hound
  try {
    cache.put(tonto.getName(), tonto);
  } catch (ClassCastException e) {
    //expected but not mandatory. The RI throws these.
  }

  //cannot get out wrong key types
  //assertNotNull(cache.get(1));
  assertNotNull(cache.get(pistachio.getName()));
  //not necessarily
  //assertNotNull(cache.get(tonto.getName()));

  //cannot remove wrong key types
  //assertTrue(cache.remove(1));
  assertTrue(cache.remove(pistachio.getName()));
  //not necessarily
  //assertTrue(cache.remove(tonto.getName()));
}
 
Example 12
Source File: ConfigurationMergerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void jsr107DefaultEh107IdentityCopierForImmutableTypesWithoutTemplates() {
  MutableConfiguration<Long, String> stringCacheConfiguration  = new MutableConfiguration<>();
  stringCacheConfiguration.setTypes(Long.class, String.class);
  ConfigurationMerger.ConfigHolder<Long, String> configHolder1 = merger.mergeConfigurations("stringCache", stringCacheConfiguration);

  assertDefaultCopier(configHolder1.cacheConfiguration.getServiceConfigurations());
}
 
Example 13
Source File: ConfigurationMergerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void jsr107DefaultEh107IdentityCopierForImmutableTypes() {
  XmlConfiguration xmlConfiguration = new XmlConfiguration(getClass().getResource("/ehcache-107-copiers-immutable-types.xml"));
  DefaultJsr107Service jsr107Service = new DefaultJsr107Service(ServiceUtils.findSingletonAmongst(Jsr107Configuration.class, xmlConfiguration.getServiceCreationConfigurations()));
  merger = new ConfigurationMerger(xmlConfiguration, jsr107Service, mock(Eh107CacheLoaderWriterProvider.class));

  MutableConfiguration<Long, String> stringCacheConfiguration  = new MutableConfiguration<>();
  stringCacheConfiguration.setTypes(Long.class, String.class);
  ConfigurationMerger.ConfigHolder<Long, String> configHolder1 = merger.mergeConfigurations("stringCache", stringCacheConfiguration);

  assertDefaultCopier(configHolder1.cacheConfiguration.getServiceConfigurations());

  MutableConfiguration<Long, Double> doubleCacheConfiguration  = new MutableConfiguration<>();
  doubleCacheConfiguration.setTypes(Long.class, Double.class);
  ConfigurationMerger.ConfigHolder<Long, Double> configHolder2 = merger.mergeConfigurations("doubleCache", doubleCacheConfiguration);

  assertDefaultCopier(configHolder2.cacheConfiguration.getServiceConfigurations());

  MutableConfiguration<Long, Character> charCacheConfiguration  = new MutableConfiguration<>();
  charCacheConfiguration.setTypes(Long.class, Character.class);
  ConfigurationMerger.ConfigHolder<Long, Character> configHolder3 = merger.mergeConfigurations("charCache", charCacheConfiguration);

  assertDefaultCopier(configHolder3.cacheConfiguration.getServiceConfigurations());

  MutableConfiguration<Long, Float> floatCacheConfiguration  = new MutableConfiguration<>();
  floatCacheConfiguration.setTypes(Long.class, Float.class);
  ConfigurationMerger.ConfigHolder<Long, Float> configHolder4 = merger.mergeConfigurations("floatCache", floatCacheConfiguration);

  assertDefaultCopier(configHolder4.cacheConfiguration.getServiceConfigurations());

  MutableConfiguration<Long, Integer> integerCacheConfiguration  = new MutableConfiguration<>();
  integerCacheConfiguration.setTypes(Long.class, Integer.class);
  ConfigurationMerger.ConfigHolder<Long, Integer> configHolder5 = merger.mergeConfigurations("integerCache", integerCacheConfiguration);

  assertDefaultCopier(configHolder5.cacheConfiguration.getServiceConfigurations());

}
 
Example 14
Source File: ConfigurationMergerTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void setWriteThroughWithoutWriterFails() {
  MutableConfiguration<Long, String> config = new MutableConfiguration<>();
  config.setTypes(Long.class, String.class);
  config.setWriteThrough(true);

  try {
    merger.mergeConfigurations("cache", config);
    fail("Expected exception as no CacheLoader factory is configured and read-through is enabled.");
  } catch (IllegalArgumentException e) {
    assertThat(e.getMessage(), containsString("write-through"));
  }
}
 
Example 15
Source File: Eh107XmlIntegrationTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void test107ExpiryOverriddenByEhcacheTemplateExpiry() {
  final AtomicBoolean expiryFactoryInvoked = new AtomicBoolean(false);
  MutableConfiguration<Long, Product> configuration = new MutableConfiguration<>();
  configuration.setTypes(Long.class, Product.class);
  configuration.setExpiryPolicyFactory(() -> {
    expiryFactoryInvoked.set(true);
    return new CreatedExpiryPolicy(Duration.FIVE_MINUTES);
  });

  cacheManager.createCache("productCache3", configuration);
  assertThat(expiryFactoryInvoked.get(), is(false));
}
 
Example 16
Source File: TypesTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
/**
 * What happens when you:
 *
 * 1) declare using generics and
 * 2) specify types during configuration but using Object.class, which is permissive
 */
@Test
public void simpleAPITypeEnforcementObject() {


  //configure the cache
  MutableConfiguration<Object, Object> config = new MutableConfiguration<>();
  config.setTypes(Object.class, Object.class);

  //create the cache
  Cache<Object, Object> cache = cacheManager.createCache("simpleCache4", config);

  //can put different things in
  cache.put(1, "something");
  cache.put(pistachio.getName(), pistachio);
  cache.put(tonto.getName(), tonto);
  cache.put(bonzo.getName(), bonzo);
  cache.put(juno.getName(), juno);
  cache.put(talker.getName(), talker);
  try {
    cache.put(skinny.getName(), skinny);
  } catch(Exception e) {
    //not serializable expected
  }
  //can get them out
  assertNotNull(cache.get(1));
  assertNotNull(cache.get(pistachio.getName()));

  //can remove them
  assertTrue(cache.remove(1));
  assertTrue(cache.remove(pistachio.getName()));
}
 
Example 17
Source File: EhCache107ConfigurationIntegrationDocTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testTemplateOverridingStoreByValue() throws Exception {
  cacheManager = cachingProvider.getCacheManager(
      getClass().getResource("/org/ehcache/docs/ehcache-jsr107-template-override.xml").toURI(),
      getClass().getClassLoader());

  MutableConfiguration<Long, Client> mutableConfiguration = new MutableConfiguration<>();
  mutableConfiguration.setTypes(Long.class, Client.class);

  Client client1 = new Client("client1", 1);

  Cache<Long, Client> myCache = null;
  myCache = cacheManager.createCache("anyCache", mutableConfiguration);
  myCache.put(1L, client1);
  assertNotSame(client1, myCache.get(1L));
  assertTrue(myCache.getConfiguration(Configuration.class).isStoreByValue());

  myCache = cacheManager.createCache("byRefCache", mutableConfiguration);
  myCache.put(1L, client1);
  assertSame(client1, myCache.get(1L));
  assertFalse(myCache.getConfiguration(Configuration.class).isStoreByValue());

  myCache = cacheManager.createCache("weirdCache1", mutableConfiguration);
  myCache.put(1L, client1);
  assertNotSame(client1, myCache.get(1L));
  assertTrue(myCache.getConfiguration(Configuration.class).isStoreByValue());

  myCache = cacheManager.createCache("weirdCache2", mutableConfiguration);
  myCache.put(1L, client1);
  assertSame(client1, myCache.get(1L));
  assertFalse(myCache.getConfiguration(Configuration.class).isStoreByValue());
}
 
Example 18
Source File: EhCache107ConfigurationIntegrationDocTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testGettingToEhcacheConfiguration() {
  // tag::mutableConfigurationExample[]
  MutableConfiguration<Long, String> configuration = new MutableConfiguration<>();
  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.getExpiryPolicy().getExpiryForCreation(random.nextLong(), Long.toOctalString(random.nextLong())),
              equalTo(org.ehcache.expiry.ExpiryPolicy.INFINITE));
  assertThat(runtimeConfiguration.getExpiryPolicy().getExpiryForAccess(random.nextLong(),
    () -> Long.toOctalString(random.nextLong())), nullValue());
  assertThat(runtimeConfiguration.getExpiryPolicy().getExpiryForUpdate(random.nextLong(),
    () -> Long.toOctalString(random.nextLong()), Long.toOctalString(random.nextLong())), nullValue());
}
 
Example 19
Source File: JCacheAdapter.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <C extends Configuration<K, V>> C getConfiguration(Class<C> _class) {
  if (CompleteConfiguration.class.isAssignableFrom(_class)) {
    MutableConfiguration<K, V> cfg = new MutableConfiguration<K, V>();
    cfg.setTypes(keyType, valueType);
    cfg.setStatisticsEnabled(jmxStatisticsEnabled);
    cfg.setManagementEnabled(jmxEnabled);
    cfg.setStoreByValue(storeByValue);
    Collection<CacheEntryListenerConfiguration<K,V>> _listenerConfigurations = eventHandling.getAllListenerConfigurations();
    for (CacheEntryListenerConfiguration<K,V> _listenerConfig : _listenerConfigurations) {
      cfg.addCacheEntryListenerConfiguration(_listenerConfig);
    }
    return (C) cfg;
  }
  return (C) new Configuration<K, V>() {
    @Override
    public Class<K> getKeyType() {
      return keyType;
    }

    @Override
    public Class<V> getValueType() {
      return valueType;
    }

    @Override
    public boolean isStoreByValue() {
      return storeByValue;
    }
  };
}
 
Example 20
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 4 votes vote down vote up
/**
 * Ensure that a cache using a {@link javax.cache.expiry.ExpiryPolicy} configured to
 * return a {@link Duration#ZERO} for newly created entries will immediately
 * expire the entries.
 */
private void expire_whenCreated(Factory<? extends ExpiryPolicy> expiryPolicyFactory) {
  MutableConfiguration<Integer, Integer> config = new MutableConfiguration<Integer, Integer>();
  config.setTypes(Integer.class, Integer.class);
  config.setExpiryPolicyFactory(expiryPolicyFactory);
  config = extraSetup(config);
  config.setStatisticsEnabled(true);

  Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);

  cache.put(1, 1);


  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));
  assertEquals(0, listener.getCreated());

  cache.put(1, 1);

  assertFalse(cache.remove(1));

  cache.put(1, 1);

  assertFalse(cache.remove(1, 1));

  cache.getAndPut(1, 1);
  assertEquals(0, listener.getCreated());

  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  cache.putIfAbsent(1, 1);
  assertEquals(0, listener.getCreated());

  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
  map.put(1, 1);
  cache.putAll(map);
  assertEquals(0, listener.getCreated());

  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  cache.put(1, 1);
  assertEquals(0, listener.getCreated());

  assertFalse(cache.iterator().hasNext());

  cache.getAndPut(1, 1);
  assertEquals(0, listener.getCreated());
}