You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2004/01/03 03:06:30 UTC
cvs commit: db-ojb/src/test/org/apache/ojb/odmg ODMGRollbackTest.java OdmgExamples.java DListTest.java CollectionsTest.java
arminw 2004/01/02 18:06:30
Modified: src/test/org/apache/ojb/odmg ODMGRollbackTest.java
OdmgExamples.java DListTest.java
CollectionsTest.java
Log:
add new odmg test cases
Revision Changes Path
1.17 +39 -0 db-ojb/src/test/org/apache/ojb/odmg/ODMGRollbackTest.java
Index: ODMGRollbackTest.java
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/ODMGRollbackTest.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- ODMGRollbackTest.java 16 Oct 2003 16:57:39 -0000 1.16
+++ ODMGRollbackTest.java 3 Jan 2004 02:06:30 -0000 1.17
@@ -13,6 +13,8 @@
import org.apache.ojb.broker.query.Criteria;
import org.apache.ojb.broker.query.Query;
import org.apache.ojb.broker.query.QueryByCriteria;
+import org.apache.ojb.broker.query.QueryFactory;
+import org.apache.ojb.broker.query.QueryByIdentity;
import org.odmg.Database;
import org.odmg.Implementation;
import org.odmg.OQLQuery;
@@ -29,6 +31,43 @@
public ODMGRollbackTest(String s)
{
super(s);
+ }
+
+ public void testTransactionFlush() throws Exception
+ {
+ Implementation odmg = OJB.getInstance();
+ Database db = odmg.newDatabase();
+ db.open(databaseName, Database.OPEN_READ_WRITE);
+ TransactionExt tx = (TransactionExt) odmg.newTransaction();
+
+ tx.begin();
+ PersistenceBroker broker = tx.getBroker();
+ ODMGZoo obj = new ODMGZoo();
+ tx.lock(obj, tx.WRITE);
+ obj.setName("testTransactionFlush");
+
+ tx.flush();
+
+ Criteria crit = new Criteria();
+ crit.addEqualTo("name", obj.getName());
+ QueryByCriteria query = QueryFactory.newQuery(ODMGZoo.class, crit);
+ // we flushed all objects, thus we should found object in DB/cache
+ Iterator it = broker.getIteratorByQuery(query);
+ assertTrue(it.hasNext());
+ assertSame(obj, it.next());
+ assertFalse(it.hasNext());
+ // now we abort tx, so all flushed objects shouldn't be found
+ // in further queries
+ tx.abort();
+
+ tx = (TransactionExt) odmg.newTransaction();
+ tx.begin();
+ broker = tx.getBroker();
+ QueryByIdentity query2 = new QueryByIdentity(obj);
+ Object result = broker.getObjectByQuery(query2);
+ tx.commit();
+
+ assertNull("We should not find objects from aborted tx", result);
}
/**
1.13 +3 -17 db-ojb/src/test/org/apache/ojb/odmg/OdmgExamples.java
Index: OdmgExamples.java
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/OdmgExamples.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- OdmgExamples.java 6 Aug 2003 00:20:13 -0000 1.12
+++ OdmgExamples.java 3 Jan 2004 02:06:30 -0000 1.13
@@ -1,5 +1,7 @@
package org.apache.ojb.odmg;
+import java.util.List;
+
import junit.framework.TestCase;
import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.PersistenceBroker;
@@ -16,8 +18,6 @@
import org.odmg.OQLQuery;
import org.odmg.Transaction;
-import java.util.List;
-
/** Demo Application that shows basic concepts for Applications using the OJB ODMG
* implementation as an transactional object server.
*/
@@ -25,36 +25,22 @@
{
public static void main(String[] args)
{
- String[] arr = {CLASS.getName()};
+ String[] arr = {OdmgExamples.class.getName()};
junit.textui.TestRunner.main(arr);
}
- private static Class CLASS = OdmgExamples.class;
private String databaseName;
- /**
- * Insert the method's description here.
- * Creation date: (24.12.2000 00:33:40)
- */
public OdmgExamples(String name)
-
{
super(name);
}
- /**
- * Insert the method's description here.
- * Creation date: (06.12.2000 21:58:53)
- */
public void setUp()
{
databaseName = TestHelper.DEF_DATABASE_NAME;
}
- /**
- * Insert the method's description here.
- * Creation date: (06.12.2000 21:59:14)
- */
public void tearDown()
{
databaseName = null;
1.18 +135 -7 db-ojb/src/test/org/apache/ojb/odmg/DListTest.java
Index: DListTest.java
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/DListTest.java,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -r1.17 -r1.18
--- DListTest.java 30 Aug 2003 23:07:43 -0000 1.17
+++ DListTest.java 3 Jan 2004 02:06:30 -0000 1.18
@@ -1,17 +1,20 @@
package org.apache.ojb.odmg;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+
import junit.framework.TestCase;
-import org.apache.ojb.broker.TestHelper;
import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.ojb.broker.TestHelper;
import org.odmg.DBag;
import org.odmg.DList;
import org.odmg.DSet;
import org.odmg.Database;
import org.odmg.Implementation;
+import org.odmg.OQLQuery;
import org.odmg.Transaction;
-import java.util.Iterator;
-
/**
* Demo Application that shows basic concepts for Applications using the OJB ODMG
* implementation as an transactional object server.
@@ -51,7 +54,7 @@
databaseName = null;
}
- public void testAdding() throws Exception
+ public void testAddingLockupWithTx() throws Exception
{
// create a unique name:
final String name = "testAdding_" + System.currentTimeMillis();
@@ -64,6 +67,7 @@
Transaction tx = odmg.newTransaction();
tx.begin();
+ // create DList and bound by name
DList list = odmg.newDList();
db.bind(list, name);
tx.commit();
@@ -75,6 +79,7 @@
tx = odmg.newTransaction();
tx.begin();
+ // add objects to list
for (int i = 0; i < 5; i++)
{
DListObject a = createObject(name);
@@ -82,6 +87,7 @@
}
tx.commit();
+ // check current list
Iterator iter = list.iterator();
while (iter.hasNext())
{
@@ -91,8 +97,9 @@
tx = odmg.newTransaction();
tx.begin();
- ((HasBroker) odmg.currentTransaction()).getBroker().clearCache();
+ ((TransactionExt) odmg.currentTransaction()).getBroker().clearCache();
+ // lookup list and check entries
DList lookedUp = (DList) db.lookup(name);
assertNotNull("binded DList not found", lookedUp);
@@ -106,12 +113,133 @@
assertNotNull(a);
assertNotNull(b);
assertEquals(a.getId(), b.getId());
+ }
+ tx.commit();
+
+ // add new entries to list
+ tx.begin();
+ for (int i = 0; i < 3; i++)
+ {
+ DListObject a = createObject(name + "_new_entry");
+ list.add(a);
+ }
+ tx.commit();
+
+ tx = odmg.newTransaction();
+ tx.begin();
+ ((TransactionExt) odmg.currentTransaction()).getBroker().clearCache();
+ lookedUp = (DList) db.lookup(name);
+ iter = lookedUp.iterator();
+ iter1 = list.iterator();
+ assertEquals("Wrong number of DListEntry found", 8, list.size());
+ while (iter.hasNext())
+ {
+ DListObject a = (DListObject) iter.next();
+ DListObject b = (DListObject) iter1.next();
+ assertNotNull(a);
+ assertNotNull(b);
+ assertEquals(a.getId(), b.getId());
+ }
+ tx.commit();
+ assertNotNull("binded DList not found", lookedUp);
+ }
+
+ public void testReadAndStore() throws Exception
+ {
+ // create a unique name:
+ final String name = "testReadAndStore_" + System.currentTimeMillis();
+
+ // get facade instance
+ Implementation odmg = OJB.getInstance();
+ Database db = odmg.newDatabase();
+ //open database
+ db.open(databaseName, Database.OPEN_READ_WRITE);
+
+ // create test objects
+ Transaction tx = odmg.newTransaction();
+ tx.begin();
+ // add objects to list
+ for (int i = 0; i < 5; i++)
+ {
+ tx.lock(createObject(name), Transaction.WRITE);
+ }
+ tx.commit();
+
+ tx.begin();
+ // query test objects
+ OQLQuery q = odmg.newOQLQuery();
+ q.create("select all from "+DListObject.class.getName()+" where name=$1");
+ q.bind(name);
+ Collection ret = (Collection) q.execute();
+ // check result list size
+ assertEquals(5, ret.size());
+ // do read lock
+ for (Iterator it = ret.iterator(); it.hasNext(); )
+ {
+ tx.lock(it.next(), Transaction.READ);
+ }
+ // create new list for results
+ ArrayList result = new ArrayList();
+ result.addAll(ret);
+ tx.commit();
+ }
- //System.out.print(a.getArticleId() + ", ");
+ public void testIterateWithoutTx() throws Exception
+ {
+ // create a unique name:
+ final String name = "testAdding_" + System.currentTimeMillis();
+
+ // get facade instance
+ Implementation odmg = OJB.getInstance();
+ Database db = odmg.newDatabase();
+ //open database
+ db.open(databaseName, Database.OPEN_READ_WRITE);
+ // get DList and fill with objects
+ DList list = odmg.newDList();
+ Transaction tx = odmg.newTransaction();
+ tx.begin();
+ for (int i = 0; i < 5; i++)
+ {
+ DListObject a = createObject(name);
+ list.add(a);
}
- //System.out.println();
+ // bind the new list
+ db.bind(list, name);
+ tx.commit();
+ tx = odmg.newTransaction();
+ tx.begin();
+ Object obj = db.lookup(name);
tx.commit();
+ assertNotNull("binded DList not found", obj);
+
+ // iterate list
+ Iterator iter = list.iterator();
+ while (iter.hasNext())
+ {
+ DListObject a = (DListObject) iter.next();
+ assertNotNull(a);
+ }
+ assertEquals(5, list.size());
+
+ tx = odmg.newTransaction();
+ tx.begin();
+ ((TransactionExt) odmg.currentTransaction()).getBroker().clearCache();
+ DList lookedUp = (DList) db.lookup(name);
+ tx.commit();
+ assertNotNull("binded DList not found", lookedUp);
+
+ //System.out.println("sequence of items in lookedup list:");
+ iter = lookedUp.iterator();
+ Iterator iter1 = list.iterator();
+ while (iter.hasNext())
+ {
+ DListObject a = (DListObject) iter.next();
+ DListObject b = (DListObject) iter1.next();
+ assertNotNull(a);
+ assertNotNull(b);
+ assertEquals(a.getId(), b.getId());
+ }
}
/**
1.6 +146 -1 db-ojb/src/test/org/apache/ojb/odmg/CollectionsTest.java
Index: CollectionsTest.java
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/CollectionsTest.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- CollectionsTest.java 7 Jun 2003 10:19:04 -0000 1.5
+++ CollectionsTest.java 3 Jan 2004 02:06:30 -0000 1.6
@@ -13,6 +13,7 @@
import java.util.Collection;
import java.util.List;
import java.util.Vector;
+import java.util.Iterator;
/**
* Test case handles with collections.
@@ -124,6 +125,150 @@
fetchedGat = (Gatherer) result.iterator().next();
colsA = fetchedGat.getCollectiblesA();
assertEquals("Wrong number of CollectiblesA found in Gatherer", 2, colsA.size());
+ tx.commit();
+ }
+
+ public void testStoreUpdate() throws Exception
+ {
+ String prefix = "testInsertDelete" + System.currentTimeMillis();
+ String queryStr = "select gatherer from " + Gatherer.class.getName() + " where gatId=$1";
+
+ // prepare test case
+ Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
+ CollectibleA[] cols = prepareCollectibleA(prefix);
+ CollectibleB[] colsB = prepareCollectibleB(prefix);
+ List colList = Arrays.asList(cols);
+ List colListB = Arrays.asList(colsB);
+ // set List of CollectibleBase objects
+ gat.setCollectiblesA(colList);
+ gat.setCollectiblesB(colListB);
+ TransactionExt tx = (TransactionExt) odmg.newTransaction();
+ tx.begin();
+ db.makePersistent(gat);
+ tx.commit();
+
+ // check if gatherer was stored
+ tx.begin();
+ tx.getBroker().clearCache();
+ OQLQuery query = odmg.newOQLQuery();
+ query.create(queryStr);
+ Integer gatId = gat.getGatId();
+ assertNotNull(gatId);
+ query.bind(gatId);
+ Collection result = (Collection) query.execute();
+ tx.commit();
+ assertEquals("Wrong number of objects found", 1, result.size());
+ Gatherer fetchedGat = (Gatherer) result.iterator().next();
+
+ assertNotNull(fetchedGat.getCollectiblesA());
+ assertNotNull(fetchedGat.getCollectiblesB());
+ assertEquals(3, fetchedGat.getCollectiblesA().size());
+ assertEquals(3, fetchedGat.getCollectiblesB().size());
+ assertNotNull(fetchedGat.getCollectiblesA().get(0));
+ assertNotNull(fetchedGat.getCollectiblesB().get(0));
+
+ tx.begin();
+ tx.getBroker().clearCache();
+ tx.lock(fetchedGat, Transaction.WRITE);
+ fetchedGat.getCollectiblesA().remove(0);
+ fetchedGat.getCollectiblesB().remove(0);
+ tx.commit();
+
+ tx.begin();
+ tx.getBroker().clearCache();
+ query = odmg.newOQLQuery();
+ query.create(queryStr);
+ gatId = gat.getGatId();
+ assertNotNull(gatId);
+ query.bind(gatId);
+ result = (Collection) query.execute();
+ tx.commit();
+ assertEquals("Wrong number of objects found", 1, result.size());
+ fetchedGat = (Gatherer) result.iterator().next();
+
+ assertNotNull(fetchedGat.getCollectiblesA());
+ assertNotNull(fetchedGat.getCollectiblesB());
+ assertEquals(2, fetchedGat.getCollectiblesA().size());
+ assertEquals(2, fetchedGat.getCollectiblesB().size());
+ assertNotNull(fetchedGat.getCollectiblesA().get(0));
+ assertNotNull(fetchedGat.getCollectiblesB().get(0));
+ }
+
+ public void testStoreUpdateReference() throws Exception
+ {
+ String name = "testStoreUpdateReference" + System.currentTimeMillis();
+ String queryStr = "select colls from " + CollectibleA.class.getName() + " where name=$1";
+
+ // prepare test case
+ Gatherer gat_1 = new Gatherer(null, "Gatherer_" + name);
+ CollectibleA coll_1 = new CollectibleA(name);
+ Gatherer gat_2 = new Gatherer(null, "Gatherer_" + name);
+ CollectibleA coll_2 = new CollectibleA(name);
+
+ coll_1.setGatherer(gat_1);
+ coll_2.setGatherer(gat_2);
+
+ Transaction tx = odmg.newTransaction();
+ tx.begin();
+ db.makePersistent(coll_1);
+ db.makePersistent(coll_2);
+ tx.commit();
+
+ tx.begin();
+ ((TransactionExt)tx).getBroker().clearCache();
+ OQLQuery query = odmg.newOQLQuery();
+ query.create(queryStr);
+ query.bind(name);
+ Collection result = (Collection) query.execute();
+ assertNotNull(result);
+ assertEquals(2, result.size());
+
+ for (Iterator iterator = result.iterator(); iterator.hasNext();)
+ {
+ CollectibleA collectible = (CollectibleA) iterator.next();
+ Gatherer gat = collectible.getGatherer();
+ assertNotNull(gat);
+ assertEquals("Gatherer_"+name, gat.getName());
+ tx.lock(collectible, Transaction.WRITE);
+ collectible.getGatherer().setName("New_"+name);
+ }
+ tx.commit();
+
+ tx.begin();
+ ((TransactionExt)tx).getBroker().clearCache();
+ query = odmg.newOQLQuery();
+ query.create(queryStr);
+ query.bind(name);
+ result = (Collection) query.execute();
+ assertNotNull(result);
+ assertEquals(2, result.size());
+
+ for (Iterator iterator = result.iterator(); iterator.hasNext();)
+ {
+ CollectibleA collectible = (CollectibleA) iterator.next();
+ Gatherer gat = collectible.getGatherer();
+ assertNotNull(gat);
+ assertEquals("New_"+name, gat.getName());
+ tx.lock(collectible, Transaction.WRITE);
+ collectible.setGatherer(null);
+ }
+ tx.commit();
+
+ tx.begin();
+ ((TransactionExt)tx).getBroker().clearCache();
+ query = odmg.newOQLQuery();
+ query.create(queryStr);
+ query.bind(name);
+ result = (Collection) query.execute();
+ assertNotNull(result);
+ assertEquals(2, result.size());
+
+ for (Iterator iterator = result.iterator(); iterator.hasNext();)
+ {
+ CollectibleA collectible = (CollectibleA) iterator.next();
+ Gatherer gat = collectible.getGatherer();
+ assertNull(gat);
+ }
tx.commit();
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org