Package org.apache.ojb.odmg

Source Code of org.apache.ojb.odmg.DListTest$DListObject

package org.apache.ojb.odmg;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import junit.framework.TestCase;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.ojb.broker.TestHelper;
import org.odmg.DBag;
import org.odmg.DList;
import org.odmg.DSet;
import org.odmg.Database;
import org.odmg.Implementation;
import org.odmg.OQLQuery;
import org.odmg.Transaction;

/**
* Demo Application that shows basic concepts for Applications using the OJB ODMG
* implementation as an transactional object server.
*/
public class DListTest extends TestCase
{
    public static void main(String[] args)
    {
        String[] arr = {DListTest.class.getName()};
        junit.textui.TestRunner.main(arr);
    }

    private String databaseName;

    public DListTest(String name)

    {
        super(name);
    }

    protected DListObject createObject(String name) throws Exception
    {
        DListObject obj = new DListObject();
        obj.setName(name);
        obj.setRandomName("rnd_"+((int)(Math.random()*1000)));

        return obj;
    }

    public void setUp()
    {
        databaseName = TestHelper.DEF_DATABASE_NAME;
    }

    public void tearDown()
    {
        databaseName = null;
    }

    public void testAddingLockupWithTx() throws Exception
    {
        // create a unique name:
        final String name = "testAdding_" + System.currentTimeMillis();

        // 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();
        // create DList and bound by name
        DList list = odmg.newDList();
        db.bind(list, name);
        tx.commit();
        tx = odmg.newTransaction();
        tx.begin();
        Object obj = db.lookup(name);
        tx.commit();
        assertNotNull("binded DList not found", obj);

        tx = odmg.newTransaction();
        tx.begin();
        // add objects to list
        for (int i = 0; i < 5; i++)
        {
            DListObject a = createObject(name);
            list.add(a);
        }
        tx.commit();

        // check current list
        Iterator iter = list.iterator();
        while (iter.hasNext())
        {
            DListObject a = (DListObject) iter.next();
            assertNotNull(a);
        }

        tx = odmg.newTransaction();
        tx.begin();
        ((TransactionExt) odmg.currentTransaction()).getBroker().clearCache();

        // lookup list and check entries
        DList lookedUp = (DList) db.lookup(name);
        assertNotNull("binded DList not found", lookedUp);

        //System.out.println("sequence of items in lookedup list:");
        iter = lookedUp.iterator();
        Iterator iter1 = list.iterator();
        while (iter.hasNext())
        {
            DListObject a = (DListObject) iter.next();
            DListObject b = (DListObject) iter1.next();
            assertNotNull(a);
            assertNotNull(b);
            assertEquals(a.getId(), b.getId());
        }
        tx.commit();

        // add new entries to list
        tx.begin();
        for (int i = 0; i < 3; i++)
        {
            DListObject a = createObject(name + "_new_entry");
            list.add(a);
        }
        tx.commit();

        tx = odmg.newTransaction();
        tx.begin();
        ((TransactionExt) odmg.currentTransaction()).getBroker().clearCache();
        lookedUp = (DList) db.lookup(name);
        iter = lookedUp.iterator();
        iter1 = list.iterator();
        assertEquals("Wrong number of DListEntry found", 8, list.size());
        while (iter.hasNext())
        {
            DListObject a = (DListObject) iter.next();
            DListObject b = (DListObject) iter1.next();
            assertNotNull(a);
            assertNotNull(b);
            assertEquals(a.getId(), b.getId());
        }
        tx.commit();
        assertNotNull("binded DList not found", lookedUp);
    }

    public void testReadAndStore() throws Exception
    {
        // create a unique name:
        final String name = "testReadAndStore_" + System.currentTimeMillis();

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

        // create test objects
        Transaction tx = odmg.newTransaction();
        tx.begin();
        // add objects to list
        for (int i = 0; i < 5; i++)
        {
            tx.lock(createObject(name), Transaction.WRITE);
        }
        tx.commit();

        tx.begin();
        // query test objects
        OQLQuery q = odmg.newOQLQuery();
        q.create("select all from "+DListObject.class.getName()+" where name=$1");
        q.bind(name);
        Collection ret = (Collection) q.execute();
        // check result list size
        assertEquals(5, ret.size());
        // do read lock
        for (Iterator it = ret.iterator(); it.hasNext(); )
        {
            tx.lock(it.next(), Transaction.READ);
        }
        // create new list for results
        ArrayList result = new ArrayList();
        result.addAll(ret);
        tx.commit();
    }

    public void testIterateWithoutTx() throws Exception
    {
        // create a unique name:
        final String name = "testAdding_" + System.currentTimeMillis();

        // get facade instance
        Implementation odmg = OJB.getInstance();
        Database db = odmg.newDatabase();
        //open database
        db.open(databaseName, Database.OPEN_READ_WRITE);
        // get DList and fill with objects
        DList list = odmg.newDList();
        Transaction tx = odmg.newTransaction();
        tx.begin();
        for (int i = 0; i < 5; i++)
        {
            DListObject a = createObject(name);
            list.add(a);
        }
        // bind the new list
        db.bind(list, name);
        tx.commit();

        tx = odmg.newTransaction();
        tx.begin();
        Object obj = db.lookup(name);
        tx.commit();
        assertNotNull("binded DList not found", obj);

        // iterate list
        Iterator iter = list.iterator();
        while (iter.hasNext())
        {
            DListObject a = (DListObject) iter.next();
            assertNotNull(a);
        }
        assertEquals(5, list.size());

        tx = odmg.newTransaction();
        tx.begin();
        ((TransactionExt) odmg.currentTransaction()).getBroker().clearCache();
        DList lookedUp = (DList) db.lookup(name);
        tx.commit();
        assertNotNull("binded DList not found", lookedUp);

        //System.out.println("sequence of items in lookedup list:");
        iter = lookedUp.iterator();
        Iterator iter1 = list.iterator();
        while (iter.hasNext())
        {
            DListObject a = (DListObject) iter.next();
            DListObject b = (DListObject) iter1.next();
            assertNotNull(a);
            assertNotNull(b);
            assertEquals(a.getId(), b.getId());
        }
    }

    /**
     * this test checks if removing item from DList works
     */
    public void testRemoving() throws Exception
    {
        // create a unique name:
        String name = "testRemoving_" + System.currentTimeMillis();

        // 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();
        DList list = odmg.newDList();
        // bind the list to the name:
        db.bind(list, name);

        for (int i = 0; i < 5; i++)
        {
            DListObject a = createObject(name);
            list.add(a);
        }
        assertEquals(5, list.size());
        tx.commit();

        // delete two items
        tx = odmg.newTransaction();
        tx.begin();
        ((HasBroker) odmg.currentTransaction()).getBroker().clearCache();
        DList lookedUp = (DList) db.lookup(name);
        assertNotNull("database lookup does not find the named DList", lookedUp);
        assertEquals("Wrong number of list entries", 5, lookedUp.size());
        lookedUp.remove(2);
        lookedUp.remove(1);
        tx.commit();

        // check if deletion was successful
        tx = odmg.newTransaction();
        tx.begin();
        ((HasBroker) odmg.currentTransaction()).getBroker().clearCache();
        lookedUp = (DList) db.lookup(name);
        tx.commit();

        assertEquals(3, lookedUp.size());
    }


    public void testAddingWithIndex() throws Exception
    {
        // create a unique name:
        String name = "testAddingWithIndex_" + System.currentTimeMillis();

        // 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();
        DList list = odmg.newDList();
        db.bind(list, name);
        tx.commit();

        tx = odmg.newTransaction();
        tx.begin();
        for (int i = 0; i < 5; i++)
        {
            DListObject a = createObject(name);
            list.add(a);
        }

        list.add(2, createObject(name+"_pos2"));
        list.add(0, createObject(name+"_pos0"));
        list.add(7, createObject(name+"_pos7"));
        tx.commit();

        tx.begin();
        ((TransactionImpl) tx).getBroker().clearCache();
        // System.out.println("list: " + list);
        // System.out.println("lookup list: " + db.lookup(name));
        tx.commit();

        //System.out.println("sequence of items in list:");
        Iterator iter = list.iterator();
        DListObject a;
        while (iter.hasNext())
        {
            a = (DListObject) iter.next();
            assertNotNull(a);
            //System.out.print(a.getArticleId() + ", ");
        }


        tx = odmg.newTransaction();
        tx.begin();
        ((TransactionImpl) tx).getBroker().clearCache();
        DList lookedUp = (DList) db.lookup(name);
        // System.out.println("lookup list: " + lookedUp);
        assertNotNull("database lookup does not find DList", lookedUp);
        assertEquals(8, lookedUp.size());
        iter = lookedUp.iterator();
        while (iter.hasNext())
        {
            a = (DListObject) iter.next();
        }
        tx.commit();
    }

    public void testDBag() throws Exception
    {
        String name = "testDBag_" + System.currentTimeMillis();
        // 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();
        DBag bag1 = odmg.newDBag();
        DBag bag2 = odmg.newDBag();
        DListObject a, b, c, d, e;
        a = createObject(name);
        b = createObject(name);
        c = createObject(name);
        d = createObject(name);
        e = createObject(name);
        bag1.add(a);
        bag1.add(b);
        bag1.add(c);
        bag2.add(b);
        bag2.add(c);
        bag2.add(d);
        bag2.add(e);
        DBag bag3 = bag1.difference(bag2);
        assertEquals("should contain only 1 element", 1, bag3.size());

        bag3 = bag1.intersection(bag2);
        assertEquals("should contain two elements", 2, bag3.size());

        tx.commit();
    }

    public void testDSet() throws Exception
    {
        String name = "testDSet_" + System.currentTimeMillis();
        String set_1 = "set_1_" + System.currentTimeMillis();
        String set_2 = "set_2_" + System.currentTimeMillis();
        // 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();

        DListObject a, b, c, d, e;
        a = createObject(name);
        b = createObject(name);
        c = createObject(name);
        d = createObject(name);
        e = createObject(name);

        DSet set1 = odmg.newDSet();
        DSet set2 = odmg.newDSet();

        set1.add(a);
        set1.add(b);
        set1.add(c);

        set2.add(b);
        set2.add(c);
        set2.add(d);
        set2.add(e);

        db.bind(set1, set_1);
        db.bind(set2, set_2);
        tx.commit();

        // low lookup both sets
        tx = odmg.newTransaction();
        tx.begin();
        ((HasBroker) tx).getBroker().clearCache();
        DSet set1a = (DSet) db.lookup(set_1);
        DSet set2a = (DSet) db.lookup(set_2);

        // check looked up sets
        assertTrue(set1a.containsAll(set1));
        assertTrue(set2a.containsAll(set2));

        // now TestThreadsNLocks set operations:
        DSet set3 = set1.difference(set2);
        assertEquals(1, set3.size());

        set3 = set1.intersection(set2);
        assertEquals(2, set3.size());

        set3 = set1.union(set2);
        assertEquals(5, set3.size());

        assertTrue(set1.properSubsetOf(set3));
        assertTrue(set2.properSubsetOf(set3));

        assertTrue(set3.properSupersetOf(set1));
        assertTrue(set3.properSupersetOf(set2));

        assertTrue(!set1.properSubsetOf(set2));

        tx.commit();
    }

    public static class DListObject
    {
        Integer id;
        String name;
        String randomName;

        public DListObject()
        {
        }

        public boolean equals(Object obj)
        {
            if(obj instanceof DListObject)
            {
                String all = id + name+ randomName;
                DListObject target = ((DListObject)obj);
                return all != null
                        ? all.equals(target.getId()+target.getName()+target.getRandomName())
                        : target.getId()+target.getName()+target.getRandomName() == null;
            }
            else
            {
                return false;
            }
        }

        public String toString()
        {
            ToStringBuilder buf = new ToStringBuilder(this);
            buf.append("id", id);
            buf.append("name", name);
            buf.append("randonName", randomName);
            return buf.toString();
        }

        public Integer getId()
        {
            return id;
        }

        public void setId(Integer id)
        {
            this.id = id;
        }

        public String getName()
        {
            return name;
        }

        public void setName(String name)
        {
            this.name = name;
        }

        public String getRandomName()
        {
            return randomName;
        }

        public void setRandomName(String randomName)
        {
            this.randomName = randomName;
        }
    }
}
TOP

Related Classes of org.apache.ojb.odmg.DListTest$DListObject

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.