/** * Update the cache using either invokeAll() or putAll(). * * @param cache the cache * @param newVal the new value to put to the entries * @param invoke whether to use invokeAll() or putAll() * @param keys Keys to update. */ private void updateEntries( Cache<Integer, Integer> cache, int newVal, boolean invoke, Set<Integer> keys ) { if (invoke) cache.invokeAll(keys, new IntegerSetValue(newVal)); else { final Map<Integer, Integer> entries = new HashMap<>(ENTRY_COUNT); for (final Integer key : keys) entries.put(key, newVal); cache.putAll(entries); } }
private void innerClear() { cache.get(1); // one miss cache.getAll(asSet(1, 2, 3)); // 3 misses cache.put(1, "a"); // one put cache.put(1, "b"); // one put and update cache.putAll(Collections.singletonMap(2, "b")); // 1 put cache.get(1); // one hit cache.remove(1); // one remove cache.removeAll(); // one remove changesOf(1, 4, 3, 2); cacheStatistics.clear(); changesOf(-1, -4, -3, -2); } }
@Override void execute(Cache<Object, Object> cache, Exchange exchange) { cache.putAll( exchange.getIn().getBody(Map.class) ); } },
@Override public void putAll(Map<? extends K, ? extends V> map) { cache.putAll(compactMap(map)); }
@Test public void putAll_Null() { try { cache.putAll(null); fail("should have thrown an exception - null map not allowed"); } catch (NullPointerException e) { //good } }
private <K> void doIterator(Supplier<K> keySupplier, Cache<K, String> map1, Cache<K, String> map2) { K key1 = keySupplier.get(), key2 = keySupplier.get(), key3 = keySupplier.get(), key4 = keySupplier.get(), key5 = keySupplier.get(), key6 = keySupplier.get(); assertFalse(map1.iterator().hasNext()); Map<K, String> data = new HashMap<>(); data.put(key1, "one"); data.put(key2, "two"); data.put(key3, "three"); data.put(key4, "four"); data.put(key5, "five"); data.put(key6, "five"); map2.putAll(data); Map<K, String> res0 = new HashMap<>(); for (Cache.Entry<K, String> e : map1) res0.put(e.getKey(), e.getValue()); assertEquals(data, res0); map2.clear(); }
@Test public void putAll_NullValue() { Map<Long, String> data = createLSData(3); // note: using LinkedHashMap, we have made an effort to ensure the null // be added after other "good" values. data.put(System.currentTimeMillis(), null); try { cache.putAll(data); fail("should have thrown an exception - null value not allowed"); } catch (NullPointerException e) { //good } }
@Test public void putAll() { Map<Date, Date> data = createDDData(3); cache.putAll(data); for (Map.Entry<Date, Date> entry : data.entrySet()) { assertSame(entry.getValue(), cache.get(entry.getKey())); } }
@Test public void putAll() { Map<Long, String> data = createLSData(3); cache.putAll(data); for (Map.Entry<Long, String> entry : data.entrySet()) { assertEquals(entry.getValue(), cache.get(entry.getKey())); } } }
private <K> void doClear(Supplier<K> keySupplier, Cache<K, String> map1, Cache<K, String> map2) { K key1 = keySupplier.get(), key2 = keySupplier.get(), key3 = keySupplier.get(); Map<K, String> data = new HashMap<>(); data.put(key1, "one"); data.put(key2, "two"); data.put(key3, "two"); map2.putAll(data); map2.clear(); assertEquals(null, map1.get(key1)); assertEquals(null, map1.get(key2)); assertEquals(null, map1.get(key3)); }
@Test public void removeAll_1arg() { Map<Long, String> data = createLSData(3); cache.putAll(data); Iterator<Map.Entry<Long, String>> it = data.entrySet().iterator(); it.next(); Map.Entry removedEntry = it.next(); it.remove(); cache.removeAll(data.keySet()); for (Long key : data.keySet()) { assertFalse(cache.containsKey(key)); } assertEquals(removedEntry.getValue(), cache.get((Long) removedEntry.getKey())); }
@Test public void putAll_NullKey() { Map<Long, String> data = createLSData(3); // note: using LinkedHashMap, we have made an effort to ensure the null // be added after other "good" values. data.put(null, ""); try { cache.putAll(data); fail("should have thrown an exception - null key not allowed"); } catch (NullPointerException e) { //expected } }
@Test public void iterator() { LinkedHashMap<Long, String> data = createLSData(3); cache.putAll(data); Iterator<Cache.Entry<Long, String>> iterator = cache.iterator(); while (iterator.hasNext()) { Cache.Entry<Long, String> next = iterator.next(); assertEquals(next.getValue(), data.get(next.getKey())); iterator.remove(); data.remove(next.getKey()); } assertTrue(data.isEmpty()); }
@Test public void putAll_Closed() { cache.close(); try { cache.putAll(null); fail("should have thrown an exception - cache closed"); } catch (IllegalStateException e) { //good } }
@Test public void removeAll_0arg() { Map<Long, String> data = createLSData(3); cache.putAll(data); cache.removeAll(); for (Long key : data.keySet()) { assertFalse(cache.containsKey(key)); } } }
/** * Ensure that a {@link Cache#putAll(java.util.Map)} does not cause * an entry to be loaded. */ @Test public void shouldNotLoadUsingPutAll() { RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>(); cacheLoaderServer.setCacheLoader(cacheLoader); HashMap<String, String> map = new HashMap<>(); map.put("gudday", "gudday"); map.put("hello", "hello"); map.put("howdy", "howdy"); map.put("bonjour", "bonjour"); //assert that the cache doesn't contain the map entries for (String key : map.keySet()) { assertThat(cache.containsKey(key), is(false)); } cache.putAll(map); //assert that the cache contains the map entries for (String key : map.keySet()) { assertThat(cache.containsKey(key), is(true)); assertThat(cache.get(key), is(map.get(key))); } //assert that nothing was loaded assertThat(cacheLoader.getLoadCount(), is(0)); for (String key : map.keySet()) { assertThat(cacheLoader.hasLoaded(key), is(false)); } }
@Test public void checkWriteAllAndDeleteAll() { assertTrue(copy.isEmpty()); assertFalse(cache.iterator().hasNext()); cache.put("foo", "bar"); assertEquals(1, copy.size()); cache.remove("foo"); assertTrue(copy.isEmpty()); cache.putAll(new HashMap<String, String>() {{ put("a", "b"); put("b", "c"); }}); assertEquals(2, copy.size()); cache.removeAll(new HashSet<String>(asList("a", "b"))); assertTrue(copy.isEmpty()); } }
@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"); assertEventually(new AssertionRunnable() { @Override public void run() throws Exception { assertEquals(0L, lookupManagementAttribute(cache, CacheStatistics, "CachePuts")); } }, statisticsUpdateTimeoutMillis); Map<Long, String> map = new HashMap<Long, String>(); map.put(2L, "goodbye"); map.put(3L, "world"); cache.putAll(map); assertEventually(new AssertionRunnable() { @Override public void run() throws Exception { assertEquals(0L, lookupManagementAttribute(cache, CacheStatistics, "CachePuts")); } }, statisticsUpdateTimeoutMillis); }
@Test public void putAllShouldCallGetExpiry() { CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy(); expiryPolicyServer.setExpiryPolicy(expiryPolicy); MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>(); config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient)); Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config); Map<Integer, Integer> map = new HashMap<>(); map.put(1, 1); map.put(2, 2); cache.put(1, 1); assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(1)); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); expiryPolicy.resetCount(); cache.putAll(map); assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(1)); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), greaterThanOrEqualTo(1)); expiryPolicy.resetCount(); }