Package org.apache.ojb.odmg

Source Code of org.apache.ojb.odmg.BidirectionalAssociationTest

/**
* Author: Matthew Baird
* mattbaird@yahoo.com
*/
package org.apache.ojb.odmg;

import junit.framework.TestCase;
import org.apache.ojb.broker.BidirectionalAssociationObjectA;
import org.apache.ojb.broker.BidirectionalAssociationObjectB;
import org.apache.ojb.broker.ManageableCollection;
import org.apache.ojb.broker.TestHelper;
import org.odmg.Database;
import org.odmg.Implementation;
import org.odmg.ODMGException;
import org.odmg.OQLQuery;
import org.odmg.Transaction;

/**
* tests a bidirectional association A<-->B
* @see org.apache.ojb.broker.BidirectionalAssociationTest for equivalent test in PB API
*/
public class BidirectionalAssociationTest extends TestCase
{
    private static Class CLASS = BidirectionalAssociationTest.class;
    private String databaseName;
    public static void main(String[] args)
    {
        String[] arr = {CLASS.getName()};
        junit.textui.TestRunner.main(arr);
    }

    /**
     * test that we can create 2 objects that have a bidirectional association in ODMG API
     */
    public void testCreateWithUpdate() throws ODMGException
    {
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        db.open(databaseName, Database.OPEN_READ_WRITE);
        Transaction tx = odmg.newTransaction();
        long currentTime = System.currentTimeMillis();

        BidirectionalAssociationObjectA a = new BidirectionalAssociationObjectA();
        a.setPk("A" + currentTime);
        BidirectionalAssociationObjectB b = new BidirectionalAssociationObjectB();
        b.setPk("B" + currentTime);

        tx.begin();
        db.makePersistent(a);
        db.makePersistent(b);
        tx.commit();

        tx.begin();
        tx.lock(a, Transaction.WRITE);
        tx.lock(b, Transaction.WRITE);
        a.setRelatedB(b);
        b.setRelatedA(a);
        tx.commit();

         /**
         * now make sure they are in db, A first, then B
         */
        tx.begin();
        OQLQuery query = odmg.newOQLQuery();
        int i = 0;
        query.create("select bidirectionalAssociationObjectA from " + BidirectionalAssociationObjectA.class.getName() + " where pk=$1");
        query.bind("A"+currentTime);
        ManageableCollection all = (ManageableCollection) query.execute();
        java.util.Iterator it = all.ojbIterator();
        while (it.hasNext())
        {
            i++;
            a = (BidirectionalAssociationObjectA) it.next();
            if (a.getRelatedB() == null)
                fail("a should have had a related b");
        }
        if (i > 1)
            fail("should have found only one bidirectionalAssociationObjectA, instead found: " + i);

        query = odmg.newOQLQuery();
        i = 0;
        query.create("select bidirectionalAssociationObjectB from " + BidirectionalAssociationObjectB.class.getName() + " where pk=$1");
        query.bind("B"+currentTime);
        all = (ManageableCollection) query.execute();
        it = all.ojbIterator();
        while (it.hasNext())
        {
            i++;
            b = (BidirectionalAssociationObjectB) it.next();
            if (b.getRelatedA() == null)
                fail("b should have had a related a");

        }
        if (i > 1)
            fail("should have found only one bidirectionalAssociationObjectB, instead found: " + i);
        tx.commit();
    }

    /**
     * this test doesn't work as OJB won't do the insert then execute the update.
     * @throws ODMGException
     */
    public void NOTWORKINGtestCreateWithoutUpdate() throws ODMGException
    {
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        db.open(databaseName, Database.OPEN_READ_WRITE);
        Transaction tx = odmg.newTransaction();
        long currentTime = System.currentTimeMillis();
        BidirectionalAssociationObjectA a = new BidirectionalAssociationObjectA();
        a.setPk("A" + currentTime);
        BidirectionalAssociationObjectB b = new BidirectionalAssociationObjectB();
        b.setPk("B" + currentTime);

        tx.begin();
        b.setRelatedA(a);
        a.setRelatedB(b);
        db.makePersistent(a);
        db.makePersistent(b);
        tx.commit();

        /**
         * now make sure they are in db, A first, then B
         */
        tx.begin();
        OQLQuery query = odmg.newOQLQuery();
        int i = 0;
        query.create("select bidirectionalAssociationObjectA from " + BidirectionalAssociationObjectA.class.getName() + " where pk=$1");
        query.bind("A"+currentTime);
         ManageableCollection all = (ManageableCollection) query.execute();
        java.util.Iterator it = all.ojbIterator();
        while (it.hasNext())
        {
            i++;
            it.next();
        }
        if (i > 1)
            fail("should have found only one bidirectionalAssociationObjectA, instead found: " + i);

        query = odmg.newOQLQuery();
        i = 0;
        query.create("select bidirectionalAssociationObjectB from " + BidirectionalAssociationObjectB.class.getName() + " where pk=$1");
        query.bind("B"+currentTime);
        all = (ManageableCollection) query.execute();
        it = all.ojbIterator();
        while (it.hasNext())
        {
            i++;
            it.next();
        }
        if (i > 1)
            fail("should have found only one bidirectionalAssociationObjectB, instead found: " + i);

    }

    /**
     * no clue why this isn't working.
     * @throws Exception
     */
    public void NOTWORKINGtestGetA() throws Exception
    {
        /**
         * create at least one A/B combo
         */
        testDeleteA();
        testDeleteB();
        testCreateWithUpdate();
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        db.open(databaseName, Database.OPEN_READ_WRITE);
        OQLQuery query = odmg.newOQLQuery();
        int i = 0;
        query.create("select bidirectionalAssociationObjectA from " + BidirectionalAssociationObjectA.class.getName());
        Transaction tx = odmg.newTransaction();
        tx.begin();
        ManageableCollection all = (ManageableCollection) query.execute();
        java.util.Iterator it = all.ojbIterator();
        BidirectionalAssociationObjectA temp = null;
        while (it.hasNext())
        {
            temp = (BidirectionalAssociationObjectA) it.next();
            if (temp.getRelatedB() == null)
                fail("should have relatedB");
            i++;
        }
        tx.commit();
        if (i == 0)
            fail("Should have found at least 1  bidirectionalAssociationObjectA object");
    }

    public void testGetB() throws Exception
    {
        /**
         * create at least one A/B combo
         */
        testDeleteA();
        testDeleteB();
        testCreateWithUpdate();
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        db.open(databaseName, Database.OPEN_READ_WRITE);
        OQLQuery query = odmg.newOQLQuery();
        int i = 0;
        query.create("select bidirectionalAssociationObjectB from " + BidirectionalAssociationObjectB.class.getName());
        Transaction tx = odmg.newTransaction();
        tx.begin();
        ManageableCollection all = (ManageableCollection) query.execute();
        java.util.Iterator it = all.ojbIterator();
        BidirectionalAssociationObjectB temp = null;
        while (it.hasNext())
        {
            temp = (BidirectionalAssociationObjectB) it.next();
            if (temp.getRelatedA() == null)
                fail("should have relatedA");
            i++;
        }
        tx.commit();
        if (i == 0)
            fail("Should have found at least 1  bidirectionalAssociationObjectA object");
    }

    /**
     * nothing to update yet.
     */
    public void testUpdate()
    {

    }

    /**
     * test deleting an object participating in a bidirectional associative relationship. Will throw if it can't delete.
     * @throws Exception
     */
    public void testDeleteA() throws Exception
    {
        BidirectionalAssociationObjectA a;
        BidirectionalAssociationObjectB b;
        /**
         * create at least one A/B
         */
        testCreateWithUpdate();

        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        db.open(databaseName, Database.OPEN_READ_WRITE);
        OQLQuery query = odmg.newOQLQuery();
        query.create("select bidirectionalAssociationObjectA from " + BidirectionalAssociationObjectA.class.getName());
        Transaction tx = odmg.newTransaction();
        tx.begin();
        ManageableCollection all = (ManageableCollection) query.execute();
        java.util.Iterator it = all.ojbIterator();

        while (it.hasNext())
        {
            a = (BidirectionalAssociationObjectA)it.next();
            b = a.getRelatedB();
            if (b != null)
            {
                tx.lock(b, Transaction.WRITE);
                b.setRelatedA(null);   // break relationship to avoid ri violation
            }
            db.deletePersistent(a);
        }

        tx.commit();
    }

    /**
     * test deleting an object participating in a bidirectional associative relationship. Will throw if it can't delete.
     * @throws Exception
     */
    public void testDeleteB() throws Exception
    {
        BidirectionalAssociationObjectA a;
        BidirectionalAssociationObjectB b;

        /**
         * create at least one A/B
         */
        testCreateWithUpdate();
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        db.open(databaseName, Database.OPEN_READ_WRITE);
        OQLQuery query = odmg.newOQLQuery();
        query.create("select bidirectionalAssociationObjectB from " + BidirectionalAssociationObjectB.class.getName());
        Transaction tx = odmg.newTransaction();
        tx.begin();
        ManageableCollection all = (ManageableCollection) query.execute();
        java.util.Iterator it = all.ojbIterator();

        while (it.hasNext())
        {
            b = (BidirectionalAssociationObjectB)it.next();
            a = b.getRelatedA();
            if (a != null)
            {
                tx.lock(a, Transaction.WRITE);
                a.setRelatedB(null);    // break relationship to avoid ri violation
            }
            db.deletePersistent(b);
        }

        tx.commit();
    }

    /**
     * Insert the method's description here.
     * Creation date: (24.12.2000 00:33:40)
     */
    public BidirectionalAssociationTest(String name)
    {
        super(name);
    }

    public void setUp()
    {
        databaseName = TestHelper.DEF_DATABASE_NAME;
    }
    /**
     * Insert the method's description here.
     * Creation date: (06.12.2000 21:59:14)
     */
    public void tearDown()
    {
        try
        {
            testDeleteA();
        }
        catch (Exception e)
        {
            e.printStackTrace()//To change body of catch statement use Options | File Templates.
        }
        try
        {
            testDeleteB();
        }
        catch (Exception e)
        {
            e.printStackTrace()//To change body of catch statement use Options | File Templates.
        }
    }
}
TOP

Related Classes of org.apache.ojb.odmg.BidirectionalAssociationTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.