use of org.infinispan.jcache.embedded.JCacheManager in project infinispan by infinispan.
the class JCacheLoaderTest method testLoadEntryWithExpiration.
public void testLoadEntryWithExpiration(Method m) {
final String cacheName = m.getName();
withCacheManager(new CacheManagerCallable(TestCacheManagerFactory.createCacheManager(false)) {
@Override
public void call() {
ControlledTimeService timeService = new ControlledTimeService();
TestingUtil.replaceComponent(cm, TimeService.class, timeService, true);
JCacheManager jCacheManager = createJCacheManager(cm, this);
InMemoryJCacheLoader<Integer, String> cacheLoader = new InMemoryJCacheLoader<>();
cacheLoader.store(1, "v1").store(2, "v2");
MutableConfiguration<Integer, String> cfg = new MutableConfiguration<>();
final long lifespan = 3000;
cfg.setReadThrough(true);
cfg.setExpiryPolicyFactory((Factory<ExpiryPolicy>) () -> new ExpiryPolicy() {
@Override
public Duration getExpiryForCreation() {
return new Duration(TimeUnit.MILLISECONDS, lifespan);
}
@Override
public Duration getExpiryForAccess() {
return null;
}
@Override
public Duration getExpiryForUpdate() {
return Duration.ZERO;
}
});
cfg.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
Cache<Integer, String> cache = jCacheManager.createCache(cacheName, cfg);
assertEquals("v2", cache.get(2));
assertEquals("v1", cache.get(1));
timeService.advance(lifespan + 100);
DataContainer<Integer, String> dc = cache.unwrap(AdvancedCache.class).getDataContainer();
assertEquals(null, dc.peek(2));
assertEquals(null, dc.peek(1));
}
});
}
use of org.infinispan.jcache.embedded.JCacheManager in project infinispan by infinispan.
the class JCacheLoaderTest method testLoadAllWithJCacheLoader.
public void testLoadAllWithJCacheLoader(Method m) {
final String cacheName = m.getName();
// GlobalConfigurationBuilder globalBuilder = new GlobalConfigurationBuilder();
// globalBuilder.asyncTransportExecutor().addProperty("maxThreads", "1");
withCacheManager(new CacheManagerCallable(TestCacheManagerFactory.createCacheManager(false)) {
@Override
public void call() {
JCacheManager jCacheManager = createJCacheManager(cm, this);
InMemoryJCacheLoader<Integer, String> cacheLoader = new InMemoryJCacheLoader<>();
cacheLoader.store(1, "v1").store(2, "v2");
MutableConfiguration<Integer, String> cfg = new MutableConfiguration<>();
cfg.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
Cache<Integer, String> cache = jCacheManager.createCache(cacheName, cfg);
assertEquals(0, cacheLoader.getLoadCount());
CompletionListenerFuture future = new CompletionListenerFuture();
cache.loadAll(Util.asSet(1, 2), true, future);
futureGet(future);
assertEquals(2, cacheLoader.getLoadCount());
}
});
}
use of org.infinispan.jcache.embedded.JCacheManager in project infinispan by infinispan.
the class JCacheConfigurationTest method testProgrammaticConfigurationHolder.
public void testProgrammaticConfigurationHolder() throws Exception {
URI uri = JCacheConfigurationTest.class.getClassLoader().getResource("infinispan_uri.xml").toURI();
Properties properties = getProperties();
ConfigurationBuilderHolder cbh = new ConfigurationBuilderHolder();
cbh.getGlobalConfigurationBuilder().addModule(PrivateGlobalConfigurationBuilder.class);
properties.put(ConfigurationBuilderHolder.class.getName(), cbh);
withCachingProvider(provider -> {
try (JCacheManager jCacheManager = new JCacheManager(uri, provider.getClass().getClassLoader(), provider, properties)) {
DefaultCacheManager defaultCacheManager = jCacheManager.unwrap(DefaultCacheManager.class);
assertNotNull(defaultCacheManager.getCacheManagerConfiguration().module(PrivateGlobalConfiguration.class));
}
});
}
use of org.infinispan.jcache.embedded.JCacheManager in project infinispan by infinispan.
the class JCacheConfigurationTest method testJCacheManagerWithRealJarFileSchema.
public void testJCacheManagerWithRealJarFileSchema() throws Exception {
File sampleJarWithResourceFile = null;
ClassLoader originalClassLoader = null;
try {
// given - creating sample jar file
String tmpDir = CommonsTestingUtil.tmpDirectory(JCacheConfigurationTest.class);
File tmpDirFile = new File(tmpDir);
if (!tmpDirFile.exists()) {
tmpDirFile.mkdirs();
}
String sampleJarWithResourcePathString = Paths.get(tmpDir, "sampleJarWithResource.jar").toString();
sampleJarWithResourceFile = new File(sampleJarWithResourcePathString);
String existingResourceToAddInJar = "infinispan_uri.xml";
String targetPathInJar = "sample-dir-inside-jar/";
createJar(sampleJarWithResourceFile, existingResourceToAddInJar, targetPathInJar);
// given - adding jar file into classpath
String fullTargetPath = targetPathInJar + existingResourceToAddInJar;
originalClassLoader = Thread.currentThread().getContextClassLoader();
MyClassLoader myClassLoader = new MyClassLoader(new URL[] {}, originalClassLoader);
myClassLoader.addURL(new URI("jar:" + sampleJarWithResourceFile.toURI() + "!/").toURL());
Thread.currentThread().setContextClassLoader(myClassLoader);
// when
URI resourceInsideJarUri = new URI("jar:" + sampleJarWithResourceFile.toURI() + "!" + fullTargetPath);
withCachingProvider(provider -> {
try (JCacheManager jCacheManager = new JCacheManager(resourceInsideJarUri, provider.getClass().getClassLoader(), provider, getProperties())) {
// then
assertNotNull(jCacheManager.getCache("foo"));
}
});
} finally {
// cleanup
if (sampleJarWithResourceFile != null && sampleJarWithResourceFile.exists()) {
sampleJarWithResourceFile.delete();
}
if (originalClassLoader != null) {
Thread.currentThread().setContextClassLoader(originalClassLoader);
}
}
}
use of org.infinispan.jcache.embedded.JCacheManager in project infinispan by infinispan.
the class JCacheConfigurationTest method testProgrammaticGlobalConfiguration.
public void testProgrammaticGlobalConfiguration() throws Exception {
URI uri = JCacheConfigurationTest.class.getClassLoader().getResource("infinispan_uri.xml").toURI();
Properties properties = getProperties();
GlobalConfigurationBuilder global = new GlobalConfigurationBuilder();
global.addModule(PrivateGlobalConfigurationBuilder.class);
properties.put(GlobalConfigurationBuilder.class.getName(), global);
withCachingProvider(provider -> {
try (JCacheManager jCacheManager = new JCacheManager(uri, provider.getClass().getClassLoader(), provider, properties)) {
DefaultCacheManager defaultCacheManager = jCacheManager.unwrap(DefaultCacheManager.class);
assertNotNull(defaultCacheManager.getCacheManagerConfiguration().module(PrivateGlobalConfiguration.class));
}
});
}
Aggregations