@Override void execute(Cache<Object, Object> cache, Exchange exchange) { exchange.getIn().setBody( cache.getAndPut( exchange.getIn().getHeader(JCacheConstants.KEY), exchange.getIn().getBody()) ); } },
@Override public Exchange add(CamelContext camelContext, String key, Exchange exchange) { if (optimistic) { throw new UnsupportedOperationException(); } LOG.trace("Adding an Exchange with ID {} for key {} in a thread-safe manner.", exchange.getExchangeId(), key); DefaultExchangeHolder newHolder = DefaultExchangeHolder.marshal(exchange, true, allowSerializedHeaders); DefaultExchangeHolder oldHolder = cache.getAndPut(key, newHolder); return unmarshallExchange(camelContext, oldHolder); }
@Test public void getAndPut_Existing_MutateValue() { long now = System.currentTimeMillis(); Date existingKey = new Date(now); Date value1 = new Date(now); cache.getAndPut(existingKey, value1); Date value2 = new Date(now + 1); value1.setTime(now + 2); assertEquals(new Date(now), cache.getAndPut(existingKey, value2)); value2.setTime(now + 3); assertEquals(new Date(now + 1), cache.get(existingKey)); }
@Test public void getAndPut_Existing() { long now = System.currentTimeMillis(); Date existingKey = new Date(now); Date value1 = new Date(now); cache.getAndPut(existingKey, value1); Date value2 = new Date(now + 1); assertSame(value1, cache.getAndPut(existingKey, value2)); assertSame(value2, cache.get(existingKey)); }
@Test public void getAndPut_NotThere() { if (cache == null) return; long now = System.currentTimeMillis(); Date existingKey = new Date(now); Date existingValue = new Date(now); assertNull(cache.getAndPut(existingKey, existingValue)); existingValue.setTime(now + 1); assertEquals(new Date(now), cache.get(existingKey)); }
@Test public void getAndPut_Existing() throws Exception { Long existingKey = System.currentTimeMillis(); String value1 = "value1"; cache.getAndPut(existingKey, value1); String value2 = "value2"; assertEquals(value1, cache.getAndPut(existingKey, value2)); assertEquals(value2, cache.get(existingKey)); }
@Test public void getAndPut_NullKey() throws Exception { try { cache.getAndPut(null, ""); fail("should have thrown an exception - null key not allowed"); } catch (NullPointerException e) { //good } }
@Test public void getAndPut_NullValue() throws Exception { try { cache.getAndPut(1L, null); fail("should have thrown an exception - null value not allowed"); } catch (NullPointerException e) { //good } }
@Test public void getAndPut_NotThere() { long now = System.currentTimeMillis(); Date existingKey = new Date(now); Date existingValue = new Date(now); assertNull(cache.getAndPut(existingKey, existingValue)); // unnecessary since after we test for same (not equals), but "advertises" consequence existingValue.setTime(now + 1); assertSame(existingValue, cache.get(existingKey)); }
private <K> void doPutGet(Supplier<K> keySupplier, Cache<K, String> readCache, Cache<K, String> writeCache) { K key = keySupplier.get(); assertEquals(null, writeCache.getAndPut(key, "one")); assertEquals("one", readCache.get(key)); }
@Test public void getAndPut_Existing_NonSameKey() throws Exception { Long key1 = System.currentTimeMillis(); String value1 = "value1"; assertNull(cache.getAndPut(key1, value1)); Long key2 = new Long(key1); String value2 = "value2"; assertEquals(value1, cache.getAndPut(key2, value2)); assertEquals(value2, cache.get(key1)); assertEquals(value2, cache.get(key2)); }
@Test public void getAndPut_NotThere() { Long existingKey = System.currentTimeMillis(); String existingValue = "value" + existingKey; assertNull(cache.getAndPut(existingKey, existingValue)); assertEquals(existingValue, cache.get(existingKey)); }
@Test public void getAndPut_Closed() { cache.close(); try { cache.getAndPut(null, null); fail("should have thrown an exception - cache closed"); } catch (IllegalStateException e) { //good } }
private <K> void doContainsKey(Supplier<K> keySupplier, Cache<K, String> readCache, Cache<K, String> writeCache) { K key = keySupplier.get(); assertEquals(false, readCache.containsKey(key)); assertEquals(null, writeCache.getAndPut(key, "one")); assertEquals(true, readCache.containsKey(key)); }
@Test public void shouldWriteThroughUsingGetAndPut_SingleEntryMultipleTimes() { assertEquals(0, cacheWriter.getWriteCount()); assertEquals(0, cacheWriter.getDeleteCount()); cache.getAndPut(1, "Gudday World"); cache.getAndPut(1, "Gudday World"); cache.getAndPut(1, "Gudday World"); assertEquals(3, cacheWriter.getWriteCount()); assertEquals(0, cacheWriter.getDeleteCount()); assertTrue(cacheWriter.hasWritten(1)); assertEquals("Gudday World", cacheWriter.get(1)); }
private <K> void doReplace(Supplier<K> keySupplier, Cache<K, String> readCache, Cache<K, String> writeCache) { K key = keySupplier.get(); assertEquals(null, readCache.get(key)); assertFalse(writeCache.replace(key, "xxx")); assertEquals(null, writeCache.getAndPut(key, "one")); assertEquals("one", readCache.get(key)); assertTrue(writeCache.replace(key, "uno")); assertEquals("uno", readCache.get(key)); assertTrue(writeCache.remove(key)); assertEquals(null, readCache.get(key)); }
private <K> void doConditionalRemove(Supplier<K> keySupplier, Cache<K, String> readCache, Cache<K, String> writeCache) { K key = keySupplier.get(); assertEquals(null, readCache.get(key)); assertFalse(writeCache.remove(key, "xxx")); assertEquals(null, writeCache.getAndPut(key, "one")); assertEquals("one", readCache.get(key)); assertFalse(writeCache.remove(key, "xxx")); assertEquals("one", readCache.get(key)); assertTrue(writeCache.remove(key, "one")); assertEquals(null, readCache.get(key)); }
private <K> void doGetAndReplace(Supplier<K> keySupplier, Cache<K, String> readCache, Cache<K, String> writeCache) { K key = keySupplier.get(); assertEquals(null, readCache.get(key)); assertEquals(null, writeCache.getAndReplace(key, "xxx")); assertEquals(null, writeCache.getAndPut(key, "one")); assertEquals("one", readCache.get(key)); assertEquals("one", writeCache.getAndReplace(key, "uno")); assertEquals("uno", readCache.get(key)); assertTrue(writeCache.remove(key)); assertEquals(null, readCache.get(key)); }