001: package org.shiftone.cache.util;
002:
003: import junit.framework.TestCase;
004: import org.shiftone.cache.Cache;
005: import org.shiftone.cache.CacheFactory;
006:
007: import java.util.Random;
008:
009: /**
010: * Class TestCaseBase
011: *
012: *
013: * @author <a href="mailto:jeff@shiftone.org">Jeff Drost</a>
014: * @version $Revision: 1.13 $
015: */
016: public abstract class TestCaseBase extends TestCase {
017:
018: /// private static final Logger LOG = Logger.getLogger(TestCaseBase.class);
019: static Random random = new Random(System.currentTimeMillis());
020: private CacheFactory cacheFactory = getCacheFactory();
021:
022: /**
023: * Constructor TestCaseBase
024: *
025: *
026: * @param name
027: */
028: public TestCaseBase(String name) {
029: super (name);
030: }
031:
032: /**
033: * Method getCacheFactory
034: */
035: public abstract CacheFactory getCacheFactory();
036:
037: /**
038: * Method newInstance
039: */
040: public Cache newCache(long timeoutMilliSeconds, int maxSize) {
041: return cacheFactory.newInstance("testCaseBase",
042: timeoutMilliSeconds, maxSize);
043: }
044:
045: /**
046: * Method test10000Puts
047: */
048: public void test10000UniquePuts() {
049:
050: Cache cache = newCache(1000 * 60, 1000);
051:
052: for (int i = 0; i < 10000; i++) {
053: cache.addObject("k-" + i, "v-" + i);
054: }
055:
056: assertEquals(1000, cache.size());
057: cache.clear();
058: assertEquals(0, cache.size());
059: }
060:
061: /**
062: * Method test10000Puts
063: */
064: public void test10000Puts() {
065:
066: Cache cache = newCache(1000 * 60, 1000);
067:
068: for (int i = 0; i < 10000; i++) {
069: cache.addObject("k-" + (i % 500), "v-" + i);
070: }
071:
072: assertEquals(500, cache.size());
073: cache.clear();
074: assertEquals(0, cache.size());
075: }
076:
077: /**
078: * Method test10000Puts
079: */
080: public void test1000Puts10000GetsSameKey() {
081:
082: Cache cache = newCache(1000 * 60, 500);
083:
084: for (int i = 0; i < 1000; i++) {
085: cache.addObject("k-" + i, "v-" + i);
086: }
087:
088: assertEquals(500, cache.size());
089:
090: for (int i = 0; i < 10000; i++) {
091: assertNotNull(cache.getObject("k-987"));
092: }
093: }
094:
095: /**
096: * Method test10000Puts99999Gets
097: */
098: public void test10000Puts99999Gets() {
099:
100: Cache cache = newCache(1000 * 60, 1000);
101: String key = null;
102:
103: cache.addObject("k-0", "v-0");
104:
105: for (int i = 0; i < 10000; i++) {
106: key = "k-" + (i % 200);
107:
108: cache.addObject(key, "v-" + i);
109: assertNotNull(key, cache.getObject(key));
110: }
111:
112: assertEquals(200, cache.size());
113: }
114:
115: /**
116: * Method test1Put10000GetsSameKey10Puts
117: */
118: public void test1Put10000GetsSameKey2Puts() {
119:
120: Cache cache = newCache(1000 * 60, 2);
121:
122: cache.addObject("x", "x");
123: cache.addObject("y", "y");
124:
125: for (int i = 0; i < 10000; i++) {
126: assertNotNull(cache.getObject("x"));
127: assertNotNull(cache.getObject("y"));
128: }
129:
130: assertEquals(2, cache.size());
131: cache.addObject("a", "a");
132: cache.addObject("b", "b");
133: assertEquals(2, cache.size());
134: assertNotNull(cache.getObject("b"));
135: }
136:
137: /**
138: * Method testCacheOfSize1
139: */
140: public void testCacheOfSize1() {
141:
142: Cache cache = newCache(1000 * 60, 1);
143:
144: for (int i = 0; i < 20; i++) {
145: cache.addObject("k-" + i, "v-" + i);
146: }
147:
148: assertEquals(1, cache.size());
149: }
150:
151: /*
152: public void testRoyLuNullPointerException1()
153: {
154:
155: Cache cache = newCache(1000, 5);
156: AbstractPolicyCache base = (AbstractPolicyCache) cache;
157:
158: base.addObject("a", "b");
159:
160: // cache contains one element
161: base.removeLeastValuableNode();
162:
163: // cache is empty
164: base.removeLeastValuableNode();
165: }
166: */
167:
168: /**
169: * Method randKey
170: */
171: public static String randKey() {
172: return String.valueOf(Math.abs(random.nextInt()));
173: }
174:
175: /**
176: * Method randValue
177: */
178: public static String randValue() {
179:
180: StringBuffer sb = new StringBuffer();
181:
182: for (int i = 0; i < 100; i++) {
183: sb.append(randKey());
184: }
185:
186: return sb.toString();
187: }
188: }
|