@Test public void getAndRemove() { expect(cache.getAndRemove(1)).isNull(); changesOf(0, 1, 0, 0); cache.put(1, "a"); changesOf(0, 0, 1, 0); expect(cache.getAndRemove(1)).isEqualTo("a"); changesOf(1, 0, 0, 1); }
@Override public V remove(final K key) { return cache.getAndRemove(key); }
@Override void execute(Cache<Object, Object> cache, Exchange exchange) { exchange.getIn().setBody( cache.getAndRemove( exchange.getIn().getHeader(JCacheConstants.KEY)) ); } },
/** * {@inheritDoc} */ @Override public Resource remove(final Object resourceKey) { return getCache().getAndRemove(resourceKey); }
@Override public V getAndRemove(K key) { return valueTransformer.expand(cache.getAndRemove(passingKeyTransformer.compact(key))); }
/** * Get the RandomPasswordContainer object from the cache for given unique id * * @param uniqueID Get and Remove the unique id for that particualr cache * @return RandomPasswordContainer of particular unique ID */ private RandomPasswordContainer getAndRemoveRandomPasswordContainer(String uniqueID) { return RandomPasswordContainerCache.getInstance().getRandomPasswordContainerCache().getAndRemove(uniqueID); }
/** * Get the RandomPasswordContainer object from the cache for given unique id * * @param uniqueID Get and Remove the unique id for that particualr cache * @return RandomPasswordContainer of particular unique ID */ private RandomPasswordContainer getAndRemoveRandomPasswordContainer(String uniqueID) { return RandomPasswordContainerCache.getInstance().getRandomPasswordContainerCache().getAndRemove(uniqueID); }
@Test public void getAndRemove_Closed() { cache.close(); try { cache.getAndRemove(null); fail("should have thrown an exception - cache closed"); } catch (IllegalStateException e) { //good } }
@Test public void getAndRemove_NullKey() throws Exception { try { assertNull(cache.getAndRemove(null)); fail("should have thrown an exception - null key not allowed"); } catch (NullPointerException e) { //expected } }
final AtomicLong removeCount = new AtomicLong(); String result = cache.getAndRemove(1L); missCount.incrementAndGet(); assertEquals(null, result); result = cache.getAndRemove(1L); hitCount.incrementAndGet(); removeCount.incrementAndGet();
@Test public void getAndRemove_NotExistent() throws Exception { Long existingKey = System.currentTimeMillis(); String existingValue = "value" + existingKey; cache.put(existingKey, existingValue); Long keyNotExisting = existingKey + 1; assertNull(cache.getAndRemove(keyNotExisting)); assertEquals(existingValue, cache.get(existingKey)); }
/** * Ensure that a {@link Cache#getAndRemove(Object)} does not cause * an entry to be loaded. */ @Test public void shouldNotLoadUsingGetAndRemove() { RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>(); cacheLoaderServer.setCacheLoader(cacheLoader); String key = "message"; assertThat(cache.containsKey(key), is(false)); String value = cache.getAndRemove(key); assertThat(cache.containsKey(key), is(false)); assertThat(value, is(nullValue())); assertThat(cacheLoader.getLoadCount(), is(0)); assertThat(cacheLoader.hasLoaded(key), is(false)); //try again with an existing value cache.put(key, key); value = cache.getAndRemove(key); assertThat(value, is(key)); assertThat(cache.containsKey(key), is(false)); assertThat(cacheLoader.getLoadCount(), is(0)); assertThat(cacheLoader.hasLoaded(key), is(false)); }
/** * Ensure that a {@link Cache#getAndRemove(Object)} does not cause * an entry to be loaded. */ @Test public void shouldNotLoadUsingGetAndRemove() { RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>(); cacheLoaderServer.setCacheLoader(cacheLoader); String key = "message"; assertThat(cache.containsKey(key), is(false)); String value = cache.getAndRemove(key); assertThat(cache.containsKey(key), is(false)); assertThat(value, is(nullValue())); assertThat(cacheLoader.getLoadCount(), is(0)); assertThat(cacheLoader.hasLoaded(key), is(false)); //try again with an existing value cache.put(key, key); value = cache.getAndRemove(key); assertThat(value, is(key)); assertThat(cache.containsKey(key), is(false)); assertThat(cacheLoader.getLoadCount(), is(0)); assertThat(cacheLoader.hasLoaded(key), is(false)); }
@Test public void getAndRemove_EqualButNotSameKey() { Long key1 = System.currentTimeMillis(); String value1 = "value" + key1; cache.put(key1, value1); Long key2 = key1 + 1; String value2 = "value" + key2; cache.put(key2, value2); Long key3 = new Long(key1); assertNotSame(key3, key1); assertEquals(value1, cache.getAndRemove(key3)); assertNull(cache.get(key1)); assertEquals(value2, cache.get(key2)); }
private <K> void doGetAndRemove(Supplier<K> keySupplier, Cache<K, String> readCache, Cache<K, String> writeCache) { K key1 = keySupplier.get(), key2 = keySupplier.get(); assertFalse(writeCache.remove(key1)); assertEquals(null, writeCache.getAndRemove(key1)); assertEquals(null, writeCache.getAndPut(key1, "one")); assertEquals("one", readCache.get(key1)); assertTrue(writeCache.remove(key1)); assertEquals(null, readCache.get(key1)); assertEquals(null, writeCache.getAndPut(key2, "two")); assertEquals("two", readCache.get(key2)); assertEquals("two", writeCache.getAndRemove(key2)); assertEquals(null, readCache.get(key2)); }
@Test public void getAndRemove_Existing() { Long key1 = System.currentTimeMillis(); String value1 = "value" + key1; cache.put(key1, value1); Long key2 = key1 + 1; String value2 = "value" + key2; cache.put(key2, value2); assertEquals(value1, cache.getAndRemove(key1)); assertFalse(cache.containsKey(key1)); assertNull(cache.get(key1)); assertEquals(value2, cache.get(key2)); }
@Test public void getAndRemoveShouldNotCallExpiryPolicyMethods() { 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); // verify case when entry is non-existent cache.containsKey(1); assertThat(expiryPolicy.getCreationCount(), is(0)); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); cache.getAndRemove(1); assertThat(expiryPolicy.getCreationCount(), is(0)); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); // verify case when entry exist cache.put(1, 1); assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(1)); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); expiryPolicy.resetCount(); int value = cache.getAndRemove(1); assertThat(value, is(1)); assertThat(expiryPolicy.getCreationCount(), is(0)); assertThat(expiryPolicy.getAccessCount(), is(0)); assertThat(expiryPolicy.getUpdatedCount(), is(0)); }
@Test public void shouldWriteThroughUsingGetAndRemove_MissingSingleEntry() { assertEquals(0, cacheWriter.getWriteCount()); assertEquals(0, cacheWriter.getDeleteCount()); // remove a missing entry case String value = cache.getAndRemove(1); assertEquals(value, null); assertEquals(0, cacheWriter.getWriteCount()); assertEquals(1, cacheWriter.getDeleteCount()); }
@Test public void shouldWriteThroughUsingGetAndRemove_ExistingSingleEntry() { int nDelete = 0; assertEquals(0, cacheWriter.getWriteCount()); assertEquals(0, cacheWriter.getDeleteCount()); // actual remove of an entry case cache.put(1, "Gudday World"); String value = cache.getAndRemove(1); assertEquals("Gudday World", value); nDelete++; assertEquals(1, cacheWriter.getWriteCount()); assertEquals(nDelete, cacheWriter.getDeleteCount()); assertFalse(cacheWriter.hasWritten(1)); }
assertNotNull(cache.get(1)); assertFalse(cache.containsKey(1)); assertNull(cache.getAndRemove(1));