@Test
  public void testWriteBehind()
      throws ClassNotFoundException, InstantiationException, IllegalAccessException, SAXException,
          IOException, InterruptedException {

    Configuration configuration =
        new XmlConfiguration(this.getClass().getResource("/configs/writebehind-cache.xml"));
    assertThat(configuration.getCacheConfigurations().containsKey("bar"), is(true));
    final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration);
    cacheManager.init();
    final Cache<Number, String> cache = cacheManager.getCache("bar", Number.class, String.class);
    assertThat(cache, notNullValue());
    assertThat(cache.get(1), notNullValue());
    final Number key = 42L;
    TestCacheLoaderWriter.latch = new CountDownLatch(1);
    cache.put(key, "Bye y'all!");
    TestCacheLoaderWriter.latch.await(2, TimeUnit.SECONDS);
    assertThat(TestCacheLoaderWriter.lastWrittenKey, is(key));

    assertThat(configuration.getCacheConfigurations().containsKey("template1"), is(true));
    final Cache<Number, String> templateCache =
        cacheManager.getCache("template1", Number.class, String.class);
    assertThat(templateCache, notNullValue());
    assertThat(templateCache.get(1), notNullValue());
    final Number key1 = 100L;
    TestCacheLoaderWriter.latch = new CountDownLatch(2);
    templateCache.put(42L, "Howdy!");
    templateCache.put(key1, "Bye y'all!");
    TestCacheLoaderWriter.latch.await(2, TimeUnit.SECONDS);
    assertThat(TestCacheLoaderWriter.lastWrittenKey, is(key1));
  }
 @Test
 public void testCacheEventListener() throws Exception {
   Configuration configuration =
       new XmlConfiguration(
           this.getClass().getResource("/configs/ehcache-cacheEventListener.xml"));
   assertThat(configuration.getCacheConfigurations().containsKey("bar"), is(true));
   final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration);
   cacheManager.init();
   final Cache<Number, String> cache = cacheManager.getCache("bar", Number.class, String.class);
   cache.put(10, "dog");
   assertThat(TestCacheEventListener.FIRED_EVENT.getType(), is(EventType.CREATED));
   cache.put(10, "cat");
   assertThat(TestCacheEventListener.FIRED_EVENT.getType(), is(EventType.UPDATED));
   cache.remove(10);
   assertThat(TestCacheEventListener.FIRED_EVENT.getType(), is(EventType.REMOVED));
   cache.put(10, "dog");
   resetValues();
   assertThat(configuration.getCacheConfigurations().containsKey("template1"), is(true));
   final Cache<Number, String> templateCache =
       cacheManager.getCache("template1", Number.class, String.class);
   templateCache.put(10, "cat");
   assertThat(TestCacheEventListener.FIRED_EVENT, nullValue());
   templateCache.put(10, "dog");
   assertThat(TestCacheEventListener.FIRED_EVENT.getType(), is(EventType.UPDATED));
 }
  @Test
  public void testLoaderWriter()
      throws ClassNotFoundException, SAXException, InstantiationException, IOException,
          IllegalAccessException {
    Configuration configuration =
        new XmlConfiguration(this.getClass().getResource("/configs/cache-integration.xml"));
    assertThat(configuration.getCacheConfigurations().containsKey("bar"), is(true));
    final CacheManager cacheManager = CacheManagerBuilder.newCacheManager(configuration);
    cacheManager.init();
    final Cache<Number, String> cache = cacheManager.getCache("bar", Number.class, String.class);
    assertThat(cache, notNullValue());
    assertThat(cache.get(1), notNullValue());
    final Number key = new Long(42);
    cache.put(key, "Bye y'all!");
    assertThat(TestCacheLoaderWriter.lastWrittenKey, is(key));

    assertThat(configuration.getCacheConfigurations().containsKey("template1"), is(true));
    final Cache<Number, String> templateCache =
        cacheManager.getCache("template1", Number.class, String.class);
    assertThat(templateCache, notNullValue());
    assertThat(templateCache.get(1), notNullValue());
    final Number key1 = new Long(100);
    templateCache.put(key1, "Bye y'all!");
    assertThat(TestCacheLoaderWriter.lastWrittenKey, is(key1));
  }