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_reference.xml
Date Wed, 23 Jun 2004 18:24:17 GMT
arminw      2004/06/23 11:24:17

  Modified:    src/schema ojbtest-schema.xml
               src/test/org/apache/ojb/broker AllTests.java
               src/test/org/apache/ojb repository_junit_reference.xml
  Added:       src/test/org/apache/ojb/broker M2NGraphTest.java
  Log:
  add new test case
  show to setup object hierarchy using m:n relation
  
  Revision  Changes    Path
  1.80      +13 -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.79
  retrieving revision 1.80
  diff -u -r1.79 -r1.80
  --- ojbtest-schema.xml	21 Jun 2004 11:09:59 -0000	1.79
  +++ ojbtest-schema.xml	23 Jun 2004 18:24:17 -0000	1.80
  @@ -902,6 +902,19 @@
           </foreign-key>
       </table>
   
  +    <!-- =================================================== -->
  +    <!-- M2NGraph test table                                 -->
  +    <!-- =================================================== -->
  +    <table name="M2N_NODE">
  +        <column name="OBJ_ID" required="true" primaryKey="true" type="INTEGER"/>
  +        <column name="NAME" type="VARCHAR" size="150"/>
  +    </table>
  +
  +    <table name="M2N_KINDSHIP">
  +        <column name="PARENT_ID" required="true" primaryKey="true" type="INTEGER"/>
  +        <column name="CHILD_ID" required="true" primaryKey="true" type="INTEGER"/>
  +    </table>
  +
   
   
       <!-- =================================================== -->
  
  
  
  1.48      +1 -0      db-ojb/src/test/org/apache/ojb/broker/AllTests.java
  
  Index: AllTests.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/AllTests.java,v
  retrieving revision 1.47
  retrieving revision 1.48
  diff -u -r1.47 -r1.48
  --- AllTests.java	12 Jun 2004 13:43:12 -0000	1.47
  +++ AllTests.java	23 Jun 2004 18:24:17 -0000	1.48
  @@ -96,6 +96,7 @@
           suite.addTestSuite(AutoIncrementTest.class);
           suite.addTestSuite(PathTest.class);
           suite.addTestSuite(PrimaryKeyForeignKeyTest.class);
  +        suite.addTestSuite(M2NGraphTest.class);
           return suite;
       }
   
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/broker/M2NGraphTest.java
  
  Index: M2NGraphTest.java
  ===================================================================
  package org.apache.ojb.broker;
  
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  import org.apache.commons.lang.builder.ToStringBuilder;
  import org.apache.ojb.junit.PBTestCase;
  import org.apache.ojb.broker.metadata.ClassDescriptor;
  import org.apache.ojb.broker.metadata.CollectionDescriptor;
  
  /* Copyright 2002-2004 The Apache Software Foundation
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  /**
   * Realize object tree via m:n relation.
   *
   * IMPORTANT NOTE: The global runtime metadata changes made by this test case
   * are NOT recommended in multithreaded environments, because they are global
   * and each thread will be affected.
   *
   * @author <a href="mailto:arminw@apache.org">Armin Waibel</a>
   * @version $Id: M2NGraphTest.java,v 1.1 2004/06/23 18:24:17 arminw Exp $
   */
  public class M2NGraphTest extends PBTestCase
  {
      public static void main(String[] args)
      {
          String[] arr = {M2NGraphTest.class.getName()};
          junit.textui.TestRunner.main(arr);
      }
  
      public void tearDown() throws Exception
      {
          changeRelationMetadata("children", true, CollectionDescriptor.CASCADE_NONE, CollectionDescriptor.CASCADE_NONE,
false);
          changeRelationMetadata("parents", true, CollectionDescriptor.CASCADE_NONE, CollectionDescriptor.CASCADE_NONE,
false);
          super.tearDown();
      }
  
      /**
       * All stuff is done by OJB
       */
      public void testAddNewChild_1() throws Exception
      {
          changeRelationMetadata("children", true, CollectionDescriptor.CASCADE_OBJECT, CollectionDescriptor.CASCADE_OBJECT,
false);
          changeRelationMetadata("parents", true, CollectionDescriptor.CASCADE_OBJECT, CollectionDescriptor.CASCADE_OBJECT,
false);
  
  
          String postfix = "_testAddNewChild_1_" + System.currentTimeMillis();
  
          Node nodeA = new Node("nodeA" + postfix);
          Node nodeB = new Node("nodeB" + postfix);
          Node nodeB2 = new Node("nodeB2" + postfix);
          Node nodeC = new Node("nodeC" + postfix);
  
          //===============================================
          broker.beginTransaction();
          nodeA.addChildNode(nodeB);
          nodeA.addChildNode(nodeB2);
          nodeB.addChildNode(nodeC);
          broker.store(nodeA);
          broker.commitTransaction();
          //===============================================
  
          Identity oidA = new Identity(nodeA, broker);
          Identity oidB = new Identity(nodeB, broker);
          Identity oidC = new Identity(nodeC, broker);
  
          broker.clearCache();
  
          // get the stored stuff
          Node retrievednodeB = (Node) broker.getObjectByIdentity(oidB);
          assertNotNull("Verifying the nodeB was retrieved", retrievednodeB);
          assertEquals("verify the retrieved nodeB has 1 parent (the nodeA)",
                  1, retrievednodeB.getParents().size());
          Node retrievednodeA = (Node) retrievednodeB.getParents().get(0);
          assertEquals("verify the nodeA was stored", nodeA.getName(),
                  retrievednodeA.getName());
          assertNotNull(retrievednodeA.getChildren());
          assertEquals("verify the retrieved nodeA has 2 childs (the nodeB)", 2,
                  retrievednodeA.getChildren().size());
          for (Iterator i = retrievednodeA.getChildren().iterator(); i.hasNext();)
          {
              Node b = (Node) i.next();
              //this next test fails because the child is null
              assertNotNull("Verifying nodeA's children are not null", b);
              assertTrue("verify, child is the nodeBx",
                      nodeB.getName().equals(b.getName()) || nodeB2.getName().equals(b.getName()));
          }
          assertNotNull(retrievednodeB.getChildren());
          assertEquals(1, retrievednodeB.getChildren().size());
          assertNotNull(nodeC.getName(), ((Node) retrievednodeB.getChildren().get(0)).getName());
  
          // get the stored stuff vice versa
          retrievednodeA = (Node) broker.getObjectByIdentity(oidA);
          assertNotNull(retrievednodeA);
          assertNotNull(retrievednodeA.getChildren());
          assertEquals(2, retrievednodeA.getChildren().size());
          retrievednodeB = (Node) retrievednodeA.getChildren().get(0);
          assertNotNull(retrievednodeB);
          assertNotNull(retrievednodeB.getParents());
          assertEquals(1, retrievednodeB.getParents().size());
  
          broker.clearCache();
          retrievednodeB = (Node) broker.getObjectByIdentity(oidB);
          //===============================================
          broker.beginTransaction();
          broker.delete(nodeB);
          broker.commitTransaction();
          //===============================================
  
          retrievednodeA = (Node) broker.getObjectByIdentity(oidA);
          retrievednodeB = (Node) broker.getObjectByIdentity(oidB);
          Node retrievednodeC = (Node) broker.getObjectByIdentity(oidC);
  
          // complete hierachy will be deleted
          assertNull(retrievednodeA);
          assertNull(retrievednodeB);
          assertNull(retrievednodeC);
      }
  
      /**
       * Autoretrieve is true, update/delete is done by hand
       */
      public void testAddNewChild_2() throws Exception
      {
          changeRelationMetadata("children", true, CollectionDescriptor.CASCADE_NONE, CollectionDescriptor.CASCADE_NONE,
false);
          changeRelationMetadata("parents", true, CollectionDescriptor.CASCADE_NONE, CollectionDescriptor.CASCADE_NONE,
false);
  
  
          String postfix = "_testAddNewChild_2_" + System.currentTimeMillis();
  
          Node nodeA = new Node("nodeA" + postfix);
          Node nodeB = new Node("nodeB" + postfix);
          Node nodeB2 = new Node("nodeB2" + postfix);
          Node nodeC = new Node("nodeC" + postfix);
  
          //===============================================
          broker.beginTransaction();
          nodeA.addChildNode(nodeB);
          nodeA.addChildNode(nodeB2);
          nodeB.addChildNode(nodeC);
  
          //store nodeA first
          broker.store(nodeA);
          //then store the nodeB
          broker.store(nodeB);
          broker.store(nodeB2);
          broker.store(nodeC);
          //make A the parent of B because they are not linked in the code automatically
          broker.serviceBrokerHelper().link(nodeA, true);
          // broker.serviceBrokerHelper().link(nodeB, true);
          // this will not work, because nodeB already linked by nodeA
          // so better specify the field to be link
          broker.serviceBrokerHelper().link(nodeB, "children", true);
          broker.commitTransaction();
          //===============================================
  
          Identity oidA = new Identity(nodeA, broker);
          Identity oidB = new Identity(nodeB, broker);
          Identity oidC = new Identity(nodeC, broker);
  
          broker.clearCache();
  
          // get the stored stuff
          Node retrievednodeB = (Node) broker.getObjectByIdentity(oidB);
  
          assertNotNull("Verifying the nodeB was retrieved", retrievednodeB);
          assertEquals("verify the retrieved nodeB has 1 parent (the nodeA)",
                  1, retrievednodeB.getParents().size());
  
          Node retrievednodeA = (Node) retrievednodeB.getParents().get(0);
  
          assertEquals("verify the nodeA was stored", nodeA.getName(),
                  retrievednodeA.getName());
          assertNotNull(retrievednodeA.getChildren());
          assertEquals("verify the retrieved nodeA has 2 childs (the nodeB)", 2,
                  retrievednodeA.getChildren().size());
          for (Iterator i = retrievednodeA.getChildren().iterator(); i.hasNext();)
          {
              Node b = (Node) i.next();
              //this next test fails because the child is null
              assertNotNull("Verifying nodeA's children are not null", b);
              assertTrue("verify, child is the nodeBx",
                      nodeB.getName().equals(b.getName()) || nodeB2.getName().equals(b.getName()));
          }
          assertNotNull(retrievednodeB.getChildren());
          assertEquals(1, retrievednodeB.getChildren().size());
          assertNotNull(nodeC.getName(), ((Node) retrievednodeB.getChildren().get(0)).getName());
  
          // get the stored stuff vice versa
          retrievednodeA = (Node) broker.getObjectByIdentity(oidA);
          assertNotNull(retrievednodeA);
          assertNotNull(retrievednodeA.getChildren());
          assertEquals(2, retrievednodeA.getChildren().size());
          retrievednodeB = (Node) retrievednodeA.getChildren().get(0);
          assertNotNull(retrievednodeB);
          assertNotNull(retrievednodeB.getParents());
          assertEquals(1, retrievednodeB.getParents().size());
  
          broker.clearCache();
          retrievednodeB = (Node) broker.getObjectByIdentity(oidB);
          // cascade delete is not set and we only want to delete node B
          //===============================================
          broker.beginTransaction();
          broker.serviceBrokerHelper().unlink(nodeB);
          broker.delete(nodeB);
          broker.commitTransaction();
          //===============================================
  
          retrievednodeA = (Node) broker.getObjectByIdentity(oidA);
          retrievednodeB = (Node) broker.getObjectByIdentity(oidB);
          Node retrievednodeC = (Node) broker.getObjectByIdentity(oidC);
  
          // only nodeB should be deleted
          assertNotNull(retrievednodeA);
          assertNull(retrievednodeB);
          assertNotNull(retrievednodeC);
      }
  
      /**
       * Do all work manually
       */
      public void testAddNewChild_3() throws Exception
      {
          changeRelationMetadata("children", false, CollectionDescriptor.CASCADE_NONE, CollectionDescriptor.CASCADE_NONE,
false);
          changeRelationMetadata("parents", false, CollectionDescriptor.CASCADE_NONE, CollectionDescriptor.CASCADE_NONE,
false);
  
          String postfix = "_testAddNewChild_3_" + System.currentTimeMillis();
  
          Node nodeA = new Node("nodeA" + postfix);
          Node nodeB = new Node("nodeB" + postfix);
          Node nodeC = new Node("nodeC" + postfix);
  
          //===============================================
          broker.beginTransaction();
          nodeA.addChildNode(nodeB);
          nodeB.addChildNode(nodeC);
          //store nodeA first
          broker.store(nodeA);
          //then store the nodeB
          broker.store(nodeB);
          broker.store(nodeC);
          //make A the parent of B because they are not linked in the code automatically
          broker.serviceBrokerHelper().link(nodeA, "children", true);
          broker.serviceBrokerHelper().link(nodeB, "children", true);
          broker.commitTransaction();
          //===============================================
  
          Identity oidA = new Identity(nodeA, broker);
          Identity oidB = new Identity(nodeB, broker);
  
          broker.clearCache();
  
          // get the stored stuff
          Node retrievednodeB = (Node) broker.getObjectByIdentity(oidB);
          assertNotNull("Verifying the nodeB was retrieved", retrievednodeB);
          assertEquals(nodeB.getName(), retrievednodeB.getName());
          //===============================================
          broker.retrieveReference(retrievednodeB, "parents");
          assertEquals("verify the retrieved nodeB has 1 parent (the nodeA)",
                  1, retrievednodeB.getParents().size());
          Node retrievednodeA = (Node) retrievednodeB.getParents().get(0);
          //===============================================
          broker.retrieveReference(retrievednodeA, "children");
          assertEquals("verify the nodeA was stored", nodeA.getName(),
                  retrievednodeA.getName());
          assertNotNull(retrievednodeA.getChildren());
          assertEquals("verify the retrieved nodeA has 1 child (the nodeB)", 1,
                  retrievednodeA.getChildren().size());
          for (Iterator i = retrievednodeA.getChildren().iterator(); i.hasNext();)
          {
              Node b = (Node) i.next();
              //this next test fails because the child is null
              assertNotNull("Verifying nodeA's children are not null", b);
              assertEquals("verify, using hashcode, that the nodeAs child is the nodeB",
                      retrievednodeB.getName(), b.getName());
          }
          //===============================================
          broker.retrieveReference(retrievednodeB, "children");
          assertNotNull(retrievednodeB.getChildren());
          assertEquals(1, retrievednodeB.getChildren().size());
          assertNotNull(nodeC.getName(), ((Node) retrievednodeB.getChildren().get(0)).getName());
  
          // get the stored stuff vice versa
          retrievednodeA = (Node) broker.getObjectByIdentity(oidA);
          assertNotNull(retrievednodeA);
          assertNotNull(retrievednodeA.getChildren());
          assertEquals(1, retrievednodeA.getChildren().size());
          retrievednodeB = (Node) retrievednodeA.getChildren().get(0);
          assertNotNull(retrievednodeB);
          assertNotNull(retrievednodeB.getParents());
          assertEquals(1, retrievednodeB.getParents().size());
      }
  
      void changeRelationMetadata(String field, boolean autoRetrieve, int autoUpdate, int
autoDelete, boolean proxy)
      {
          ClassDescriptor cld = broker.getClassDescriptor(Node.class);
          CollectionDescriptor cod = (CollectionDescriptor) cld.getCollectionDescriptorByName(field);
          cod.setLazy(proxy);
          cod.setCascadeRetrieve(autoRetrieve);
          cod.setCascadingStore(autoUpdate);
          cod.setCascadingDelete(autoDelete);
      }
  
      //=========================================
      // test classes
      //=========================================
      public static class Node
      {
          private Integer id;
          private String name;
          private List children;
          private List parents;
  
          public Node()
          {
          }
  
          public Node(String name)
          {
              this.name = name;
          }
  
          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 void addChildNode(Node child)
          {
              // Cannot add same child twice
              if (!getChildren().contains(child))
              {
                  getChildren().add(child);
                  if (!child.getParents().contains(this))
                  {
                      child.getParents().add(this);
                  }
              }
          }
  
  //        public void addParentNode(Node parent)
  //        {
  //            if(! getParents().contains(parent))
  //            {
  //                getParents().add(parent);
  //                if(!parent.getChildren().contains(this))
  //                {
  //                    parent.getChildren().add(this);
  //                }
  //            }
  //        }
  
          public void removeChild(Node child)
          {
              getChildren().remove(child);
              child.getParents().remove(this);
          }
  
          public List getParents()
          {
              if (parents == null)
              {
                  parents = new ArrayList();
              }
              return parents;
          }
  
          public List getChildren()
          {
              if (children == null)
              {
                  children = new ArrayList();
              }
              return children;
          }
  
          public String toString()
          {
              return new ToStringBuilder(this)
                      .append("id", id)
                      .append("name", name)
                      .append("children", children != null ? children.size() : 0)
                      .append("parents", parents != null ? parents.size() : 0)
                      .toString();
          }
  
          public boolean equals(Object ref)
          {
              if (ref == null || !(ref instanceof Node))
              {
                  return false;
              }
              else
              {
                  return (getId() != null ? getId().equals(((Node) ref).getId()) : false);
              }
          }
      }
  }
  
  
  
  1.17      +51 -1     db-ojb/src/test/org/apache/ojb/repository_junit_reference.xml
  
  Index: repository_junit_reference.xml
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/repository_junit_reference.xml,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- repository_junit_reference.xml	21 Jun 2004 11:10:00 -0000	1.16
  +++ repository_junit_reference.xml	23 Jun 2004 18:24:17 -0000	1.17
  @@ -1569,4 +1569,54 @@
          />
       </class-descriptor>
   
  +
  +    <!-- ************************************************* -->
  +    <!--     M2NGraphTest                                  -->
  +    <!-- ************************************************* -->
  +    <class-descriptor
  +        class="org.apache.ojb.broker.M2NGraphTest$Node"
  +        table="M2N_NODE"
  +    >
  +
  +        <field-descriptor
  +            name="id"
  +            column="OBJ_ID"
  +            jdbc-type="INTEGER"
  +            primarykey="true"
  +            autoincrement="true"
  +        />
  +
  +        <field-descriptor
  +            name="name"
  +            column="NAME"
  +            jdbc-type="VARCHAR"
  +        />
  +
  +        <collection-descriptor
  +            name="children"
  +            element-class-ref="org.apache.ojb.broker.M2NGraphTest$Node"
  +            collection-class="org.apache.ojb.broker.util.collections.ManageableArrayList"
  +            indirection-table="M2N_KINDSHIP"
  +            auto-retrieve="true"
  +            auto-update="none"
  +            auto-delete="none"
  +        >
  +            <fk-pointing-to-this-class column="PARENT_ID"/>
  +            <fk-pointing-to-element-class column="CHILD_ID"/>
  +        </collection-descriptor>
  +
  +        <collection-descriptor
  +            name="parents"
  +            element-class-ref="org.apache.ojb.broker.M2NGraphTest$Node"
  +            collection-class="org.apache.ojb.broker.util.collections.ManageableArrayList"
  +            indirection-table="M2N_KINDSHIP"
  +            auto-retrieve="true"
  +            auto-update="none"
  +            auto-delete="none"
  +        >
  +            <fk-pointing-to-this-class column="CHILD_ID"/>
  +            <fk-pointing-to-element-class column="PARENT_ID"/>
  +        </collection-descriptor>
  +    </class-descriptor>
  +
   <!-- Mapping of classes used in junit tests and tutorials ends here -->
  
  
  

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