KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > objectserver > impl > PersistentManagedObjectStoreTest


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

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 JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.HashSet JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26
27 import junit.framework.TestCase;
28
29 public class PersistentManagedObjectStoreTest extends TestCase {
30
31   private Map JavaDoc map;
32   private TestManagedObjectPersistor persistor;
33   private PersistentManagedObjectStore objectStore;
34
35   protected void setUp() throws Exception JavaDoc {
36     super.setUp();
37     map = new HashMap JavaDoc();
38     persistor = new TestManagedObjectPersistor(map);
39     objectStore = new PersistentManagedObjectStore(persistor);
40   }
41
42   public void testGetObjectByID() throws Exception JavaDoc {
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 JavaDoc managed = new ArrayList JavaDoc();
52     Collection JavaDoc managedIDs = new ArrayList JavaDoc();
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 JavaDoc i = managed.iterator(); i.hasNext();) {
60       ManagedObject o = (ManagedObject) i.next();
61       assertFalse(objectStore.containsObject(o.getID()));
62       // add new shouldn't commit the object to the data store yet, but it should keep track of
63
// the reference.
64
objectStore.addNewObject(o);
65       assertTrue(objectStore.containsObject(o.getID()));
66       assertFalse(map.containsKey(o.getID()));
67
68       // remove should remove the local reference.
69
Collection JavaDoc toDelete = new HashSet JavaDoc();
70       toDelete.add(o.getID());
71       objectStore.removeAllObjectsByIDNow(null, toDelete);
72       assertFalse(objectStore.containsObject(o.getID()));
73
74       // put should commit the object to the data store.
75
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     // removeAll should enqueue an event on the sink to remove all of them.
82
objectStore.removeAllObjectsByIDNow(null, managedIDs);
83
84     // clear the object store...
85
for (Iterator JavaDoc i = managed.iterator(); i.hasNext();) {
86       ManagedObject o = (ManagedObject) i.next();
87       Collection JavaDoc toDelete = new HashSet JavaDoc();
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 JavaDoc map;
100     public boolean closeCalled = false;
101     public SyncObjectIdSet allObjectIDs = new SyncObjectIdSetImpl();
102
103     public TestManagedObjectPersistor(Map JavaDoc 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 JavaDoc name) {
113       return null;
114     }
115
116     public void addRoot(PersistenceTransaction tx, String JavaDoc 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 JavaDoc managed) {
125       for (Iterator JavaDoc 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 JavaDoc ids) {
159       for (Iterator JavaDoc i = ids.iterator(); i.hasNext();) {
160         deleteObjectByID(tx, (ObjectID) i.next());
161       }
162     }
163
164     public Map JavaDoc loadRootNamesToIDs() {
165       return null;
166     }
167
168   }
169 }
170
Popular Tags