01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.objectserver.persistence.api;
05:
06: import com.tc.io.serializer.api.StringIndex;
07: import com.tc.objectserver.persistence.impl.StringIndexImpl;
08: import com.tc.util.concurrent.NoExceptionLinkedQueue;
09:
10: import gnu.trove.TLongObjectHashMap;
11: import gnu.trove.TLongObjectIterator;
12: import junit.framework.TestCase;
13:
14: public class StringIndexTest extends TestCase {
15:
16: private TestStringIndexPersistor persistor;
17: private StringIndex index;
18:
19: public void setUp() throws Exception {
20: persistor = new TestStringIndexPersistor();
21: }
22:
23: public void test() throws Exception {
24: index = new StringIndexImpl(persistor);
25: // make sure it loads all the data on construction
26: assertNotNull(persistor.loadCalls.poll(0));
27:
28: // make sure index 0 returns null;
29: assertNull(index.getStringFor(0));
30:
31: // make sure it assigns the index as expected
32: String testString = ":SLDKFJSD";
33: assertEquals(1, index.getOrCreateIndexFor(testString));
34:
35: // make sure it save the mapping as expected
36: assertEquals(testString, persistor.target.get(1));
37:
38: // clear the map
39: persistor.target.clear();
40:
41: // load up the persistor
42: int max = 100;
43: for (int i = 1; i < max; i++) {
44: persistor.target.put(i, "" + i);
45: }
46: index = new StringIndexImpl(persistor);
47:
48: // make sure the mappings are loaded as expected
49: for (int i = 1; i < max; i++) {
50: String string = "" + i;
51: assertEquals(string, index.getStringFor(i));
52: assertEquals(i, index.getOrCreateIndexFor(string));
53: }
54:
55: // make sure new index assignments start at the max
56: testString = "lksdfkljdfkjl";
57: assertEquals(max, index.getOrCreateIndexFor(testString));
58:
59: assertNull(index.getStringFor(0));
60: }
61:
62: private static final class TestStringIndexPersistor implements
63: StringIndexPersistor {
64:
65: public TLongObjectHashMap target = new TLongObjectHashMap();
66: public NoExceptionLinkedQueue loadCalls = new NoExceptionLinkedQueue();
67:
68: public TLongObjectHashMap loadMappingsInto(
69: TLongObjectHashMap theTarget) {
70: loadCalls.put(theTarget);
71: for (TLongObjectIterator i = target.iterator(); i.hasNext();) {
72: i.advance();
73: theTarget.put(i.key(), i.value());
74: }
75: return theTarget;
76: }
77:
78: public void saveMapping(long index, String string) {
79: target.put(index, string);
80: }
81:
82: }
83: }
|