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 2006/02/09 18:16:15 UTC
svn commit: r376353 -
/db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/odmg/CollectionsTest.java
Author: arminw
Date: Thu Feb 9 09:16:11 2006
New Revision: 376353
URL: http://svn.apache.org/viewcvs?rev=376353&view=rev
Log:
add new tests (test proxy collection, test autoRetrieve disabled)
Modified:
db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/odmg/CollectionsTest.java
Modified: db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/odmg/CollectionsTest.java
URL: http://svn.apache.org/viewcvs/db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/odmg/CollectionsTest.java?rev=376353&r1=376352&r2=376353&view=diff
==============================================================================
--- db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/odmg/CollectionsTest.java (original)
+++ db/ojb/branches/OJB_1_0_RELEASE/src/test/org/apache/ojb/odmg/CollectionsTest.java Thu Feb 9 09:16:11 2006
@@ -8,15 +8,18 @@
import java.util.List;
import java.util.Vector;
+import org.apache.commons.lang.builder.ToStringBuilder;
+import org.apache.commons.lang.builder.ToStringStyle;
+import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.PersistenceBroker;
import org.apache.ojb.broker.PersistenceBrokerFactory;
+import org.apache.ojb.broker.core.proxy.CollectionProxy;
+import org.apache.ojb.broker.core.proxy.ProxyHelper;
import org.apache.ojb.broker.metadata.CollectionDescriptor;
import org.apache.ojb.broker.query.Criteria;
import org.apache.ojb.broker.query.Query;
import org.apache.ojb.broker.query.QueryFactory;
import org.apache.ojb.junit.ODMGTestCase;
-import org.apache.commons.lang.builder.ToStringBuilder;
-import org.apache.commons.lang.builder.ToStringStyle;
import org.odmg.OQLQuery;
import org.odmg.Transaction;
@@ -49,6 +52,133 @@
super.tearDown();
}
+ public void testProxiedReferences() throws Exception
+ {
+ ojbChangeReferenceSetting(Gatherer.class, "collectiblesA", true,
+ CollectionDescriptor.CASCADE_NONE, CollectionDescriptor.CASCADE_OBJECT, true);
+
+ String prefix = "testProxiedReferences_" + System.currentTimeMillis();
+ String queryId = "select gatherer from " + Gatherer.class.getName() + " where gatId=$1";
+ String queryName = "select gatherer from " + Gatherer.class.getName() + " where name=$1";
+
+ // prepare test case
+ Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
+ CollectibleA[] cols = prepareCollectibleA(gat, prefix);
+ List colList = Arrays.asList(cols);
+ // set List of CollectiblesA objects
+ gat.setCollectiblesA(colList);
+ TransactionExt tx = (TransactionExt)odmg.newTransaction();
+ tx.begin();
+ database.makePersistent(gat);
+ tx.commit();
+
+ // check if gatherer was stored
+ tx.begin();
+ // to run this test with all cache implementation classes
+ // clear the cache before query
+ tx.getBroker().clearCache();
+ OQLQuery query = odmg.newOQLQuery();
+ query.create(queryId);
+ Integer gatId = gat.getGatId();
+ assertNotNull(gatId);
+ query.bind(gatId);
+ Collection result = (Collection) query.execute();
+ tx.commit();
+ assertNotNull(result);
+ assertEquals(1, result.size());
+ Gatherer newGat = (Gatherer) result.iterator().next();
+ assertTrue(ProxyHelper.isCollectionProxy(newGat.getCollectiblesA()));
+ CollectionProxy cp = ProxyHelper.getCollectionProxy(newGat.getCollectiblesA());
+ assertFalse(cp.isLoaded());
+
+ tx.begin();
+ query = odmg.newOQLQuery();
+ query.create(queryName);
+ query.bind(gat.getName());
+ result = (Collection) query.execute();
+ tx.commit();
+ assertNotNull(result);
+ assertEquals(1, result.size());
+ newGat = (Gatherer) result.iterator().next();
+ assertTrue(ProxyHelper.isCollectionProxy(newGat.getCollectiblesA()));
+ cp = ProxyHelper.getCollectionProxy(newGat.getCollectiblesA());
+ assertFalse(cp.isLoaded());
+
+ tx.begin();
+ PersistenceBroker pb = tx.getBroker();
+ Identity oid = pb.serviceIdentity().buildIdentity(Gatherer.class, gat.getGatId());
+ newGat = (Gatherer) pb.getObjectByIdentity(oid);
+ tx.commit();
+ assertNotNull(newGat);
+ assertTrue(ProxyHelper.isCollectionProxy(newGat.getCollectiblesA()));
+ cp = ProxyHelper.getCollectionProxy(newGat.getCollectiblesA());
+ assertFalse(cp.isLoaded());
+ }
+
+ /**
+ * tests behavior of 1:n references in OJB (odmg-api) when auto-retrieve is disabled.
+ * @throws Exception
+ */
+ public void testSetAutoRetrieveFalse() throws Exception
+ {
+ ojbChangeReferenceSetting(Gatherer.class, "collectiblesA", false,
+ CollectionDescriptor.CASCADE_NONE, CollectionDescriptor.CASCADE_OBJECT, true);
+ ojbChangeReferenceSetting(Gatherer.class, "collectiblesC", false,
+ CollectionDescriptor.CASCADE_NONE, CollectionDescriptor.CASCADE_OBJECT, true);
+
+ String prefix = "testSetAutoRetrieveFalse_" + System.currentTimeMillis();
+
+ // prepare test case
+ Gatherer gat = new Gatherer(null, prefix + "_Gatherer");
+ CollectibleA[] cols = prepareCollectibleA(gat, prefix);
+ List colList = Arrays.asList(cols);
+ // set List of CollectiblesA objects
+ gat.setCollectiblesA(colList);
+ TransactionExt tx = (TransactionExt)odmg.newTransaction();
+ tx.begin();
+ database.makePersistent(gat);
+ tx.commit();
+
+ tx.begin();
+ PersistenceBroker pb = tx.getBroker();
+ pb.clearCache();
+ Identity oid = pb.serviceIdentity().buildIdentity(Gatherer.class, gat.getGatId());
+ Gatherer newGat = (Gatherer) pb.getObjectByIdentity(oid);
+ tx.commit();
+
+ tx.begin();
+ tx.lock(newGat, Transaction.WRITE);
+ newGat.setName(prefix + "_updated");
+ //tx.getBroker().retrieveAllReferences(newGat);
+ tx.commit();
+
+ tx.begin();
+ pb = tx.getBroker();
+ newGat = (Gatherer) pb.getObjectByIdentity(oid);
+ pb.retrieveAllReferences(newGat);
+ assertEquals(cols.length, newGat.getCollectiblesA().size());
+ tx.commit();
+
+ tx.begin();
+ pb = tx.getBroker();
+ pb.clearCache();
+ newGat = (Gatherer) pb.getObjectByIdentity(oid);
+ tx.commit();
+
+ tx.begin();
+ tx.lock(newGat, Transaction.WRITE);
+ newGat.addCollectibleA(new CollectibleA(prefix + "_later_added"));
+ tx.commit();
+
+ tx.begin();
+ pb = tx.getBroker();
+ newGat = (Gatherer) pb.getObjectByIdentity(oid);
+ pb.retrieveAllReferences(newGat);
+ tx.commit();
+ assertNotNull(newGat);
+ assertEquals(cols.length + 1, newGat.getCollectiblesA().size());
+
+ }
public void testStoreDeleteCascadeDelete() throws Exception
{
@@ -1169,9 +1299,9 @@
{
private Integer gatId;
private String name;
- private List collectiblesA = new Vector();
- private List collectiblesB = new Vector();
- private List collectiblesC = new Vector();
+ private List collectiblesA = new ArrayList();
+ private List collectiblesB = new ArrayList();
+ private List collectiblesC = new ArrayList();
public Gatherer()
{
---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org