42 lines
1.3 KiB
Java
Raw Normal View History

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;
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";
private Cache<Integer, String> cache;
@Before
public void setup() {
CachingProvider cachingProvider = Caching.getCachingProvider();
CacheManager cacheManager = cachingProvider.getCacheManager();
2018-03-04 17:39:09 +02:00
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() {
2018-03-04 17:39:09 +02:00
Caching.getCachingProvider().getCacheManager().destroyCache(CACHE_NAME);
2017-09-14 11:02:05 +03:00
}
@Test
public void whenReadingFromStorage_thenCorrect() {
for (int i = 1; i < 4; i++) {
String value = cache.get(i);
assertEquals("fromCache" + i, value);
}
}
}