Example #1
0
  public void testSelect() throws Exception {
    cache.put(new MyKey("a"), 1);
    cache.put(new MyKey("b"), 2);
    cache.put(new MyKey("c"), 3);
    final AtomicInteger count = new AtomicInteger();
    CachedObjectSelector<Serializable, Object> selector =
        new CachedObjectSelector<Serializable, Object>() {

          public void onSelect(
              ExoCache<? extends Serializable, ? extends Object> cache,
              Serializable key,
              ObjectCacheInfo<? extends Object> ocinfo)
              throws Exception {
            assertTrue(
                key.equals(new MyKey("a"))
                    || key.equals(new MyKey("b"))
                    || key.equals(new MyKey("c")));
            assertTrue(ocinfo.get().equals(1) || ocinfo.get().equals(2) || ocinfo.get().equals(3));
            count.incrementAndGet();
          }

          public boolean select(Serializable key, ObjectCacheInfo<? extends Object> ocinfo) {
            return true;
          }
        };
    try {
      cache.select(selector);
      assertEquals(3, count.intValue());
    } catch (UnsupportedOperationException e) {
      // OK
    }
  }
Example #2
0
 public void testGetCachedObjects() throws Exception {
   cache.put(new MyKey("a"), "a");
   cache.put(new MyKey("b"), "b");
   cache.put(new MyKey("c"), "c");
   cache.put(new MyKey("d"), null);
   assertEquals(3, cache.getCacheSize());
   try {
     List<Object> values = cache.getCachedObjects();
     assertEquals(3, values.size());
     assertTrue(values.contains("a"));
     assertTrue(values.contains("b"));
     assertTrue(values.contains("c"));
   } catch (UnsupportedOperationException e) {
     // OK
   }
 }
Example #3
0
 public void testPut() throws Exception {
   assertEquals(0, listener.put);
   cache.put(new MyKey("a"), "a");
   cache.put(new MyKey("b"), "b");
   cache.put(new MyKey("c"), "c");
   cache.put(new MyKey("d"), null);
   assertEquals(3, listener.put);
   assertEquals(3, cache.getCacheSize());
   assertEquals("a", cache.get(new MyKey("a")));
   cache.put(new MyKey("a"), "c");
   assertEquals(4, listener.put);
   assertEquals(3, cache.getCacheSize());
   assertEquals("c", cache.get(new MyKey("a")));
   cache.put(new MyKey("d"), "c");
   assertEquals(5, listener.put);
   assertEquals(4, cache.getCacheSize());
 }
Example #4
0
 public void testGet() throws Exception {
   assertEquals(0, cache.getCacheSize());
   cache.put(new MyKey("a"), "a");
   assertEquals(1, cache.getCacheSize());
   assertEquals(0, listener.get);
   assertEquals("a", cache.get(new MyKey("a")));
   assertEquals(1, listener.get);
   cache.put(new MyKey("a"), "c");
   assertEquals(1, cache.getCacheSize());
   assertEquals("c", cache.get(new MyKey("a")));
   assertEquals(2, listener.get);
   cache.remove(new MyKey("a"));
   assertEquals(0, cache.getCacheSize());
   assertNull(cache.get(new MyKey("a")));
   assertNull(cache.get(new MyKey("x")));
   assertEquals(4, listener.get);
 }
Example #5
0
 public void testGetHitsNMisses() throws Exception {
   int hits = cache.getCacheHit();
   int misses = cache.getCacheMiss();
   cache.put(new MyKey("a"), "a");
   cache.get(new MyKey("a"));
   cache.remove(new MyKey("a"));
   cache.get(new MyKey("a"));
   cache.get(new MyKey("z"));
   assertEquals(1, cache.getCacheHit() - hits);
   assertEquals(2, cache.getCacheMiss() - misses);
 }
Example #6
0
 public void setUp() throws Exception {
   this.service =
       (CacheService) PortalContainer.getInstance().getComponentInstanceOfType(CacheService.class);
   this.cache = (MCExoCache<Serializable, Object>) service.getCacheInstance("myCache");
   this.factory =
       (ExoCacheFactory)
           PortalContainer.getInstance().getComponentInstanceOfType(ExoCacheFactory.class);
   listener = new MyCacheListener<Object>();
   cache.addCacheListener(listener);
 }
Example #7
0
 public void testRemove() throws Exception {
   cache.put(new MyKey("a"), 1);
   cache.put(new MyKey("b"), 2);
   cache.put(new MyKey("c"), 3);
   assertEquals(3, cache.getCacheSize());
   assertEquals(0, listener.remove);
   assertEquals(1, cache.remove(new MyKey("a")));
   assertEquals(1, listener.remove);
   assertEquals(2, cache.getCacheSize());
   assertEquals(2, cache.remove(new MyKey("b")));
   assertEquals(2, listener.remove);
   assertEquals(1, cache.getCacheSize());
   assertNull(cache.remove(new MyKey("x")));
   assertEquals(2, listener.remove);
   assertEquals(1, cache.getCacheSize());
 }
Example #8
0
  public void testPutMap() throws Exception {
    Map<Serializable, Object> values = new HashMap<Serializable, Object>();
    values.put(new MyKey("a"), "a");
    values.put(new MyKey("b"), "b");
    assertEquals(0, listener.put);
    assertEquals(0, cache.getCacheSize());
    cache.putMap(values);
    assertEquals(2, listener.put);
    assertEquals(2, cache.getCacheSize());
    values =
        new HashMap<Serializable, Object>() {
          private static final long serialVersionUID = 1L;

          public Set<Entry<Serializable, Object>> entrySet() {
            Set<Entry<Serializable, Object>> set =
                new LinkedHashSet<Entry<Serializable, Object>>(super.entrySet());
            set.add(
                new Entry<Serializable, Object>() {

                  public Object setValue(Object paramV) {
                    return null;
                  }

                  public Object getValue() {
                    throw new RuntimeException("An exception");
                  }

                  public Serializable getKey() {
                    return "c";
                  }
                });
            return set;
          }
        };
    values.put(new MyKey("e"), "e");
    values.put(new MyKey("d"), "d");
    cache.putMap(values);
    assertEquals(4, listener.put);
    assertEquals(4, cache.getCacheSize());
  }
Example #9
0
 public void testClearCache() throws Exception {
   cache.put(new MyKey("a"), "a");
   cache.put(new MyKey("b"), "b");
   cache.put(new MyKey("c"), "c");
   assertEquals("a", cache.get(new MyKey("a")));
   assertTrue(cache.getCacheSize() > 0);
   assertEquals(0, listener.clearCache);
   cache.clearCache();
   assertEquals(1, listener.clearCache);
   assertTrue(cache.getCacheSize() == 0);
   assertNull(cache.get(new MyKey("a")));
 }
Example #10
0
 protected void tearDown() throws Exception {
   cache.clearCache();
 }