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 Mon, 05 Jul 2004 14:46:26 GMT
arminw      2004/07/05 07:46:26

  Modified:    src/schema ojbtest-schema.xml
               src/test/org/apache/ojb repository_junit_reference.xml
  Added:       src/test/org/apache/ojb/broker TreeExtentedTest.java
  Log:
  add new test case
  a tree test a using abstract base class for parent, child nodes
  and two real implementation classes
  
  Revision  Changes    Path
  1.82      +17 -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.81
  retrieving revision 1.82
  diff -u -r1.81 -r1.82
  --- ojbtest-schema.xml	3 Jul 2004 23:32:16 -0000	1.81
  +++ ojbtest-schema.xml	5 Jul 2004 14:46:26 -0000	1.82
  @@ -1205,6 +1205,23 @@
       <!-- =================================================== -->
       <!-- Runtime reference setting test                      -->
       <!-- =================================================== -->
  +    <table name="TREE_EXTENDED_NODE">
  +        <column name="OBJ_ID" required="true" primaryKey="true" type="INTEGER"/>
  +        <column name="NAME" type="VARCHAR" size="150"/>
  +        <column name="PARENT_ID" type="INTEGER"/>
  +        <column name="CHILD_ID" type="INTEGER"/>
  +        <foreign-key foreignTable="TREE_EXTENDED_NODE">
  +    	  <reference local="CHILD_ID" foreign="OBJ_ID"/>
  +	    </foreign-key>
  +        <foreign-key foreignTable="TREE_EXTENDED_NODE">
  +    	  <reference local="PARENT_ID" foreign="OBJ_ID"/>
  +	    </foreign-key>
  +    </table>
  +
  +
  +    <!-- =================================================== -->
  +    <!-- Runtime reference setting test                      -->
  +    <!-- =================================================== -->
        <table name="RUNTIME_SETTING_MAIN">
           <column name="OBJ_ID" required="true" primaryKey="true" type="INTEGER"/>
           <column name="NAME" type="VARCHAR" size="150"/>
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/broker/TreeExtentedTest.java
  
  Index: TreeExtentedTest.java
  ===================================================================
  package org.apache.ojb.broker;
  
  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.List;
  
  import org.apache.commons.lang.builder.ToStringBuilder;
  import org.apache.ojb.junit.PBTestCase;
  
  /**
   * Testing selfjoins and tree structures
   */
  public class TreeExtentedTest extends PBTestCase
  {
      public static void main(String[] args)
      {
          String[] arr = {TreeExtentedTest.class.getName()};
          junit.textui.TestRunner.main(arr);
      }
  
      public TreeExtentedTest(String name)
  
      {
          super(name);
      }
  
  
      public void testTree()
      {
          String postfix = "_" + System.currentTimeMillis();
  
          NodeCategory cat = new NodeCategory();
          cat.setName("Start" + postfix);
          NodeCategory mobile = new NodeCategory();
          mobile.setName("mobile" + postfix);
          NodeCategory building = new NodeCategory();
          building.setName("building" + postfix);
          NodeCategory car = new NodeCategory();
          car.setName("car" + postfix);
          NodeCategory bicycle = new NodeCategory();
          bicycle.setName("bicycle" + postfix);
          NodeProduct mercedes = new NodeProduct();
          mercedes.setName("mercedes" + postfix);
          NodeProduct slc = new NodeProduct();
          slc.setName("mercedes slc" + postfix);
          NodeCategory extras = new NodeCategory();
          extras.setName("mercedes - extras" + postfix);
          NodeProduct sticker = new NodeProduct();
          sticker.setName("mercedes - sticker" + postfix);
  
          cat.addChild(mobile);
          cat.addChild(building);
          mobile.addChild(bicycle);
          mobile.addChild(car);
          car.addChild(mercedes);
          mercedes.addChild(slc);
          mercedes.addChild(extras);
          mercedes.addChild(sticker);
  
          broker.beginTransaction();
          broker.store(cat);
          broker.commitTransaction();
  
          Identity oidCat = new Identity(cat, broker);
          Identity oidSticker = new Identity(sticker, broker);
          broker.clearCache();
  
          Node newSticker = (Node) broker.getObjectByIdentity(oidSticker);
          Node newCat = (Node) broker.getObjectByIdentity(oidCat);
          Node newMercedes = newSticker.getParent();
          assertEquals(3, newMercedes.getChilds().size());
          assertEquals(2, newCat.getChilds().size());
          assertEquals(8, newCat.size());
  
          broker.beginTransaction();
          broker.delete(newMercedes);
          broker.commitTransaction();
  
          // current used cache needs sync with DB
          broker.clearCache();
          newCat = (Node) broker.getObjectByIdentity(oidCat);
          assertEquals(4, newCat.size());
  
          broker.beginTransaction();
          broker.delete(newCat);
          broker.commitTransaction();
  
          newCat = (Node) broker.getObjectByIdentity(oidCat);
          assertNull(newCat);
      }
  
  
      //*****************************************************************
      // inner classes - test objects
      //*****************************************************************
  
      public static class NodeCategory extends Node
      {
  
      }
  
      public static class NodeProduct extends Node
      {
  
      }
  
      /**
       * Node is  recursive type: a Node element contains some data
       * and a Vector of child Node elements.
       * This sample demonstrates what is needed to map such a data
       * structure on a DB table
       *
       * @author Thomas Mahler
       */
      public static abstract class Node implements Serializable
      {
          private Integer id;
          private String name;
          private List childs;
          private Node parent;
          private Integer childId;
  
          public Node()
          {
          }
  
          public Node(Integer id, String name, List childs, Node parent)
          {
              this.id = id;
              this.name = name;
              this.childs = childs;
              this.parent = parent;
          }
  
          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 List getChilds()
          {
              return childs;
          }
  
          public void setChilds(List childs)
          {
              this.childs = childs;
          }
  
          public void addChild(Node newChild)
          {
              if (childs == null) childs = new ArrayList();
              newChild.setParent(this);
              childs.add(newChild);
          }
  
          public Node getParent()
          {
              return parent;
          }
  
          public void setParent(Node parent)
          {
              this.parent = parent;
          }
  
          public Integer getChildId()
          {
              return childId;
          }
  
          public void setChildId(Integer childId)
          {
              this.childId = childId;
          }
  
          public int size()
          {
              return countChilds() - 1;
          }
  
          private int countChilds()
          {
              int result = 1;
              if(childs != null)
              {
                  for (int i = 0; i < childs.size(); i++)
                  {
                      int c = ((Node) childs.get(i)).countChilds();
                      result += c;
                  }
              }
              return result;
          }
  
          public String toString()
          {
              return new ToStringBuilder(this)
                      .append("id", id)
                      .append("name", name)
                      .append("parent-name", parent != null ? parent.getName() : null)
                      .append("childs-size", childs != null ? childs.size() : 0)
                      .toString();
          }
      }
  }
  
  
  
  1.18      +116 -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.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- repository_junit_reference.xml	23 Jun 2004 18:24:17 -0000	1.17
  +++ repository_junit_reference.xml	5 Jul 2004 14:46:26 -0000	1.18
  @@ -1619,4 +1619,119 @@
           </collection-descriptor>
       </class-descriptor>
   
  +
  +
  +    <!-- ************************************************* -->
  +    <!--     TreeExtendedTest                              -->
  +    <!-- ************************************************* -->
  +    <class-descriptor class="org.apache.ojb.broker.TreeExtentedTest$Node">
  +        <extent-class class-ref="org.apache.ojb.broker.TreeExtentedTest$NodeCategory"
/>
  +        <extent-class class-ref="org.apache.ojb.broker.TreeExtentedTest$NodeProduct"
/>
  +    </class-descriptor>
  +
  +    <class-descriptor
  +      class="org.apache.ojb.broker.TreeExtentedTest$NodeCategory"
  +      table="TREE_EXTENDED_NODE"
  +   >
  +      <field-descriptor
  +         name="id"
  +         column="OBJ_ID"
  +         jdbc-type="INTEGER"
  +         primarykey="true"
  +          autoincrement="true"
  +      />
  +      <field-descriptor
  +         name="name"
  +         column="NAME"
  +         jdbc-type="VARCHAR"
  +      />
  +      <field-descriptor
  +         name="parentId"
  +         column="PARENT_ID"
  +         jdbc-type="INTEGER"
  +          access="anonymous"
  +      />
  +
  +      <field-descriptor
  +         name="childId"
  +         column="CHILD_ID"
  +         jdbc-type="INTEGER"
  +      />
  +
  +        <reference-descriptor
  +          name="parent"
  +          class-ref="org.apache.ojb.broker.TreeExtentedTest$Node"
  +          auto-retrieve="true"
  +          auto-update="object"
  +          auto-delete="none"
  +          proxy="false"
  +       >
  +          <foreignkey field-ref="parentId"/>
  +       </reference-descriptor>
  +
  +      <collection-descriptor
  +         name="childs"
  +         element-class-ref="org.apache.ojb.broker.TreeExtentedTest$Node"
  +         auto-retrieve="true"
  +         auto-update="object"
  +         auto-delete="object"
  +      >
  +         <inverse-foreignkey field-ref="childId"/>
  +      </collection-descriptor>
  +   </class-descriptor>
  +
  +    <!-- ************************************************* -->
  +    <!--     TreeExtendedTest                              -->
  +    <!-- ************************************************* -->
  +    <class-descriptor
  +      class="org.apache.ojb.broker.TreeExtentedTest$NodeProduct"
  +      table="TREE_EXTENDED_NODE"
  +   >
  +      <field-descriptor
  +         name="id"
  +         column="OBJ_ID"
  +         jdbc-type="INTEGER"
  +         primarykey="true"
  +          autoincrement="true"
  +      />
  +      <field-descriptor
  +         name="name"
  +         column="NAME"
  +         jdbc-type="VARCHAR"
  +      />
  +      <field-descriptor
  +         name="parentId"
  +         column="PARENT_ID"
  +         jdbc-type="INTEGER"
  +          access="anonymous"
  +      />
  +
  +      <field-descriptor
  +         name="childId"
  +         column="CHILD_ID"
  +         jdbc-type="INTEGER"
  +      />
  +
  +        <reference-descriptor
  +          name="parent"
  +          class-ref="org.apache.ojb.broker.TreeExtentedTest$Node"
  +          auto-retrieve="true"
  +          auto-update="object"
  +          auto-delete="object"
  +          proxy="false"
  +       >
  +          <foreignkey field-ref="parentId"/>
  +       </reference-descriptor>
  +
  +      <collection-descriptor
  +         name="childs"
  +         element-class-ref="org.apache.ojb.broker.TreeExtentedTest$Node"
  +         auto-retrieve="true"
  +         auto-update="object"
  +         auto-delete="object"
  +      >
  +         <inverse-foreignkey field-ref="childId"/>
  +      </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