001: package junit;
002:
003: import static org.junit.Assert.*;
004:
005: import java.util.*;
006: import javax.cache.Cache;
007: import javax.cache.CacheStatistics;
008:
009: import ri.cache.BasicCache;
010: import ri.cache.loader.NullCacheLoader;
011: import ri.cache.eviction.NullEvictionStrategy;
012:
013: import org.junit.Test;
014:
015: /**
016: * BasicTestCase
017: *
018: * @author Brian Goetz
019: */
020: public class BasicTestCase extends CacheTestCase {
021: private static final Object KEY1 = "key1";
022: private static final Object VALUE1 = "value1";
023: private static final Object KEY2 = "key2";
024:
025: @Test
026: public void testEmpty() throws Exception {
027: Cache c = createNewCache();
028:
029: assertInternallyConsistent(c);
030:
031: assertTrue(c.size() >= 0);
032: assertDoesNotContain(c, KEY1);
033: assertTrue(c.put(KEY1, VALUE1) == null);
034:
035: assertInternallyConsistent(c);
036: }
037:
038: @Test
039: public void testSinglePut() throws Exception {
040: Cache c = createNewCache();
041:
042: assertTrue(c.put(KEY1, VALUE1) == null);
043:
044: assertInternallyConsistent(c);
045:
046: assertTrue(c.size() >= 1);
047: assertContains(c, KEY1, VALUE1);
048:
049: assertInternallyConsistent(c);
050: }
051:
052: @Test
053: public void testGetAll() throws Exception {
054: Cache c = createNewCache();
055:
056: c.put(KEY1, VALUE1);
057: Set s = new HashSet();
058: s.add(KEY1);
059: s.add(KEY2);
060: Map m = c.getAll(s);
061:
062: assertTrue(m != null);
063: assertTrue(VALUE1.equals(m.get(KEY1)));
064: assertTrue(m.get(KEY2) == null);
065: assertInternallyConsistent(m);
066:
067: CacheStatistics stats = c.getCacheStatistics();
068:
069: assertTrue(stats.getCacheHits() == 1);
070: assertTrue(stats.getCacheMisses() == 1);
071:
072: assertInternallyConsistent(c);
073: }
074:
075: @Test
076: public void testClear() throws Exception {
077: Cache c = createNewCache();
078:
079: TestListener l = new TestListener();
080: c.addListener(l);
081:
082: c.get(KEY1);
083: c.put(KEY1, VALUE1);
084: c.get(KEY1);
085:
086: c.clear();
087:
088: CacheStatistics stats = c.getCacheStatistics();
089: assertTrue(stats.getCacheHits() == 0);
090: assertTrue(stats.getCacheMisses() == 0);
091:
092: assertTrue(l.clears == 1);
093:
094: assertDoesNotContain(c, KEY1);
095:
096: assertInternallyConsistent(c);
097: }
098:
099: @Test
100: public void testStatistics() throws Exception {
101: Cache c = createNewCache();
102:
103: c.put(KEY1, VALUE1);
104:
105: for (int i = 0; i < 10; i++)
106: c.get(KEY1);
107:
108: c.remove(KEY1);
109:
110: c.get(KEY1);
111:
112: CacheStatistics s = c.getCacheStatistics();
113: assertTrue(s != null);
114: assertTrue(s.getCacheHits() == 10);
115: assertTrue(s.getCacheMisses() == 1);
116:
117: assertInternallyConsistent(c);
118: }
119:
120: @Test
121: public void testListener() throws Exception {
122: Cache c = createNewCache();
123:
124: TestListener l = new TestListener();
125: c.addListener(l);
126:
127: c.put(KEY1, VALUE1);
128: assertTrue(l.puts == 1);
129: c.put(KEY1, VALUE1);
130: assertTrue(l.puts == 2);
131:
132: c.remove(KEY1);
133: c.remove(KEY1);
134: assertTrue(l.removes == 1);
135:
136: assertInternallyConsistent(c);
137: }
138:
139: @Test
140: public void testLoader() throws Exception {
141: Cache c = new BasicCache("foo", new NullEvictionStrategy(),
142: new SingleKeyLoader(KEY1, VALUE1));
143: TestListener l = new TestListener();
144: c.addListener(l);
145: assertTrue(VALUE1.equals(c.get(KEY1)));
146: assertTrue(c.get(VALUE1) == null);
147: assertTrue(l.loads == 1);
148: assertInternallyConsistent(c);
149: }
150:
151: @Test
152: public void testExpiration() throws Exception {
153: Cache c = new BasicCache("foo", 10, 10L,
154: new NullEvictionStrategy(), new NullCacheLoader());
155: c.put(KEY1, VALUE1);
156: assertContains(c, KEY1, VALUE1);
157: Thread.sleep(100L);
158: assertDoesNotContain(c, KEY1);
159: assertTrue(c.put(KEY1, VALUE1) == null);
160: assertInternallyConsistent(c);
161: }
162: }
|