001: package org.apache.ojb.odmg;
002:
003: import java.util.ArrayList;
004: import java.util.Collection;
005: import java.util.Iterator;
006:
007: import org.apache.ojb.junit.ODMGTestCase;
008: import org.odmg.DSet;
009: import org.odmg.OQLQuery;
010: import org.odmg.Transaction;
011:
012: /**
013: * Tests for OJB {@link org.odmg.DSet} implementation.
014: */
015: public class DSetTest extends ODMGTestCase {
016: public static void main(String[] args) {
017: String[] arr = { DSetTest.class.getName() };
018: junit.textui.TestRunner.main(arr);
019: }
020:
021: public DSetTest(String name)
022:
023: {
024: super (name);
025: }
026:
027: protected DListTest.DObject createObject(String name)
028: throws Exception {
029: DListTest.DObject obj = new DListTest.DObject();
030: obj.setName(name);
031: obj.setRandomName("rnd_" + ((int) (Math.random() * 1000)));
032:
033: return obj;
034: }
035:
036: public void testAddingLockupWithTx() throws Exception {
037: // create a unique name:
038: final String name = "testAdding_" + System.currentTimeMillis();
039:
040: TransactionExt tx = (TransactionExt) odmg.newTransaction();
041: tx.begin();
042: // create DSet and bound by name
043: DSet list = odmg.newDSet();
044: database.bind(list, name);
045: tx.commit();
046:
047: tx.begin();
048: tx.getBroker().clearCache();
049: Object obj = database.lookup(name);
050: tx.commit();
051: assertNotNull("binded DSet not found", obj);
052:
053: tx.begin();
054: // add objects to list
055: for (int i = 0; i < 5; i++) {
056: DListTest.DObject a = createObject(name);
057: list.add(a);
058: }
059: tx.commit();
060:
061: // check current list
062: Iterator iter = list.iterator();
063: while (iter.hasNext()) {
064: DListTest.DObject a = (DListTest.DObject) iter.next();
065: assertNotNull(a);
066: }
067:
068: tx.begin();
069: tx.getBroker().clearCache();
070:
071: // lookup list and check entries
072: DSet lookedUp = (DSet) database.lookup(name);
073: assertNotNull("binded DSet not found", lookedUp);
074:
075: //System.out.println("sequence of items in lookedup list:");
076: iter = lookedUp.iterator();
077: Iterator iter1 = list.iterator();
078: while (iter.hasNext()) {
079: DListTest.DObject a = (DListTest.DObject) iter.next();
080: DListTest.DObject b = (DListTest.DObject) iter1.next();
081: assertNotNull(a);
082: assertNotNull(b);
083: assertEquals(a.getId(), b.getId());
084: }
085: tx.commit();
086:
087: // add new entries to list
088: tx.begin();
089: for (int i = 0; i < 3; i++) {
090: DListTest.DObject a = createObject(name + "_new_entry");
091: list.add(a);
092: }
093: tx.commit();
094:
095: tx.begin();
096: tx.getBroker().clearCache();
097: lookedUp = (DSet) database.lookup(name);
098: iter = lookedUp.iterator();
099: iter1 = list.iterator();
100: assertEquals("Wrong number of DListEntry found", 8, list.size());
101: while (iter.hasNext()) {
102: DListTest.DObject a = (DListTest.DObject) iter.next();
103: DListTest.DObject b = (DListTest.DObject) iter1.next();
104: assertNotNull(a);
105: assertNotNull(b);
106: assertEquals(a.getId(), b.getId());
107: }
108: tx.commit();
109: assertNotNull("binded DSet not found", lookedUp);
110: }
111:
112: public void testReadAndStore() throws Exception {
113: // create a unique name:
114: final String name = "testReadAndStore_"
115: + System.currentTimeMillis();
116:
117: // create test objects
118: Transaction tx = odmg.newTransaction();
119: tx.begin();
120: // add objects to list
121: for (int i = 0; i < 5; i++) {
122: tx.lock(createObject(name), Transaction.WRITE);
123: }
124: tx.commit();
125:
126: tx.begin();
127: // query test objects
128: OQLQuery q = odmg.newOQLQuery();
129: q.create("select all from " + DListTest.DObject.class.getName()
130: + " where name=$1");
131: q.bind(name);
132: Collection ret = (Collection) q.execute();
133: // check result list size
134: assertEquals(5, ret.size());
135: // do read lock
136: for (Iterator it = ret.iterator(); it.hasNext();) {
137: tx.lock(it.next(), Transaction.READ);
138: }
139: // create new list for results
140: ArrayList result = new ArrayList();
141: result.addAll(ret);
142: tx.commit();
143: }
144:
145: public void testIterateWithoutTx() throws Exception {
146: // create a unique name:
147: final String name = "testAdding_" + System.currentTimeMillis();
148:
149: // get DSet and fill with objects
150: DSet list = odmg.newDSet();
151: Transaction tx = odmg.newTransaction();
152: tx.begin();
153: for (int i = 0; i < 5; i++) {
154: DListTest.DObject a = createObject(name);
155: list.add(a);
156: }
157: // bind the new list
158: database.bind(list, name);
159: tx.commit();
160:
161: tx = odmg.newTransaction();
162: tx.begin();
163: Object obj = database.lookup(name);
164: tx.commit();
165: assertNotNull("binded DSet not found", obj);
166:
167: // iterate list
168: Iterator iter = list.iterator();
169: while (iter.hasNext()) {
170: DListTest.DObject a = (DListTest.DObject) iter.next();
171: assertNotNull(a);
172: }
173: assertEquals(5, list.size());
174:
175: tx = odmg.newTransaction();
176: tx.begin();
177: ((TransactionExt) odmg.currentTransaction()).getBroker()
178: .clearCache();
179: DSet lookedUp = (DSet) database.lookup(name);
180: tx.commit();
181: assertNotNull("binded DSet not found", lookedUp);
182:
183: //System.out.println("sequence of items in lookedup list:");
184: iter = lookedUp.iterator();
185: Iterator iter1 = list.iterator();
186: while (iter.hasNext()) {
187: DListTest.DObject a = (DListTest.DObject) iter.next();
188: DListTest.DObject b = (DListTest.DObject) iter1.next();
189: assertNotNull(a);
190: assertNotNull(b);
191: assertEquals(a.getId(), b.getId());
192: }
193: }
194:
195: /**
196: * this test checks if removing item from DSet works
197: */
198: public void testRemoving() throws Exception {
199: // create a unique name:
200: String name = "testRemoving_" + System.currentTimeMillis();
201:
202: TransactionExt tx = (TransactionExt) odmg.newTransaction();
203: tx.begin();
204: DSet list = odmg.newDSet();
205: // bind the list to the name:
206: database.bind(list, name);
207:
208: Object first = null;
209: Object second = null;
210: for (int i = 0; i < 5; i++) {
211: DListTest.DObject a = createObject(name);
212: list.add(a);
213: if (i == 1)
214: first = a;
215: if (i == 2)
216: second = a;
217: }
218: assertEquals(5, list.size());
219: tx.commit();
220:
221: // delete two items
222: tx = (TransactionExt) odmg.newTransaction();
223: tx.begin();
224: ((HasBroker) odmg.currentTransaction()).getBroker()
225: .clearCache();
226: DSet lookedUp = (DSet) database.lookup(name);
227: assertNotNull("database lookup does not find the named DSet",
228: lookedUp);
229: assertEquals("Wrong number of list entries", 5, lookedUp.size());
230: lookedUp.remove(first);
231: lookedUp.remove(second);
232: tx.commit();
233:
234: // check if deletion was successful
235: tx = (TransactionExt) odmg.newTransaction();
236: tx.begin();
237: tx.getBroker().clearCache();
238: lookedUp = (DSet) database.lookup(name);
239: tx.commit();
240:
241: assertEquals(3, lookedUp.size());
242: }
243:
244: public void testAdding() throws Exception {
245: // create a unique name:
246: String name = "testAdding_" + System.currentTimeMillis();
247:
248: TransactionExt tx = (TransactionExt) odmg.newTransaction();
249: tx.begin();
250: DSet list = odmg.newDSet();
251: database.bind(list, name);
252: tx.commit();
253:
254: tx = (TransactionExt) odmg.newTransaction();
255: tx.begin();
256: for (int i = 0; i < 5; i++) {
257: DListTest.DObject a = createObject(name);
258: list.add(a);
259: }
260:
261: list.add(createObject(name + "_posNew1"));
262: list.add(createObject(name + "_posNew2"));
263: list.add(createObject(name + "_posNew3"));
264: tx.commit();
265:
266: tx.begin();
267: tx.getBroker().clearCache();
268: // System.out.println("list: " + list);
269: // System.out.println("lookup list: " + db.lookup(name));
270: tx.commit();
271:
272: //System.out.println("sequence of items in list:");
273: Iterator iter = list.iterator();
274: DListTest.DObject a;
275: while (iter.hasNext()) {
276: a = (DListTest.DObject) iter.next();
277: assertNotNull(a);
278: //System.out.print(a.getArticleId() + ", ");
279: }
280: assertEquals(8, list.size());
281:
282: tx = (TransactionExt) odmg.newTransaction();
283: tx.begin();
284: tx.getBroker().clearCache();
285: DSet lookedUp = (DSet) database.lookup(name);
286: // System.out.println("lookup list: " + lookedUp);
287: assertNotNull("database lookup does not find DSet", lookedUp);
288: assertEquals(8, lookedUp.size());
289: iter = lookedUp.iterator();
290: while (iter.hasNext()) {
291: a = (DListTest.DObject) iter.next();
292: }
293: tx.commit();
294: }
295:
296: public void testDSet() throws Exception {
297: String name = "testDSet_" + System.currentTimeMillis();
298: String set_1 = "set_1_" + System.currentTimeMillis();
299: String set_2 = "set_2_" + System.currentTimeMillis();
300:
301: Transaction tx = odmg.newTransaction();
302: tx.begin();
303:
304: DListTest.DObject a, b, c, d, e;
305: a = createObject(name);
306: b = createObject(name);
307: c = createObject(name);
308: d = createObject(name);
309: e = createObject(name);
310:
311: DSet set1 = odmg.newDSet();
312: DSet set2 = odmg.newDSet();
313:
314: set1.add(a);
315: set1.add(b);
316: set1.add(c);
317:
318: set2.add(b);
319: set2.add(c);
320: set2.add(d);
321: set2.add(e);
322:
323: database.bind(set1, set_1);
324: database.bind(set2, set_2);
325: tx.commit();
326:
327: // low lookup both sets
328: tx = odmg.newTransaction();
329: tx.begin();
330: ((HasBroker) tx).getBroker().clearCache();
331: DSet set1a = (DSet) database.lookup(set_1);
332: DSet set2a = (DSet) database.lookup(set_2);
333:
334: // check looked up sets
335: assertTrue(set1a.containsAll(set1));
336: assertTrue(set2a.containsAll(set2));
337:
338: // now TestThreadsNLocks set operations:
339: DSet set3 = set1.difference(set2);
340: assertEquals(1, set3.size());
341:
342: set3 = set1.intersection(set2);
343: assertEquals(2, set3.size());
344:
345: set3 = set1.union(set2);
346: assertEquals(5, set3.size());
347:
348: assertTrue(set1.properSubsetOf(set3));
349: assertTrue(set2.properSubsetOf(set3));
350:
351: assertTrue(set3.properSupersetOf(set1));
352: assertTrue(set3.properSupersetOf(set2));
353:
354: assertTrue(!set1.properSubsetOf(set2));
355:
356: tx.commit();
357: }
358: }
|