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 2003/08/08 22:38:25 UTC
cvs commit: db-ojb/src/test/org/apache/ojb/odmg PersonWithArrayTest.java PersonImpl.java
arminw 2003/08/08 13:38:25
Modified: src/java/org/apache/ojb/odmg/collections DListEntry.java
src/test/org/apache/ojb/odmg PersonWithArrayTest.java
PersonImpl.java
Log:
-add new tests
- update test
Thomas Dudziak wrote:
Is there a reason that I have to put the objects
in exactly the order "collection-owner first, then
collection elements" and that I have to lock the
subobjects in the same transaction ?
one test does not pass. Can
anyone shed some light on this.
Revision Changes Path
1.20 +10 -4 db-ojb/src/java/org/apache/ojb/odmg/collections/DListEntry.java
Index: DListEntry.java
===================================================================
RCS file: /home/cvs/db-ojb/src/java/org/apache/ojb/odmg/collections/DListEntry.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- DListEntry.java 27 Jul 2003 10:21:13 -0000 1.19
+++ DListEntry.java 8 Aug 2003 20:38:25 -0000 1.20
@@ -126,7 +126,7 @@
oid = new Identity(theObject, tx.getBroker());
else
{
- PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
+ PersistenceBroker broker = PersistenceBrokerFactory.defaultPersistenceBroker();
oid = new Identity(theObject, broker);
broker.close();
}
@@ -166,7 +166,7 @@
PersistenceBroker broker = capsule.getBroker();
FieldDescriptor fld = broker.getClassDescriptor(this.getClass()).getAutoIncrementField();
Integer val = (Integer) broker.serviceSequenceManager().getUniqueValue(fld);
-
+
int result = val.intValue();
return result;
}
@@ -218,8 +218,14 @@
private void materializeRealSubject() throws PersistenceBrokerException
{
PBCapsule capsule = new PBCapsule(pbKey, TxManagerFactory.instance().getTransaction());
- realSubject = capsule.getBroker().getObjectByIdentity(oid);
- capsule.destroy();
+ try
+ {
+ realSubject = capsule.getBroker().getObjectByIdentity(oid);
+ }
+ finally
+ {
+ if(capsule != null) capsule.destroy();
+ }
}
/**
1.3 +271 -82 db-ojb/src/test/org/apache/ojb/odmg/PersonWithArrayTest.java
Index: PersonWithArrayTest.java
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/PersonWithArrayTest.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- PersonWithArrayTest.java 6 Mar 2003 00:13:10 -0000 1.2
+++ PersonWithArrayTest.java 8 Aug 2003 20:38:25 -0000 1.3
@@ -1,22 +1,20 @@
/**
* (C) 2003 ppi Media
* User: om
- */
-package org.apache.ojb.odmg;
+ */
+
+package org.apache.ojb.odmg;
+
+import junit.framework.TestCase;
import org.apache.ojb.broker.TestHelper;
-import org.odmg.Implementation;
import org.odmg.Database;
-import org.odmg.Transaction;
-import org.odmg.OQLQuery;
+import org.odmg.Implementation;
import org.odmg.ODMGException;
-import org.odmg.QueryInvalidException;
-import org.odmg.QueryException;
+import org.odmg.OQLQuery;
+import org.odmg.Transaction;
-import java.util.List;
-import java.util.Iterator;
+import java.util.Arrays;
import java.util.Collection;
-import java.util.Arrays;
-import junit.framework.TestCase;
/**
* class PersonWithArrayTest
@@ -27,101 +25,292 @@
public class PersonWithArrayTest extends TestCase
{
private static Class CLASS = PersonWithArrayTest.class;
- private String databaseName;
+ private String databaseName;
+
public void setUp() throws ODMGException
{
databaseName = TestHelper.DEF_DATABASE_NAME;
- deleteAll(PersonImpl.class);
- }
- final void deleteAll(Class clazz) throws ODMGException
+ }
+
+ /**
+ * Insert the method's description here.
+ * Creation date: (06.12.2000 21:59:14)
+ */
+ public void tearDown() throws Exception
+ {
+ databaseName = null;
+ }
+
+ /*
+ * lock only the father, let OJB do the rest
+ * delete father then children
+ */
+ public void testStoreDeleteThreePersons_1() throws Exception
{
+ String postfix = "_" + System.currentTimeMillis();
+ String firstnameFather = "Father" + postfix;
+ String firstnameChild_1 = "Child_One" + postfix;
+ String firstnameChild_2 = "Child_Two" + postfix;
+
Implementation odmg = OJB.getInstance();
Database db = odmg.newDatabase();
+ //open database
db.open(databaseName, Database.OPEN_READ_WRITE);
+
+ Person father = createPerson(firstnameFather, "testStoreThreePersons_1", null, null);
+ Person child_1 = createPerson(firstnameChild_1, "testStoreThreePersons_1", null, null);
+ Person child_2 = createPerson(firstnameChild_2, "testStoreThreePersons_1", null, null);
+
+ Person[] children = new Person[]{child_1, child_2};
+ father.setChildren(children);
+ child_1.setFather(father);
+ child_2.setFather(father);
+
+ /*
+ * lock only the father, let OJB do the rest
+ */
Transaction tx = odmg.newTransaction();
tx.begin();
- OQLQuery query = odmg.newOQLQuery();
- query.create("select person from " + clazz.getName());
- List persons = (List) query.execute();
- Iterator it = persons.iterator();
- while (it.hasNext())
- {
- Object obj = it.next();
- db.deletePersistent(obj);
- }
+ tx.lock(father, Transaction.WRITE);
tx.commit();
- }
- /**
- * Insert the method's description here.
- * Creation date: (06.12.2000 21:59:14)
+
+ // without running tx, should not take effect
+ father.setChildren(null);
+ father.setFirstname(null);
+
+ tx.begin();
+ // make sure all objects are retrieved freshly in subsequent transactions
+ ((TransactionImpl) tx).getBroker().clearCache();
+ OQLQuery qry = odmg.newOQLQuery();
+ qry.create("select a from " + PersonImpl.class.getName() + " where firstname=\"" + firstnameFather + "\"");
+ Collection result = (Collection) qry.execute();
+
+ assertEquals("Exactly one element in result set", 1, result.size());
+ Person returnedFather = (Person) result.iterator().next();
+ // should retrieve new instance
+ assertTrue("not same", returnedFather != father);
+ Person[] returnedChildren = returnedFather.getChildren();
+ assertNotNull(returnedChildren);
+ assertEquals(2, returnedChildren.length);
+ Person child = returnedChildren[0];
+ Person lookupFather = child.getFather();
+ assertNotNull(lookupFather);
+ assertEquals(returnedFather.getFirstname(), lookupFather.getFirstname());
+ // unfortunately, PersonImpl does not have a suitable equals method.
+ assertEquals(
+ "children's names are equal",
+ Arrays.asList(getFirstNames(returnedChildren)),
+ Arrays.asList(getFirstNames(children)));
+ // System.out.println(Arrays.asList(getFirstNames(returnedChildren)));
+ tx.commit();
+
+ /*
+ delete father then children
+ fk-constraints?
+ */
+ tx.begin();
+ db.deletePersistent(returnedFather);
+ db.deletePersistent(returnedFather.getChildren()[0]);
+ db.deletePersistent(returnedFather.getChildren()[1]);
+
+ tx.commit();
+
+ qry = odmg.newOQLQuery();
+ qry.create("select a from " + PersonImpl.class.getName() + " where firstname=\"" + firstnameFather + "\"");
+ result = (Collection) qry.execute();
+ assertEquals("Exactly one element in result set", 0, result.size());
+
+ qry = odmg.newOQLQuery();
+ qry.create("select a from " + PersonImpl.class.getName() + " where firstname=\"" + firstnameChild_1 + "\"");
+ result = (Collection) qry.execute();
+ // System.out.println("child: "+result.iterator().next());
+ assertEquals("Exactly one element in result set", 0, result.size());
+ }
+
+ /*
+ lock father then all childs
+ delete children then father
*/
- public void tearDown() throws Exception
+ public void testStoreDeleteThreePersons_2() throws Exception
{
- databaseName = null;
- }
- public void testStoreThreePersons()
+ String postfix = "_" + System.currentTimeMillis();
+ String firstnameFather = "Father" + postfix;
+ String firstnameChild_1 = "Child_One" + postfix;
+ String firstnameChild_2 = "Child_Two" + postfix;
+
+ Implementation odmg = OJB.getInstance();
+ Database db = odmg.newDatabase();
+ //open database
+ db.open(databaseName, Database.OPEN_READ_WRITE);
+
+ Person father = createPerson(firstnameFather, "testStoreThreePersons_1", null, null);
+ Person child_1 = createPerson(firstnameChild_1, "testStoreThreePersons_1", null, null);
+ Person child_2 = createPerson(firstnameChild_2, "testStoreThreePersons_1", null, null);
+
+ Person[] children = new Person[]{child_1, child_2};
+ father.setChildren(children);
+ child_1.setFather(father);
+ child_2.setFather(father);
+
+ /*
+ lock father then all childs
+ */
+ Transaction tx = odmg.newTransaction();
+ tx.begin();
+ tx.lock(father, Transaction.WRITE);
+ tx.lock(child_2, Transaction.WRITE);
+ tx.lock(child_1, Transaction.WRITE);
+ tx.commit();
+
+ // without running tx, should not take effect
+ father.setChildren(null);
+ father.setFirstname(null);
+
+ tx.begin();
+ // make sure all objects are retrieved freshly in subsequent transactions
+ ((TransactionImpl) tx).getBroker().clearCache();
+ OQLQuery qry = odmg.newOQLQuery();
+ qry.create("select a from " + PersonImpl.class.getName() + " where firstname=\"" + firstnameFather + "\"");
+ Collection result = (Collection) qry.execute();
+
+ assertEquals("Exactly one element in result set", 1, result.size());
+ Person returnedFather = (Person) result.iterator().next();
+ // should retrieve new instance
+ assertTrue("not same", returnedFather != father);
+ Person[] returnedChildren = returnedFather.getChildren();
+ assertNotNull(returnedChildren);
+ assertEquals(2, returnedChildren.length);
+ Person child = returnedChildren[0];
+ Person lookupFather = child.getFather();
+ assertNotNull(lookupFather);
+ assertEquals(returnedFather.getFirstname(), lookupFather.getFirstname());
+ // unfortunately, PersonImpl does not have a suitable equals method.
+ assertEquals(
+ "children's names are equal",
+ Arrays.asList(getFirstNames(returnedChildren)),
+ Arrays.asList(getFirstNames(children)));
+ // System.out.println(Arrays.asList(getFirstNames(returnedChildren)));
+ tx.commit();
+
+ /*
+ delete children then father
+ fk-constraints?
+ */
+ tx.begin();
+ db.deletePersistent(returnedFather.getChildren()[0]);
+ db.deletePersistent(returnedFather.getChildren()[1]);
+ db.deletePersistent(returnedFather);
+ tx.commit();
+
+ qry = odmg.newOQLQuery();
+ qry.create("select a from " + PersonImpl.class.getName() + " where firstname=\"" + firstnameFather + "\"");
+ result = (Collection) qry.execute();
+ assertEquals("Exactly one element in result set", 0, result.size());
+
+ qry = odmg.newOQLQuery();
+ qry.create("select a from " + PersonImpl.class.getName() + " where firstname=\"" + firstnameChild_1 + "\"");
+ result = (Collection) qry.execute();
+ // System.out.println("child: "+result.iterator().next());
+ assertEquals("Exactly one element in result set", 0, result.size());
+ }
+
+ public void testStoreDeleteThreePersons_3() throws Exception
{
+ String postfix = "_" + System.currentTimeMillis();
+ String firstnameFather = "Father" + postfix;
+ String firstnameChild_1 = "Child_One" + postfix;
+ String firstnameChild_2 = "Child_Two" + postfix;
+
Implementation odmg = OJB.getInstance();
Database db = odmg.newDatabase();
- int id = (int) System.currentTimeMillis();
//open database
- try
- {
- db.open(databaseName, Database.OPEN_READ_WRITE);
- }
- catch (ODMGException ex)
- {
- fail("ODMGException: " + ex.getMessage());
- }
- Person karlHeinz = new PersonImpl(id++, "Karl-Heinz", "Schultz", 0, 0);
- Person knuth = new PersonImpl(id++, "Knuth", null, 0, 0);
- Person kurt = new PersonImpl(id++, "Kurt", null, 0, 0);
Person[] karlHeinz_children = new Person[] { knuth, kurt };
karlHeinz.setChildren(karlHeinz_children);
- knuth.setFather(karlHeinz);
- kurt.setFather(karlHeinz);
+ db.open(databaseName, Database.OPEN_READ_WRITE);
+
+ Person father = createPerson(firstnameFather, "testStoreThreePersons_1", null, null);
+ Person child_1 = createPerson(firstnameChild_1, "testStoreThreePersons_1", null, null);
+ Person child_2 = createPerson(firstnameChild_2, "testStoreThreePersons_1", null, null);
+
+ Person[] children = new Person[]{child_1, child_2};
+ father.setChildren(children);
+ child_1.setFather(father);
+ child_2.setFather(father);
+
+ /*
+ lock childs first, then lock father
+ TODO: Does not pass - why? A defined lock
+ order necessary?
+ if this doesn't make sense remove the test
+ */
Transaction tx = odmg.newTransaction();
tx.begin();
- tx.lock(karlHeinz, Transaction.WRITE);
- tx.lock(kurt, Transaction.WRITE);
- tx.lock(knuth, Transaction.WRITE);
+ tx.lock(child_2, Transaction.WRITE);
+ tx.lock(child_1, Transaction.WRITE);
+ tx.lock(father, Transaction.WRITE);
tx.commit();
- karlHeinz.setChildren(null);
- karlHeinz.setFirstname(null);
+ // without running tx, should not take effect
+ father.setChildren(null);
+ father.setFirstname(null);
+
tx.begin();
// make sure all objects are retrieved freshly in subsequent transactions
- ((TransactionImpl) tx).getBroker().clearCache();
+ ((TransactionImpl) tx).getBroker().clearCache();
OQLQuery qry = odmg.newOQLQuery();
- try
- {
- qry.create(
- "select a from " + PersonImpl.class.getName() + " where firstname=\"Karl-Heinz\"");
- }
- catch (QueryInvalidException exc)
- {
- fail("Exception: " + exc.toString());
- }
- Collection result = null;
- try
- {
- result = (Collection) qry.execute();
- }
- catch (QueryException exc)
- {
- fail("Exception: " + exc.toString());
- }
- assertEquals("Exactly one element in result set", result.size(), 1);
- Person retKarlHeinz = (Person) (result.iterator().next());
- assertTrue("not same", retKarlHeinz != karlHeinz);
- Person[] retKarlHeinz_children = retKarlHeinz.getChildren();
- assertNotNull(retKarlHeinz_children);
+ qry.create("select a from " + PersonImpl.class.getName() + " where firstname=\"" + firstnameFather + "\"");
+ Collection result = (Collection) qry.execute();
+
+ assertEquals("Exactly one element in result set", 1, result.size());
+ Person returnedFather = (Person) result.iterator().next();
+ // should retrieve new instance
+ assertTrue("not same", returnedFather != father);
+ Person[] returnedChildren = returnedFather.getChildren();
+ assertNotNull(returnedChildren);
+ assertEquals(2, returnedChildren.length);
+ Person child = returnedChildren[0];
+ Person lookupFather = child.getFather();
+ assertNotNull(lookupFather);
+ assertEquals(returnedFather.getFirstname(), lookupFather.getFirstname());
// unfortunately, PersonImpl does not have a suitable equals method.
assertEquals(
- "children's names are equal",
- Arrays.asList(getFirstNames(retKarlHeinz_children)),
- Arrays.asList(getFirstNames(karlHeinz_children)));
- // System.out.println(Arrays.asList(getFirstNames(retKarlHeinz_children)));
- tx.abort();
- }
+ "children's names are equal",
+ Arrays.asList(getFirstNames(returnedChildren)),
+ Arrays.asList(getFirstNames(children)));
+ // System.out.println(Arrays.asList(getFirstNames(returnedChildren)));
+ tx.commit();
+
+ /*
+ delete father then children
+ fk-constraints?
+ */
+ tx.begin();
+ db.deletePersistent(returnedFather.getChildren()[0]);
+ db.deletePersistent(returnedFather.getChildren()[1]);
+ db.deletePersistent(returnedFather);
+ tx.commit();
+
+ qry = odmg.newOQLQuery();
+ qry.create("select a from " + PersonImpl.class.getName() + " where firstname=\"" + firstnameFather + "\"");
+ result = (Collection) qry.execute();
+ assertEquals("Exactly one element in result set", 0, result.size());
+
+ qry = odmg.newOQLQuery();
+ qry.create("select a from " + PersonImpl.class.getName() + " where firstname=\"" + firstnameChild_1 + "\"");
+ result = (Collection) qry.execute();
+ // System.out.println("child: "+result.iterator().next());
+ assertEquals("Exactly one element in result set", 0, result.size());
+ }
+
+ private Person createPerson(String firstname, String lastname, Person father, Person mother)
+ {
+ Person p = new PersonImpl();
+ p.setFirstname(firstname);
+ p.setLastname(lastname);
+ p.setFather(father);
+ p.setMother(mother);
+ // p.setChildren(null);
+ return p;
+ }
+
private static String[] getFirstNames(Person[] persons)
{
int length = persons == null ? 0 : persons.length;
@@ -135,7 +324,7 @@
public static void main(String[] args)
{
- String[] arr = { CLASS.getName()};
+ String[] arr = {CLASS.getName()};
junit.textui.TestRunner.main(arr);
}
}
1.4 +14 -3 db-ojb/src/test/org/apache/ojb/odmg/PersonImpl.java
Index: PersonImpl.java
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/odmg/PersonImpl.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- PersonImpl.java 29 Apr 2003 15:43:26 -0000 1.3
+++ PersonImpl.java 8 Aug 2003 20:38:25 -0000 1.4
@@ -8,9 +8,12 @@
*/
package org.apache.ojb.odmg;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
+
+import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
-import java.io.Serializable;
public class PersonImpl implements Person, Serializable
{
@@ -97,6 +100,7 @@
list.add(pChild);
list.toArray(newKids);
}
+
/**
* Gets the fatherId.
* @return Returns a int
@@ -153,7 +157,14 @@
public String toString()
{
- int numOfChildren = ((children == null) ? 0 : children.length);
- return "PersonImpl@" + System.identityHashCode(this) + "(" + id + "): " + getFirstname() + " " + getLastname() + ", " + numOfChildren + " children";
+ ToStringBuilder buf = new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE);
+ buf.append("id", id);
+ buf.append("firstname", firstname);
+ buf.append("lastname", lastname);
+ buf.append("motherId", motherId);
+ buf.append("mother", mother != null ? "PersonImpl@" + System.identityHashCode(mother) + "(" + motherId + ")" : null);
+ buf.append("fatherId", fatherId);
+ buf.append("father", father != null ? "PersonImpl@" + System.identityHashCode(father) + "(" + fatherId + ")" : null);
+ return buf.toString();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org