arminw 2005/04/03 10:21:55
Modified: src/schema Tag: OJB_1_0_RELEASE ojbtest-schema.xml
src/test/org/apache/ojb/odmg Tag: OJB_1_0_RELEASE
CircularTest.java
Log:
fix test case using circular and bidirectional references
fix FK declaration in table schema
Revision Changes Path
No revision
No revision
1.80.2.16 +4 -4 db-ojb/src/schema/ojbtest-schema.xml
Index: ojbtest-schema.xml
===================================================================
RCS file: /home/cvs/db-ojb/src/schema/ojbtest-schema.xml,v
retrieving revision 1.80.2.15
retrieving revision 1.80.2.16
diff -u -r1.80.2.15 -r1.80.2.16
--- ojbtest-schema.xml 3 Apr 2005 01:48:10 -0000 1.80.2.15
+++ ojbtest-schema.xml 3 Apr 2005 17:21:55 -0000 1.80.2.16
@@ -1342,15 +1342,15 @@
<column name="OBJ_ID" required="true" primaryKey="true"
type="INTEGER"/>
<column name="NAME" type="VARCHAR" size="250"/>
<column name="DETAIL_FK" type="INTEGER"/>
+ <foreign-key foreignTable="CT_DETAIL">
+ <reference local="DETAIL_FK" foreign="OBJ_ID"/>
+ </foreign-key>
</table>
<table name="CT_DETAIL">
<column name="OBJ_ID" required="true" primaryKey="true"
type="INTEGER"/>
<column name="NAME" type="VARCHAR" size="250"/>
<column name="SHOP_FK" type="INTEGER"/>
- <foreign-key foreignTable="CT_SHOP">
- <reference local="SHOP_FK" foreign="OBJ_ID"/>
- </foreign-key>
</table>
<table name="CT_DISTRIBUTOR">
No revision
No revision
1.1.2.2 +171 -42
db-ojb/src/test/org/apache/ojb/odmg/Attic/CircularTest.java
Index: CircularTest.java
===================================================================
RCS file:
/home/cvs/db-ojb/src/test/org/apache/ojb/odmg/Attic/CircularTest.java,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -u -r1.1.2.1 -r1.1.2.2
--- CircularTest.java 3 Apr 2005 01:48:10 -0000 1.1.2.1
+++ CircularTest.java 3 Apr 2005 17:21:55 -0000 1.1.2.2
@@ -1,9 +1,9 @@
package org.apache.ojb.odmg;
-import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
+import java.util.List;
import org.apache.ojb.junit.ODMGTestCase;
import org.odmg.OQLQuery;
@@ -24,8 +24,22 @@
*/
/**
- * Testing complex object graphs with circular references.
- *
+ * Testing complex object graphs with circular and bidirectional references.
+ * <p/>
+ * The class hierachy looks like:
+ * <br/>
+ * - Class Shop has a bidirectional 1:1 reference with ShopDetail.<br/>
+ * - Shop has a 1:n relation with Product, Product has a 1:1 reference to
Shop.<br/>
+ * - Shop has a m:n relation with Distributor.<br/>
+ * - Product has a 1:n relation to itself to handle sub-Products.<br/>
+ * <p/>
+ * In the database the following foreign keys are declared:
+ * <br/>
+ * - Shop has a FK to ShopDetail<br/>
+ * - Product has a FK to Product<br/>
+ * - Product has a FK to Shop<br/>
+ * - CT_SHOP_DISTRIBUTOR indirection table has FK's to Shop and
Distributor<br/>
+ *
* @version $Id$
*/
public class CircularTest extends ODMGTestCase
@@ -36,11 +50,59 @@
junit.textui.TestRunner.main(arr);
}
- public void testShowOrderBug_1() throws Exception
+ public void testBidirectionalWithConstraint_1a() throws Exception
+ {
+ String name = "testBidirectionalWithConstraint_1a_" +
System.currentTimeMillis();
+ TransactionExt tx = (TransactionExt) odmg.newTransaction();
+ tx.begin();
+
+ Shop s1 = new Shop(name + "_1");
+ ShopDetail sd = new ShopDetail(name + "_1");
+ s1.setDetail(sd);
+ sd.setShop(s1);
+
+ // madatory to persist referenced ShopDetail first, the Shop
+ // object will be detected automatic. In this case first the
ShopDetail
+ // will be created and then the Shop
+ database.makePersistent(sd);
+ tx.commit();
+
+ tx.begin();
+ // madatory to mark object with DB FK constraint first on delete
+ // then OJB will use this order to delete the bidirectional objects
+ database.deletePersistent(s1);
+ database.deletePersistent(sd);
+ tx.commit();
+ }
+
+ public void testBidirectionalWithConstraint_1b() throws Exception
+ {
+ String name = "testBidirectionalWithConstraint_1b_" +
System.currentTimeMillis();
+ TransactionExt tx = (TransactionExt) odmg.newTransaction();
+ tx.begin();
+
+ Shop s1 = new Shop(name + "_1");
+ // when using flush() we can use a more "natural" order
+ // without getting DB constraint violence.
+ database.makePersistent(s1);
+ tx.flush();
+ ShopDetail sd = new ShopDetail(name + "_1");
+ s1.setDetail(sd);
+ sd.setShop(s1);
+ tx.commit();
+
+ tx.begin();
+ // madatory to mark object with DB FK constraint first on delete
+ // then OJB will use this order to delete the bidirectional objects
+ database.deletePersistent(s1);
+ database.deletePersistent(sd);
+ tx.commit();
+ }
+
+ public void testBidirectionalWithConstraint_2a() throws Exception
{
- if(ojbSkipKnownIssueProblem("Seems we have a bug in
ObjectEnvelopeOrdering")) return;
+ String name = "testBidirectionalWithConstraint_2a_" +
System.currentTimeMillis();
- String name = "testShowOrderBug_1_" + System.currentTimeMillis();
Shop s1 = new Shop(name + "_1");
ShopDetail sd = new ShopDetail(name + "_1");
s1.setDetail(sd);
@@ -51,15 +113,54 @@
TransactionExt tx = (TransactionExt) odmg.newTransaction();
tx.begin();
- database.makePersistent(s1);
+ // mandatory to first persist the referenced object of the
+ // bidirectional 1:1 reference, because of the FK in Shop
+ // the m:n relation will be handled without problems
+ database.makePersistent(sd);
+ // it's not needed to declare all objects in this case,
+ // but it wouldn't affect
+ // database.makePersistent(s1);
+ // database.makePersistent(d1);
+ tx.commit();
+
+ // Now we delete the Shop with ShopDetail, but don't
+ // touch the Distributor object
+ tx.begin();
+ database.deletePersistent(s1);
+ database.deletePersistent(sd);
tx.commit();
}
- public void testMtoNWithBackReference_1() throws Exception
+ public void testBidirectionalWithConstraint_2b() throws Exception
{
- if(ojbSkipKnownIssueProblem("Seems we have a bug in
ObjectEnvelopeOrdering")) return;
+ String name = "testBidirectionalWithConstraint_2b_" +
System.currentTimeMillis();
+ TransactionExt tx = (TransactionExt) odmg.newTransaction();
+ tx.begin();
+ // When using flush() we can use a more "natural" object persisting
order
+ Shop s1 = new Shop(name + "_1");
+ Distributor d1 = new Distributor(name + "_1");
+ s1.addDistributor(d1);
+ d1.addShop(s1);
+ database.makePersistent(s1);
+ tx.flush();
+
+ ShopDetail sd = new ShopDetail(name + "_1");
+ s1.setDetail(sd);
+ sd.setShop(s1);
+
+ tx.commit();
+
+ // Delete all created objects
+ tx.begin();
+ database.deletePersistent(d1);
+ database.deletePersistent(s1);
+ database.deletePersistent(sd);
+ tx.commit();
+ }
- String name = "testMtoNWithBackReference_1_" +
System.currentTimeMillis();
+ public void testBidirectionalWithConstraint_3() throws Exception
+ {
+ String name = "testBidirectionalWithConstraint_3_" +
System.currentTimeMillis();
Shop s1 = new Shop(name + "_1");
ShopDetail sd = new ShopDetail(name + "_1");
s1.setDetail(sd);
@@ -89,6 +190,8 @@
TransactionExt tx = (TransactionExt) odmg.newTransaction();
tx.begin();
+ // mandatory to add the ShopDetail object first
+ database.makePersistent(sd);
database.makePersistent(s1);
database.makePersistent(s2);
database.makePersistent(s3);
@@ -134,10 +237,9 @@
{
String name = "testMtoNWithBackReference_2_" +
System.currentTimeMillis();
Shop s1 = new Shop(name + "_1");
-// we only use the m:n relation
-// ShopDetail sd = new ShopDetail(name + "_1");
-// s1.setDetail(sd);
-// sd.setShop(s1);
+ ShopDetail sd = new ShopDetail(name + "_1");
+ s1.setDetail(sd);
+ sd.setShop(s1);
Shop s2 = new Shop(name + "_2");
Shop s3 = new Shop(name + "_3");
Distributor d1 = new Distributor(name + "_1");
@@ -163,6 +265,8 @@
TransactionExt tx = (TransactionExt) odmg.newTransaction();
tx.begin();
+ // madatory to add the ShopDetail object first
+ database.makePersistent(sd);
database.makePersistent(s1);
database.makePersistent(s2);
database.makePersistent(s3);
@@ -205,9 +309,9 @@
assertEquals(0, d3New.getShops().size());
}
- public void testOneToNWithBackReference_1() throws Exception
+ public void testOneToNWithSelfReference_1() throws Exception
{
- String name = "testOneToNWithBackReference_1_" +
System.currentTimeMillis();
+ String name = "testOneToNWithSelfReference_1_" +
System.currentTimeMillis();
Shop s = new Shop();
s.setName(name);
Product p1 = new Product();
@@ -259,7 +363,7 @@
boolean match = false;
for(Iterator iterator = newShop.getProducts().iterator();
iterator.hasNext();)
{
- Product p = (Product) iterator.next();
+ Product p = (Product) iterator.next();
if(p.getSubProducts() != null && p.getSubProducts().size() > 0)
{
match = true;
@@ -303,9 +407,9 @@
assertEquals(3, result.size());
}
- public void testOneToNWithBackReference_2() throws Exception
+ public void testOneToNWithSelfReference_2() throws Exception
{
- String name = "testOneToNWithBackReference_2_" +
System.currentTimeMillis();
+ String name = "testOneToNWithSelfReference_2_" +
System.currentTimeMillis();
Shop s = new Shop();
s.setName(name);
Product p1 = new Product();
@@ -357,7 +461,7 @@
boolean match = false;
for(Iterator iterator = newShop.getProducts().iterator();
iterator.hasNext();)
{
- Product p = (Product) iterator.next();
+ Product p = (Product) iterator.next();
if(p.getSubProducts() != null && p.getSubProducts().size() > 0)
{
match = true;
@@ -390,9 +494,9 @@
tx.commit();
}
- public void testOneToNWithBackReference_3() throws Exception
+ public void testOneToNWithSelfReference_3() throws Exception
{
- String name = "testOneToNWithBackReference_3_" +
System.currentTimeMillis();
+ String name = "testOneToNWithSelfReference_3_" +
System.currentTimeMillis();
Shop s = new Shop();
s.setName(name);
Product p1 = new Product();
@@ -444,7 +548,7 @@
boolean match = false;
for(Iterator iterator = newShop.getProducts().iterator();
iterator.hasNext();)
{
- Product p = (Product) iterator.next();
+ Product p = (Product) iterator.next();
if(p.getSubProducts() != null && p.getSubProducts().size() > 0)
{
match = true;
@@ -490,6 +594,9 @@
TransactionExt tx = (TransactionExt) odmg.newTransaction();
tx.begin();
+ // madatory to persist first the ShopDetail object to avoid
+ // DB constraint violation
+ database.makePersistent(sd);
database.makePersistent(s);
tx.commit();
@@ -507,7 +614,7 @@
assertNotNull(sdNew.getShop());
tx.begin();
- database.deletePersistent(sdNew);
+ database.deletePersistent(sdNew.getShop());
tx.flush();
query = odmg.newOQLQuery();
@@ -515,24 +622,22 @@
query.bind(name);
result = (Collection) query.execute();
tx.commit();
- assertEquals(0, result.size());
+ assertEquals(1, result.size());
tx.begin();
query = odmg.newOQLQuery();
query.create("select detail from " + Shop.class.getName() + " where
name like $1");
query.bind(name);
- result = (Collection) query.execute();
-
- assertEquals(1, result.size());
+ Collection resultShop = (Collection) query.execute();
+ assertEquals(0, resultShop.size());
+ // delete ShopDetail too
database.deletePersistent(result.iterator().next());
tx.commit();
}
public void testOneToOneWithBackReference_3() throws Exception
{
- if(ojbSkipKnownIssueProblem("Seems we have a bug in
ObjectEnvelopeOrdering")) return;
-
String name = "testOneToOneWithBackReference_3_" +
System.currentTimeMillis();
Shop s = new Shop();
s.setName(name);
@@ -551,36 +656,57 @@
tx.begin();
OQLQuery query = odmg.newOQLQuery();
- query.create("select detail from " + ShopDetail.class.getName() + "
where name like $1");
+ query.create("select shops from " + Shop.class.getName() + " where
name like $1");
query.bind(name);
Collection result = (Collection) query.execute();
tx.commit();
assertEquals(1, result.size());
- ShopDetail sdNew = (ShopDetail) result.iterator().next();
- assertNotNull(sdNew.getShop());
+ Shop newShop = (Shop) result.iterator().next();
+ assertNotNull(newShop.getDetail());
tx.begin();
- tx.setCascadingDelete(ShopDetail.class, true);
- database.deletePersistent(sdNew);
- database.makePersistent(sdNew);
+ // We enable cascading delete for all references of Shop class
+ tx.setCascadingDelete(Shop.class, true);
+ database.deletePersistent(newShop);
+ database.makePersistent(newShop);
tx.flush();
query = odmg.newOQLQuery();
- query.create("select detail from " + ShopDetail.class.getName() + "
where name like $1");
+ query.create("select shops from " + Shop.class.getName() + " where
name like $1");
query.bind(name);
result = (Collection) query.execute();
tx.commit();
assertEquals(1, result.size());
tx.begin();
- database.deletePersistent(sdNew);
- database.makePersistent(sdNew);
- database.deletePersistent(sdNew);
+ database.deletePersistent(newShop);
+ database.makePersistent(newShop);
+ database.deletePersistent(newShop);
tx.flush();
query = odmg.newOQLQuery();
- query.create("select detail from " + Shop.class.getName() + " where
name like $1");
+ query.create("select detail from " + ShopDetail.class.getName() + "
where name like $1");
+ query.bind(name);
+ result = (Collection) query.execute();
+ assertEquals(0, result.size());
+
+ query = odmg.newOQLQuery();
+ query.create("select shops from " + Shop.class.getName() + " where
name like $1");
+ query.bind(name);
+ result = (Collection) query.execute();
+ assertEquals(0, result.size());
+ tx.commit();
+
+ tx.begin();
+ query = odmg.newOQLQuery();
+ query.create("select detail from " + ShopDetail.class.getName() + "
where name like $1");
+ query.bind(name);
+ result = (Collection) query.execute();
+ assertEquals(0, result.size());
+
+ query = odmg.newOQLQuery();
+ query.create("select shops from " + Shop.class.getName() + " where
name like $1");
query.bind(name);
result = (Collection) query.execute();
assertEquals(0, result.size());
@@ -599,6 +725,7 @@
TransactionExt tx = (TransactionExt) odmg.newTransaction();
tx.begin();
+ database.makePersistent(sd);
database.makePersistent(s);
tx.commit();
@@ -616,7 +743,9 @@
assertNotNull(sdNew.getShop());
tx.begin();
+ // cascading delete should not affect Shop deletion
tx.setCascadingDelete(ShopDetail.class, true);
+ database.deletePersistent(sdNew.getShop());
database.deletePersistent(sdNew);
tx.flush();
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]