Search in sources :

Example 11 with CacheManager

use of org.cache2k.CacheManager in project cache2k by cache2k.

the class OsgiIT method testSimple.

@Test
public void testSimple() {
    CacheManager m = CacheManager.getInstance("testSimple");
    Cache<String, String> c = Cache2kBuilder.of(String.class, String.class).manager(m).eternal(true).build();
    c.put("abc", "123");
    assertTrue(c.containsKey("abc"));
    assertEquals("123", c.peek("abc"));
    c.close();
}
Also used : CacheManager(org.cache2k.CacheManager) Test(org.junit.Test)

Example 12 with CacheManager

use of org.cache2k.CacheManager in project cache2k by cache2k.

the class OsgiIT method testWithAnonBuilder.

@Test
public void testWithAnonBuilder() {
    CacheManager m = CacheManager.getInstance("testWithAnonBuilder");
    Cache<String, String> c = new Cache2kBuilder<String, String>() {
    }.manager(m).eternal(true).build();
    c.put("abc", "123");
    assertTrue(c.containsKey("abc"));
    assertEquals("123", c.peek("abc"));
    c.close();
}
Also used : CacheManager(org.cache2k.CacheManager) Test(org.junit.Test)

Example 13 with CacheManager

use of org.cache2k.CacheManager in project cache2k by cache2k.

the class OsgiIT method testEventPackage.

/**
 * Simple test to see whether event package is exported.
 */
@Test
public void testEventPackage() {
    CacheManager m = CacheManager.getInstance("testEventPackage");
    final AtomicInteger _count = new AtomicInteger();
    Cache<String, String> c = new Cache2kBuilder<String, String>() {
    }.manager(m).eternal(true).addListener(new CacheEntryCreatedListener<String, String>() {

        @Override
        public void onEntryCreated(final Cache<String, String> cache, final CacheEntry<String, String> entry) {
            _count.incrementAndGet();
        }
    }).build();
    c.put("abc", "123");
    assertTrue(c.containsKey("abc"));
    assertEquals("123", c.peek("abc"));
    assertEquals(1, _count.get());
    c.close();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryCreatedListener(org.cache2k.event.CacheEntryCreatedListener) CacheManager(org.cache2k.CacheManager) CacheEntry(org.cache2k.CacheEntry) Cache2kBuilder(org.cache2k.Cache2kBuilder) Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 14 with CacheManager

use of org.cache2k.CacheManager in project cache2k by cache2k.

the class JmxSupportTest method multipleWarnings.

/**
 * Construct three caches with multiple issues and check the health string of the manager JMX.
 */
@Test
public void multipleWarnings() throws Exception {
    final String _MANAGER_NAME = getClass().getName() + ".multipleWarnings";
    final String _CACHE_NAME_BAD_HASHING = "cacheWithBadHashing";
    final String _CACHE_NAME_KEY_MUTATION = "cacheWithKeyMutation";
    final String _CACHE_NAME_MULTIPLE_ISSUES = "cacheWithMultipleIssues";
    final String _SUPPRESS1 = "org.cache2k.Cache/" + _MANAGER_NAME + ":" + _CACHE_NAME_KEY_MUTATION;
    final String _SUPPRESS2 = "org.cache2k.Cache/" + _MANAGER_NAME + ":" + _CACHE_NAME_MULTIPLE_ISSUES;
    Log.registerSuppression(_SUPPRESS1, new Log.SuppressionCounter());
    Log.registerSuppression(_SUPPRESS2, new Log.SuppressionCounter());
    CacheManager m = CacheManager.getInstance(_MANAGER_NAME);
    Cache _cacheWithBadHashing = Cache2kBuilder.of(Object.class, Object.class).manager(m).name(_CACHE_NAME_BAD_HASHING).eternal(true).build();
    Cache _cacheWithKeyMutation = Cache2kBuilder.of(Object.class, Object.class).manager(m).name(_CACHE_NAME_KEY_MUTATION).eternal(true).entryCapacity(50).build();
    Cache _cacheWithMultipleIssues = Cache2kBuilder.of(Object.class, Object.class).manager(m).name(_CACHE_NAME_MULTIPLE_ISSUES).entryCapacity(50).eternal(true).build();
    for (int i = 0; i < 9; i++) {
        _cacheWithBadHashing.put(new KeyForMutation(), 1);
    }
    for (int i = 0; i < 100; i++) {
        _cacheWithMultipleIssues.put(new KeyForMutation(), 1);
    }
    for (int i = 0; i < 100; i++) {
        KeyForMutation v = new KeyForMutation();
        _cacheWithKeyMutation.put(v, 1);
        _cacheWithMultipleIssues.put(v, 1);
        v.value = 1;
    }
    String _health = (String) server.getAttribute(getCacheManagerObjectName(_MANAGER_NAME), "HealthStatus");
    assertEquals("FAILURE: [cacheWithKeyMutation] hash quality is 0 (threshold: 5); " + "FAILURE: [cacheWithMultipleIssues] hash quality is 0 (threshold: 5); " + "WARNING: [cacheWithBadHashing] hash quality is 7 (threshold: 20); " + "WARNING: [cacheWithKeyMutation] key mutation detected; " + "WARNING: [cacheWithMultipleIssues] key mutation detected", _health);
    Log.deregisterSuppression(_SUPPRESS1);
    Log.deregisterSuppression(_SUPPRESS2);
    m.close();
}
Also used : Log(org.cache2k.core.util.Log) CacheManager(org.cache2k.CacheManager) Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 15 with CacheManager

use of org.cache2k.CacheManager in project cache2k by cache2k.

the class JmxSupportTest method testManagerPresent.

@Test
public void testManagerPresent() throws Exception {
    String _name = getClass().getName() + ".testManagerPresent";
    CacheManager m = CacheManager.getInstance(_name);
    MBeanInfo i = getCacheManagerInfo(_name);
    assertEquals(ManagerMXBeanImpl.class.getName(), i.getClassName());
    m.close();
}
Also used : MBeanInfo(javax.management.MBeanInfo) CacheManager(org.cache2k.CacheManager) Test(org.junit.Test)

Aggregations

CacheManager (org.cache2k.CacheManager)27 Test (org.junit.Test)24 Cache (org.cache2k.Cache)11 MBeanInfo (javax.management.MBeanInfo)4 URLClassLoader (java.net.URLClassLoader)3 Cache2kBuilder (org.cache2k.Cache2kBuilder)3 Log (org.cache2k.core.util.Log)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 WeakHashMap (java.util.WeakHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 CacheEntry (org.cache2k.CacheEntry)1 Cache2kConfiguration (org.cache2k.configuration.Cache2kConfiguration)1 CacheEntryCreatedListener (org.cache2k.event.CacheEntryCreatedListener)1