@Test
 public void testPut() throws Exception {
   buildStore(Collections.<String, String>emptyMap());
   final Ehcache<String, String> ehcache = getEhcache();
   ehcache.put("key", "value");
   verify(cacheEventListener, times(1)).onEvent(argThat(isCreated));
 }
  @Test
  public void testPutStoreThrowsNoWriter() throws Exception {
    buildStore(Collections.singletonMap("key", "oldValue"));
    doThrow(new CacheAccessException("")).when(store).compute(eq("key"), getAnyBiFunction());

    final Ehcache<String, String> ehcache = getEhcache();

    ehcache.put("key", "value");
    verify(cacheEventListener, never()).onEvent(Matchers.<CacheEvent<String, String>>any());
  }
 @Test
 public void testPutKeyNotNullValueNull() {
   final Ehcache<String, String> ehcache = getEhcache();
   try {
     ehcache.put("key", null);
     fail();
   } catch (NullPointerException e) {
     // expected
   }
   verify(cacheEventListener, never()).onEvent(Matchers.<CacheEvent<String, String>>any());
 }
 @Test
 public void testPutWriterThrows() throws Exception {
   buildStore(Collections.singletonMap("key", "oldValue"));
   doThrow(new Exception()).when(cacheLoaderWriter).write("key", "value");
   final Ehcache<String, String> ehcache =
       getEhcache(cacheLoaderWriter, "EhcacheBasicPutEventsTest");
   try {
     ehcache.put("key", "value");
     fail();
   } catch (CacheWritingException e) {
     // Expected
   }
   verify(cacheEventListener, never()).onEvent(Matchers.<CacheEvent<String, String>>any());
 }
 @Test
 public void testPutStoreAndWriterThrow() throws Exception {
   buildStore(Collections.<String, String>emptyMap());
   doThrow(new CacheAccessException("")).when(store).compute(eq("key"), getAnyBiFunction());
   doThrow(new Exception()).when(cacheLoaderWriter).write("key", "value");
   final Ehcache<String, String> ehcache =
       getEhcache(cacheLoaderWriter, "EhcacheBasicPutEventsTest");
   try {
     ehcache.put("key", "value");
     fail();
   } catch (CacheWritingException e) {
     // Expected
   }
   verify(cacheEventListener, never()).onEvent(Matchers.<CacheEvent<String, String>>any());
 }
  /** Checks we can disable ehcache using a system property */
  @Test
  public void testDisableEhcache() throws CacheException, InterruptedException {
    System.setProperty(Cache.NET_SF_EHCACHE_DISABLED, "true");
    Thread.sleep(1000);
    instanceManager = CacheManager.create();
    Ehcache cache = instanceManager.getCache("sampleCache1");
    assertNotNull(cache);
    cache.put(new Element("key123", "value"));
    Element element = cache.get("key123");
    assertNull("When the disabled property is set all puts should be discarded", element);

    cache.putQuiet(new Element("key1234", "value"));
    assertNull(
        "When the disabled property is set all puts should be discarded", cache.get("key1234"));

    System.setProperty(Cache.NET_SF_EHCACHE_DISABLED, "false");
  }
  /** Test using a cache which has been removed and replaced. */
  @Test
  public void testStaleCacheReference() throws CacheException {
    singletonManager = CacheManager.create();
    singletonManager.addCache("test");
    Ehcache cache = singletonManager.getCache("test");
    assertNotNull(cache);
    cache.put(new Element("key1", "value1"));

    assertEquals("value1", cache.get("key1").getObjectValue());
    singletonManager.removeCache("test");
    singletonManager.addCache("test");

    try {
      cache.get("key1");
      fail();
    } catch (IllegalStateException e) {
      assertEquals("The test Cache is not alive.", e.getMessage());
    }
  }
Beispiel #8
0
  /** An integration test, at the CacheManager level, to make sure persistence works */
  @Test
  public void testPersistentStoreFromCacheManager()
      throws IOException, InterruptedException, CacheException {
    // initialise with an instance CacheManager so that the following line actually does something
    CacheManager manager = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-disk.xml");
    Ehcache cache = manager.getCache("persistentLongExpiryIntervalCache");

    LOG.info("DiskStore path: {}", manager.getDiskStorePath());

    for (int i = 0; i < 100; i++) {
      byte[] data = new byte[1024];
      cache.put(new Element("key" + (i + 100), data));
    }
    assertEquals(100, cache.getSize());

    manager.shutdown();

    manager = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-disk.xml");
    cache = manager.getCache("persistentLongExpiryIntervalCache");
    assertEquals(100, cache.getSize());

    manager.shutdown();
  }
Beispiel #9
0
  /**
   * An integration test, at the CacheManager level, to make sure persistence works This test checks
   * the config where a cache is not configured overflow to disk, but is disk persistent. It should
   * work by putting elements in the DiskStore initially and then loading them into memory as they
   * are called.
   */
  @Test
  public void testPersistentNonOverflowToDiskStoreFromCacheManager()
      throws IOException, InterruptedException, CacheException {
    // initialise with an instance CacheManager so that the following line actually does something
    CacheManager manager = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-disk.xml");
    Ehcache cache = manager.getCache("persistentLongExpiryIntervalNonOverflowCache");

    for (int i = 0; i < 100; i++) {
      byte[] data = new byte[1024];
      cache.put(new Element("key" + (i + 100), data));
    }
    assertEquals(100, cache.getSize());

    manager.shutdown();

    manager = new CacheManager(AbstractCacheTest.TEST_CONFIG_DIR + "ehcache-disk.xml");
    cache = manager.getCache("persistentLongExpiryIntervalNonOverflowCache");

    // Now check that the DiskStore is involved in Cache methods it needs to be involved in.
    assertEquals(100, cache.getSize());
    assertEquals(100, cache.getDiskStoreSize());
    assertEquals(100, cache.getKeysNoDuplicateCheck().size());
    assertEquals(100, cache.getKeys().size());
    assertEquals(100, cache.getKeysWithExpiryCheck().size());

    // now check some of the Cache methods work
    assertNotNull(cache.get("key100"));
    assertNotNull(cache.getQuiet("key100"));
    cache.remove("key100");
    assertNull(cache.get("key100"));
    assertNull(cache.getQuiet("key100"));
    cache.removeAll();
    assertEquals(0, cache.getSize());
    assertEquals(0, cache.getDiskStoreSize());

    manager.shutdown();
  }