2017-09-09 21:37:26 +02:00
|
|
|
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;
|
|
|
|
|
2017-09-14 11:02:05 +03:00
|
|
|
import org.junit.After;
|
2017-09-09 21:37:26 +02:00
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
|
|
|
public class CacheLoaderTest {
|
|
|
|
|
2017-09-14 11:02:05 +03:00
|
|
|
private static final String CACHE_NAME = "SimpleCache";
|
|
|
|
|
2017-09-09 21:37:26 +02:00
|
|
|
private Cache<Integer, String> cache;
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setup() {
|
|
|
|
CachingProvider cachingProvider = Caching.getCachingProvider();
|
|
|
|
CacheManager cacheManager = cachingProvider.getCacheManager();
|
|
|
|
MutableConfiguration<Integer, String> config = new MutableConfiguration<Integer, String>().setReadThrough(true)
|
|
|
|
.setCacheLoaderFactory(new FactoryBuilder.SingletonFactory<>(new SimpleCacheLoader()));
|
|
|
|
this.cache = cacheManager.createCache("SimpleCache", config);
|
|
|
|
}
|
|
|
|
|
2017-09-14 11:02:05 +03:00
|
|
|
@After
|
|
|
|
public void tearDown() {
|
|
|
|
Caching.getCachingProvider()
|
|
|
|
.getCacheManager().destroyCache(CACHE_NAME);
|
|
|
|
}
|
|
|
|
|
2017-09-09 21:37:26 +02:00
|
|
|
@Test
|
|
|
|
public void whenReadingFromStorage_thenCorrect() {
|
|
|
|
for (int i = 1; i < 4; i++) {
|
|
|
|
String value = cache.get(i);
|
|
|
|
assertEquals("fromCache" + i, value);
|
|
|
|
}
|
|
|
|
}
|
2017-09-23 11:42:36 +02:00
|
|
|
}
|