1 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 { 20 persistor = new TestStringIndexPersistor(); 21 } 22 23 public void test() throws Exception { 24 index = new StringIndexImpl(persistor); 25 assertNotNull(persistor.loadCalls.poll(0)); 27 28 assertNull(index.getStringFor(0)); 30 31 String testString = ":SLDKFJSD"; 33 assertEquals(1, index.getOrCreateIndexFor(testString)); 34 35 assertEquals(testString, persistor.target.get(1)); 37 38 persistor.target.clear(); 40 41 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 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 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 string) { 77 target.put(index, string); 78 } 79 80 } 81 } 82 | Popular Tags |