01: /*
02: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
03: * notice. All rights reserved.
04: */
05: package com.tctest;
06:
07: import net.sf.ehcache.Cache;
08: import net.sf.ehcache.CacheManager;
09: import net.sf.ehcache.Element;
10: import net.sf.ehcache.constructs.blocking.BlockingCache;
11:
12: import com.tc.simulator.app.ApplicationConfig;
13: import com.tc.simulator.listener.ListenerProvider;
14: import com.tc.util.Assert;
15: import com.tctest.runner.AbstractErrorCatchingTransparentApp;
16:
17: import java.util.Arrays;
18:
19: /*
20: * Basic sanity test for Blocking cache.
21: */
22: public abstract class BlockingCacheTestApp extends
23: AbstractErrorCatchingTransparentApp {
24:
25: public BlockingCacheTestApp(String appId, ApplicationConfig cfg,
26: ListenerProvider listenerProvider) {
27: super (appId, cfg, listenerProvider);
28: }
29:
30: protected void runTest() throws Throwable {
31: System.out.println(Arrays.asList(getCacheManger()
32: .getCacheNames()));
33:
34: try {
35: testBlockingCache();
36:
37: barrier();
38: } finally {
39: getCacheManger().shutdown();
40: }
41:
42: }
43:
44: protected abstract int barrier() throws Exception;
45:
46: protected abstract CacheManager getCacheManger();
47:
48: private void testBlockingCache() throws Exception {
49: Cache cache = getCacheManger().getCache("sampleCache1");
50: BlockingCache bCache = new BlockingCache(cache);
51: populateCache(bCache);
52: barrier();
53:
54: Assert.assertEquals(new Element("k1", "v1"), bCache.get("k1"));
55: Assert.assertEquals(new Element("k2", "v2"), bCache.get("k2"));
56: Assert.assertEquals(new Element("k3", "v3"), bCache.get("k3"));
57: Assert.assertTrue(bCache.isElementInMemory("k1"));
58: Assert.assertTrue(bCache.isElementInMemory("k2"));
59: Assert.assertTrue(bCache.isElementInMemory("k3"));
60: }
61:
62: private void populateCache(BlockingCache cache) throws Exception {
63: if (barrier() == 0) {
64: cache.removeAll();
65: cache.put(new Element("k1", "v1"));
66: cache.put(new Element("k2", "v2"));
67: cache.put(new Element("k3", "v3"));
68: }
69: }
70:
71: }
|