Search in sources :

Example 61 with CacheManager

use of javax.cache.CacheManager in project hazelcast by hazelcast.

the class CacheConfigTest method createCache_WhenCacheConfigIsNull.

@Test
public void createCache_WhenCacheConfigIsNull() {
    String cacheName = "cacheNull";
    CacheManager cacheManager = Caching.getCachingProvider().getCacheManager();
    try {
        cacheManager.createCache(cacheName, (Configuration<Object, Object>) null);
        fail("NullPointerException expected");
    } catch (NullPointerException expected) {
        EmptyStatement.ignore(expected);
    }
}
Also used : HazelcastCacheManager(com.hazelcast.cache.HazelcastCacheManager) CacheManager(javax.cache.CacheManager) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 62 with CacheManager

use of javax.cache.CacheManager in project hazelcast by hazelcast.

the class CacheConfigTest method cacheManagerByLocationFileTest.

@Test
public void cacheManagerByLocationFileTest() throws URISyntaxException {
    URI uri = new URI("MY-SCOPE");
    String urlStr = configUrl1.toString();
    assertEquals("file", urlStr.substring(0, 4));
    Properties properties = new Properties();
    properties.setProperty(HazelcastCachingProvider.HAZELCAST_CONFIG_LOCATION, urlStr);
    CacheManager cacheManager = Caching.getCachingProvider().getCacheManager(uri, null, properties);
    assertNotNull(cacheManager);
    URI uri2 = new URI("MY-SCOPE-OTHER");
    String urlStr2 = configUrl2.toString();
    assertEquals("file", urlStr2.substring(0, 4));
    Properties properties2 = new Properties();
    properties2.setProperty(HazelcastCachingProvider.HAZELCAST_CONFIG_LOCATION, urlStr2);
    CacheManager cacheManager2 = Caching.getCachingProvider().getCacheManager(uri2, null, properties2);
    assertNotNull(cacheManager2);
    assertEquals(2, Hazelcast.getAllHazelcastInstances().size());
}
Also used : HazelcastCacheManager(com.hazelcast.cache.HazelcastCacheManager) CacheManager(javax.cache.CacheManager) Properties(java.util.Properties) URI(java.net.URI) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 63 with CacheManager

use of javax.cache.CacheManager in project hazelcast by hazelcast.

the class CacheReadWriteThroughTest method do_putAsAdd_writeThrough.

private void do_putAsAdd_writeThrough(boolean acceptAll) {
    final int ENTRY_COUNT = 100;
    final String cacheName = randomName();
    CacheManager cacheManager = cachingProvider.getCacheManager();
    assertNotNull(cacheManager);
    assertNull(cacheManager.getCache(cacheName));
    PutCacheWriter putCacheWriter;
    if (acceptAll) {
        putCacheWriter = new PutCacheWriter();
    } else {
        putCacheWriter = new PutCacheWriter(new ModValueChecker(ENTRY_COUNT / 10));
    }
    CacheConfig<Integer, Integer> config = createCacheConfig();
    config.setWriteThrough(true);
    config.setCacheWriterFactory(FactoryBuilder.factoryOf(putCacheWriter));
    ICache<Integer, Integer> cache = cacheManager.createCache(cacheName, config).unwrap(ICache.class);
    assertNotNull(cache);
    List<Integer> bannedKeys = new ArrayList<Integer>();
    for (int i = 0; i < ENTRY_COUNT; i++) {
        try {
            cache.put(i, i);
        } catch (CacheWriterException e) {
            bannedKeys.add(i);
        }
    }
    assertEquals(ENTRY_COUNT - bannedKeys.size(), cache.size());
    for (Integer bannedKey : bannedKeys) {
        assertNull(cache.get(bannedKey));
    }
}
Also used : ArrayList(java.util.ArrayList) CacheManager(javax.cache.CacheManager) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 64 with CacheManager

use of javax.cache.CacheManager in project hazelcast by hazelcast.

the class CacheReadWriteThroughTest method loadAll_readThrough.

private void loadAll_readThrough(boolean throwError) throws Exception {
    final String cacheName = randomName();
    CacheManager cacheManager = cachingProvider.getCacheManager();
    assertNotNull(cacheManager);
    assertNull(cacheManager.getCache(cacheName));
    CacheConfig<Integer, Integer> config = createCacheConfig();
    config.setReadThrough(true);
    config.setCacheLoaderFactory(FactoryBuilder.factoryOf(new GetAllAsyncCacheLoader(throwError)));
    Cache<Integer, Integer> cache = cacheManager.createCache(cacheName, config);
    assertNotNull(cache);
    Set<Integer> keys = new HashSet<Integer>();
    for (int i = 0; i < 150; i++) {
        keys.add(i);
    }
    final CountDownLatch latch = new CountDownLatch(1);
    cache.loadAll(keys, false, new CompletionListener() {

        @Override
        public void onCompletion() {
            latch.countDown();
        }

        @Override
        public void onException(Exception e) {
            e.printStackTrace();
            latch.countDown();
        }
    });
    assertOpenEventually(latch);
    if (!throwError) {
        assertEquals(100, cache.unwrap(ICache.class).size());
    }
}
Also used : CompletionListener(javax.cache.integration.CompletionListener) CountDownLatch(java.util.concurrent.CountDownLatch) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheWriterException(javax.cache.integration.CacheWriterException) CacheManager(javax.cache.CacheManager) HashSet(java.util.HashSet)

Example 65 with CacheManager

use of javax.cache.CacheManager in project hazelcast by hazelcast.

the class CacheReadWriteThroughTest method test_getAll_readThrough.

@Test
public void test_getAll_readThrough() throws Exception {
    final String cacheName = randomName();
    CacheManager cacheManager = cachingProvider.getCacheManager();
    assertNotNull(cacheManager);
    assertNull(cacheManager.getCache(cacheName));
    CacheConfig<Integer, Integer> config = createCacheConfig();
    config.setReadThrough(true);
    config.setCacheLoaderFactory(FactoryBuilder.factoryOf(new GetAllAsyncCacheLoader(false)));
    Cache<Integer, Integer> cache = cacheManager.createCache(cacheName, config);
    assertNotNull(cache);
    Set<Integer> keys = new HashSet<Integer>();
    for (int i = 0; i < 150; i++) {
        keys.add(i);
    }
    Map<Integer, Integer> loaded = cache.getAll(keys);
    assertEquals(100, loaded.size());
}
Also used : CacheManager(javax.cache.CacheManager) HashSet(java.util.HashSet) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

CacheManager (javax.cache.CacheManager)105 Test (org.junit.Test)63 QuickTest (com.hazelcast.test.annotation.QuickTest)56 CachingProvider (javax.cache.spi.CachingProvider)48 ParallelTest (com.hazelcast.test.annotation.ParallelTest)41 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)40 HazelcastInstance (com.hazelcast.core.HazelcastInstance)34 CacheConfig (com.hazelcast.config.CacheConfig)32 AssertTask (com.hazelcast.test.AssertTask)18 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)18 MutableConfiguration (javax.cache.configuration.MutableConfiguration)17 URI (java.net.URI)16 Cache (javax.cache.Cache)15 HazelcastCacheManager (com.hazelcast.cache.HazelcastCacheManager)13 HazelcastClientCachingProvider (com.hazelcast.client.cache.impl.HazelcastClientCachingProvider)12 ICache (com.hazelcast.cache.ICache)10 Properties (java.util.Properties)10 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)9 ClientConfig (com.hazelcast.client.config.ClientConfig)8 HazelcastCachingProvider (com.hazelcast.cache.HazelcastCachingProvider)7