Package org.apache.ojb.odmg

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

package org.apache.ojb.odmg;

//#ifdef JDK13
import java.lang.reflect.Proxy;
//#else
/*
import net.sf.cglib.proxy.Proxy;
*/
//#endif

import java.util.List;
import junit.framework.TestCase;
import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.TestHelper;
import org.apache.ojb.odmg.shared.TestClassA;
import org.apache.ojb.odmg.shared.TestClassAWithBProxy;
import org.apache.ojb.odmg.shared.TestClassB;
import org.apache.ojb.odmg.shared.TestClassBProxy;
import org.apache.ojb.odmg.shared.TestClassBProxyI;
import org.odmg.ODMGException;
import org.odmg.OQLQuery;
import org.odmg.Transaction;


public class OneToOneTest extends TestCase
{
  private static Class CLASS = OneToOneTest.class;
  private static final String DATABASE_NAME = TestHelper.DEF_DATABASE_NAME;
  private org.odmg.Implementation odmg;
  private org.odmg.Database db;

  TestClassA m_a;
  TestClassB m_b;

    public static void main(String[] args)
    {
        String[] arr = {CLASS.getName()};
        junit.textui.TestRunner.main(arr);
    }


    /**
     * Constructor for OneToOneTest.
     * @param arg0
     */
    public OneToOneTest(String arg0)
    {
        super(arg0);
    }

    protected void setUp()
    {
      // get facade instance
        odmg = org.apache.ojb.odmg.OJB.getInstance();
        db = odmg.newDatabase();

        //open database
        try
        {
            db.open(DATABASE_NAME, org.odmg.Database.OPEN_READ_WRITE);

        }
        catch (org.odmg.ODMGException ex)
        {
            ex.printStackTrace();
        }

      m_a = new org.apache.ojb.odmg.shared.TestClassA();
      m_b = new org.apache.ojb.odmg.shared.TestClassB();

      // init a
      m_a.setValue1("A.One");
      m_a.setValue2("B.Two");
      m_a.setValue3(3);
      m_a.setB(m_b);

      // init b
      m_b.setValue1("B.One");
      m_b.setA(m_a);
    }

    public void testSave()
    {
        org.odmg.Transaction tx = odmg.newTransaction();
        tx.begin();
        tx.lock(m_a, Transaction.WRITE);
        tx.lock(m_b, Transaction.WRITE);
        tx.commit();

        assertTrue(m_a.getOid() != null);
        assertTrue(m_b.getOid() != null);
    }

    public void testRead()
    {

    }
    /**
     * This method tests the correct assignment of a foreign
     * key field in case that the referenced object is a
     * dynamic proxy
     */
    public void testFKAssignForProxy()
    {
        try
        {
            Transaction tx = odmg.newTransaction();
            tx.begin();

            //create a TestClassBProxy and persist it
            //so that when loading it again we will
            //get a dynamic proxy object
            TestClassBProxy b = new TestClassBProxy();
            new Identity(b,((HasBroker)tx).getBroker());


            tx.lock(b, Transaction.WRITE);
            tx.commit();
            String bOid = b.getOid();

            //reload the object created in the previous step
            tx = odmg.newTransaction();
            tx.begin();
            OQLQuery query = odmg.newOQLQuery();
            query.create("select bproxies from " + TestClassBProxy.class.getName() +
            " where oid = $1");
            query.bind(bOid);
            List bList = (List) query.execute();
            assertEquals(1, bList.size());

            TestClassBProxyI bI = (TestClassBProxyI) bList.get(0);

            //bI should now be a dynamic proxy
            assertTrue(bI instanceof Proxy);

            TestClassAWithBProxy a = new TestClassAWithBProxy();
            a.setBProxy(bI);
            tx.lock(a, Transaction.WRITE);
            tx.commit();

            //on commit the foreign key in "a" should have been set to
            //bOid
            String aBOid = a.getBoid();

            assertEquals("foreign key should match", bOid, aBOid);

        } catch(ODMGException ex) {
          fail("ODMGException" + ex);
        }
    }
}
TOP

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

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.