db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From arm...@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb repository_junit.xml
Date Fri, 13 Feb 2004 18:58:24 GMT
arminw      2004/02/13 10:58:24

  Modified:    src/schema ojbtest-schema.xml
               src/test/org/apache/ojb/broker BatchModeTest.java
               src/test/org/apache/ojb repository_junit.xml
  Log:
  add more tests for batch mode
  
  Revision  Changes    Path
  1.63      +19 -0     db-ojb/src/schema/ojbtest-schema.xml
  
  Index: ojbtest-schema.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/schema/ojbtest-schema.xml,v
  retrieving revision 1.62
  retrieving revision 1.63
  diff -u -r1.62 -r1.63
  --- ojbtest-schema.xml	4 Feb 2004 00:25:27 -0000	1.62
  +++ ojbtest-schema.xml	13 Feb 2004 18:58:24 -0000	1.63
  @@ -680,12 +680,31 @@
   
   
       <!-- =================================================== -->
  +    <!-- PB - BatchModeTest tables                           -->
  +    <!-- =================================================== -->
  +    <table name="BATCH_MAIN_OBJ">
  +        <column name="OBJ_ID" required="true" primaryKey="true" type="BIGINT"/>
  +        <column name="NAME" type="VARCHAR" size="100"/>
  +    </table>
  +
  +    <table name="BATCH_SUB_OBJ">
  +        <column name="OBJ_ID" required="true" primaryKey="true" type="BIGINT"/>
  +        <column name="FK_MAIN_ID" type="BIGINT"/>
  +        <column name="NAME" type="VARCHAR" size="100"/>
  +        <foreign-key foreignTable="BATCH_MAIN_OBJ">
  +            <reference local="FK_MAIN_ID" foreign="OBJ_ID"/>
  +        </foreign-key>
  +    </table>
  +
  +
  +    <!-- =================================================== -->
       <!-- ObjectCache tests                                   -->
       <!-- =================================================== -->
       <table name="CACHE_TEST">
           <column name="OBJ_ID" required="true" primaryKey="true" type="INTEGER"/>
           <column name="NAME" type="VARCHAR" size="100"/>
       </table>
  +
   
       <!-- =================================================== -->
       <!-- Reference test tables                               -->
  
  
  
  1.7       +248 -25   db-ojb/src/test/org/apache/ojb/broker/BatchModeTest.java
  
  Index: BatchModeTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/BatchModeTest.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- BatchModeTest.java	18 Jan 2004 16:42:21 -0000	1.6
  +++ BatchModeTest.java	13 Feb 2004 18:58:24 -0000	1.7
  @@ -7,18 +7,15 @@
   
   package org.apache.ojb.broker;
   
  -import junit.framework.Test;
  -import junit.framework.TestCase;
  -import junit.framework.TestSuite;
  +import java.util.ArrayList;
  +import java.util.Collection;
  +import java.util.Iterator;
   
  +import junit.framework.TestCase;
   import org.apache.ojb.broker.accesslayer.ConnectionManagerIF;
  -import org.apache.ojb.broker.util.logging.LoggerFactory;
  -import org.apache.ojb.broker.query.QueryFactory;
  -import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.query.Criteria;
  -
  -import java.util.Collection;
  -import java.util.Iterator;
  +import org.apache.ojb.broker.query.Query;
  +import org.apache.ojb.broker.query.QueryFactory;
   
   /**
    * @author Oleg Nitz
  @@ -27,6 +24,7 @@
   {
   
       PersistenceBroker broker;
  +    private static long timestamp = System.currentTimeMillis();
   
       public BatchModeTest(String testName)
       {
  @@ -35,14 +33,7 @@
   
       public void setUp()
       {
  -        try
  -        {
  -            broker = PersistenceBrokerFactory.defaultPersistenceBroker();
  -        }
  -        catch (Throwable t)
  -        {
  -            LoggerFactory.getBootLogger().error("error getting broker", t);
  -        }
  +        broker = PersistenceBrokerFactory.defaultPersistenceBroker();
       }
   
       public void tearDown()
  @@ -50,22 +41,118 @@
           try
           {
               broker.clearCache();
  -            broker.close();
           }
  -        catch (Throwable t)
  +        catch(Exception e)
           {
           }
  +        finally
  +        {
  +            if(broker != null) broker.close();
  +        }
  +    }
  +
  +    private long getNewId()
  +    {
  +        return timestamp++;
       }
   
       public static void main(String[] args)
       {
  -        junit.textui.TestRunner.run(suite());
  +        String[] arr = {BatchModeTest.class.getName()};
  +        junit.textui.TestRunner.main(arr);
  +    }
  +
  +    /**
  +     * A common (no specific batch) test
  +     */
  +    public void testEquals()
  +    {
  +        long pk = getNewId();
  +        String nameMain = "testEquals_Main_" + pk;
  +        String nameSub = "testEquals_Sub_" + pk;
  +
  +        MainObject main1 = new MainObject(pk, nameMain);
  +        main1.add(new SubObject(nameSub));
  +        MainObject main2 = new MainObject(pk, nameMain );
  +        main2.add(new SubObject(nameSub));
  +
  +        broker.beginTransaction();
  +        broker.store(main1);
  +        // delete object before add new instance with same PK
  +        broker.delete(main1);
  +        broker.store(main2);
  +        broker.commitTransaction();
  +
  +        // new PB instance
  +        tearDown();
  +        setUp();
  +
  +        MainObject main3 = new MainObject(pk, nameMain);
  +        main3.add(new SubObject(nameSub));
  +
  +        Identity oid = new Identity(main1, broker);
  +        broker.clearCache();
  +        MainObject main4 = (MainObject) broker.getObjectByIdentity(oid);
  +
  +        assertEquals(main3, main4);
  +    }
  +
  +    /**
  +     * A common (no specific batch) test
  +     */
  +    public void testDelete()
  +    {
  +        long pk = getNewId();
  +        String nameMain = "testDelete_Main_" + pk;
  +        String nameSub = "testDelete_Sub_" + pk;
  +
  +        MainObject main1 = new MainObject(pk, nameMain);
  +        main1.add(new SubObject(nameSub));
  +        main1.add(new SubObject(nameSub));
  +
  +        broker.beginTransaction();
  +        broker.store(main1);
  +        broker.delete(main1);
  +        broker.commitTransaction();
  +
  +        Identity oid = new Identity(main1, broker);
  +        MainObject newMain = (MainObject) broker.getObjectByIdentity(oid);
  +        assertNull(newMain);
  +        Criteria crit = new Criteria();
  +        crit.addLike("name", nameSub);
  +        Query q = QueryFactory.newQuery(SubObject.class, crit);
  +        Collection result = broker.getCollectionByQuery(q);
  +        assertEquals(0, result.size());
       }
   
  -    public static Test suite()
  +    public void testDeleteInsert()
       {
  -        TestSuite suite = new TestSuite(BatchModeTest.class);
  -        return suite;
  +        long pk = getNewId();
  +        String nameMain = "testDeleteInsert_Main_" + pk;
  +        String nameSub = "testDeleteInsert_Sub_" + pk;
  +
  +        MainObject main1 = new MainObject(pk, nameMain);
  +        main1.add(new SubObject("normal_" + nameSub));
  +        broker.beginTransaction();
  +        broker.store(main1);
  +        broker.commitTransaction();
  +
  +        // enable batch mode before start tx
  +        broker.serviceConnectionManager().setBatchMode(true);
  +        Identity oid = new Identity(main1, broker);
  +        broker.beginTransaction();
  +        broker.delete(main1);
  +        MainObject main2 = new MainObject(pk, nameMain);
  +        main2.add(new SubObject("updated_" + nameSub));
  +        broker.store(main2);
  +
  +        broker.commitTransaction();
  +
  +        broker.clearCache();
  +        MainObject newMain = (MainObject) broker.getObjectByIdentity(oid);
  +        assertNotNull(newMain);
  +        assertNotNull(newMain.getSubObjects());
  +        assertEquals(1, newMain.getSubObjects().size());
       }
   
       public void testBatchStatementsOrder()
  @@ -228,7 +315,7 @@
   
           broker.serviceConnectionManager().setBatchMode(true);
           broker.beginTransaction();
  -        for (int i = 200 - 1; i >= 0; i--)
  +        for(int i = 200 - 1; i >= 0; i--)
           {
               Person p = new Person();
               p.setFirstname("a mass test");
  @@ -244,7 +331,7 @@
           assertEquals(200, result.size());
   
           broker.beginTransaction();
  -        for (Iterator iterator = result.iterator(); iterator.hasNext();)
  +        for(Iterator iterator = result.iterator(); iterator.hasNext();)
           {
               broker.delete(iterator.next());
           }
  @@ -312,5 +399,141 @@
           broker.serviceConnectionManager().setBatchMode(true);
           broker.serviceConnectionManager().getConnection();
           broker.abortTransaction();
  +    }
  +
  +
  +    //==========================================================
  +    // inner classes used for testing
  +    //==========================================================
  +    public static class MainObject
  +    {
  +        private Long id;
  +        private String name;
  +        private Collection subObjects;
  +
  +        public MainObject()
  +        {
  +        }
  +
  +        public MainObject(long id, String name)
  +        {
  +            setId(new Long(id));
  +            setName(name);
  +        }
  +
  +        public Long getId()
  +        {
  +            return id;
  +        }
  +
  +        public void setId(Long id)
  +        {
  +            this.id = id;
  +        }
  +
  +        public String getName()
  +        {
  +            return name;
  +        }
  +
  +        public void setName(String name)
  +        {
  +            this.name = name;
  +        }
  +
  +        public Collection getSubObjects()
  +        {
  +            return subObjects;
  +        }
  +
  +        public void setSubObjects(Collection subObjects)
  +        {
  +            this.subObjects = subObjects;
  +        }
  +
  +        public void add(SubObject obj)
  +        {
  +            if(subObjects == null)
  +            {
  +                subObjects = new ArrayList();
  +            }
  +            subObjects.add(obj);
  +        }
  +
  +        public boolean equals(Object other)
  +        {
  +            if(other instanceof MainObject)
  +            {
  +                MainObject main = (MainObject) other;
  +                return ((name == null) ? main.name == null : name.equals(main.name))
  +                        && ((subObjects == null || subObjects.isEmpty())
  +                        ? (main.subObjects == null || main.subObjects.isEmpty())
  +                        : subObjects.equals(main.subObjects));
  +            }
  +            else
  +            {
  +                return false;
  +            }
  +        }
  +    }
  +
  +    public static class SubObject
  +    {
  +        private Long id;
  +        private String name;
  +        private Long mainId;
  +
  +        public SubObject()
  +        {
  +        }
  +
  +        public SubObject(String name)
  +        {
  +            setName(name);
  +        }
  +
  +        public Long getId()
  +        {
  +            return id;
  +        }
  +
  +        public void setId(Long id)
  +        {
  +            this.id = id;
  +        }
  +
  +        public Long getMainId()
  +        {
  +            return mainId;
  +        }
  +
  +        public void setMainId(Long mainId)
  +        {
  +            this.mainId = mainId;
  +        }
  +
  +        public String getName()
  +        {
  +            return name;
  +        }
  +
  +        public void setName(String name)
  +        {
  +            this.name = name;
  +        }
  +
  +        public boolean equals(Object other)
  +        {
  +            if(other instanceof SubObject)
  +            {
  +                SubObject sub = (SubObject) other;
  +                return (name == null) ? sub.name == null : name.equals(sub.name);
  +            }
  +            else
  +            {
  +                return false;
  +            }
  +        }
  +
       }
   }
  
  
  
  1.105     +55 -1     db-ojb/src/test/org/apache/ojb/repository_junit.xml
  
  Index: repository_junit.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/repository_junit.xml,v
  retrieving revision 1.104
  retrieving revision 1.105
  diff -u -r1.104 -r1.105
  --- repository_junit.xml	27 Jan 2004 20:45:24 -0000	1.104
  +++ repository_junit.xml	13 Feb 2004 18:58:24 -0000	1.105
  @@ -2527,6 +2527,60 @@
   
   
   <!-- ************************************************* -->
  +<!--      Classes used in BatchModeTest                -->
  +<!-- ************************************************* -->
  +    <class-descriptor
  +    	class="org.apache.ojb.broker.BatchModeTest$MainObject"
  +    	table="BATCH_MAIN_OBJ">
  +
  +    	<field-descriptor
  +    		name="id"
  +    		column="OBJ_ID"
  +    		jdbc-type="BIGINT"
  +    		primarykey="true"
  +    		autoincrement="false"/>
  +
  +        <field-descriptor
  +            name="name"
  +            column="NAME"
  +            jdbc-type="VARCHAR"
  +        />
  +		<collection-descriptor
  +			name="subObjects"
  +			element-class-ref="org.apache.ojb.broker.BatchModeTest$SubObject"
  +			auto-update="true"
  +			auto-retrieve="true"
  +			auto-delete="true">
  +			<inverse-foreignkey field-ref="mainId"/>
  +			<orderby name="OBJ_ID"/>
  +		</collection-descriptor>
  +
  +    </class-descriptor>
  +
  +    <class-descriptor
  +    	class="org.apache.ojb.broker.BatchModeTest$SubObject"
  +    	table="BATCH_SUB_OBJ">
  +
  +    	<field-descriptor
  +    		name="id"
  +    		column="OBJ_ID"
  +    		jdbc-type="BIGINT"
  +    		primarykey="true"
  +    		autoincrement="true" />
  +
  +        <field-descriptor
  +    		name="name"
  +    		column="NAME"
  +    		jdbc-type="VARCHAR" />
  +
  +        <field-descriptor
  +	   		name="mainId"
  +	   		column="FK_MAIN_ID"
  +	   		jdbc-type="BIGINT" />
  +    </class-descriptor>
  +
  +
  +<!-- ************************************************* -->
   <!--      Tests checking field conversion              -->
   <!-- ************************************************* -->
       <class-descriptor
  
  
  

---------------------------------------------------------------------
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