Refine search
@Test public void testTypeOverriding() throws Exception { CachingProvider provider = Caching.getCachingProvider(); javax.cache.CacheManager cacheManager = provider.getCacheManager(this.getClass().getResource("/ehcache-107-types.xml").toURI(), getClass().getClassLoader()); MutableConfiguration<Long, String> cache1Conf = new MutableConfiguration<>(); cache1Conf.setTypes(Long.class, String.class); javax.cache.Cache<Long, String> cache = cacheManager.createCache("defaultCache", cache1Conf); @SuppressWarnings("unchecked") Configuration<Long, String> cache1CompleteConf = cache.getConfiguration(Configuration.class); assertThat(cache1CompleteConf.getKeyType(), is(equalTo(Long.class))); assertThat(cache1CompleteConf.getValueType(), is(equalTo(String.class))); }
@Test public void testDefaultUriOverride() throws Exception { URI override = getClass().getResource("/ehcache-107.xml").toURI(); Properties props = new Properties(); props.put(DefaultConfigurationResolver.DEFAULT_CONFIG_PROPERTY_NAME, override); CacheManager cacheManager = Caching.getCachingProvider().getCacheManager(null, null, props); assertEquals(override, cacheManager.getURI()); Caching.getCachingProvider().close(); }
@Test public void testCacheUsesCacheManagerClassLoaderForDefaultURI() { CachingProvider cachingProvider = Caching.getCachingProvider(); LimitedClassLoader limitedClassLoader = new LimitedClassLoader(cachingProvider.getDefaultClassLoader()); CacheManager cacheManager = cachingProvider.getCacheManager(cachingProvider.getDefaultURI(), limitedClassLoader); MutableConfiguration<Object, Object> configuration = new MutableConfiguration<>(); Cache<Object, Object> cache = cacheManager.createCache("test", configuration); cache.put(1L, new Customer(1L)); try { cache.get(1L); fail("Expected AssertionError"); } catch (AssertionError e) { assertThat(e.getMessage(), is("No com.pany here")); } }
@BeforeClass public void beforeClass() { CachingProvider provider = Caching.getCachingProvider(PROVIDER_NAME); cacheManager = provider.getCacheManager( provider.getDefaultURI(), provider.getDefaultClassLoader()); }
@SuppressWarnings("WeakerAccess") protected CacheManager resolveCacheManager(SessionFactoryOptions settings, Map properties) { final Object explicitCacheManager = properties.get( ConfigSettings.CACHE_MANAGER ); if ( explicitCacheManager != null ) { return useExplicitCacheManager( settings, explicitCacheManager ); } final CachingProvider cachingProvider = getCachingProvider( properties ); final CacheManager cacheManager; final URI cacheManagerUri = getUri( settings, properties ); if ( cacheManagerUri != null ) { cacheManager = cachingProvider.getCacheManager( cacheManagerUri, getClassLoader( cachingProvider ) ); } else { cacheManager = cachingProvider.getCacheManager( cachingProvider.getDefaultURI(), getClassLoader( cachingProvider ) ); } return cacheManager; }
JCacheProfiler() { random = new Random(); count = new LongAdder(); CachingProvider provider = Caching.getCachingProvider(CaffeineCachingProvider.class.getName()); CacheManager cacheManager = provider.getCacheManager( provider.getDefaultURI(), provider.getDefaultClassLoader()); cache = cacheManager.createCache("profiler", new MutableConfiguration<>()); executor = Executors.newCachedThreadPool(new ThreadFactoryBuilder() .setPriority(Thread.MIN_PRIORITY).setDaemon(true).build()); }
@Test public void testCopierAtServiceLevel() throws Exception { CacheManager cacheManager = cachingProvider.getCacheManager( getClass().getResource("/ehcache-107-default-copiers.xml").toURI(), getClass().getClassLoader()); MutableConfiguration<Long, Client> config = new MutableConfiguration<>(); config.setTypes(Long.class, Client.class); Cache<Long, Client> bar = cacheManager.createCache("bar", config); Client client = new Client("tc", 1000000L); long key = 42L; bar.put(key, client); assertThat(bar.get(key), not(sameInstance(client))); }
@Test @SuppressWarnings("unchecked") public void testCompileTimeTypeSafety() throws Exception { CachingProvider provider = Caching.getCachingProvider(); javax.cache.CacheManager cacheManager = provider.getCacheManager(this.getClass().getResource("/ehcache-107-types.xml").toURI(), getClass().getClassLoader()); MutableConfiguration<Long, String> cache1Conf = new MutableConfiguration<>(); javax.cache.Cache<Long, String> cache = cacheManager.createCache("cache1", cache1Conf); cache.put(1l, "one"); cache.put(2l, "two"); Configuration<Object, Object> cache1CompleteConf = cache.getConfiguration(Configuration.class); //This ensures that we have compile time type safety, i.e when configuration does not have types defined but // what you get cache as should work. assertThat(cache1CompleteConf.getKeyType(), is(equalTo(Object.class))); assertThat(cache1CompleteConf.getValueType(), is(equalTo(Object.class))); assertThat(cache.get(1l), is(equalTo("one"))); assertThat(cache.get(2l), is(equalTo("two"))); javax.cache.Cache<String, String> second = cacheManager.getCache("cache1"); second.put("3","three"); assertThat(second.get("3"), is(equalTo("three"))); cacheManager.destroyCache("cache1"); cacheManager.close(); }
public JCache(URL url) { String method = url.getParameter(Constants.METHOD_KEY, ""); String key = url.getAddress() + "." + url.getServiceKey() + "." + method; // jcache parameter is the full-qualified class name of SPI implementation String type = url.getParameter("jcache"); CachingProvider provider = StringUtils.isEmpty(type) ? Caching.getCachingProvider() : Caching.getCachingProvider(type); CacheManager cacheManager = provider.getCacheManager(); Cache<Object, Object> cache = cacheManager.getCache(key); if (cache == null) { try { //configure the cache MutableConfiguration config = new MutableConfiguration<Object, Object>() .setTypes(Object.class, Object.class) .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MILLISECONDS, url.getMethodParameter(method, "cache.write.expire", 60 * 1000)))) .setStoreByValue(false) .setManagementEnabled(true) .setStatisticsEnabled(true); cache = cacheManager.createCache(key, config); } catch (CacheException e) { // concurrent cache initialization cache = cacheManager.getCache(key); } } this.store = cache; }
@Test public void basicConfiguration() throws Exception { // tag::basicConfigurationExample[] CachingProvider provider = Caching.getCachingProvider(); // <1> CacheManager cacheManager = provider.getCacheManager(); // <2> MutableConfiguration<Long, String> configuration = new MutableConfiguration<Long, String>() // <3> .setTypes(Long.class, String.class) // <4> .setStoreByValue(false) // <5> .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE)); // <6> Cache<Long, String> cache = cacheManager.createCache("jCache", configuration); // <7> cache.put(1L, "one"); // <8> String value = cache.get(1L); // <9> // end::basicConfigurationExample[] assertThat(value, is("one")); }
@Test public void testCacheManagerCloseLenientToEhcacheClosed() throws Exception { CachingProvider provider = Caching.getCachingProvider(); javax.cache.CacheManager cacheManager = provider.getCacheManager(this.getClass() .getResource("/ehcache-107-types.xml") .toURI(), getClass().getClassLoader()); MutableConfiguration<Long, String> cache1Conf = new MutableConfiguration<>(); javax.cache.Cache<Long, String> cache = cacheManager.createCache("cache1", cache1Conf); cacheManager.unwrap(org.ehcache.CacheManager.class).removeCache(cache.getName()); cacheManager.close(); } }
@Test public void getPut() { final CachingProvider cachingProvider = Caching.getCachingProvider(); final CacheManager cacheManager = cachingProvider.getCacheManager(); final Cache<String, String> cache = cacheManager.createCache("default", new MutableConfiguration<String, String>()); assertFalse(cache.containsKey("foo")); cache.put("foo", "bar"); assertTrue(cache.containsKey("foo")); assertEquals("bar", cache.get("foo")); cache.remove("foo"); assertFalse(cache.containsKey("foo")); cachingProvider.close(); }
@Test public void testRunTimeTypeLaxity() throws Exception { CachingProvider provider = Caching.getCachingProvider(); javax.cache.CacheManager cacheManager = provider.getCacheManager(this.getClass().getResource("/ehcache-107-types.xml").toURI(), getClass().getClassLoader()); MutableConfiguration<Long, String> cache1Conf = new MutableConfiguration<>(); cache1Conf.setTypes(Long.class, String.class); javax.cache.Cache<Long, String> cache = cacheManager.createCache("cache1", cache1Conf); @SuppressWarnings("unchecked") Configuration<Long, String> cache1CompleteConf = cache.getConfiguration(Configuration.class); assertThat(cache1CompleteConf.getKeyType(), is(equalTo(Long.class))); assertThat(cache1CompleteConf.getValueType(), is(equalTo(String.class))); try { cacheManager.getCache("cache1"); } finally { cacheManager.destroyCache("cache1"); cacheManager.close(); } }
@Test public void testManagementDisabledOverriddenFromTemplate() throws Exception { CacheManager cacheManager = provider.getCacheManager(getClass().getResource("/ehcache-107-mbeans-template-config.xml") .toURI(), provider.getDefaultClassLoader()); MutableConfiguration<Long, String> configuration = new MutableConfiguration<>(); configuration.setTypes(Long.class, String.class); configuration.setManagementEnabled(false); configuration.setStatisticsEnabled(false); Cache<Long, String> cache = cacheManager.createCache("enables-mbeans", configuration); @SuppressWarnings("unchecked") Eh107Configuration<Long, String> eh107Configuration = cache.getConfiguration(Eh107Configuration.class); assertThat(eh107Configuration.isManagementEnabled(), is(true)); assertThat(eh107Configuration.isStatisticsEnabled(), is(true)); assertThat(isMbeanRegistered("enables-mbeans", MBEAN_MANAGEMENT_TYPE), is(true)); assertThat(isMbeanRegistered("enables-mbeans", MBEAN_STATISTICS_TYPE), is(true)); }
@Test public void immediate() { final CachingProvider cachingProvider = Caching.getCachingProvider(); final CacheManager cacheManager = cachingProvider.getCacheManager(); cacheManager.createCache("default", new MutableConfiguration<Object, Object>() .setExpiryPolicyFactory( new FactoryBuilder.SingletonFactory<ExpiryPolicy>(new CreatedExpiryPolicy(Duration.ZERO)))); final Cache<String, String> cache = cacheManager.getCache("default"); assertFalse(cache.containsKey("foo")); cache.put("foo", "bar"); assertFalse(cache.containsKey("foo")); cachingProvider.close(); } }
/** * Tests start -> destroy -> start -> close using CacheManager. */ @Test public void testTckStyleCreateDestroyClose() throws Exception { startGridsMultiThreaded(gridCount()); CacheManager mgr = Caching.getCachingProvider().getCacheManager(); String cacheName = "cache"; mgr.createCache(cacheName, new MutableConfiguration<Integer, String>().setTypes(Integer.class, String.class)); mgr.destroyCache(cacheName); Cache<Integer, String> cache = mgr.createCache(cacheName, new MutableConfiguration<Integer, String>().setTypes(Integer.class, String.class)); cache.close(); // Check second close succeeds without exception. cache.close(); try { cache.get(1); fail(); } catch (IllegalStateException ignored) { // No-op; } }
/** * */ @Test public void testStartIgnite() { javax.cache.spi.CachingProvider cachingProvider = Caching.getCachingProvider(); assert cachingProvider instanceof CachingProvider; CacheManager cacheMgr = cachingProvider.getCacheManager(); assertEquals(Collections.<String>emptySet(), Sets.newHashSet(cacheMgr.getCacheNames())); Cache<Integer, String> cacheA = cacheMgr.createCache("a", new CacheConfiguration()); cacheA.put(1, "1"); assertEquals("1", cacheA.get(1)); cacheMgr.createCache("b", new CacheConfiguration()); assertEquals(Sets.newHashSet("a", "b"), Sets.newHashSet(cacheMgr.getCacheNames())); cacheMgr.destroyCache("a"); cacheMgr.destroyCache("b"); assertEquals(Collections.<String>emptySet(), Sets.newHashSet(cacheMgr.getCacheNames())); }
@Test public void cachingProviderGetCache() { String name = "c1"; Caching.getCachingProvider().getCacheManager().createCache(name, new MutableConfiguration().setTypes(Long.class, String.class)); Cache cache = Caching.getCache(name, Long.class, String.class); assertEquals(name, cache.getName()); Caching.getCachingProvider().getCacheManager().destroyCache(name); }
@Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); CachingProvider provider = Caching.getCachingProvider(); cacheManager = provider.getCacheManager(this.getClass().getResource("/ehcache-loader-writer-107-load-atomics.xml").toURI(), getClass().getClassLoader()); testCache = cacheManager.createCache("testCache", new MutableConfiguration<Number, CharSequence>() .setReadThrough(true) .setWriteThrough(true) .setCacheLoaderFactory(() -> cacheLoader) .setCacheWriterFactory(() -> cacheWriter) .setTypes(Number.class, CharSequence.class)); }
@Test public void enablingReadThroughDoesNotForceWriteThrough() throws Exception { MutableConfiguration<Long, String> config = getConfiguration(true, cacheLoader, false, cacheWriter); Cache<Long, String> cache = cachingProvider.getCacheManager().createCache("writingCache", config); cache.put(42L, "Tadam!!!"); cache.get(100L); verifyZeroInteractions(cacheWriter); verify(cacheLoader).load(100L); }