Refactor jcache examples (#2620)

This commit is contained in:
Grzegorz Piwowarek 2017-09-14 11:02:05 +03:00 committed by GitHub
parent 8e7fc431a0
commit 556e8fa90b
4 changed files with 40 additions and 12 deletions

View File

@ -9,11 +9,14 @@ import javax.cache.configuration.FactoryBuilder;
import javax.cache.configuration.MutableConfiguration; import javax.cache.configuration.MutableConfiguration;
import javax.cache.spi.CachingProvider; import javax.cache.spi.CachingProvider;
import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
public class CacheLoaderTest { public class CacheLoaderTest {
private static final String CACHE_NAME = "SimpleCache";
private Cache<Integer, String> cache; private Cache<Integer, String> cache;
@Before @Before
@ -25,6 +28,12 @@ public class CacheLoaderTest {
this.cache = cacheManager.createCache("SimpleCache", config); this.cache = cacheManager.createCache("SimpleCache", config);
} }
@After
public void tearDown() {
Caching.getCachingProvider()
.getCacheManager().destroyCache(CACHE_NAME);
}
@Test @Test
public void whenReadingFromStorage_thenCorrect() { public void whenReadingFromStorage_thenCorrect() {
for (int i = 1; i < 4; i++) { for (int i = 1; i < 4; i++) {

View File

@ -1,6 +1,8 @@
package com.baeldung.jcache; package com.baeldung.jcache;
import static org.junit.Assert.assertEquals; import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import javax.cache.Cache; import javax.cache.Cache;
import javax.cache.CacheManager; import javax.cache.CacheManager;
@ -8,22 +10,29 @@ import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration; import javax.cache.configuration.MutableConfiguration;
import javax.cache.spi.CachingProvider; import javax.cache.spi.CachingProvider;
import org.junit.Before; import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class EntryProcessorTest { public class EntryProcessorTest {
private static final String CACHE_NAME = "MyCache";
private Cache<String, String> cache; private Cache<String, String> cache;
@Before @Before
public void instantiateCache() { public void instantiateCache() {
CachingProvider cachingProvider = Caching.getCachingProvider(); CachingProvider cachingProvider = Caching.getCachingProvider();
CacheManager cacheManager = cachingProvider.getCacheManager(); CacheManager cacheManager = cachingProvider.getCacheManager();
MutableConfiguration<String, String> config = new MutableConfiguration<String, String>(); MutableConfiguration<String, String> config = new MutableConfiguration<>();
this.cache = cacheManager.createCache("MyCache", config); this.cache = cacheManager.createCache(CACHE_NAME, config);
this.cache.put("key", "value"); this.cache.put("key", "value");
} }
@After
public void tearDown() {
Caching.getCachingProvider()
.getCacheManager().destroyCache(CACHE_NAME);
}
@Test @Test
public void whenModifyValue_thenCorrect() { public void whenModifyValue_thenCorrect() {
this.cache.invoke("key", new SimpleEntryProcessor()); this.cache.invoke("key", new SimpleEntryProcessor());

View File

@ -1,6 +1,8 @@
package com.baeldung.jcache; package com.baeldung.jcache;
import static org.junit.Assert.assertEquals; import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import javax.cache.Cache; import javax.cache.Cache;
import javax.cache.CacheManager; import javax.cache.CacheManager;
@ -10,14 +12,15 @@ import javax.cache.configuration.MutableCacheEntryListenerConfiguration;
import javax.cache.configuration.MutableConfiguration; import javax.cache.configuration.MutableConfiguration;
import javax.cache.spi.CachingProvider; import javax.cache.spi.CachingProvider;
import org.junit.Before; import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class EventListenerTest { public class EventListenerTest {
private static final String CACHE_NAME = "MyCache";
private Cache<String, String> cache; private Cache<String, String> cache;
private SimpleCacheEntryListener listener; private SimpleCacheEntryListener listener;
MutableCacheEntryListenerConfiguration<String, String> listenerConfiguration; private MutableCacheEntryListenerConfiguration<String, String> listenerConfiguration;
@Before @Before
public void setup() { public void setup() {
@ -28,9 +31,16 @@ public class EventListenerTest {
this.listener = new SimpleCacheEntryListener(); this.listener = new SimpleCacheEntryListener();
} }
@After
public void tearDown() {
Caching.getCachingProvider()
.getCacheManager().destroyCache(CACHE_NAME);
}
@Test @Test
public void whenRunEvent_thenCorrect() throws InterruptedException { public void whenRunEvent_thenCorrect() throws InterruptedException {
this.listenerConfiguration = new MutableCacheEntryListenerConfiguration<String, String>(FactoryBuilder.factoryOf(this.listener), null, false, true); this.listenerConfiguration = new MutableCacheEntryListenerConfiguration<>(FactoryBuilder
.factoryOf(this.listener), null, false, true);
this.cache.registerCacheEntryListener(this.listenerConfiguration); this.cache.registerCacheEntryListener(this.listenerConfiguration);
assertEquals(false, this.listener.getCreated()); assertEquals(false, this.listener.getCreated());

View File

@ -1,6 +1,6 @@
package com.baeldung.jcache; package com.baeldung.jcache;
import static org.junit.Assert.assertEquals; import org.junit.Test;
import javax.cache.Cache; import javax.cache.Cache;
import javax.cache.CacheManager; import javax.cache.CacheManager;
@ -8,7 +8,7 @@ import javax.cache.Caching;
import javax.cache.configuration.MutableConfiguration; import javax.cache.configuration.MutableConfiguration;
import javax.cache.spi.CachingProvider; import javax.cache.spi.CachingProvider;
import org.junit.Test; import static org.junit.Assert.assertEquals;
public class JCacheTest { public class JCacheTest {