Exemple #1
0
 static {
   try {
     cacheManager = CacheManager.create(CacheFactory.class.getResource("/ehcache.xml"));
   } catch (CacheException e) {
     e.printStackTrace();
   }
 }
  public CasAuthentication get(String serviceTicket) {
    Element element = null;
    try {
      element = cache.get(serviceTicket);
    } catch (CacheException cacheException) {
      throw new CacheException("Cache failure: " + cacheException.getMessage());
    }

    if (logger.isDebugEnabled()) {
      logger.debug("Cache hit: " + (element != null) + "; service ticket: " + serviceTicket);
    }

    if (element == null) {
      return null;
    } else {
      return (CasAuthentication) element.getValue();
    }
  }
  /** Tests refreshing the entries. */
  @Test
  public void testRefreshWithException() throws Exception {
    final String value = "value";
    final CountingCacheEntryFactory factory = new CountingCacheEntryFactory(value);
    selfPopulatingCache = new SelfPopulatingCache(cache, factory);

    // Check the value
    String explodingKey = "explode";
    assertSame(value, selfPopulatingCache.get(explodingKey).getObjectValue());
    assertEquals(1, factory.getCount());

    // Refresh
    try {
      selfPopulatingCache.refresh();
      fail("This should have exploded!");
    } catch (CacheException e) {
      assertNotNull(e.getCause());
      assertEquals(
          e.getCause().getMessage() + " on refresh with key " + explodingKey, e.getMessage());
    }
  }
  @Test
  public void testCASOperationsNotSupported() throws Exception {
    LOG.info("START TEST");

    final Ehcache cache1 = manager1.getEhcache(cacheName);
    final Ehcache cache2 = manager2.getEhcache(cacheName);
    final Ehcache cache3 = manager3.getEhcache(cacheName);
    final Ehcache cache4 = manager4.getEhcache(cacheName);

    try {
      cache1.putIfAbsent(new Element("foo", "poo"));
      throw new AssertionError("CAS operation should have failed.");
    } catch (CacheException ce) {
      assertEquals(true, ce.getMessage().contains("CAS"));
    }

    try {
      cache2.removeElement(new Element("foo", "poo"));
      throw new AssertionError("CAS operation should have failed.");
    } catch (CacheException ce) {
      assertEquals(true, ce.getMessage().contains("CAS"));
    }

    try {
      cache3.replace(new Element("foo", "poo"));
      throw new AssertionError("CAS operation should have failed.");
    } catch (CacheException ce) {
      assertEquals(true, ce.getMessage().contains("CAS"));
    }

    try {
      cache4.replace(new Element("foo", "poo"), new Element("foo", "poo2"));
      throw new AssertionError("CAS operation should have failed.");
    } catch (CacheException ce) {
      assertEquals(true, ce.getMessage().contains("CAS"));
    }

    LOG.info("END TEST");
  }