db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject svn commit: r428990 [5/5] - in /db/ojb/trunk/proposals/otm: ./ java/ java/org/ java/org/apache/ java/org/apache/ojb/ java/org/apache/ojb/otm/ java/org/apache/ojb/otm/copy/ java/org/apache/ojb/otm/core/ java/org/apache/ojb/otm/kit/ java/org/apache/ojb/o...
Date Sat, 05 Aug 2006 12:18:15 GMT
Added: db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/MtoNTest.java
URL: http://svn.apache.org/viewvc/db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/MtoNTest.java?rev=428990&view=auto
==============================================================================
--- db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/MtoNTest.java (added)
+++ db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/MtoNTest.java Sat Aug  5 05:18:12 2006
@@ -0,0 +1,372 @@
+package org.apache.ojb.otm;
+
+/* Copyright 2002-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+import java.util.Vector;
+
+import org.apache.ojb.broker.Identity;
+import org.apache.ojb.broker.Paper;
+import org.apache.ojb.broker.Qualifier;
+import org.apache.ojb.broker.Topic;
+import org.apache.ojb.broker.metadata.ClassDescriptor;
+import org.apache.ojb.broker.metadata.CollectionDescriptor;
+import org.apache.ojb.broker.util.collections.ManageableListImpl;
+import org.apache.ojb.broker.util.collections.RemovalAwareCollection;
+import org.apache.ojb.junit.OJBTestCase;
+import org.apache.ojb.otm.core.Transaction;
+import org.apache.ojb.otm.lock.LockingException;
+
+/**
+ * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird</a>
+ * @version $Id$
+ */
+public class MtoNTest extends OJBTestCase
+{
+	private TestKit _kit;
+	private OTMConnection _conn;
+	//private static final int COUNT = 1;
+	//private static final long TIME = System.currentTimeMillis();
+
+	public void setUp() throws Exception
+	{
+		super.setUp();
+        _kit = TestKit.getTestInstance();
+		_conn = _kit.acquireConnection(ojb.getDefaultConfiguration());
+	}
+
+	public void tearDown() throws Exception
+	{
+		_conn.close();
+		_conn = null;
+        super.tearDown();
+	};
+
+	public static void main(String[] args)
+	{
+		String[] arr = {MtoNTest.class.getName()};
+		junit.textui.TestRunner.main(arr);
+	}
+
+	private Paper createPaper() throws LockingException
+	{
+		String now = new Date().toString();
+		Paper paper = new Paper();
+		paper.setAuthor("Jonny Myers");
+		paper.setDate(now);
+
+		Qualifier qual1 = new Topic();
+		qual1.setName("qual1 " + now);
+		Qualifier qual2 = new Topic();
+		qual2.setName("qual2 " + now);
+
+		List qualifiers = new Vector();
+		qualifiers.add(qual1);
+		qualifiers.add(qual2);
+		paper.setQualifiers(qualifiers);
+		Transaction trans = _kit.getTransaction(_conn);
+		trans.begin();
+		_conn.makePersistent(qual1);
+		_conn.makePersistent(qual2);
+		_conn.makePersistent(paper);
+		Identity paperId = _conn.getIdentity(paper);
+		trans.commit();
+
+		// sanity check
+		trans = _kit.getTransaction(_conn);
+		trans.begin();
+		Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
+		qualifiers = retPaper.getQualifiers();
+
+		assertEquals(2, qualifiers.size());
+		trans.commit();
+
+		return retPaper;
+	}
+
+	public void testCreate() throws Exception
+	{
+		Paper paper = createPaper();
+	}
+
+	public void testStoringWithAutoUpdateFalse() throws Exception
+	{
+		ClassDescriptor cld = _conn.getDescriptorFor(Paper.class);
+		CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
+		boolean autoUpdate = cod.getCascadeStore();
+		cod.setCascadeStore(false);
+		try
+		{
+			String now = new Date().toString();
+			Paper paper = new Paper();
+			paper.setAuthor("Jonny Myers");
+			paper.setDate(now);
+			Qualifier qual = new Topic();
+			qual.setName("qual " + now);
+			paper.setQualifiers(Arrays.asList(new Qualifier[]{qual}));
+			Transaction trans = _kit.getTransaction(_conn);
+			trans.begin();
+			_conn.makePersistent(paper);        // store Paper and intermediary table only
+			Identity paperId = _conn.getIdentity(paper);
+			trans.commit();
+
+		//	broker.clearCache();
+			trans = _kit.getTransaction(_conn);
+			trans.begin();
+			Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
+			assertEquals(0, retPaper.getQualifiers().size());
+			trans.commit();
+			;
+		}
+		finally
+		{
+			cod.setCascadeStore(autoUpdate);
+		}
+	}
+
+	public void testStoringWithAutoUpdateTrue() throws Exception
+	{
+		ClassDescriptor cld = _conn.getDescriptorFor(Paper.class);
+		CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
+		boolean autoUpdate = cod.getCascadeStore();
+
+		cod.setCascadeStore(true);
+
+		try
+		{
+			String now = new Date().toString();
+			Paper paper = new Paper();
+			paper.setAuthor("Jonny Myers");
+			paper.setDate(now);
+			Qualifier qual = new Topic();
+			qual.setName("qual " + now);
+			paper.setQualifiers(Arrays.asList(new Qualifier[]{qual}));
+			Transaction trans = _kit.getTransaction(_conn);
+			trans.begin();
+			_conn.makePersistent(paper);        // store Paper, intermediary and Qualifier
+			Identity paperId = _conn.getIdentity(paper);
+			trans.commit();
+			//broker.clearCache();
+			trans = _kit.getTransaction(_conn);
+			trans.begin();
+			Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
+			assertEquals(1, retPaper.getQualifiers().size());
+			trans.commit();
+			;
+		}
+		finally
+		{
+			cod.setCascadeStore(autoUpdate);
+		}
+	}
+
+
+	// delete from intermediary table only when collection NOT removal aware
+	public void testDelete_NonRemovalAware() throws Exception
+	{
+		ClassDescriptor cld = _conn.getDescriptorFor(Paper.class);
+		CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
+		Class collectionClass = cod.getCollectionClass();
+
+		cod.setCollectionClass(ManageableListImpl.class);
+
+		try
+		{
+			Paper paper = createPaper();
+			Identity paperId = _conn.getIdentity(paper);
+			List qualifiers = paper.getQualifiers();
+			Qualifier qual1 = (Qualifier) qualifiers.get(0);
+			Qualifier qual2 = (Qualifier) qualifiers.get(1);
+
+			// remove first object
+			qualifiers.remove(0);
+			Transaction trans = _kit.getTransaction(_conn);
+			trans.begin();
+			_conn.makePersistent(paper);
+			trans.commit();
+			;
+
+			//broker.clearCache();
+			trans = _kit.getTransaction(_conn);
+			trans.begin();
+			Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
+			assertEquals(1, retPaper.getQualifiers().size());
+			// target object qual1 should NOT be deleted
+			Qualifier retQual1 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual1));
+			Qualifier retQual2 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual2));
+
+			assertNotNull(retQual1);
+			assertNotNull(retQual2);
+
+			trans.commit();
+			;
+		}
+		finally
+		{
+			cod.setCollectionClass(collectionClass);
+		}
+
+	}
+
+	// delete from intermediary AND target-table when collection removal aware
+	public void testDelete_RemovalAware() throws Exception
+	{
+		ClassDescriptor cld = _conn.getDescriptorFor(Paper.class);
+		CollectionDescriptor cod = cld.getCollectionDescriptorByName("qualifiers");
+		Class collectionClass = cod.getCollectionClass();
+
+		cod.setCollectionClass(RemovalAwareCollection.class);
+
+		try
+		{
+			Paper paper = createPaper();
+			List qualifiers = paper.getQualifiers();
+			Qualifier qual1 = (Qualifier) qualifiers.get(0);
+			Qualifier qual2 = (Qualifier) qualifiers.get(1);
+			Identity paperId = _conn.getIdentity(paper);
+
+			// remove first object
+			qualifiers.remove(0);
+			Transaction trans = _kit.getTransaction(_conn);
+			trans.begin();
+			_conn.makePersistent(paper);
+			trans.commit();
+			;
+
+		//	broker.clearCache();
+			trans = _kit.getTransaction(_conn);
+			trans.begin();
+			Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
+			assertEquals(1, retPaper.getQualifiers().size());
+
+			// target object qual1 should be deleted
+			Qualifier retQual1 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual1));
+			Qualifier retQual2 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual2));
+
+			assertNull(retQual1);
+			assertNotNull(retQual2);
+
+			trans.commit();
+			;
+		}
+		finally
+		{
+			cod.setCollectionClass(collectionClass);
+		}
+	}
+
+	public void testDeletionFromIntermediaryTableWithNullList() throws Exception
+	{
+		Paper paper = createPaper();
+		Identity paperId = _conn.getIdentity(paper);
+		List qualifiers = paper.getQualifiers();
+		Qualifier qual1 = (Qualifier) qualifiers.get(0);
+		Qualifier qual2 = (Qualifier) qualifiers.get(1);
+
+		// now set collection to null and check if changes get persisted
+		paper.setQualifiers(null);
+		Transaction trans = _kit.getTransaction(_conn);
+		trans.begin();
+		_conn.makePersistent(paper);
+		trans.commit();
+		;
+
+		//broker.clearCache();
+		trans = _kit.getTransaction(_conn);
+		trans.begin();
+		Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
+		assertEquals(0, retPaper.getQualifiers().size());
+
+		// target objects should NOT be deleted
+		Qualifier retQual1 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual1));
+		Qualifier retQual2 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual2));
+
+		assertNotNull(retQual1);
+		assertNotNull(retQual2);
+
+		trans.commit();
+		;
+	}
+
+	public void testDeletionWithClearedList() throws Exception
+	{
+		Paper paper = createPaper();
+		Identity paperId = _conn.getIdentity(paper);
+		List qualifiers = paper.getQualifiers();
+		Qualifier qual1 = (Qualifier) qualifiers.get(0);
+		Qualifier qual2 = (Qualifier) qualifiers.get(1);
+
+		// now clear collection
+		paper.getQualifiers().clear();
+		Transaction trans = _kit.getTransaction(_conn);
+		trans.begin();
+		_conn.makePersistent(paper);
+		trans.commit();
+		;
+
+		//broker.clearCache();
+		trans = _kit.getTransaction(_conn);
+		trans.begin();
+		Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
+		assertEquals(0, retPaper.getQualifiers().size());
+
+		// target objects should be deleted
+		Qualifier retQual1 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual1));
+		Qualifier retQual2 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual2));
+
+		assertNull(retQual1);
+		assertNull(retQual2);
+
+		trans.commit();
+		;
+	}
+
+	public void testDeletionFromIntermediaryTableWithEmptyList() throws Exception
+	{
+		Paper paper = createPaper();
+		Identity paperId = _conn.getIdentity(paper);
+		List qualifiers = paper.getQualifiers();
+		Qualifier qual1 = (Qualifier) qualifiers.get(0);
+		Qualifier qual2 = (Qualifier) qualifiers.get(1);
+
+		// now empty collection and check if changes get persisted
+		paper.setQualifiers(new RemovalAwareCollection());
+		Transaction trans = _kit.getTransaction(_conn);
+		trans.begin();
+		_conn.makePersistent(paper);
+		trans.commit();
+		;
+
+	//	broker.clearCache();
+		trans = _kit.getTransaction(_conn);
+		trans.begin();
+		Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
+		assertEquals(0, retPaper.getQualifiers().size());
+
+		// target objects should NOT be deleted
+		Qualifier retQual1 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual1));
+		Qualifier retQual2 = (Qualifier) _conn.getObjectByIdentity(_conn.getIdentity(qual2));
+
+		assertNotNull(retQual1);
+		assertNotNull(retQual2);
+
+		trans.commit();
+		;
+	}
+
+}

Added: db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/MultipleConnectionsTest.java
URL: http://svn.apache.org/viewvc/db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/MultipleConnectionsTest.java?rev=428990&view=auto
==============================================================================
--- db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/MultipleConnectionsTest.java (added)
+++ db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/MultipleConnectionsTest.java Sat Aug  5 05:18:12 2006
@@ -0,0 +1,113 @@
+package org.apache.ojb.otm;
+
+/* Copyright 2002-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import junit.framework.Assert;
+
+import org.apache.ojb.broker.Article;
+import org.apache.ojb.broker.Identity;
+
+import org.apache.ojb.odmg.oql.EnhancedOQLQuery;
+import org.apache.ojb.otm.core.Transaction;
+import org.apache.ojb.junit.OJBTestCase;
+
+
+
+/**
+ * Ensure a Transaction can attach multiple connections
+ */
+public class MultipleConnectionsTest extends OJBTestCase
+{
+    public MultipleConnectionsTest(String name)
+    {
+        super(name);
+    }
+
+    private TestKit _kit;
+
+    public void setUp()
+    {
+        _kit = TestKit.getTestInstance();
+    }
+
+    public void tearDown()
+    {
+
+    }
+
+    /**
+     * TODO: I think this only passes because both transactions are in the same thread,
+     *       otherwise it would throw an exception every time saying
+     *       "Attempt to re-assign a different transaction to a open connection"
+     *
+     * @throws Throwable
+     */
+    public void testJustAttachConnections() throws Throwable
+    {
+        Transaction tx = null;
+        Article example;
+
+        OTMConnection conn1 = _kit.acquireConnection(ojb.getDefaultConfiguration());
+        OTMConnection conn2 = _kit.acquireConnection(ojb.getDefaultConfiguration());
+        try
+        {
+            tx = _kit.getTransaction(conn1);
+            tx.begin();
+
+            tx.registerConnection(conn2);
+
+            example = (Article) conn1.getObjectByIdentity(
+                    new Identity(Article.class, Article.class,
+                                 new Object[]{new Integer(77779)}));
+            if (example == null)
+            {
+                example = Article.createInstance();
+                example.setArticleId(new Integer(77779));
+            }
+            example.setProductGroupId(new Integer(7));
+            example.setStock(333);
+            example.setArticleName("333");
+            conn1.makePersistent(example);
+
+            EnhancedOQLQuery query = conn2.newOQLQuery();
+            query.create("select obj from " + Article.class.getName()
+                         + " where " + "articleId = " + example.getArticleId());
+            Article same = (Article) conn2.getIteratorByOQLQuery(query).next();
+            Assert.assertNotNull("Didn't find object in context of transaction", same);
+
+            tx.commit();
+
+        }
+        catch (Throwable ex)
+        {
+            try
+            {
+                if (tx != null && tx.isInProgress())
+                {
+                    tx.rollback();
+                }
+            }
+            catch (Exception ex2)
+            {
+            }
+            throw ex;
+        }
+        finally
+        {
+            conn1.close();
+        }
+    }
+}

Added: db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/NaturalPerson.java
URL: http://svn.apache.org/viewvc/db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/NaturalPerson.java?rev=428990&view=auto
==============================================================================
--- db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/NaturalPerson.java (added)
+++ db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/NaturalPerson.java Sat Aug  5 05:18:12 2006
@@ -0,0 +1,5 @@
+package org.apache.ojb.otm;
+
+public class NaturalPerson extends AbstractPerson
+{
+}

Added: db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/OtmExamples.java
URL: http://svn.apache.org/viewvc/db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/OtmExamples.java?rev=428990&view=auto
==============================================================================
--- db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/OtmExamples.java (added)
+++ db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/OtmExamples.java Sat Aug  5 05:18:12 2006
@@ -0,0 +1,876 @@
+package org.apache.ojb.otm;
+
+/* Copyright 2002-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.apache.ojb.broker.Article;
+import org.apache.ojb.broker.Identity;
+import org.apache.ojb.broker.InterfaceArticle;
+import org.apache.ojb.broker.ProductGroup;
+import org.apache.ojb.broker.ProductGroupWithCollectionProxy;
+import org.apache.ojb.broker.core.proxy.CollectionProxy;
+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.OJBTestCase;
+import org.apache.ojb.odmg.oql.EnhancedOQLQuery;
+import org.apache.ojb.otm.core.Transaction;
+import org.apache.ojb.otm.lock.LockType;
+import org.apache.ojb.otm.lock.LockingException;
+import org.apache.ojb.otm.lock.wait.DeadlockException;
+import org.apache.ojb.otm.lock.wait.NoWaitStrategy;
+import org.apache.ojb.otm.lock.wait.TimeoutStrategy;
+
+/**
+ * Demo Application that shows basic concepts for Applications
+ * using the OJB OTM layer directly.
+ */
+public class OtmExamples extends OJBTestCase
+{
+    private TestKit _kit;
+    private OTMConnection _conn;
+
+    public static void main(String[] args)
+    {
+        String[] arr = {OtmExamples.class.getName()};
+        junit.textui.TestRunner.main(arr);
+    }
+
+
+    public OtmExamples(String name)
+    {
+        super(name);
+    }
+
+    public void setUp() throws Exception
+    {
+        super.setUp();
+        ojbChangeReferenceSetting(ProductGroup.class, "allArticlesInGroup", true, true, true, false);
+		ojbChangeReferenceSetting(Article.class, "productGroup", true, true, true, false);
+        _kit = TestKit.getTestInstance();
+        _conn = _kit.acquireConnection(ojb.getDefaultConfiguration());
+    }
+
+    public void tearDown() throws Exception
+    {
+        _conn.close();
+        _conn = null;
+        super.tearDown();
+    }
+
+    public void testOtmSession() throws Throwable
+    {
+        Transaction tx = null;
+        Criteria crit;
+        Query q;
+        EnhancedOQLQuery oql;
+        Iterator it;
+        Article example;
+
+        //perform transaction
+        try
+        {
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+
+            example = (Article) _conn.getObjectByIdentity(
+                    new Identity(Article.class, Article.class,
+                                 new Object[] {new Integer(77777)}));
+            if (example == null)
+            {
+                example = Article.createInstance();
+                example.setArticleId(new Integer(77777));
+            }
+            example.setProductGroupId(new Integer(7));
+            example.setStock(333);
+            example.setArticleName("333");
+            _conn.makePersistent(example);
+
+            tx.commit();
+
+            Identity oid = _conn.getIdentity(example);
+
+            // get from the cache
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            example = (Article) _conn.getObjectByIdentity(oid);
+            assertEquals("should be equal", 7, example.getProductGroupId().intValue());
+            assertEquals("should be equal", 333, example.getStock());
+            assertEquals("should be equal", "333", example.getArticleName());
+            tx.commit();
+
+            // get from the database
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            _conn.invalidate(oid);
+            example = (Article) _conn.getObjectByIdentity(oid);
+            assertEquals("should be equal", 7, example.getProductGroupId().intValue());
+            assertEquals("should be equal", "333", example.getArticleName());
+            example.setArticleName("334"); // test update
+            tx.commit();
+
+            // get from the database via Query
+            tx = _kit.getTransaction(_conn);
+            _conn.invalidate(oid);
+            tx.begin();
+            crit = new Criteria();
+            crit.addEqualTo("articleId", new Integer(77777));
+            crit.addEqualTo("articleName", "334");
+            q = QueryFactory.newQuery(Article.class, crit);
+            it = _conn.getIteratorByQuery(q);
+            if (it.hasNext())
+            {
+                InterfaceArticle article = (InterfaceArticle) it.next();
+                assertEquals("should be equal", 77777, article.getArticleId().intValue());
+                assertEquals("should be equal", "334", article.getArticleName());
+                article.setArticleName("335"); // test update
+                if (it.hasNext())
+                {
+                    fail("Query returned more than 1 object");
+                }
+            }
+            else
+            {
+                fail("Query returned empty result set");
+            }
+            tx.commit();
+
+            // get from the database via OQLQuery Iterator
+            tx = _kit.getTransaction(_conn);
+            _conn.invalidate(oid);
+            tx.begin();
+            oql = _conn.newOQLQuery();
+            oql.create("select a from " + Article.class.getName()
+                + " where articleId=$1 and articleName=$2");
+            oql.bind(new Integer(77777));
+            oql.bind("335");
+            it = _conn.getIteratorByOQLQuery(oql);
+            if (it.hasNext())
+            {
+                InterfaceArticle article = (InterfaceArticle) it.next();
+                assertEquals("should be equal", 77777, article.getArticleId().intValue());
+                assertEquals("should be equal", "335", article.getArticleName());
+                article.setArticleName("336"); // test update
+                if (it.hasNext())
+                {
+                    fail("Query returned more than 1 object");
+                }
+            }
+            else
+            {
+                fail("Query returned empty result set");
+            }
+            tx.commit();
+
+            // get from the database via OQLQuery Collection
+            tx = _kit.getTransaction(_conn);
+            _conn.invalidate(oid);
+            tx.begin();
+            oql.bind(new Integer(77777));
+            oql.bind("336");
+            it = ((Collection) oql.execute()).iterator();
+            if (it.hasNext())
+            {
+                InterfaceArticle article = (InterfaceArticle) it.next();
+                assertEquals("should be equal", 77777, article.getArticleId().intValue());
+                assertEquals("should be equal", "336", article.getArticleName());
+                article.setArticleName("337"); // test update
+                if (it.hasNext())
+                {
+                    fail("Query returned more than 1 object");
+                }
+            }
+            else
+            {
+                fail("Query returned empty result set");
+            }
+            tx.commit();
+
+            // get from the database
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            _conn.invalidate(oid);
+            example = (Article) _conn.getObjectByIdentity(oid);
+            assertEquals("should be equal", "337", example.getArticleName());
+            tx.commit();
+
+            try
+            {
+                tx = _kit.getTransaction(_conn);
+                tx.begin();
+                example = (Article) _conn.getObjectByIdentity(oid);
+                _conn.deletePersistent(example);
+                tx.commit();
+            }
+            catch (Throwable ex)
+            {
+                ex.printStackTrace();
+                tx.rollback();
+            }
+        }
+        catch (Throwable ex)
+        {
+            try
+            {
+                if (tx != null && tx.isInProgress())
+                {
+                    tx.rollback();
+                }
+            }
+            catch (Exception ex2)
+            {
+            }
+            throw ex;
+        }
+    }
+
+    public void testCollectionProxy() throws Throwable
+    {
+        Transaction tx = null;
+
+        //perform transaction
+        try
+        {
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+
+            ProductGroupWithCollectionProxy pg = new ProductGroupWithCollectionProxy();
+            pg.setId(new Integer(77777));
+            pg.setName("1");
+            _conn.makePersistent(pg);
+
+            tx.commit();
+
+            Identity oid = _conn.getIdentity(pg);
+
+            // get from the database
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            _conn.invalidate(oid);
+            pg = (ProductGroupWithCollectionProxy) _conn.getObjectByIdentity(oid);
+            assertTrue("CollectionProxy isn't loaded",
+                    !((CollectionProxy) pg.getAllArticlesInGroup()).isLoaded());
+            Article article = Article.createInstance();
+            article.setArticleId(new Integer(77777));
+            article.setProductGroup(pg);
+            article.setStock(333);
+            article.setArticleName("333");
+            pg.getAllArticlesInGroup().add(article);
+            _conn.makePersistent(article);
+            tx.commit();
+
+            // get from the database
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            _conn.invalidate(oid);
+            pg = (ProductGroupWithCollectionProxy) _conn.getObjectByIdentity(oid);
+            assertEquals("CollectionProxy size", 1, pg.getAllArticlesInGroup().size());
+            ((InterfaceArticle) pg.getAllArticlesInGroup().get(0)).setArticleName("444");
+            tx.commit();
+
+            // test isolation of the cache
+            ((InterfaceArticle) pg.getAllArticlesInGroup().get(0)).setArticleName("555");
+
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            pg = (ProductGroupWithCollectionProxy) _conn.getObjectByIdentity(oid);
+            assertEquals("Article name", "444",
+                    ((InterfaceArticle) pg.getAllArticlesInGroup().get(0)).getArticleName());
+            tx.commit();
+
+            try
+            {
+                tx = _kit.getTransaction(_conn);
+                tx.begin();
+                pg = (ProductGroupWithCollectionProxy) _conn.getObjectByIdentity(oid);
+                _conn.deletePersistent(pg.getAllArticlesInGroup().get(0));
+                _conn.deletePersistent(pg);
+                tx.commit();
+            }
+            catch (Throwable ex)
+            {
+                ex.printStackTrace();
+                tx.rollback();
+            }
+        }
+        catch (Throwable ex)
+        {
+            try
+            {
+                if (tx != null && tx.isInProgress())
+                {
+                    tx.rollback();
+                }
+            }
+            catch (Exception ex2)
+            {
+            }
+            throw ex;
+        }
+    }
+
+    public void testOtmCache() throws Throwable
+    {
+        Transaction tx = null;
+
+        //perform transaction
+        try
+        {
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+
+            ProductGroup pg = new ProductGroup();
+            pg.setId(new Integer(77777));
+            pg.setName("1");
+            _conn.makePersistent(pg);
+            Article article = Article.createInstance();
+            article.setArticleId(new Integer(77777));
+            article.setStock(373);
+            pg.add(article);
+            article.setProductGroup(pg);
+            _conn.makePersistent(article);
+            tx.commit();
+
+            Identity aOid = _conn.getIdentity(article);
+            Identity pgOid = _conn.getIdentity(pg);
+
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
+            pg.setName("2");
+            _conn.makePersistent(pg);
+            tx.rollback();
+
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            article = (Article) _conn.getObjectByIdentity(aOid);
+            assertEquals("should be equal", "1", article.getProductGroup().getName());
+            tx.commit();
+
+            // test checkpoint
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
+            pg.setName("2");
+            _conn.makePersistent(pg);
+            tx.checkpoint();
+            tx.rollback();
+
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            article = (Article) _conn.getObjectByIdentity(aOid);
+            assertEquals("should be equal", "1", article.getProductGroup().getName());
+            tx.commit();
+
+            try
+            {
+                tx = _kit.getTransaction(_conn);
+                tx.begin();
+                article = (Article) _conn.getObjectByIdentity(aOid);
+                _conn.deletePersistent(article);
+                _conn.deletePersistent(article.getProductGroup());
+                tx.commit();
+            }
+            catch (Throwable ex)
+            {
+                ex.printStackTrace();
+                tx.rollback();
+            }
+        }
+        catch (Throwable ex)
+        {
+            try
+            {
+                if (tx != null && tx.isInProgress())
+                {
+                    tx.rollback();
+                }
+            }
+            catch (Exception ex2)
+            {
+            }
+            throw ex;
+        }
+    }
+
+    public void testOtmIsolation() throws Throwable
+    {
+        Transaction tx = null;
+        Transaction tx2 = null;
+        OTMConnection conn2;
+
+        conn2 = _kit.acquireConnection(ojb.getDefaultConfiguration());
+
+        try
+        {
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+
+            ProductGroup pg = new ProductGroup();
+            pg.setId(new Integer(77777));
+            pg.setName("1");
+            _conn.makePersistent(pg);
+            tx.commit();
+
+            Identity pgOid = _conn.getIdentity(pg);
+
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
+            pg.setName("2");
+
+            tx2 = _kit.getTransaction(conn2);
+            tx2.begin();
+            pg = (ProductGroup) conn2.getObjectByIdentity(pgOid);
+            assertEquals("should be equal", "1", pg.getName());
+            tx2.commit();
+            tx.commit();
+
+            try
+            {
+                tx = _kit.getTransaction(_conn);
+                tx.begin();
+                pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
+                _conn.deletePersistent(pg);
+                tx.commit();
+            }
+            catch (Throwable ex)
+            {
+                ex.printStackTrace();
+                tx.rollback();
+            }
+        }
+        catch (Throwable ex)
+        {
+            try
+            {
+                if (tx != null && tx.isInProgress())
+                {
+                    tx.rollback();
+                }
+            }
+            catch (Exception ex2)
+            {
+            }
+            throw ex;
+        }
+    }
+
+    public void testOtmLocks() throws Throwable
+    {
+        Transaction tx = null;
+        Transaction tx2 = null;
+        OTMConnection conn2;
+        ProductGroup pg = null;
+        ProductGroup pg2 = null;
+        Identity pOid = null;
+        Identity pOid2 = null;
+
+        conn2 = _kit.acquireConnection(ojb.getDefaultConfiguration());
+
+        try
+        {
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            pg = new ProductGroup();
+            pg.setId(new Integer(77777));
+            pg.setName("1");
+            pOid = _conn.getIdentity(pg);
+            if (_conn.getObjectByIdentity(pOid) == null)
+            {
+                _conn.makePersistent(pg);
+            }
+            pg2 = new ProductGroup();
+            pg2.setId(new Integer(77778));
+            pg2.setName("1");
+            pOid2 = _conn.getIdentity(pg2);
+            if (_conn.getObjectByIdentity(pOid2) == null)
+            {
+                _conn.makePersistent(pg2);
+            }
+            tx.commit();
+
+            final Identity pgOid = _conn.getIdentity(pg);
+            final Identity pgOid2 = _conn.getIdentity(pg2);
+
+
+            final Transaction tx3 = _kit.getTransaction(_conn);
+            tx3.begin();
+            _conn.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
+            // we can write lock twice from the same tx
+            _conn.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
+
+            // test different LockWaitStrategies
+            _kit.setLockWaitStrategy(new NoWaitStrategy());
+            failIfLockForWrite(conn2, pgOid);
+            _kit.setLockWaitStrategy(new TimeoutStrategy(1));
+            failIfLockForWrite(conn2, pgOid);
+
+            // Second test for the TimeoutStrategy:
+            // let the second tx to lock
+            _kit.setLockWaitStrategy(new TimeoutStrategy(2000));
+            tx2 = _kit.getTransaction(conn2);
+            tx2.begin();
+            (new Thread()
+            {
+                public void run()
+                {
+                    try
+                    {
+                        Thread.sleep(1000);
+                        tx3.commit();
+                    }
+                    catch (InterruptedException ex)
+                    {
+                    }
+                }
+            }).start();
+            conn2.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
+            tx2.commit();
+
+            // Third test for the TimeoutStrategy:
+            // test deadlock detection
+            _kit.setLockWaitStrategy(new TimeoutStrategy(4000));
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            _conn.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
+            tx2 = _kit.getTransaction(conn2);
+            tx2.begin();
+            conn2.getObjectByIdentity(pgOid2, LockType.WRITE_LOCK);
+            (new Thread()
+            {
+                public void run()
+                {
+                    try
+                    {
+                        _conn.getObjectByIdentity(pgOid2, LockType.WRITE_LOCK);
+                    }
+                    catch (LockingException ex)
+                    {
+                        ex.printStackTrace();
+                    }
+                }
+            }).start();
+
+            try
+            {
+                Thread.sleep(2000);
+            }
+            catch (InterruptedException ex)
+            {
+            }
+
+            try
+            {
+                conn2.getObjectByIdentity(pgOid, LockType.WRITE_LOCK);
+                fail("DeadlockException was not thrown");
+            }
+            catch (DeadlockException ex)
+            {
+                // ok, deadlock was detected
+            }
+
+            tx2.rollback();
+            try
+            {
+                Thread.sleep(2000);
+            }
+            catch (InterruptedException ex)
+            {
+            }
+
+            tx.commit();
+        }
+        catch (Throwable ex)
+        {
+            try
+            {
+                if (tx != null && tx.isInProgress())
+                {
+                    tx.rollback();
+                }
+            }
+            catch (Exception ex2)
+            {
+            }
+            throw ex;
+        }
+        finally
+        {
+            try
+            {
+                tx = _kit.getTransaction(_conn);
+                tx.begin();
+                if (pOid != null)
+                {
+                    pg = (ProductGroup) _conn.getObjectByIdentity(pOid);
+                    if (pg != null)
+                    {
+                        _conn.deletePersistent(pg);
+                    }
+                }
+                if (pOid2 != null)
+                {
+                    pg2 = (ProductGroup) _conn.getObjectByIdentity(pOid2);
+                    if (pg2 != null)
+                    {
+                        _conn.deletePersistent(pg2);
+                    }
+                }
+                tx.commit();
+            }
+            catch (Throwable ex)
+            {
+                ex.printStackTrace();
+                tx.rollback();
+            }
+        }
+    }
+
+    public void testUpdateByReachability() throws Throwable
+    {
+        if(ojbSkipKnownIssueProblem("Update by reachabilitiy doesn't work proper"))
+        {
+            return;
+        }
+        Transaction tx = null;
+        ProductGroup pg;
+        Article article;
+        Article article2;
+        org.apache.ojb.broker.Person person;
+        org.apache.ojb.broker.Project project;
+        Identity aOid = null;
+        Identity aOid2 = null;
+        Identity pgOid = null;
+        Identity prsOid = null;
+        Identity prjOid = null;
+
+        //perform transaction
+        try
+        {
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+
+            pg = new ProductGroup();
+            pg.setId(new Integer(77777));
+            pgOid = _conn.getIdentity(pg);
+            pg.setName("1");
+            _conn.makePersistent(pg);
+            article = Article.createInstance();
+            article.setArticleId(new Integer(77777));
+            aOid = _conn.getIdentity(article);
+            article.setStock(333);
+            pg.add(article);
+            article.setProductGroup(pg);
+            article2 = Article.createInstance();
+            article2.setArticleId(new Integer(77778));
+            aOid2 = _conn.getIdentity(article2);
+            article2.setStock(334);
+            pg.add(article2);
+            article2.setProductGroup(pg);
+            _conn.makePersistent(article);
+            _conn.makePersistent(article2);
+            person = new org.apache.ojb.broker.Person(77777, "first", "last");
+            prsOid = _conn.getIdentity(person);
+            project = new org.apache.ojb.broker.Project(77777, "title", "desc");
+            prjOid = _conn.getIdentity(project);
+            _conn.makePersistent(person);
+            _conn.makePersistent(project);
+            tx.commit();
+
+
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
+            InterfaceArticle articleNew1 = (InterfaceArticle) pg.getAllArticles().get(0);
+            InterfaceArticle articleNew2 = (InterfaceArticle) pg.getAllArticles().get(1);
+            if (!_conn.getIdentity(articleNew2).equals(aOid2))
+            {
+                articleNew2 = (InterfaceArticle) pg.getAllArticles().get(0);
+                articleNew1 = (InterfaceArticle) pg.getAllArticles().get(1);
+                if (!_conn.getIdentity(article2).equals(aOid2))
+                {
+                    fail("Missing the second article");
+                }
+            }
+            articleNew1.setStock(433);
+            articleNew2.setStock(434);
+            pg.setName("2");
+            tx.commit();
+
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            _conn.invalidateAll();
+            articleNew1 = (InterfaceArticle) _conn.getObjectByIdentity(aOid);
+            articleNew2 = (InterfaceArticle) _conn.getObjectByIdentity(aOid2);
+            assertEquals("should be equal", "2", article.getProductGroup().getName());
+            assertEquals("should be equal", 433, article.getStock());
+            assertEquals("should be equal", 434, article2.getStock());
+            tx.commit();
+
+            // Test M:N relations
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            person = (org.apache.ojb.broker.Person) _conn.getObjectByIdentity(prsOid);
+            project = (org.apache.ojb.broker.Project) _conn.getObjectByIdentity(prjOid);
+            person.getProjects().add(project);
+            tx.commit();
+
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            _conn.invalidateAll();
+            person = (org.apache.ojb.broker.Person) _conn.getObjectByIdentity(prsOid);
+            project = (org.apache.ojb.broker.Project) _conn.getObjectByIdentity(prjOid);
+            assertEquals("should be equal", 1, person.getProjects().size());
+            tx.commit();
+        }
+        catch (Throwable ex)
+        {
+            try
+            {
+                if (tx != null && tx.isInProgress())
+                {
+                    tx.rollback();
+                }
+            }
+            catch (Exception ex2)
+            {
+            }
+            throw ex;
+        }
+        finally
+        {
+            try
+            {
+                if (tx == null || !tx.isInProgress())
+                {
+                    tx = _kit.getTransaction(_conn);
+                    tx.begin();
+                }
+
+                if (aOid != null)
+                {
+                    article = (Article) _conn.getObjectByIdentity(aOid);
+                    if (article != null)
+                    {
+                        _conn.deletePersistent(article);
+                    }
+                }
+                if (aOid2 != null)
+                {
+                    article2 = (Article) _conn.getObjectByIdentity(aOid2);
+                    if (article2 != null)
+                    {
+                        _conn.deletePersistent(article2);
+                    }
+                }
+                if (pgOid != null)
+                {
+                    pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
+                    if (pg != null)
+                    {
+                        _conn.deletePersistent(pg);
+                    }
+                }
+                if (prsOid != null)
+                {
+                    person = (org.apache.ojb.broker.Person) _conn.getObjectByIdentity(prsOid);
+                    if (person != null)
+                    {
+                        _conn.deletePersistent(person);
+                    }
+                }
+                if (prjOid != null)
+                {
+                    project = (org.apache.ojb.broker.Project) _conn.getObjectByIdentity(prjOid);
+                    if (project != null)
+                    {
+                        _conn.deletePersistent(project);
+                    }
+                }
+                tx.commit();
+            }
+            catch (Throwable ex)
+            {
+                ex.printStackTrace();
+                tx.rollback();
+            }
+        }
+    }
+
+    public void testSwizzling() throws Throwable
+    {
+        Transaction tx = null;
+        ProductGroup pg;
+        Article article;
+        Article article2;
+
+        try
+        {
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+
+            pg = new ProductGroup();
+            pg.setId(new Integer(77777));
+            _conn.makePersistent(pg);
+            article = Article.createInstance();
+            article.setArticleId(new Integer(77777));
+            article.setStock(333);
+            pg.add(article);
+            article.setProductGroup(pg);
+            _conn.makePersistent(article);
+            article2 = Article.createInstance();
+            article2.setArticleId(article.getArticleId());
+            article2.setStock(334);
+            article2.setProductGroup(pg);
+            _conn.makePersistent(article2);
+            article = (Article) pg.getAllArticles().get(0);
+            assertEquals("should be equal", 334, article.getStock());
+        }
+        finally
+        {
+            if (tx != null)
+            {
+                try
+                {
+                    tx.rollback();
+                }
+                catch (Throwable ex)
+                {
+                    ex.printStackTrace();
+                }
+            }
+        }
+    }
+
+    private void failIfLockForWrite(OTMConnection conn2, Identity oid)
+            throws Exception
+    {
+        Transaction tx2 = null;
+
+        tx2 = _kit.getTransaction(conn2);
+        tx2.begin();
+        try {
+            conn2.getObjectByIdentity(oid, LockType.WRITE_LOCK);
+            fail("LockingException was not thrown");
+        } catch (LockingException ex) {
+            // ok: we cannot write lock from another tx
+            tx2.rollback();
+        }
+    }
+}

Added: db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/Person.java
URL: http://svn.apache.org/viewvc/db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/Person.java?rev=428990&view=auto
==============================================================================
--- db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/Person.java (added)
+++ db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/Person.java Sat Aug  5 05:18:12 2006
@@ -0,0 +1,112 @@
+package org.apache.ojb.otm;
+
+/* Copyright 2002-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.util.ArrayList;
+import java.io.Serializable;
+
+public class Person implements Serializable
+{
+
+    private int id;
+    private String firstname;
+    private String lastname;
+    private Integer mainAddressId;
+    private Address mainAddress;
+    private ArrayList otherAddresses;
+
+    public Person()
+    {
+    }
+
+    public Person(String firstname, String lastname)
+    {
+        this.firstname = firstname;
+        this.lastname = lastname;
+    }
+
+    public int getId()
+    {
+        return id;
+    }
+
+    public void setId(int id)
+    {
+        this.id = id;
+    }
+
+    public String getFirstname()
+    {
+        return firstname;
+    }
+
+    public void setFirstname(String firstname)
+    {
+        this.firstname = firstname;
+    }
+
+    public String getLastname()
+    {
+        return lastname;
+    }
+
+    public void setLastname(String lastname)
+    {
+        this.lastname = lastname;
+    }
+
+    public Integer getMainAddressId()
+    {
+        return mainAddressId;
+    }
+
+    public void setMainAddressId(Integer mainAddressId)
+    {
+        this.mainAddressId = mainAddressId;
+    }
+
+    public Address getMainAddress()
+    {
+        return mainAddress;
+    }
+
+    public void setMainAddress(Address mainAddress)
+    {
+        this.mainAddress = mainAddress;
+    }
+
+    public ArrayList getOtherAddresses()
+    {
+        return otherAddresses;
+    }
+
+    public void setOtherAddresses(ArrayList otherAddresses)
+    {
+        this.otherAddresses = otherAddresses;
+    }
+
+    public void addOtherAddress(String desc, Address address)
+    {
+        if (otherAddresses == null)
+        {
+            otherAddresses = new ArrayList();
+        }
+        AddressDesc addrDesc = new AddressDesc(desc, address);
+        this.otherAddresses.add(addrDesc);
+        addrDesc.setPerson(this);
+    }
+
+}

Added: db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/SwizzleTests.java
URL: http://svn.apache.org/viewvc/db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/SwizzleTests.java?rev=428990&view=auto
==============================================================================
--- db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/SwizzleTests.java (added)
+++ db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/SwizzleTests.java Sat Aug  5 05:18:12 2006
@@ -0,0 +1,672 @@
+package org.apache.ojb.otm;
+
+/* Copyright 2002-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import java.sql.Timestamp;
+import java.util.Collection;
+import java.util.Iterator;
+
+import org.apache.ojb.broker.Article;
+import org.apache.ojb.broker.Contract;
+import org.apache.ojb.broker.Effectiveness;
+import org.apache.ojb.broker.Identity;
+import org.apache.ojb.broker.PBFactoryException;
+import org.apache.ojb.broker.PersistenceBrokerException;
+import org.apache.ojb.broker.ProductGroup;
+import org.apache.ojb.broker.RelatedToContract;
+import org.apache.ojb.broker.Version;
+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.OJBTestCase;
+import org.apache.ojb.odmg.shared.TestClassA;
+import org.apache.ojb.odmg.shared.TestClassB;
+import org.apache.ojb.otm.core.Transaction;
+import org.apache.ojb.otm.core.TransactionException;
+import org.apache.ojb.otm.lock.LockType;
+import org.apache.ojb.otm.lock.LockingException;
+
+
+/**
+ * User: Matthew Baird
+ * Date: Jun 21, 2003
+ * Time: 3:59:08 PM
+ */
+public class SwizzleTests extends OJBTestCase
+{
+    private TestKit _kit;
+    private OTMConnection _conn;
+    private static final int COUNT = 1;
+    private static final long TIME = System.currentTimeMillis();
+
+    public void setUp() throws Exception
+    {
+        super.setUp();
+        ojbChangeReferenceSetting(TestClassA.class, "b", true, true, true, false);
+		ojbChangeReferenceSetting(TestClassB.class, "a", true, true, true, false);
+		ojbChangeReferenceSetting(ProductGroup.class, "allArticlesInGroup", true, true, true, false);
+		ojbChangeReferenceSetting(Article.class, "productGroup", true, true, true, false);
+        _kit = TestKit.getTestInstance();
+        _conn = _kit.acquireConnection(ojb.getDefaultConfiguration());
+    }
+
+    public void tearDown() throws Exception
+    {
+        _conn.close();
+        _conn = null;
+        super.tearDown();
+    }
+
+    public static void main(String[] args)
+    {
+        String[] arr = {SwizzleTests.class.getName()};
+        junit.textui.TestRunner.main(arr);
+    }
+
+    public void testSwizzle() throws TransactionException, LockingException, PBFactoryException, PersistenceBrokerException
+    {
+        deleteAllData();
+        createTestData();
+        /**
+        * first get the contract object.
+        */
+        ojb.lookupBroker().clearCache();
+        Transaction tx = _kit.getTransaction(_conn);
+        tx.begin();
+        Criteria crit = new Criteria();
+        crit.addEqualTo("pk", "C" + TIME);
+        Query q = QueryFactory.newQuery(Contract.class, crit);
+        Iterator it = _conn.getIteratorByQuery(q, LockType.WRITE_LOCK);
+        Object retval = null;
+        RelatedToContract r2c = new RelatedToContract();
+        r2c.setPk("R2C" + TIME);
+        r2c.setRelatedValue1("matt");
+        r2c.setRelatedValue2(34);
+        r2c.setRelatedValue3(new Timestamp(TIME));
+        _conn.makePersistent(r2c);
+        while (it.hasNext())
+        {
+            retval = it.next();
+            ((Contract) retval).setRelatedToContract(r2c);
+        }
+        tx.commit();
+        r2c = null;
+        tx = _kit.getTransaction(_conn);
+        tx.begin();
+        crit = new Criteria();
+        crit.addEqualTo("pk", "E" + TIME);
+        q = QueryFactory.newQuery(Effectiveness.class, crit);
+        it = _conn.getIteratorByQuery(q);
+        retval = null;
+        while (it.hasNext())
+        {
+            retval = it.next();
+        }
+        tx.commit();
+        assertTrue("contract object should have a RelatedToContract instance attached", ((Effectiveness) retval).getVersion().getContract().getRelatedToContract() != null);
+    }
+
+	 public void testSwizzle3() throws TransactionException, LockingException, PBFactoryException, PersistenceBrokerException
+    {
+        clearTestData();
+        TestClassA a = generateTestData();
+        Transaction tx = _kit.getTransaction(_conn);
+        tx.begin();
+        _conn.makePersistent(a.getB());
+        _conn.makePersistent(a);
+		TestClassB b = a.getB();
+        tx.commit();
+        /**
+        * clear to start test
+        */
+        _conn.invalidateAll();
+        tx = _kit.getTransaction(_conn);
+        tx.begin();
+        /**
+		 * load B
+		 */
+		Identity oidb = _conn.getIdentity(b);
+        TestClassB b1 = (TestClassB) _conn.getObjectByIdentity(oidb);
+        assertTrue(b1 != null);
+		/**
+		 * load A
+ 		 */
+		Identity oida = _conn.getIdentity(a);
+		TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oida);
+
+		/**
+		 * B, as navigated from A, should be the same as B gotten directly.
+		 */
+		assertTrue(a1.getB().equals(b1));
+        tx.commit();
+
+		/**
+		 * clear
+		 */
+        clearTestData();
+    }
+
+    private void createTestData() throws TransactionException, LockingException
+    {
+        for (int i = 0; i < COUNT; i++)
+        {
+            Transaction tx = _kit.getTransaction(_conn);
+            tx.begin();
+            Contract contract = new Contract();
+            contract.setPk("C" + TIME);
+            contract.setContractValue1("contractvalue1");
+            contract.setContractValue2(1);
+            contract.setContractValue3("contractvalue3");
+            contract.setContractValue4(new Timestamp(TIME));
+            _conn.makePersistent(contract);
+            tx.commit();
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            Version version = new Version();
+            version.setPk("V" + TIME);
+            version.setVersionValue1("versionvalue1");
+            version.setVersionValue2(1);
+            version.setVersionValue3(new Timestamp(TIME));
+            version.setContract(contract);
+            _conn.makePersistent(version);
+            tx.commit();
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            Effectiveness eff = new Effectiveness();
+            eff.setPk("E" + TIME);
+            eff.setEffValue1("effvalue1");
+            eff.setEffValue2(1);
+            eff.setEffValue3(new Timestamp(TIME));
+            eff.setVersion(version);
+            _conn.makePersistent(eff);
+            tx.commit();
+        }
+    }
+
+    public void deleteAllData() throws LockingException
+    {
+        Criteria crit = new Criteria();
+        Query q;
+        Iterator iter;
+        /**
+        * delete effectiveness first
+        */
+        Transaction tx = _kit.getTransaction(_conn);
+        tx.begin();
+        q = QueryFactory.newQuery(Effectiveness.class, crit);
+        iter = _conn.getIteratorByQuery(q);
+        while (iter.hasNext())
+        {
+            _conn.deletePersistent(iter.next());
+        }
+        tx.commit();
+        /**
+        * then version
+        */
+        tx = _kit.getTransaction(_conn);
+        tx.begin();
+        q = QueryFactory.newQuery(Version.class, crit);
+        iter = _conn.getIteratorByQuery(q);
+        while (iter.hasNext())
+        {
+            _conn.deletePersistent(iter.next());
+        }
+        tx.commit();
+        /**
+        * the contract
+        */
+        tx = _kit.getTransaction(_conn);
+        tx.begin();
+        q = QueryFactory.newQuery(Contract.class, crit);
+        iter = _conn.getIteratorByQuery(q);
+        while (iter.hasNext())
+        {
+            _conn.deletePersistent(iter.next());
+        }
+        tx.commit();
+    }
+
+    public void testSwizzle2() throws TransactionException, LockingException, PBFactoryException, PersistenceBrokerException
+    {
+        clearTestData();
+        TestClassA a = generateTestData();
+        Transaction tx = _kit.getTransaction(_conn);
+        tx.begin();
+        _conn.makePersistent(a.getB());
+        _conn.makePersistent(a);
+        tx.commit();
+        /**
+        * clear to start test
+        */
+        _conn.invalidateAll();
+        /**
+        * get A to make it and the related B in cache
+        */
+        tx = _kit.getTransaction(_conn);
+        tx.begin();
+        Identity oid = _conn.getIdentity(a);
+        TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oid);
+        assertTrue(a1.getB() != null);
+        assertTrue(a1.getB().getValue1().equals("hi there"));
+        /**
+        * everything is good, update b
+        */
+        tx.commit();
+
+        /**
+        * now get B and update it, do NOT get it by traversing A
+        */
+        tx = _kit.getTransaction(_conn);
+        tx.begin();
+        Identity boid = _conn.getIdentity(a.getB());
+        TestClassB b1 = (TestClassB) _conn.getObjectByIdentity(boid);
+        assertTrue(b1 != null);
+        assertTrue(b1.getValue1().equals("hi there"));
+        /**
+        * everything is good, update b
+        */
+        _conn.lockForWrite(b1);
+        b1.setValue1("goodbye there");
+        tx.commit();
+        /**
+        * make sure b was updated
+        */
+        tx = _kit.getTransaction(_conn);
+        tx.begin();
+        boid = _conn.getIdentity(a.getB());
+        b1 = (TestClassB) _conn.getObjectByIdentity(boid);
+        assertTrue(b1 != null);
+        assertTrue(b1.getValue1().equals("goodbye there"));
+        tx.commit();
+
+        /**
+        * now get A again and make sure the related B is updated to reflect
+        * the new value.
+        */
+        tx = _kit.getTransaction(_conn);
+        tx.begin();
+        TestClassA a2 = (TestClassA) _conn.getObjectByIdentity(oid);
+        assertTrue(a2.getB() != null);
+        assertTrue(a2.getB().getValue1().equals("goodbye there"));
+        tx.commit();
+        clearTestData();
+    }
+
+    public void testSwizzleNto1() throws Exception
+    {
+        clearTestData();
+        TestClassA a = generateTestData();
+        TestClassB b2 = generateAnotherB();
+        Transaction tx = _kit.getTransaction(_conn);
+        tx.begin();
+        _conn.makePersistent(a.getB());
+        _conn.makePersistent(a);
+        tx.commit();
+        /**
+         * change B
+         */
+        tx = _kit.getTransaction(_conn);
+        tx.begin();
+        Identity oid = _conn.getIdentity(a);
+        TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oid);
+        _conn.makePersistent(b2);
+        a1.setB(b2);
+        tx.commit();
+
+        tx = _kit.getTransaction(_conn);
+        tx.begin();
+        a = (TestClassA) _conn.getObjectByIdentity(oid);
+        assertTrue(a.getB() != null);
+        assertTrue(a.getB().getValue1().equals("value2"));
+        a.setB(null);
+        tx.commit();
+
+        tx = _kit.getTransaction(_conn);
+        tx.begin();
+        a = (TestClassA) _conn.getObjectByIdentity(oid);
+        assertTrue(a.getB() == null);
+        tx.commit();
+    }
+
+    public void testSwizzle1toN() throws Exception
+    {
+        if (ojbSkipKnownIssueProblem("OTM-layer has caching issues"))
+        {
+            return;
+        }
+        clearTestData();
+        Transaction tx = _kit.getTransaction(_conn);
+        tx.begin();
+        ProductGroup pg = new ProductGroup();
+        pg.setId(new Integer(77777));
+        pg.setName("1");
+        _conn.makePersistent(pg);
+        Article article = Article.createInstance();
+        article.setArticleId(new Integer(77777));
+        article.setStock(333);
+        pg.add(article);
+        article.setProductGroup(pg);
+        _conn.makePersistent(article);
+        Identity pgOid = _conn.getIdentity(pg);
+        tx.commit();
+
+        tx = _kit.getTransaction(_conn);
+        tx.begin();
+        pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
+        pg.getAllArticlesInGroup().clear();
+        tx.commit();
+
+        tx = _kit.getTransaction(_conn);
+        tx.begin();
+        pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
+        assertEquals("should be equal", 0, pg.getAllArticlesInGroup().size());
+        tx.commit();
+
+        tx = _kit.getTransaction(_conn);
+        tx.begin();
+        pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
+        pg.getAllArticlesInGroup().add(article);
+        tx.commit();
+
+        tx = _kit.getTransaction(_conn);
+        tx.begin();
+        pg = (ProductGroup) _conn.getObjectByIdentity(pgOid);
+        assertEquals("should be equal", 1, pg.getAllArticlesInGroup().size());
+        tx.commit();
+        clearTestData();
+    }
+
+	 public void testSwizzle4() throws TransactionException, LockingException, PBFactoryException, PersistenceBrokerException
+    {
+        clearTestData();
+        TestClassA a = generateTestData();
+		TestClassB b = a.getB();
+        Transaction tx = _kit.getTransaction(_conn);
+
+        tx.begin();
+        _conn.makePersistent(b);
+        _conn.makePersistent(a);
+        b.setA(a);
+        tx.commit();
+        /**
+        * clear to start test
+        */
+        _conn.invalidateAll();
+        tx = _kit.getTransaction(_conn);
+        tx.begin();
+        /**
+		 * load B
+		 */
+		Identity oidb = _conn.getIdentity(b);
+        TestClassB b1 = (TestClassB) _conn.getObjectByIdentity(oidb);
+		/**
+		 * load A
+ 		 */
+		Identity oida = _conn.getIdentity(a);
+		TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oida);
+		assertTrue(a1 != null);
+		assertTrue(a1.getB().equals(b1));
+		assertTrue(b1.getA().equals(a1));
+        /**
+		 * update B
+		 */
+        a.setValue1("a");
+        _conn.makePersistent(a);
+
+		/**
+		 * B, as navigated from A, should be the same as B gotten directly.
+		 */
+		assertTrue(a1.getValue1().equals(a.getValue1()));
+        tx.commit();
+
+		/**
+		 * clear
+		 */
+        clearTestData();
+    }
+
+    /**
+     * Cache data must be independent of any objects available to used,
+     * otherwise modification of user objects outside transaction will
+     * damage cache data
+     */
+    public void testCacheIndependence() throws Throwable {
+        Transaction tx = null;
+        Collection addresses = this.getAddresses();
+        deleteAddresses(addresses);
+        Identity oid;
+        Address address = new Address("oldCountry", "oldCity", "oldStreet");
+
+        try {
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            _conn.makePersistent(address);
+            oid = _conn.getIdentity(address);
+            tx.commit();
+
+            address.setStreet("newStreet");
+
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            address = (Address) _conn.getObjectByIdentity(oid);
+            assertEquals("Cache was damaged.", "oldStreet", address.getStreet());
+            tx.commit();
+
+            address.setStreet("newStreet");
+
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            address = (Address) _conn.getObjectByIdentity(oid, LockType.WRITE_LOCK);
+            assertEquals("Cache was damaged.", "oldStreet", address.getStreet());
+            tx.commit();
+
+            address.setStreet("newStreet");
+
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            address = (Address) _conn.getObjectByIdentity(oid);
+            assertEquals("Cache was damaged.", "oldStreet", address.getStreet());
+            tx.commit();
+        } catch (Throwable e) {
+            if (tx != null) {
+                try {
+                    tx.rollback();
+                } catch (Throwable ex) {
+                    ex.printStackTrace();
+                }
+            }
+            throw e;
+        }
+    }
+
+    public void testSomethingSimple() throws Throwable {
+        Collection addresses = this.getAddresses();
+		addresses = deleteAddresses(addresses);
+
+        addresses.add(new Address("oldCountry", "oldCity", "oldStreet"));
+
+        addresses = this.updateAddresses(addresses);
+
+        Iterator iter = addresses.iterator();
+        while (iter.hasNext()) {
+            Address address = (Address)iter.next();
+            address.setStreet("newStreet");
+        }
+        addresses = this.updateAddresses(addresses);
+        addresses = this.getAddresses();
+        assertEquals("Collection of addresses must be 1. ", 1, addresses.size());
+        iter = addresses.iterator();
+        while (iter.hasNext()) {
+            Address address = (Address)iter.next();
+            assertEquals("New street not set.", "newStreet",
+                                                address.getStreet());
+        }
+    }
+
+    private Collection getAddresses() throws Throwable {
+        Transaction tx = null;
+        Collection addresses;
+        try {
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+            _conn.invalidateAll();
+            Query q = QueryFactory.newQuery(Address.class, (Criteria)null);
+            addresses = _conn.getCollectionByQuery(q);
+            tx.commit();
+        } catch (Throwable e) {
+            if (tx != null) {
+                try {
+                    tx.rollback();
+                } catch (Throwable ex) {
+                    ex.printStackTrace();
+                }
+            }
+            throw e;
+        }
+        return addresses;
+    }
+
+    private Collection updateAddresses(Collection newAddresses)
+            throws Throwable {
+        Transaction tx = null;
+        Collection oldAddresses;
+        try {
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+
+            Query q = QueryFactory.newQuery(Address.class, (Criteria)null);
+            oldAddresses = _conn.getCollectionByQuery(q);
+
+            Iterator oldAddressesIterator = oldAddresses.iterator();
+            while (oldAddressesIterator.hasNext()) {
+                Address oldAddress = (Address)oldAddressesIterator.next();
+                if (!newAddresses.contains(oldAddress)) {
+                    _conn.deletePersistent(oldAddress);
+                }
+            }
+
+            Iterator newAddressesIterator = newAddresses.iterator();
+            while (newAddressesIterator.hasNext()) {
+                Address newAddress = (Address)newAddressesIterator.next();
+                _conn.makePersistent(newAddress);
+            }
+            tx.commit();
+        } catch (Throwable e) {
+            if (tx != null) {
+                try {
+                    tx.rollback();
+                } catch (Throwable ex) {
+                    ex.printStackTrace();
+                }
+            }
+            throw e;
+        }
+        return newAddresses;
+    }
+
+    private Collection deleteAddresses(Collection oldAddresses)
+            throws Throwable {
+        Transaction tx = null;
+        try {
+            tx = _kit.getTransaction(_conn);
+            tx.begin();
+
+            Iterator oldAddressesIterator = oldAddresses.iterator();
+            while (oldAddressesIterator.hasNext()) {
+                Address oldAddress = (Address)oldAddressesIterator.next();
+                _conn.deletePersistent(oldAddress);
+                oldAddressesIterator.remove();
+            }
+            tx.commit();
+        } catch (Throwable e) {
+            if (tx != null) {
+                try {
+                    tx.rollback();
+                } catch (Throwable ex) {
+                    ex.printStackTrace();
+                }
+            }
+            throw e;
+        }
+        return oldAddresses;
+    }
+
+    private void clearTestData() throws LockingException
+    {
+        TestClassA a = generateTestData();
+        TestClassB b2 = generateAnotherB();
+        Transaction tx = _kit.getTransaction(_conn);
+        tx.begin();
+        Identity oid = _conn.getIdentity(a);
+        Identity oidb = _conn.getIdentity(a.getB());
+        Identity oidb2 = _conn.getIdentity(b2);
+        TestClassA a1 = (TestClassA) _conn.getObjectByIdentity(oid);
+        if (a1 != null)
+        {
+            _conn.deletePersistent(a1);
+        }
+        TestClassB b1 = (TestClassB) _conn.getObjectByIdentity(oidb);
+        if (b1 != null)
+        {
+            _conn.deletePersistent(b1);
+        }
+        b2 = (TestClassB) _conn.getObjectByIdentity(oidb2);
+        if (b2 != null)
+        {
+            _conn.deletePersistent(b2);
+        }
+
+        Article article = Article.createInstance();
+        article.setArticleId(new Integer(77777));
+        ProductGroup pg = new ProductGroup();
+        pg.setId(new Integer(77777));
+        Identity oidArt = _conn.getIdentity(article);
+        Identity oidPG = _conn.getIdentity(pg);
+        article = (Article) _conn.getObjectByIdentity(oidArt);
+        if (article != null)
+        {
+            _conn.deletePersistent(article);
+        }
+        pg = (ProductGroup) _conn.getObjectByIdentity(oidPG);
+        if (pg != null)
+        {
+            _conn.deletePersistent(pg);
+        }
+        tx.commit();
+    }
+
+    private TestClassA generateTestData()
+    {
+        TestClassA tca = new TestClassA();
+        tca.setOid("someoid");
+        tca.setValue1("abc");
+        tca.setValue2("123");
+        tca.setValue3(5);
+        TestClassB tcb = new TestClassB();
+        tcb.setOid("boid");
+        tcb.setValue1("hi there");
+        tca.setB(tcb);
+        return tca;
+    }
+
+    private TestClassB generateAnotherB()
+    {
+        TestClassB tcb = new TestClassB();
+        tcb.setOid("boid2");
+        tcb.setValue1("value2");
+        return tcb;
+    }
+}

Added: db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/TestKit.java
URL: http://svn.apache.org/viewvc/db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/TestKit.java?rev=428990&view=auto
==============================================================================
--- db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/TestKit.java (added)
+++ db/ojb/trunk/proposals/otm/test/org/apache/ojb/otm/TestKit.java Sat Aug  5 05:18:12 2006
@@ -0,0 +1,55 @@
+package org.apache.ojb.otm;
+
+/* Copyright 2002-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import org.apache.ojb.otm.kit.SimpleKit;
+import org.apache.ojb.otm.lock.wait.LockWaitStrategy;
+
+/**
+ * @author <a href="mailto:olegnitz@apache.org">Oleg Nitz</a>
+ *
+ */
+public class TestKit extends SimpleKit
+{
+
+    private static TestKit _instance;
+
+    /**
+     * Constructor for SimpleKit.
+     */
+    protected TestKit()
+    {
+        super();
+    }
+
+    public static TestKit getTestInstance()
+    {
+        if (_instance == null)
+        {
+            _instance = new TestKit();
+        }
+        return _instance;
+    }
+
+    /**
+    * This allows to test different LockWaitStrategies
+    */
+    public void setLockWaitStrategy(LockWaitStrategy lockWaitStrategy)
+    {
+        _lockWaitStrategy = lockWaitStrategy;
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org


Mime
View raw message