javax.persistence.Cache

Here are the examples of the java api class javax.persistence.Cache taken from open source projects.

1. EhCacheProviderTest#testCreateCache()

Project: Kundera
File: EhCacheProviderTest.java
/**
     * Test method for.
     * 
     * {@link com.impetus.kundera.cache.ehcache.EhCacheProvider#createCache(java.lang.String)}
     * .
     */
public void testCreateCache() {
    // Initialize Cache Provider
    assertNotNull(cacheProvider);
    try {
        cacheProvider.init(cacheResource);
    } catch (CacheException e) {
        fail(e.getMessage());
    }
    assertNotNull(cacheProvider.getCacheManager());
    // Initialize Cache
    Cache cache = null;
    try {
        cache = cacheProvider.createCache(cacheName);
    } catch (CacheException e) {
        fail(e.getMessage());
    }
    assertNotNull(cache);
    assertEquals(cache.getClass(), EhCacheWrapper.class);
    EhCacheWrapper ehCache = (EhCacheWrapper) cache;
    assertEquals(0, ehCache.size());
    // Store objects into cache
    ehCache.put(person1.getClass() + "_" + person1.getPersonId(), person1);
    assertEquals(1, ehCache.size());
    ehCache.put(person2.getClass() + "_" + person2.getPersonId(), person2);
    assertEquals(2, ehCache.size());
    // Lookup objects from cache
    Object o = ehCache.get(person1.getClass() + "_" + person1.getPersonId());
    assertEquals(PersonnelDTO.class, o.getClass());
    PersonnelDTO p1 = (PersonnelDTO) o;
    assertNotNull(p1);
    assertEquals("1", p1.getPersonId());
    assertEquals("Amresh", p1.getFirstName());
    assertEquals("Singh", p1.getLastName());
    // Remove object from cache
    ehCache.evict(PersonnelDTO.class, PersonnelDTO.class + "_" + person1.getPersonId());
    assertEquals(1, ehCache.size());
    cache = cacheProvider.getCache(cacheName);
    Assert.assertNotNull(cache);
    // Clear cache
    cacheProvider.clearAll();
    assertEquals(0, ehCache.size());
    // trying when manager is not null.
    Assert.assertNotNull(cacheProvider.getCacheManager());
    try {
        Map map = new HashMap();
        cacheProvider.init(map);
    } catch (CacheException e) {
        fail(e.getMessage());
    }
    // Shutting down.
    cacheProvider.shutdown();
    try {
        cache = cacheProvider.getCache(cacheName);
    } catch (Exception e) {
        Assert.assertEquals("CacheFactory was not initialized. Call init() before creating a cache.", e.getMessage());
    }
    Assert.assertNotNull(cache);
    Assert.assertNull(cacheProvider.getCacheManager());
    // trying after manager is null.
    try {
        Map map = new HashMap();
        cacheProvider.init(map);
    } catch (CacheException e) {
        fail(e.getMessage());
    }
    // Shutting down.
    cacheProvider.shutdown();
    try {
        Map map = new HashMap();
        map.put("net.sf.ehcache.configurationResourceName", "Kundera");
        cacheProvider.init(map);
        Assert.assertNotNull(cacheProvider.getCacheManager());
    } catch (CacheException e) {
        fail(e.getMessage());
    }
    // Shutting down.
    cacheProvider.shutdown();
    try {
        Map map = new HashMap();
        map.put("net.sf.ehcache.configurationResourceName", null);
        cacheProvider.init(map);
        Assert.assertNotNull(cacheProvider.getCacheManager());
    } catch (CacheException e) {
        fail(e.getMessage());
    }
}