Package org.apache.ojb.odmg

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

package org.apache.ojb.odmg;


import junit.framework.TestCase;
import org.apache.ojb.broker.Identity;
import org.apache.ojb.broker.TestHelper;
import org.apache.ojb.odmg.shared.Article;
import org.odmg.DList;
import org.odmg.Database;
import org.odmg.Implementation;
import org.odmg.ODMGException;
import org.odmg.ObjectNameNotFoundException;
import org.odmg.ObjectNameNotUniqueException;
import org.odmg.Transaction;

/**
* Demo Application that shows basic concepts for Applications using the OJB ODMG
* implementation as an transactional object server.
*
* @version $Id: NamedRootsTest.java,v 1.1 2004/06/22 18:16:22 arminw Exp $
*/
public class NamedRootsTest extends TestCase
{
    public static void main(String[] args)
    {
        String[] arr = {CLASS.getName()};
        junit.textui.TestRunner.main(arr);
    }

    private static Class CLASS = NamedRootsTest.class;
    private String databaseName;

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

    /**
     * Insert the method's description here.
     * Creation date: (06.12.2000 21:59:14)
     */
    private void clearNRM()
    {
        try
        {
            /*
            org.apache.ojb.broker.metadata.ClassDescriptor cld = ojb.broker.metadata.DescriptorRepository.getInstance().getDescriptorFor(NamedRootsEntry.class);

            java.sql.Statement stmt = org.apache.ojb.broker.accesslayer.StatementManager.getGenericStatement(cld);
            String nrmTable = cld.getTableName();
            String scheme = cld.getScheme();
            if (scheme != null)
            {  nrmTable = scheme + "." + nrmTable;
            }

            stmt.executeUpdate("DELETE FROM " + nrmTable);
             */
        }
        catch (Throwable t)
        {
        }
    }

    /**
     * Insert the method's description here.
     * Creation date: (06.12.2000 21:58:53)
     */
    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()
    {
        databaseName = null;
        clearNRM();
    }

    private static int count = 0;
    private Article createArticle()
    {
        Article example = new Article();
        example.setArticleId(++count);
        example.setArticleName("named roots article No. "+(count));
        example.setProductGroupId(1);

        return example;
    }

    public void testBind() throws Exception
    {
        String bindingName = "binding_for_testBind";

        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        //open database
        db.open(databaseName, Database.OPEN_READ_WRITE);
        // clean up roots map
        clearNRM();

        Article example = createArticle();

        Transaction tx = odmg.newTransaction();
        tx.begin();
        try
        {
            db.bind(example, bindingName);
            Article value = (Article) db.lookup(bindingName);
            assertTrue("Could not lookup object for binding name: "+bindingName, value != null);
            tx.commit();
        }
        catch (ObjectNameNotFoundException ex)
        {
            // tx.abort();
            fail("name " + bindingName + " should not be unknown. "+ex.getMessage());
        }
        catch (ObjectNameNotUniqueException e)
        {
            // tx.abort();
            fail("should not have happened: " + e.getMessage());
        }

        try
        {
            tx.begin();
            db.bind(example, bindingName);
            tx.commit();
            fail("We expected a ObjectNameNotUniqueException, but was not thrown");
        }
        catch (ObjectNameNotUniqueException ex)
        {
            // we wait for this exception
            assertTrue(true);
            tx.abort();
        }

        try
        {
            tx.begin();
            db.unbind(bindingName);
            tx.commit();
        }
        catch (ObjectNameNotFoundException ex)
        {
            fail("Can't unbind " + bindingName + ": "+ex.getMessage());
        }

        db.close();
    }

    public void testDoubleBindInOneTx() throws Exception
    {
        String bindingName = "binding_for_testDoubleBindInOneTx";

        DList foundList = null;
        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        //open database
        db.open(databaseName, Database.OPEN_READ_WRITE);
        Transaction tx = odmg.newTransaction();
        tx.begin();
        db.bind(odmg.newDList(), bindingName);

        foundList = (DList)db.lookup(bindingName);
        assertTrue("Could not found bound DList", foundList != null);
        foundList = null;

        db.unbind(bindingName);
        try
        {
            foundList = (DList) db.lookup(bindingName);
            fail("Found unbound DList");
        }
        catch (ObjectNameNotFoundException ex)
        {
        }

        db.bind(odmg.newDList(), bindingName);
        try
        {
            foundList = (DList) db.lookup(bindingName);
        }
        catch (ObjectNameNotFoundException ex)
        {
            fail("Could not found bound DList, binding name was: "+bindingName);
        }
        foundList = null;
        tx.commit();

        tx = odmg.newTransaction();
        tx.begin();
        try
        {
            DList newList = (DList)db.lookup(bindingName);
            db.unbind(bindingName);
        }
        catch (ObjectNameNotFoundException ex)
        {
            fail("Could not found bound DList in new tx");
        }
        tx.commit();

        db.close();
    }


    public void testLookup() throws Exception
    {
        String bindingName = "binding_for_testLookup";
        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        db.open(databaseName, Database.OPEN_READ_WRITE);

        // clear named roots.
        clearNRM();
        Transaction tx = odmg.newTransaction();
        //bind object to name
        tx.begin();
        Article example = createArticle();
        Identity oid = new Identity(example, ((HasBroker)tx).getBroker());

        try
        {
            db.bind(example, bindingName);
            tx.commit();
        }
        catch (ObjectNameNotUniqueException ex)
        {
            tx.abort();
            fail(ex.getMessage());
        }

        // TestThreadsNLocks look up
        Article lookedUp1 = null;
        tx = odmg.newTransaction();
        tx.begin();

        try
        {
            // lookup by name binding
            lookedUp1 = (Article) db.lookup(bindingName);
        }
        catch (ObjectNameNotFoundException ex)
        {
            fail("lookup by name: " + bindingName + " should not be unknown");
        }

        tx.commit();

        // looking up object by OID should return same Object as by name
        assertEquals("lookups should return identical object", example, lookedUp1);

        try
        {
            tx.begin();
            db.unbind(bindingName);
            tx.commit();
        }
        catch (ObjectNameNotFoundException ex)
        {
            fail("Can't unbind " + bindingName + ": "+ex.getMessage());
        }

        db.close();
    }

    public void testUnBind() throws Exception
    {
        String name = "binding_for_testUnBind";
        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();

        //open database
        try
        {
            db.open(databaseName, Database.OPEN_READ_WRITE);
        }
        catch (ODMGException ex)
        {
            fail("ODMGException: " + ex.getMessage());
        }

        // clear named roots.
        clearNRM();

        Transaction tx = odmg.newTransaction();
        // 1. perform binding
        tx.begin();

        Article example = createArticle();

        try
        {
            db.bind(example, name);
            tx.commit();
        }
        catch (ObjectNameNotUniqueException ex)
        {
            tx.abort();
            fail(ex.getMessage());
        }

        // 2. perform unbind
        tx = odmg.newTransaction();
        tx.begin();
        try
        {
            db.unbind(name);
            tx.commit();
        }
        catch (ObjectNameNotFoundException ex)
        {
            tx.abort();
            fail("name " + name + "should be known");
        }

        // 3. check if name is really unknown now
        tx = odmg.newTransaction();
        tx.begin();
        try
        {
            Article value = (Article) db.lookup(name);
        }
        catch (ObjectNameNotFoundException ex)
        {
            // OK !
            tx.commit();
            // close database
            try
            {
                db.close();
            }
            catch (ODMGException e)
            {
                fail("ODMGException: " + e.getMessage());
            }

            return;
        }

        // NOT OK!
        tx.abort();
        db.close();
        fail("name " + name + " should not be known after unbind");

    }
}
TOP

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

TOP
Copyright © 2015 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.