1 5 package com.tc.objectserver.impl; 6 7 import com.tc.exception.ImplementMe; 8 import com.tc.object.ObjectID; 9 import com.tc.objectserver.core.api.ManagedObject; 10 import com.tc.objectserver.core.impl.TestManagedObject; 11 import com.tc.objectserver.persistence.api.ManagedObjectPersistor; 12 import com.tc.objectserver.persistence.api.PersistenceTransaction; 13 import com.tc.objectserver.persistence.impl.TestPersistenceTransaction; 14 import com.tc.text.PrettyPrinter; 15 import com.tc.util.SyncObjectIdSet; 16 import com.tc.util.SyncObjectIdSetImpl; 17 import com.tc.util.concurrent.NoExceptionLinkedQueue; 18 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.HashSet ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import junit.framework.TestCase; 28 29 public class PersistentManagedObjectStoreTest extends TestCase { 30 31 private Map map; 32 private TestManagedObjectPersistor persistor; 33 private PersistentManagedObjectStore objectStore; 34 35 protected void setUp() throws Exception { 36 super.setUp(); 37 map = new HashMap (); 38 persistor = new TestManagedObjectPersistor(map); 39 objectStore = new PersistentManagedObjectStore(persistor); 40 } 41 42 public void testGetObjectByID() throws Exception { 43 ObjectID objectID = new ObjectID(1); 44 TestManagedObject mo = new TestManagedObject(objectID); 45 persistor.map.put(objectID, mo); 46 objectStore.getObjectByID(objectID); 47 assertEquals(objectID, persistor.loadByObjectIDCalls.poll(0)); 48 } 49 50 public void testAddRemovePutAndPutAll() { 51 Collection managed = new ArrayList (); 52 Collection managedIDs = new ArrayList (); 53 for (int i = 0; i < 10; i++) { 54 ObjectID id = new ObjectID(i); 55 managedIDs.add(id); 56 managed.add(new TestManagedObject(id)); 57 } 58 assertEquals(0, map.size()); 59 for (Iterator i = managed.iterator(); i.hasNext();) { 60 ManagedObject o = (ManagedObject) i.next(); 61 assertFalse(objectStore.containsObject(o.getID())); 62 objectStore.addNewObject(o); 65 assertTrue(objectStore.containsObject(o.getID())); 66 assertFalse(map.containsKey(o.getID())); 67 68 Collection toDelete = new HashSet (); 70 toDelete.add(o.getID()); 71 objectStore.removeAllObjectsByIDNow(null, toDelete); 72 assertFalse(objectStore.containsObject(o.getID())); 73 74 objectStore.addNewObject(o); 76 objectStore.commitObject(TestPersistenceTransaction.NULL_TRANSACTION, o); 77 assertTrue(objectStore.containsObject(o.getID())); 78 assertTrue(map.containsKey(o.getID())); 79 } 80 81 objectStore.removeAllObjectsByIDNow(null, managedIDs); 83 84 for (Iterator i = managed.iterator(); i.hasNext();) { 86 ManagedObject o = (ManagedObject) i.next(); 87 Collection toDelete = new HashSet (); 88 toDelete.add(o.getID()); 89 objectStore.removeAllObjectsByIDNow(null, toDelete); 90 assertFalse(objectStore.containsObject(o.getID())); 91 assertFalse(map.containsKey(o.getID())); 92 } 93 94 } 95 96 private static class TestManagedObjectPersistor implements ManagedObjectPersistor { 97 98 public final NoExceptionLinkedQueue loadByObjectIDCalls = new NoExceptionLinkedQueue(); 99 public final Map map; 100 public boolean closeCalled = false; 101 public SyncObjectIdSet allObjectIDs = new SyncObjectIdSetImpl(); 102 103 public TestManagedObjectPersistor(Map map) { 104 this.map = map; 105 } 106 107 public ManagedObject loadObjectByID(ObjectID id) { 108 loadByObjectIDCalls.put(id); 109 return (ManagedObject) map.get(id); 110 } 111 112 public ObjectID loadRootID(String name) { 113 return null; 114 } 115 116 public void addRoot(PersistenceTransaction tx, String name, ObjectID id) { 117 return; 118 } 119 120 public void saveObject(PersistenceTransaction tx, ManagedObject managedObject) { 121 map.put(managedObject.getID(), managedObject); 122 } 123 124 public void saveAllObjects(PersistenceTransaction tx, Collection managed) { 125 for (Iterator i = managed.iterator(); i.hasNext();) { 126 saveObject(tx, (ManagedObject) i.next()); 127 } 128 } 129 130 public void deleteObjectByID(PersistenceTransaction tx, ObjectID id) { 131 map.remove(id); 132 } 133 134 public void prettyPrint(PrettyPrinter out) { 135 return; 136 } 137 138 public Set loadRoots() { 139 return null; 140 } 141 142 public Set loadRootNames() { 143 return null; 144 } 145 146 public long nextObjectIDBatch(int batchSize) { 147 throw new ImplementMe(); 148 } 149 150 public void setNextAvailableObjectID(long startID) { 151 throw new ImplementMe(); 152 } 153 154 public SyncObjectIdSet getAllObjectIDs() { 155 return allObjectIDs; 156 } 157 158 public void deleteAllObjectsByID(PersistenceTransaction tx, Collection ids) { 159 for (Iterator i = ids.iterator(); i.hasNext();) { 160 deleteObjectByID(tx, (ObjectID) i.next()); 161 } 162 } 163 164 public Map loadRootNamesToIDs() { 165 return null; 166 } 167 168 } 169 } 170 | Popular Tags |