diff --git a/libraries/pom.xml b/libraries/pom.xml index a16a4de59d..51c79ef0dd 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -226,6 +226,10 @@ commons-logging commons-logging + + org.slf4j + slf4j-log4j12 + @@ -237,6 +241,10 @@ commons-logging commons-logging + + org.slf4j + slf4j-log4j12 + @@ -244,6 +252,12 @@ flink-test-utils_2.10 ${flink.version} test + + + org.slf4j + slf4j-log4j12 + + org.apache.commons @@ -467,11 +481,23 @@ noexception 1.1.0 - + org.eclipse.collections eclipse-collections ${eclipse-collections.version} + + javax.cache + cache-api + ${cache.version} + + + com.hazelcast + hazelcast + ${hazelcast.version} + + + 0.7.0 @@ -513,6 +539,9 @@ 1.7.1 2.1.2 1.0 - 8.2.0 + 8.2.0 + 1.0.0 + 1.0.0 + 3.8.4 \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/jcache/CacheLoaderTest.java b/libraries/src/test/java/com/baeldung/jcache/CacheLoaderTest.java new file mode 100644 index 0000000000..e2167b39ad --- /dev/null +++ b/libraries/src/test/java/com/baeldung/jcache/CacheLoaderTest.java @@ -0,0 +1,35 @@ +package com.baeldung.jcache; + +import static org.junit.Assert.assertEquals; + +import javax.cache.Cache; +import javax.cache.CacheManager; +import javax.cache.Caching; +import javax.cache.configuration.FactoryBuilder; +import javax.cache.configuration.MutableConfiguration; +import javax.cache.spi.CachingProvider; + +import org.junit.Before; +import org.junit.Test; + +public class CacheLoaderTest { + + private Cache cache; + + @Before + public void setup() { + CachingProvider cachingProvider = Caching.getCachingProvider(); + CacheManager cacheManager = cachingProvider.getCacheManager(); + MutableConfiguration config = new MutableConfiguration().setReadThrough(true) + .setCacheLoaderFactory(new FactoryBuilder.SingletonFactory<>(new SimpleCacheLoader())); + this.cache = cacheManager.createCache("SimpleCache", config); + } + + @Test + public void whenReadingFromStorage_thenCorrect() { + for (int i = 1; i < 4; i++) { + String value = cache.get(i); + assertEquals("fromCache" + i, value); + } + } +} diff --git a/libraries/src/test/java/com/baeldung/jcache/EntryProcessorTest.java b/libraries/src/test/java/com/baeldung/jcache/EntryProcessorTest.java new file mode 100644 index 0000000000..fba6067885 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/jcache/EntryProcessorTest.java @@ -0,0 +1,32 @@ +package com.baeldung.jcache; + +import static org.junit.Assert.assertEquals; + +import javax.cache.Cache; +import javax.cache.CacheManager; +import javax.cache.Caching; +import javax.cache.configuration.MutableConfiguration; +import javax.cache.spi.CachingProvider; + +import org.junit.Before; +import org.junit.Test; + +public class EntryProcessorTest { + + private Cache cache; + + @Before + public void instantiateCache() { + CachingProvider cachingProvider = Caching.getCachingProvider(); + CacheManager cacheManager = cachingProvider.getCacheManager(); + MutableConfiguration config = new MutableConfiguration(); + this.cache = cacheManager.createCache("MyCache", config); + this.cache.put("key", "value"); + } + + @Test + public void whenModifyValue_thenCorrect() { + this.cache.invoke("key", new SimpleEntryProcessor()); + assertEquals("value - modified", cache.get("key")); + } +} diff --git a/libraries/src/test/java/com/baeldung/jcache/EventListenerTest.java b/libraries/src/test/java/com/baeldung/jcache/EventListenerTest.java new file mode 100644 index 0000000000..5fb0b317e2 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/jcache/EventListenerTest.java @@ -0,0 +1,46 @@ +package com.baeldung.jcache; + +import static org.junit.Assert.assertEquals; + +import javax.cache.Cache; +import javax.cache.CacheManager; +import javax.cache.Caching; +import javax.cache.configuration.FactoryBuilder; +import javax.cache.configuration.MutableCacheEntryListenerConfiguration; +import javax.cache.configuration.MutableConfiguration; +import javax.cache.spi.CachingProvider; + +import org.junit.Before; +import org.junit.Test; + +public class EventListenerTest { + + private Cache cache; + private SimpleCacheEntryListener listener; + MutableCacheEntryListenerConfiguration listenerConfiguration; + + @Before + public void setup() { + CachingProvider cachingProvider = Caching.getCachingProvider(); + CacheManager cacheManager = cachingProvider.getCacheManager(); + MutableConfiguration config = new MutableConfiguration(); + this.cache = cacheManager.createCache("MyCache", config); + this.listener = new SimpleCacheEntryListener(); + } + + @Test + public void whenRunEvent_thenCorrect() throws InterruptedException { + this.listenerConfiguration = new MutableCacheEntryListenerConfiguration(FactoryBuilder.factoryOf(this.listener), null, false, true); + this.cache.registerCacheEntryListener(this.listenerConfiguration); + + assertEquals(false, this.listener.getCreated()); + + this.cache.put("key", "value"); + assertEquals(true, this.listener.getCreated()); + assertEquals(false, this.listener.getUpdated()); + + this.cache.put("key", "newValue"); + assertEquals(true, this.listener.getUpdated()); + } + +} diff --git a/libraries/src/test/java/com/baeldung/jcache/JCacheTest.java b/libraries/src/test/java/com/baeldung/jcache/JCacheTest.java new file mode 100644 index 0000000000..2c86a236b4 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/jcache/JCacheTest.java @@ -0,0 +1,27 @@ +package com.baeldung.jcache; + +import static org.junit.Assert.assertEquals; + +import javax.cache.Cache; +import javax.cache.CacheManager; +import javax.cache.Caching; +import javax.cache.configuration.MutableConfiguration; +import javax.cache.spi.CachingProvider; + +import org.junit.Test; + +public class JCacheTest { + + @Test + public void instantiateCache() { + CachingProvider cachingProvider = Caching.getCachingProvider(); + CacheManager cacheManager = cachingProvider.getCacheManager(); + MutableConfiguration config = new MutableConfiguration<>(); + Cache cache = cacheManager.createCache("simpleCache", config); + cache.put("key1", "value1"); + cache.put("key2", "value2"); + assertEquals("value1", cache.get("key1")); + assertEquals("value2", cache.get("key2")); + cacheManager.close(); + } +} diff --git a/libraries/src/test/java/com/baeldung/jcache/SimpleCacheEntryListener.java b/libraries/src/test/java/com/baeldung/jcache/SimpleCacheEntryListener.java new file mode 100644 index 0000000000..b58182eab0 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/jcache/SimpleCacheEntryListener.java @@ -0,0 +1,34 @@ +package com.baeldung.jcache; + +import java.io.Serializable; + +import javax.cache.event.CacheEntryCreatedListener; +import javax.cache.event.CacheEntryEvent; +import javax.cache.event.CacheEntryListenerException; +import javax.cache.event.CacheEntryUpdatedListener; + +public class SimpleCacheEntryListener implements CacheEntryCreatedListener, CacheEntryUpdatedListener, Serializable { + + /** + * + */ + private static final long serialVersionUID = -712657810462878763L; + private boolean updated; + private boolean created; + + public boolean getUpdated() { + return this.updated; + } + + public boolean getCreated() { + return this.created; + } + + public void onUpdated(Iterable> events) throws CacheEntryListenerException { + this.updated = true; + } + + public void onCreated(Iterable> events) throws CacheEntryListenerException { + this.created = true; + } +} \ No newline at end of file diff --git a/libraries/src/test/java/com/baeldung/jcache/SimpleCacheLoader.java b/libraries/src/test/java/com/baeldung/jcache/SimpleCacheLoader.java new file mode 100644 index 0000000000..77ab8fb645 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/jcache/SimpleCacheLoader.java @@ -0,0 +1,24 @@ +package com.baeldung.jcache; + +import java.util.HashMap; +import java.util.Map; + +import javax.cache.integration.CacheLoader; +import javax.cache.integration.CacheLoaderException; + +public class SimpleCacheLoader implements CacheLoader { + + @Override + public String load(Integer key) throws CacheLoaderException { + return "fromCache" + key; + } + + @Override + public Map loadAll(Iterable keys) throws CacheLoaderException { + Map data = new HashMap<>(); + for (int key : keys) { + data.put(key, load(key)); + } + return data; + } +} diff --git a/libraries/src/test/java/com/baeldung/jcache/SimpleEntryProcessor.java b/libraries/src/test/java/com/baeldung/jcache/SimpleEntryProcessor.java new file mode 100644 index 0000000000..bb585807fb --- /dev/null +++ b/libraries/src/test/java/com/baeldung/jcache/SimpleEntryProcessor.java @@ -0,0 +1,25 @@ +package com.baeldung.jcache; + +import java.io.Serializable; + +import javax.cache.processor.EntryProcessor; +import javax.cache.processor.EntryProcessorException; +import javax.cache.processor.MutableEntry; + +public class SimpleEntryProcessor implements EntryProcessor, Serializable { + + /** + * + */ + private static final long serialVersionUID = -5616476363722945132L; + + public String process(MutableEntry entry, Object... args) throws EntryProcessorException { + + if (entry.exists()) { + String current = entry.getValue(); + entry.setValue(current + " - modified"); + return current; + } + return null; + } +}