@Test public void test107LoaderOverriddenByEhcacheTemplateLoaderWriter() throws Exception { final AtomicBoolean loaderFactoryInvoked = new AtomicBoolean(false); final DumbCacheLoader product2CacheLoader = new DumbCacheLoader(); MutableConfiguration<Long, Product> product2Configuration = new MutableConfiguration<>(); product2Configuration.setTypes(Long.class, Product.class).setReadThrough(true); product2Configuration.setCacheLoaderFactory(() -> { loaderFactoryInvoked.set(true); return product2CacheLoader; }); Cache<Long, Product> productCache2 = cacheManager.createCache("productCache2", product2Configuration); assertThat(loaderFactoryInvoked.get(), is(false)); Product product = productCache2.get(124L); assertThat(product.getId(), is(124L)); assertThat(ProductCacheLoaderWriter.seen, hasItem(124L)); assertThat(product2CacheLoader.seen, is(empty())); CompletionListenerFuture future = new CompletionListenerFuture(); productCache2.loadAll(Collections.singleton(42L), false, future); future.get(); assertThat(ProductCacheLoaderWriter.seen, hasItem(42L)); assertThat(product2CacheLoader.seen, is(empty())); }
@Test @SuppressWarnings("unchecked") public void enablingWriteThroughDoesNotForceReadThrough() throws Exception { MutableConfiguration<Long, String> config = getConfiguration(false, cacheLoader, true, cacheWriter); Cache<Long, String> cache = cachingProvider.getCacheManager().createCache("writingCache", config); cache.put(42L, "Tadam!!!"); Set<Long> keys = singleton(25L); cache.loadAll(keys, false, null); cache.get(100L); verify(cacheLoader).loadAll(keys); verifyNoMoreInteractions(cacheLoader); verify(cacheWriter).write(any(Cache.Entry.class)); }
@Test public void testLoaderConfiguration() throws Exception { final AtomicBoolean loaderCreated = new AtomicBoolean(false); MutableConfiguration<String, String> configuration = new MutableConfiguration<>(); configuration.setTypes(String.class, String.class).setReadThrough(true); configuration.setCacheLoaderFactory(() -> { loaderCreated.set(true); return new TestCacheLoader(); }); CachingProvider provider = Caching.getCachingProvider(); CacheManager cacheManager = provider.getCacheManager(); Cache<String, String> cache = cacheManager.createCache("cache", configuration); assertThat(loaderCreated.get(), is(true)); cache.putIfAbsent("42", "The Answer"); TestCacheLoader.seen.clear(); CompletionListenerFuture future = new CompletionListenerFuture(); cache.loadAll(Collections.singleton("42"), true, future); future.get(); assertThat(TestCacheLoader.seen, contains("42")); }
@Override public void loadAll(Set<? extends K> keys, boolean replaceExistingValues, CompletionListener completionListener) { cache.loadAll(compactBoundedKeys(keys), replaceExistingValues, completionListener); }
@Test public void load_noLoaderNoCompletionListener() { // Added for code coverage. Set<Long> keys = new HashSet<Long>(); keys.add(1L); CompletionListener NULL_COMPLETION_LISTENER = null; cache.loadAll(keys, true, NULL_COMPLETION_LISTENER); }
@TimeStep public void timeStep() throws ExecutionException, InterruptedException { CompletionListenerFuture loaded = new CompletionListenerFuture(); cache.loadAll(keySet, true, loaded); if (waitForLoadAllFutureCompletion) { loaded.get(); } }
/** * Added for code coverage. */ @Test public void testLoadAllWithExecptionAndNoCompletionListener() throws Exception { FailingCacheLoader<String, String> cacheLoader = new FailingCacheLoader<>(); cacheLoaderServer.setCacheLoader(cacheLoader); HashSet<String> keys = new HashSet<>(); keys.add("gudday"); keys.add("hello"); keys.add("howdy"); keys.add("bonjour"); CompletionListener NULL_COMPLETION_LISTENER = null; cache.loadAll(keys, false, NULL_COMPLETION_LISTENER); } }
@Test public void load_Closed() { cache.close(); try { cache.loadAll(null, true, null); fail("should have thrown an exception - cache closed"); } catch (IllegalStateException e) { //good } }
/** * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)} )} * will propagate an exception from a {@link CacheLoader}. */ @Test public void shouldPropagateExceptionUsingLoadAll() throws Exception { FailingCacheLoader<String, String> cacheLoader = new FailingCacheLoader<>(); cacheLoaderServer.setCacheLoader(cacheLoader); HashSet<String> keys = new HashSet<>(); keys.add("gudday"); keys.add("hello"); keys.add("howdy"); keys.add("bonjour"); CompletionListenerFuture future = new CompletionListenerFuture(); cache.loadAll(keys, false, future); //wait for the load to complete try{ future.get(); fail(); } catch (ExecutionException e) { assertThat(e.getCause(), instanceOf(CacheLoaderException.class)); } }
/** * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)} * won't load <code>null</code> entries. */ @Test public void shouldNotLoadMultipleNullEntriesUsingLoadAll() throws Exception { NullValueCacheLoader<String, String> cacheLoader = new NullValueCacheLoader<>(); cacheLoaderServer.setCacheLoader(cacheLoader); HashSet<String> keys = new HashSet<>(); keys.add("gudday"); keys.add("hello"); keys.add("howdy"); keys.add("bonjour"); CompletionListenerFuture future = new CompletionListenerFuture(); cache.loadAll(keys, false, future); //wait for the load to complete future.get(); assertThat(future.isDone(), is(true)); for (String key : keys) { assertThat(cache.containsKey(key), is(false)); } }
/** * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)} * won't load <code>null</code> entries. */ @Test public void shouldNotLoadMultipleNullEntriesUsingLoadAll() throws Exception { NullValueCacheLoader<String, String> cacheLoader = new NullValueCacheLoader<>(); cacheLoaderServer.setCacheLoader(cacheLoader); HashSet<String> keys = new HashSet<>(); keys.add("gudday"); keys.add("hello"); keys.add("howdy"); keys.add("bonjour"); CompletionListenerFuture future = new CompletionListenerFuture(); cache.loadAll(keys, false, future); //wait for the load to complete future.get(); assertThat(future.isDone(), is(true)); for (String key : keys) { assertThat(cache.containsKey(key), is(false)); } }
/** * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)} * won't load <code>null</code> values. */ @Test public void shouldNotLoadMultipleNullValuesUsingLoadAll() throws Exception { NullValueCacheLoader<String, String> cacheLoader = new NullValueCacheLoader<>(); cacheLoaderServer.setCacheLoader(cacheLoader); HashSet<String> keys = new HashSet<>(); keys.add("gudday"); keys.add("hello"); keys.add("howdy"); keys.add("bonjour"); CompletionListenerFuture future = new CompletionListenerFuture(); cache.loadAll(keys, false, future); //wait for the load to complete future.get(); assertThat(future.isDone(), is(true)); for (String key : keys) { assertThat(cache.containsKey(key), is(false)); } }
/** * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)} )} * using a <code>null</code> key will raise an exception */ @Test public void shouldNotLoadWithNullKeyUsingLoadAll() throws Exception { RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>(); cacheLoaderServer.setCacheLoader(cacheLoader); HashSet<String> keys = new HashSet<>(); keys.add(null); try { CompletionListenerFuture future = new CompletionListenerFuture(); cache.loadAll(keys, false, future); fail("Expected a NullPointerException"); } catch (NullPointerException e) { //SKIP: expected } finally { assertThat(cacheLoader.getLoadCount(), is(0)); } }
/** * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)} )} * using a <code>null</code> key will raise an exception */ @Test public void shouldNotLoadWithNullKeyUsingLoadAll() throws Exception { RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>(); cacheLoaderServer.setCacheLoader(cacheLoader); HashSet<String> keys = new HashSet<>(); keys.add(null); try { CompletionListenerFuture future = new CompletionListenerFuture(); cache.loadAll(keys, false, future); fail("Expected a NullPointerException"); } catch (NullPointerException e) { //SKIP: expected } finally { assertThat(cacheLoader.getLoadCount(), is(0)); } }
/** * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)} * for a non-existent single value will cause it to be loaded and not written. */ @Test public void shouldLoadSingleMissingEntryUsingLoadAll() throws Exception { String key = "message"; HashSet<String> keys = new HashSet<>(); keys.add(key); assertThat(cache.containsKey(key), is(false)); CompletionListenerFuture future = new CompletionListenerFuture(); cache.loadAll(keys, false, future); //wait for the load to complete future.get(); assertThat(future.isDone(), is(true)); assertThat(cache.get(key), is(equalTo(key))); assertThat(recordingCacheLoader.getLoadCount(), is(1)); assertThat(recordingCacheLoader.hasLoaded(key), is(true)); //ensure nothing has been written assertThat(recordingCacheWriter.getWriteCount(), is(0L)); assertThat(recordingCacheWriter.getDeleteCount(), is(0L)); }
/** * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)} * for a non-existent single value will cause it to be loaded. */ @Test public void shouldLoadSingleMissingEntryUsingLoadAll() throws Exception { RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>(); cacheLoaderServer.setCacheLoader(cacheLoader); String key = "message"; HashSet<String> keys = new HashSet<>(); keys.add(key); assertThat(cache.containsKey(key), is(false)); CompletionListenerFuture future = new CompletionListenerFuture(); cache.loadAll(keys, false, future); //wait for the load to complete future.get(); assertThat(future.isDone(), is(true)); assertThat(cache.get(key), is(equalTo(key))); assertThat(cacheLoader.getLoadCount(), is(1)); assertThat(cacheLoader.hasLoaded(key), is(true)); }
/** * Ensure that {@link Cache#loadAll(Set, boolean, javax.cache.integration.CompletionListener)} * for a non-existent single value will cause it to be loaded. */ @Test public void shouldLoadSingleMissingEntryUsingLoadAll() throws Exception { RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>(); cacheLoaderServer.setCacheLoader(cacheLoader); String key = "message"; HashSet<String> keys = new HashSet<>(); keys.add(key); assertThat(cache.containsKey(key), is(false)); CompletionListenerFuture future = new CompletionListenerFuture(); cache.loadAll(keys, false, future); //wait for the load to complete future.get(); assertThat(future.isDone(), is(true)); assertThat(cache.get(key), is(equalTo(key))); assertThat(cacheLoader.getLoadCount(), is(1)); assertThat(cacheLoader.hasLoaded(key), is(true)); }
/** * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)} * using a <code>null</code> key will raise an exception */ @Test public void shouldNotLoadWithNullKeysUsingLoadAll() throws Exception { RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>(); cacheLoaderServer.setCacheLoader(cacheLoader); try { CompletionListenerFuture future = new CompletionListenerFuture(); cache.loadAll(null, false, future); fail("Expected a NullPointerException"); } catch (NullPointerException e) { //SKIP: expected } finally { assertThat(cacheLoader.getLoadCount(), is(0)); } }
/** * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)} * using a <code>null</code> key will raise an exception */ @Test public void shouldNotLoadWithNullKeysUsingLoadAll() throws Exception { RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>(); cacheLoaderServer.setCacheLoader(cacheLoader); try { CompletionListenerFuture future = new CompletionListenerFuture(); cache.loadAll(null, false, future); fail("Expected a NullPointerException"); } catch (NullPointerException e) { //SKIP: expected } finally { assertThat(cacheLoader.getLoadCount(), is(0)); } }
@Override public void call() { JCacheManager jCacheManager = createJCacheManager(cm, this); InMemoryJCacheLoader<Integer, String> cacheLoader = new InMemoryJCacheLoader<Integer, String>(); cacheLoader.store(1, "v1").store(2, "v2"); MutableConfiguration<Integer, String> cfg = new MutableConfiguration<Integer, String>(); // JDK6 fails to compile when calling FactoryBuilder.factoryOf() :( cfg.setCacheLoaderFactory(new FactoryBuilder.SingletonFactory(cacheLoader)); Cache<Integer, String> cache = jCacheManager.createCache(cacheName, cfg); assertEquals(0, cacheLoader.getLoadCount()); CompletionListenerFuture future = new CompletionListenerFuture(); cache.loadAll(CollectionFactory.makeSet(1, 2), true, future); futureGet(future); assertEquals(2, cacheLoader.getLoadCount()); } });