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/09 14:23:59 UTC
cvs commit: db-ojb/src/test/org/apache/ojb/odmg CollectionsTest.java
arminw 2004/01/09 05:23:59
Modified: src/test/org/apache/ojb/odmg CollectionsTest.java
Log:
- fix test cases for 1:n reference by setting the 1:1 back reference
- add new test case only using 1:n reference
Revision Changes Path
1.7 +159 -25 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.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- CollectionsTest.java 3 Jan 2004 02:06:30 -0000 1.6
+++ CollectionsTest.java 9 Jan 2004 13:23:59 -0000 1.7
@@ -51,7 +51,7 @@
super.tearDown();
try
{
- db.close();
+ if(odmg.currentTransaction() == null) db.close();
odmg = null;
}
catch (Exception e)
@@ -60,24 +60,25 @@
}
}
- public void testStoreFetchDelete() throws Exception
+ public void testStoreFetchDeleteCollectionWithBackReference() throws Exception
{
- String prefix = "testInsertDelete" + System.currentTimeMillis();
+ String prefix = "testInsertDeleteCollectionWithBackReference" + 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);
+ CollectibleA[] cols = prepareCollectibleA(gat, prefix);
List colList = Arrays.asList(cols);
- // set List of CollectibleBase objects
+ // set List of CollectiblesA objects
gat.setCollectiblesA(colList);
- Transaction tx = odmg.newTransaction();
+ 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();
@@ -126,24 +127,27 @@
colsA = fetchedGat.getCollectiblesA();
assertEquals("Wrong number of CollectiblesA found in Gatherer", 2, colsA.size());
tx.commit();
+
+ colsA.get(0);
}
- public void testStoreUpdate() throws Exception
+ public void testStoreUpdateCollectionWithBackReference() throws Exception
{
- String prefix = "testInsertDelete" + System.currentTimeMillis();
+ String prefix = "testInsertDeleteCollectionWithBackReference" + System.currentTimeMillis();
String queryStr = "select gatherer from " + Gatherer.class.getName() + " where gatId=$1";
+ TransactionExt tx = (TransactionExt) odmg.newTransaction();
+ tx.begin();
// prepare test case
Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
- CollectibleA[] cols = prepareCollectibleA(prefix);
- CollectibleB[] colsB = prepareCollectibleB(prefix);
+ CollectibleA[] cols = prepareCollectibleA(gat, prefix);
+ CollectibleB[] colsB = prepareCollectibleB(gat, 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();
@@ -157,9 +161,9 @@
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());
@@ -194,9 +198,9 @@
assertNotNull(fetchedGat.getCollectiblesB().get(0));
}
- public void testStoreUpdateReference() throws Exception
+ public void testUpdateCollectionWithBackReference() throws Exception
{
- String name = "testStoreUpdateReference" + System.currentTimeMillis();
+ String name = "testUpdateCollectionWithBackReference" + System.currentTimeMillis();
String queryStr = "select colls from " + CollectibleA.class.getName() + " where name=$1";
// prepare test case
@@ -272,33 +276,129 @@
tx.commit();
}
- private CollectibleA[] prepareCollectibleA(String namePrefix)
+ /**
+ * Store/update an main object Gatherer with a collection of objects CollectiblesC.
+ * CollectiblesC hasn't a reference back to the main object, thus we don't have to set
+ * the main object in the collection objects. Further we can't set the object id of the
+ * main object, because we don't know it at creation time.
+ */
+ public void testStoreUpdateCollectionWithoutBackReference() throws Exception
+ {
+ // String queryColl = "select colls from " + CollectibleC.class.getName() + " where name=$1";
+ String queryGat = "select gatherer from " + Gatherer.class.getName() + " where gatId=$1";
+ String prefix = "testStoreUpdateCollectionWithoutBackReference" + System.currentTimeMillis();
+
+ // prepare test case
+ Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
+ // we don't set the gatId, because we don't have one
+ CollectibleC[] cols = prepareCollectibleC(null, prefix);
+ List colList = Arrays.asList(cols);
+ // set List of CollectiblesC objects
+ gat.setCollectiblesC(colList);
+ 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(queryGat);
+ 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();
+
+ // check if gatherer contains list of CollectibleBase
+ tx.begin();
+ List colC = fetchedGat.getCollectiblesA();
+ assertEquals("Wrong number of CollectiblesC", 3, colC.size());
+
+ // delete one of the CollectibleBase
+ // we have to set the new reduced list in the
+ // gatherer object
+ List newCols = new ArrayList();
+ newCols.add(colC.get(1));
+ newCols.add(colC.get(2));
+ fetchedGat.setCollectiblesA(newCols);
+ tx.lock(fetchedGat, Transaction.WRITE);
+ db.deletePersistent(colC.get(0));
+ tx.commit();
+
+ // check if the Collectibles were really deleted from DB
+ tx.begin();
+ tx.getBroker().clearCache();
+
+ query = odmg.newOQLQuery();
+ query.create("select colls from " + CollectibleC.class.getName() +
+ " where name like $1");
+ query.bind(prefix + "%");
+ result = (Collection) query.execute();
+ assertEquals("Wrong number of objects found", 2, result.size());
+ tx.commit();
+
+ // check if the gatherer now contains a CollectibleBase list
+ // reduced by the deleted
+ tx.begin();
+ query = odmg.newOQLQuery();
+ query.create(queryGat);
+ query.bind(gatId);
+ result = (Collection) query.execute();
+ assertEquals("Wrong number of objects found", 1, result.size());
+ fetchedGat = (Gatherer) result.iterator().next();
+ colC = fetchedGat.getCollectiblesC();
+ assertEquals("Wrong number of CollectiblesA found in Gatherer", 2, colC.size());
+ tx.commit();
+
+ colC.get(0);
+ }
+
+ private CollectibleA[] prepareCollectibleA(Gatherer gat, String namePrefix)
{
CollectibleA[] colA = new CollectibleA[]{
new CollectibleA(namePrefix + " colA_1"),
new CollectibleA(namePrefix + " colA_2"),
new CollectibleA(namePrefix + " colA_3")
};
+ for (int i = 0; i < colA.length; i++)
+ {
+ CollectibleA collectibleA = colA[i];
+ collectibleA.setGatherer(gat);
+ }
return colA;
}
- private CollectibleB[] prepareCollectibleB(String namePrefix)
+ private CollectibleB[] prepareCollectibleB(Gatherer gat, String namePrefix)
{
CollectibleB[] colB = new CollectibleB[]{
new CollectibleB(namePrefix + " colB_1"),
new CollectibleB(namePrefix + " colB_2"),
new CollectibleB(namePrefix + " colB_3")
};
+ for (int i = 0; i < colB.length; i++)
+ {
+ CollectibleB collectibleB = colB[i];
+ collectibleB.setGatherer(gat);
+ }
return colB;
}
- private CollectibleB[] prepareCollectibleC(String namePrefix)
+ private CollectibleC[] prepareCollectibleC(Gatherer gat, String namePrefix)
{
CollectibleC[] colC = new CollectibleC[]{
- new CollectibleC(null, namePrefix + " colB_1", null, "ext1"),
- new CollectibleC(null, namePrefix + " colB_2", null, "ext2"),
- new CollectibleC(null, namePrefix + " colB_3", null, "ext3")
+ new CollectibleC(namePrefix + " colC_1", null, "ext1"),
+ new CollectibleC(namePrefix + " colC_2", null, "ext2"),
+ new CollectibleC(namePrefix + " colC_3", null, "ext3")
};
+ for (int i = 0; i < colC.length; i++)
+ {
+ CollectibleC collectibleC = colC[i];
+ collectibleC.setGathererId(gat != null ? gat.gatId : null);
+ }
return colC;
}
@@ -566,17 +666,21 @@
void setExtentName(String extentName);
}
- public static class CollectibleC extends CollectibleB implements CollectibleCIF
+ public static class CollectibleC
{
+ private Integer colId;
+ private String name;
+ private Integer gathererId;
private String extentName;
public CollectibleC()
{
}
- public CollectibleC(Integer colId, String name, Integer gathererId, String extentName)
+ public CollectibleC(String name, Integer gathererId, String extentName)
{
- super(colId, name, gathererId);
+ this.name = name;
+ this.gathererId = gathererId;
this.extentName = extentName;
}
@@ -588,6 +692,36 @@
public void setExtentName(String extentName)
{
this.extentName = extentName;
+ }
+
+ public Integer getColId()
+ {
+ return colId;
+ }
+
+ public void setColId(Integer colId)
+ {
+ this.colId = colId;
+ }
+
+ public String getName()
+ {
+ return name;
+ }
+
+ public void setName(String name)
+ {
+ this.name = name;
+ }
+
+ public Integer getGathererId()
+ {
+ return gathererId;
+ }
+
+ public void setGathererId(Integer gathererId)
+ {
+ this.gathererId = gathererId;
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org