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/06/03 02:12:48 UTC
cvs commit: db-ojb/src/test/org/apache/ojb/broker CollectionTest.java
arminw 2003/06/02 17:12:48
Modified: src/test/org/apache/ojb repository_junit.xml
src/test/org/apache/ojb/odmg CollectionsTest.java
Added: src/test/org/apache/ojb/broker CollectionTest.java
Log:
add new test case
Revision Changes Path
1.68 +194 -0 db-ojb/src/test/org/apache/ojb/repository_junit.xml
Index: repository_junit.xml
===================================================================
RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/repository_junit.xml,v
retrieving revision 1.67
retrieving revision 1.68
diff -u -r1.67 -r1.68
--- repository_junit.xml 30 May 2003 16:05:44 -0000 1.67
+++ repository_junit.xml 3 Jun 2003 00:12:48 -0000 1.68
@@ -3890,6 +3890,200 @@
</reference-descriptor>
</class-descriptor>
+
+
+<!-- ************************************************* -->
+<!-- PB collection test objects -->
+<!-- ************************************************* -->
+
+ <!--
+ private Integer gatId;
+ private String name;
+ private Collection collectiblesA;
+ -->
+ <class-descriptor
+ class="org.apache.ojb.broker.CollectionTest$Gatherer"
+ table="COLLECTION_GATHERER">
+
+ <field-descriptor
+ name="gatId"
+ column="GAT_ID"
+ jdbc-type="INTEGER"
+ primarykey="true"
+ autoincrement="true"
+ />
+
+ <field-descriptor
+ name="name"
+ column="NAME"
+ jdbc-type="VARCHAR"
+ />
+
+ <collection-descriptor
+ name="collectiblesBase"
+ element-class-ref="org.apache.ojb.broker.CollectionTest$CollectibleBase"
+ proxy="false"
+ auto-retrieve="true"
+ auto-update="true"
+ auto-delete="true"
+ >
+ <orderby name="colId" sort="DESC"/>
+ <inverse-foreignkey field-ref="gathererId"/>
+ </collection-descriptor>
+
+ <collection-descriptor
+ name="collectiblesB"
+ element-class-ref="org.apache.ojb.broker.CollectionTest$CollectibleB"
+ proxy="true"
+ auto-retrieve="true"
+ auto-update="true"
+ auto-delete="false"
+ >
+ <orderby name="colId" sort="DESC"/>
+ <inverse-foreignkey field-ref="gathererId"/>
+ </collection-descriptor>
+
+ <collection-descriptor
+ name="collectiblesC"
+ element-class-ref="org.apache.ojb.broker.CollectionTest$CollectibleC"
+ proxy="true"
+ auto-retrieve="true"
+ auto-update="true"
+ auto-delete="false"
+ >
+ <orderby name="colId" sort="DESC"/>
+ <inverse-foreignkey field-ref="gathererId"/>
+ </collection-descriptor>
+ </class-descriptor>
+
+ <!--
+ private Integer colId;
+ private String name;
+ private Integer gathererId;
+ private Gatherer gatherer;
+ private String extentName;
+ -->
+ <class-descriptor
+ class="org.apache.ojb.broker.CollectionTest$CollectibleBase"
+ table="COLLECTION_COLLECTIBLE_A">
+ <extent-class class-ref="org.apache.ojb.broker.CollectionTest$CollectibleB" />
+ <field-descriptor
+ name="colId"
+ column="COL_ID"
+ jdbc-type="INTEGER"
+ primarykey="true"
+ autoincrement="true"
+ />
+
+ <field-descriptor
+ name="name"
+ column="NAME"
+ jdbc-type="VARCHAR"
+ />
+
+ <field-descriptor
+ name="gathererId"
+ column="GAT_ID"
+ jdbc-type="INTEGER"
+ />
+
+ <reference-descriptor
+ name="gatherer"
+ class-ref="org.apache.ojb.broker.CollectionTest$Gatherer"
+ proxy="false"
+ auto-retrieve="false"
+ auto-update="false"
+ auto-delete="false">
+ <foreignkey field-ref="gathererId"/>
+ </reference-descriptor>
+ </class-descriptor>
+
+ <class-descriptor
+ class="org.apache.ojb.broker.CollectionTest$CollectibleB"
+ table="COLLECTION_COLLECTIBLE_BC">
+
+ <extent-class class-ref="org.apache.ojb.broker.CollectionTest$CollectibleC" />
+
+ <field-descriptor
+ name="colId"
+ column="COL_ID"
+ jdbc-type="INTEGER"
+ primarykey="true"
+ autoincrement="true"
+ />
+
+ <field-descriptor
+ name="name"
+ column="NAME"
+ jdbc-type="VARCHAR"
+ />
+
+ <field-descriptor
+ name="gathererId"
+ column="GAT_ID"
+ jdbc-type="INTEGER"
+ />
+
+ <reference-descriptor
+ name="gatherer"
+ class-ref="org.apache.ojb.broker.CollectionTest$Gatherer"
+ proxy="false"
+ auto-retrieve="false"
+ auto-update="false"
+ auto-delete="false">
+ <foreignkey field-ref="gathererId"/>
+ </reference-descriptor>
+ </class-descriptor>
+
+ <class-descriptor
+ class="org.apache.ojb.broker.CollectionTest$CollectibleC"
+ table="COLLECTION_COLLECTIBLE_BC">
+
+ <field-descriptor
+ name="colId"
+ column="COL_ID"
+ jdbc-type="INTEGER"
+ primarykey="true"
+ autoincrement="true"
+ />
+
+ <field-descriptor
+ name="name"
+ column="NAME"
+ jdbc-type="VARCHAR"
+ />
+
+ <field-descriptor
+ name="gathererId"
+ column="GAT_ID"
+ jdbc-type="INTEGER"
+ />
+
+ <field-descriptor
+ name="extentName"
+ column="EXTENT_NAME"
+ jdbc-type="VARCHAR"
+ />
+
+ <reference-descriptor
+ name="gatherer"
+ class-ref="org.apache.ojb.broker.CollectionTest$Gatherer"
+ proxy="false"
+ auto-retrieve="false"
+ auto-update="false"
+ auto-delete="false">
+ <foreignkey field-ref="gathererId"/>
+ </reference-descriptor>
+ </class-descriptor>
+
+ <class-descriptor class="org.apache.ojb.broker.CollectionTest$CollectibleBaseIF">
+ <extent-class class-ref="org.apache.ojb.broker.CollectionTest$CollectibleBase" />
+ </class-descriptor>
+
+
+
+
+
<!-- ************************************************* -->
<!-- PB 'readonly' attribute tests -->
<!-- ************************************************* -->
1.4 +16 -16 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.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- CollectionsTest.java 29 Apr 2003 15:43:26 -0000 1.3
+++ CollectionsTest.java 3 Jun 2003 00:12:48 -0000 1.4
@@ -4,15 +4,15 @@
import org.apache.ojb.broker.TestHelper;
import org.odmg.Database;
import org.odmg.Implementation;
-import org.odmg.Transaction;
import org.odmg.OQLQuery;
+import org.odmg.Transaction;
-import java.util.Collection;
+import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Vector;
+import java.util.Collection;
import java.util.List;
-import java.io.Serializable;
+import java.util.Vector;
/**
* Test case handles with collections.
@@ -61,14 +61,14 @@
public void testStoreFetchDelete() throws Exception
{
- String prefix = "testInsertDelete"+System.currentTimeMillis();
- String queryStr = "select gatherer from "+Gatherer.class.getName()+" where gatId=$1";
+ 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");
+ Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
CollectibleA[] cols = prepareCollectibleA(prefix);
List colList = Arrays.asList(cols);
- // set List of CollectibleA objects
+ // set List of CollectibleBase objects
gat.setCollectiblesA(colList);
Transaction tx = odmg.newTransaction();
tx.begin();
@@ -87,12 +87,12 @@
assertEquals("Wrong number of objects found", 1, result.size());
Gatherer fetchedGat = (Gatherer) result.iterator().next();
- // check if gatherer contains list of CollectibleA
+ // check if gatherer contains list of CollectibleBase
tx.begin();
List colsA = fetchedGat.getCollectiblesA();
assertEquals("Wrong number of CollectiblesA", 3, colsA.size());
- // delete one of the CollectibleA
+ // delete one of the CollectibleBase
// we have to set the new reduced list in the
// gatherer object
List newCols = new ArrayList();
@@ -103,17 +103,17 @@
db.deletePersistent(colsA.get(0));
tx.commit();
- // check if the CollectibleA was really deleted from DB
+ // check if the CollectibleBase was really deleted from DB
tx.begin();
query = odmg.newOQLQuery();
- query.create("select allCollectibleA from "+CollectibleA.class.getName()+
+ query.create("select allCollectibleA from " + CollectibleA.class.getName() +
" where name like $1");
- query.bind(prefix+"%");
+ 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 CollectibleA list
+ // check if the gatherer now contains a CollectibleBase list
// reduced by the deleted
tx.begin();
query = odmg.newOQLQuery();
@@ -175,7 +175,7 @@
public void addCollectibleA(CollectibleA colA)
{
- if(collectiblesA == null) collectiblesA = new Vector();
+ if (collectiblesA == null) collectiblesA = new Vector();
collectiblesA.add(colA);
}
1.1 db-ojb/src/test/org/apache/ojb/broker/CollectionTest.java
Index: CollectionTest.java
===================================================================
package org.apache.ojb.broker;
import junit.framework.TestCase;
import org.apache.ojb.broker.query.Criteria;
import org.apache.ojb.broker.query.Query;
import org.apache.ojb.broker.query.QueryByCriteria;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Vector;
/**
*
* @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
* @version $Id: CollectionTest.java,v 1.1 2003/06/03 00:12:48 arminw Exp $
*/
public class CollectionTest extends TestCase
{
private PersistenceBroker broker;
public static void main(String[] args)
{
String[] arr = {CollectionTest.class.getName()};
junit.textui.TestRunner.main(arr);
}
public void setUp() throws PBFactoryException
{
broker = PersistenceBrokerFactory.defaultPersistenceBroker();
}
public void tearDown()
{
try
{
broker.close();
}
catch (PersistenceBrokerException e)
{
}
}
public void testCollectionCreation()
{
long timestamp = System.currentTimeMillis();
String colPrefix = "col_" + timestamp;
String name = timestamp + "_test gatherer";
// create gatherer with collections
Gatherer gatherer = new Gatherer(null, name);
gatherer.setCollectiblesBase(Arrays.asList(prepareCollectibleBase(colPrefix)));
gatherer.setCollectiblesB(Arrays.asList(prepareCollectibleB(colPrefix)));
gatherer.setCollectiblesC(Arrays.asList(prepareCollectibleC(colPrefix)));
broker.beginTransaction();
broker.store(gatherer);
broker.commitTransaction();
Criteria criteria = new Criteria();
criteria.addLike("name", colPrefix + "_colBase*");
Query q = new QueryByCriteria(CollectibleBase.class, criteria);
Collection result = (Collection) broker.getCollectionByQuery(q);
assertNotNull(result);
assertEquals("Wrong number of queried objects", 3, result.size());
criteria = new Criteria();
criteria.addLike("name", colPrefix + "_colB*");
q = new QueryByCriteria(CollectibleB.class, criteria);
result = (Collection) broker.getCollectionByQuery(q);
assertNotNull(result);
assertEquals("Wrong number of queried objects", 4, result.size());
criteria = new Criteria();
criteria.addLike("name", colPrefix + "_colC*");
q = new QueryByCriteria(CollectibleC.class, criteria);
result = (Collection) broker.getCollectionByQuery(q);
assertNotNull(result);
assertEquals("Wrong number of queried objects", 5, result.size());
criteria = new Criteria();
criteria.addLike("name", colPrefix + "*");
q = new QueryByCriteria(CollectibleBaseIF.class, criteria);
result = (Collection) broker.getCollectionByQuery(q);
assertNotNull(result);
assertEquals("Wrong number of queried objects", 12, result.size());
}
private CollectibleBase[] prepareCollectibleBase(String namePrefix)
{
CollectibleBase[] colA = new CollectibleBase[]{
new CollectibleBase(namePrefix + "_colBase_1"),
new CollectibleBase(namePrefix + "_colBase_2"),
new CollectibleBase(namePrefix + "_colBase_3")
};
return colA;
}
private CollectibleB[] prepareCollectibleB(String namePrefix)
{
CollectibleB[] colB = new CollectibleB[]{
new CollectibleB(namePrefix + "_colB_1"),
new CollectibleB(namePrefix + "_colB_2"),
new CollectibleB(namePrefix + "_colB_3"),
new CollectibleB(namePrefix + "_colB_4")
};
return colB;
}
private CollectibleB[] prepareCollectibleC(String namePrefix)
{
CollectibleC[] colC = new CollectibleC[]{
new CollectibleC(null, namePrefix + "_colC_1", null, "ext1"),
new CollectibleC(null, namePrefix + "_colC_2", null, "ext2"),
new CollectibleC(null, namePrefix + "_colC_3", null, "ext3"),
new CollectibleC(null, namePrefix + "_colC_4", null, "ext4"),
new CollectibleC(null, namePrefix + "_colC_5", null, "ext5")
};
return colC;
}
//*********************************************************************
// inner class - persistent object
//*********************************************************************
public static class Gatherer implements Serializable
{
private Integer gatId;
private String name;
private List collectiblesBase = new Vector();
private List collectiblesB = new Vector();
private List collectiblesC = new Vector();
public Gatherer()
{
}
public void addCollectibleA(CollectibleBase colA)
{
if (collectiblesBase == null) collectiblesBase = new Vector();
collectiblesBase.add(colA);
}
public Gatherer(Integer gatId, String name)
{
this.gatId = gatId;
this.name = name;
}
public Integer getGatId()
{
return gatId;
}
public void setGatId(Integer gatId)
{
this.gatId = gatId;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public List getCollectiblesBase()
{
return collectiblesBase;
}
public void setCollectiblesBase(List collectiblesBase)
{
this.collectiblesBase = collectiblesBase;
}
public List getCollectiblesB()
{
return collectiblesB;
}
public void setCollectiblesB(List collectiblesB)
{
this.collectiblesB = collectiblesB;
}
public List getCollectiblesC()
{
return collectiblesC;
}
public void setCollectiblesC(List collectiblesC)
{
this.collectiblesC = collectiblesC;
}
}
public static interface CollectibleBaseIF extends Serializable
{
Integer getColId();
void setColId(Integer colId);
String getName();
void setName(String name);
Integer getGathererId();
void setGathererId(Integer colId);
Gatherer getGatherer();
void setGatherer(Gatherer gatherer);
}
public static class CollectibleBase implements CollectibleBaseIF
{
private Integer colId;
private String name;
private Integer gathererId;
private Gatherer gatherer;
public CollectibleBase()
{
}
public CollectibleBase(Integer colId, String name, Integer gathererId)
{
this.colId = colId;
this.name = name;
this.gathererId = gathererId;
}
public CollectibleBase(String name, Integer gathererId)
{
this.name = name;
this.gathererId = gathererId;
}
public CollectibleBase(String name)
{
this.name = name;
}
public Gatherer getGatherer()
{
return gatherer;
}
public void setGatherer(Gatherer gatherer)
{
this.gatherer = gatherer;
}
public Integer getGathererId()
{
return gathererId;
}
public void setGathererId(Integer gathererId)
{
this.gathererId = gathererId;
}
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 static interface CollectibleBIF extends CollectibleBaseIF
{
}
public static class CollectibleB extends CollectibleBase implements CollectibleBIF
{
public CollectibleB()
{
}
public CollectibleB(String name)
{
super(name);
}
public CollectibleB(Integer colId, String name, Integer gathererId)
{
super(colId, name, gathererId);
}
}
public static interface CollectibleCIF extends CollectibleBIF
{
String getExtentName();
void setExtentName(String extentName);
}
public static class CollectibleC extends CollectibleB implements CollectibleCIF
{
private String extentName;
public CollectibleC()
{
this.extentName = extentName;
}
public CollectibleC(Integer colId, String name, Integer gathererId, String extentName)
{
super(colId, name, gathererId);
this.extentName = extentName;
}
public String getExtentName()
{
return extentName;
}
public void setExtentName(String extentName)
{
this.extentName = extentName;
}
}
}