KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > persistence > api > StringIndexTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.objectserver.persistence.api;
5
6 import com.tc.io.serializer.api.StringIndex;
7 import com.tc.objectserver.persistence.impl.StringIndexImpl;
8 import com.tc.util.concurrent.NoExceptionLinkedQueue;
9
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 JavaDoc {
20     persistor = new TestStringIndexPersistor();
21   }
22   
23   public void test() throws Exception JavaDoc {
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 JavaDoc 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 JavaDoc 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 StringIndexPersistor {
63
64     public TLongObjectHashMap target = new TLongObjectHashMap();
65     public NoExceptionLinkedQueue loadCalls = new NoExceptionLinkedQueue();
66     
67     public TLongObjectHashMap loadMappingsInto(TLongObjectHashMap theTarget) {
68       loadCalls.put(theTarget);
69       for (TLongObjectIterator i = target.iterator(); i.hasNext();) {
70         i.advance();
71         theTarget.put(i.key(), i.value());
72       }
73       return theTarget;
74     }
75
76     public void saveMapping(long index, String JavaDoc string) {
77       target.put(index, string);
78     }
79     
80   }
81 }
82
Popular Tags