@Test public void testGetAll_with_cache_loader() throws Exception { CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder( String.class, String.class, heap(100)); CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder.build(); CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class); CacheLoaderWriter cacheLoaderWriter = mock(CacheLoaderWriter.class); when(cacheLoaderWriter.load(Matchers.anyObject())) .thenThrow(new RuntimeException("We should not have called .load() but .loadAll()")); when(cacheLoaderWriterProvider.createCacheLoaderWriter( anyString(), Matchers.any(CacheConfiguration.class))) .thenReturn(cacheLoaderWriter); CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider); CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true); when(cacheLoaderWriter.loadAll(argThat(hasItem("key0")))) .thenReturn( new HashMap() { { put("key0", "value0"); } }); when(cacheLoaderWriter.loadAll(argThat(hasItem("key2")))) .thenReturn( new HashMap() { { put("key2", "value2"); } }); Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class); Set<String> fewKeysSet = new HashSet<String>() { { add("key0"); add("key2"); } }; // the call to getAll Map<String, String> fewEntries = myCache.getAll(fewKeysSet); assertThat(fewEntries.size(), is(2)); assertThat(fewEntries.get("key0"), is("value0")); assertThat(fewEntries.get("key2"), is("value2")); }
@Test public void testGetAll_cache_loader_throws_exception() throws Exception { CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder( String.class, String.class, heap(100)); CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder.build(); CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class); CacheLoaderWriter cacheLoaderWriter = mock(CacheLoaderWriter.class); when(cacheLoaderWriter.load(Matchers.anyObject())) .thenThrow(new RuntimeException("We should not have called .load() but .loadAll()")); when(cacheLoaderWriter.loadAll(Matchers.any(Iterable.class))) .thenThrow(new Exception("Simulating an exception from the cache loader")); when(cacheLoaderWriterProvider.createCacheLoaderWriter( anyString(), Matchers.any(CacheConfiguration.class))) .thenReturn(cacheLoaderWriter); CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider); CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true); Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class); Set<String> fewKeysSet = new HashSet<String>() { { add("key0"); add("key2"); } }; // the call to getAll try { myCache.getAll(fewKeysSet); fail(); } catch (BulkCacheLoadingException bcwe) { // since onHeapStore.bulkComputeIfAbsent sends batches of 1 element, assertThat(bcwe.getFailures().size(), is(2)); assertThat(bcwe.getSuccesses().size(), is(0)); } }