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 Sat, 12 Jun 2004 13:43:12 GMT
arminw      2004/06/12 06:43:12

  Modified:    src/test/org/apache/ojb/broker AllTests.java
               src/test/org/apache/ojb repository_junit_reference.xml
  Added:       src/test/org/apache/ojb/broker PrimaryKeyForeignKeyTest.java
  Log:
  add new test use the PK field of an object as FK to
  a referenced object
  
  Revision  Changes    Path
  1.47      +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.46
  retrieving revision 1.47
  diff -u -r1.46 -r1.47
  --- AllTests.java	4 Jun 2004 16:02:44 -0000	1.46
  +++ AllTests.java	12 Jun 2004 13:43:12 -0000	1.47
  @@ -95,6 +95,7 @@
           suite.addTestSuite(NumberAccuracyTest.class);
           suite.addTestSuite(AutoIncrementTest.class);
           suite.addTestSuite(PathTest.class);
  +        suite.addTestSuite(PrimaryKeyForeignKeyTest.class);
           return suite;
       }
   
  
  
  
  1.1                  db-ojb/src/test/org/apache/ojb/broker/PrimaryKeyForeignKeyTest.java
  
  Index: PrimaryKeyForeignKeyTest.java
  ===================================================================
  package org.apache.ojb.broker;
  
  import java.util.Collection;
  
  import org.apache.ojb.broker.core.proxy.ProxyHelper;
  import org.apache.ojb.broker.metadata.ClassDescriptor;
  import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
  import org.apache.ojb.broker.query.Criteria;
  import org.apache.ojb.broker.query.Query;
  import org.apache.ojb.broker.query.QueryFactory;
  import org.apache.ojb.junit.PBTestCase;
  
  /**
   * Test case for checking the management of references, handling of PK used as FK too.
   * In this test the PK of {@link org.apache.ojb.broker.PrimaryKeyForeignKeyTest.Person}
   * is a FK to {@link org.apache.ojb.broker.PrimaryKeyForeignKeyTest.PersonDetail} too.
   *
   * Person has a reference to PersonDetail, the PK of Person is the FK to PersonDetail too!!
   * PersonDetail PK is not autoincremented we have to set the PK of Person
   *
   * NOTE: Don't change metadata in production environments in such a way. All made changes
are global
   * changes and visible to all threads.
   *
   * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
   * @version $Id: PrimaryKeyForeignKeyTest.java,v 1.1 2004/06/12 13:43:12 arminw Exp $
   */
  public class PrimaryKeyForeignKeyTest extends PBTestCase
  {
      public static void main(String[] args)
      {
          String[] arr = {PrimaryKeyForeignKeyTest.class.getName()};
          junit.textui.TestRunner.main(arr);
      }
  
      public void tearDown()
      {
          try
          {
              if(broker != null)
              {
                  changeRepositoryAutoSetting(Person.class, "detail", true, ObjectReferenceDescriptor.CASCADE_NONE,
ObjectReferenceDescriptor.CASCADE_NONE, false);
                  changeRepositoryAutoSetting(PersonDetail.class, "person", false, ObjectReferenceDescriptor.CASCADE_NONE,
ObjectReferenceDescriptor.CASCADE_NONE, false);
                  changeRepositoryAutoSetting(PersonDetail.class, "gender", true, ObjectReferenceDescriptor.CASCADE_NONE,
ObjectReferenceDescriptor.CASCADE_NONE, false);
              }
          }
          finally
          {
              super.tearDown();
          }
      }
  
      public void testStoreLookup()
      {
          changeRepositoryAutoSetting(Person.class, "detail", true, ObjectReferenceDescriptor.CASCADE_NONE,
ObjectReferenceDescriptor.CASCADE_NONE, false);
          changeRepositoryAutoSetting(PersonDetail.class, "person", false, ObjectReferenceDescriptor.CASCADE_NONE,
ObjectReferenceDescriptor.CASCADE_NONE, false);
          changeRepositoryAutoSetting(PersonDetail.class, "gender", true, ObjectReferenceDescriptor.CASCADE_NONE,
ObjectReferenceDescriptor.CASCADE_NONE, false);
  
  
          String postfix = "_" + System.currentTimeMillis();
  
          broker.beginTransaction();
          Person jeff = new Person("jeff" + postfix, null);
          // store Person first to assign PK, needed to set PK in PersonDetail
          broker.store(jeff);
          GenderIF gender = new Gender("male"+postfix);
          PersonDetailIF jeffDetail = new PersonDetail(jeff.getId(), "profile"+postfix, jeff,
gender);
          broker.store(gender);
          broker.serviceBrokerHelper().link(jeffDetail, true);
          broker.store(jeffDetail);
          // now we can assign reference
          jeff.setDetail(jeffDetail);
          broker.store(jeff);
          broker.commitTransaction();
  
          broker.clearCache();
  
          Criteria crit = new Criteria();
          crit.addEqualTo("name", "jeff" + postfix);
          Query query = QueryFactory.newQuery(Person.class, crit);
          Collection result = broker.getCollectionByQuery(query);
          assertEquals(1, result.size());
          Person jeffNew = (Person) result.iterator().next();
          assertEquals("jeff" + postfix, jeffNew.getName());
          assertNotNull(jeffNew.getDetail());
          assertFalse(ProxyHelper.isProxy(jeffNew.getDetail()));
          PersonDetailIF jeffNewDetail = jeffNew.getDetail();
          assertNotNull(jeffNewDetail.getGender());
          assertEquals("male"+postfix, jeffNewDetail.getGender().getType());
  
          broker.beginTransaction();
          broker.delete(jeffNew);
          GenderIF g = jeffNewDetail.getGender();
          broker.delete(jeffNewDetail);
          broker.delete(g);
          broker.commitTransaction();
      }
  
      public void testStoreLookupProxy()
      {
          changeRepositoryAutoSetting(Person.class, "detail", true, ObjectReferenceDescriptor.CASCADE_NONE,
ObjectReferenceDescriptor.CASCADE_NONE, true);
          changeRepositoryAutoSetting(PersonDetail.class, "person", false, ObjectReferenceDescriptor.CASCADE_NONE,
ObjectReferenceDescriptor.CASCADE_NONE, true);
          changeRepositoryAutoSetting(PersonDetail.class, "gender", true, ObjectReferenceDescriptor.CASCADE_NONE,
ObjectReferenceDescriptor.CASCADE_NONE, true);
  
  
          String postfix = "_" + System.currentTimeMillis();
  
          broker.beginTransaction();
          Person jeff = new Person("jeff" + postfix, null);
          // store Person first to assign PK, needed to set PK in PersonDetail
          broker.store(jeff);
          GenderIF gender = new Gender("male"+postfix);
          PersonDetailIF jeffDetail = new PersonDetail(jeff.getId(), "profile"+postfix, jeff,
gender);
          broker.store(gender);
          broker.serviceBrokerHelper().link(jeffDetail, true);
          broker.store(jeffDetail);
          // now we can assign reference
          jeff.setDetail(jeffDetail);
          broker.store(jeff);
          broker.commitTransaction();
  
          broker.clearCache();
  
          Criteria crit = new Criteria();
          crit.addEqualTo("name", "jeff" + postfix);
          Query query = QueryFactory.newQuery(Person.class, crit);
          Collection result = broker.getCollectionByQuery(query);
          assertEquals(1, result.size());
          Person jeffNew = (Person) result.iterator().next();
          assertEquals("jeff" + postfix, jeffNew.getName());
          assertNotNull(jeffNew.getDetail());
          assertTrue(ProxyHelper.isProxy(jeffNew.getDetail()));
          PersonDetailIF jeffNewDetail = jeffNew.getDetail();
          assertNotNull(jeffNewDetail.getGender());
          assertTrue(ProxyHelper.isProxy(jeffNewDetail.getGender()));
          assertEquals("male"+postfix, jeffNewDetail.getGender().getType());
  
          broker.beginTransaction();
          broker.delete(jeffNew);
          GenderIF g = jeffNewDetail.getGender();
          broker.delete(jeffNewDetail);
          broker.delete(g);
          broker.commitTransaction();
      }
  
      public void testStoreLookup_2()
      {
          changeRepositoryAutoSetting(Person.class, "detail", true, ObjectReferenceDescriptor.CASCADE_OBJECT,
ObjectReferenceDescriptor.CASCADE_OBJECT, false);
          changeRepositoryAutoSetting(PersonDetail.class, "person", false, ObjectReferenceDescriptor.CASCADE_OBJECT,
ObjectReferenceDescriptor.CASCADE_OBJECT, false);
          changeRepositoryAutoSetting(PersonDetail.class, "gender", true, ObjectReferenceDescriptor.CASCADE_OBJECT,
ObjectReferenceDescriptor.CASCADE_OBJECT, false);
  
  
          String postfix = "_" + System.currentTimeMillis();
  
          broker.beginTransaction();
          Person jeff = new Person("jeff" + postfix, null);
          // store Person first to assign PK, needed to set PK in PersonDetail
          broker.store(jeff);
          GenderIF gender = new Gender("male"+postfix);
          PersonDetailIF jeffDetail = new PersonDetail(jeff.getId(), "profile"+postfix, jeff,
gender);
          broker.store(jeffDetail);
          // now we can assign reference
          jeff.setDetail(jeffDetail);
          broker.store(jeff);
          broker.commitTransaction();
  
          broker.clearCache();
  
          Criteria crit = new Criteria();
          crit.addEqualTo("name", "jeff" + postfix);
          Query query = QueryFactory.newQuery(Person.class, crit);
          Collection result = broker.getCollectionByQuery(query);
          assertEquals(1, result.size());
          Person jeffNew = (Person) result.iterator().next();
          assertEquals("jeff" + postfix, jeffNew.getName());
          assertNotNull(jeffNew.getDetail());
          assertFalse(ProxyHelper.isProxy(jeffNew.getDetail()));
          PersonDetailIF jeffNewDetail = jeffNew.getDetail();
          assertNotNull(jeffNewDetail.getGender());
          assertEquals("male"+postfix, jeffNewDetail.getGender().getType());
  
          broker.beginTransaction();
          broker.delete(jeffNew);
          GenderIF g = jeffNewDetail.getGender();
          broker.delete(jeffNewDetail);
          broker.delete(g);
          broker.commitTransaction();
      }
  
      public void testStoreLookupProxy_2()
      {
          changeRepositoryAutoSetting(Person.class, "detail", true, ObjectReferenceDescriptor.CASCADE_OBJECT,
ObjectReferenceDescriptor.CASCADE_OBJECT, true);
          changeRepositoryAutoSetting(PersonDetail.class, "person", false, ObjectReferenceDescriptor.CASCADE_OBJECT,
ObjectReferenceDescriptor.CASCADE_OBJECT, true);
          changeRepositoryAutoSetting(PersonDetail.class, "gender", true, ObjectReferenceDescriptor.CASCADE_OBJECT,
ObjectReferenceDescriptor.CASCADE_OBJECT, true);
  
  
          String postfix = "_" + System.currentTimeMillis();
  
          broker.beginTransaction();
          Person jeff = new Person("jeff" + postfix, null);
          // store Person first to assign PK, needed to set PK in PersonDetail
          broker.store(jeff);
          GenderIF gender = new Gender("male"+postfix);
          PersonDetailIF jeffDetail = new PersonDetail(jeff.getId(), "profile"+postfix, jeff,
gender);
          broker.store(jeffDetail);
          // now we can assign reference
          jeff.setDetail(jeffDetail);
          broker.store(jeff);
          broker.commitTransaction();
  
          broker.clearCache();
  
          Criteria crit = new Criteria();
          crit.addEqualTo("name", "jeff" + postfix);
          Query query = QueryFactory.newQuery(Person.class, crit);
          Collection result = broker.getCollectionByQuery(query);
          assertEquals(1, result.size());
          Person jeffNew = (Person) result.iterator().next();
          assertEquals("jeff" + postfix, jeffNew.getName());
          assertNotNull(jeffNew.getDetail());
          assertTrue(ProxyHelper.isProxy(jeffNew.getDetail()));
          PersonDetailIF jeffNewDetail = jeffNew.getDetail();
          assertNotNull(jeffNewDetail.getGender());
          assertTrue(ProxyHelper.isProxy(jeffNewDetail.getGender()));
          assertEquals("male"+postfix, jeffNewDetail.getGender().getType());
  
          broker.beginTransaction();
          broker.delete(jeffNew);
          GenderIF g = jeffNewDetail.getGender();
          broker.delete(jeffNewDetail);
          broker.delete(g);
          broker.commitTransaction();
      }
  
      private void changeRepositoryAutoSetting(Class clazz, String attributeName, boolean
retrieve, int update, int delete, boolean proxy)
      {
          ClassDescriptor cld = broker.getClassDescriptor(clazz);
          ObjectReferenceDescriptor ord = cld.getObjectReferenceDescriptorByName(attributeName);
          ord.setCascadeRetrieve(retrieve);
          ord.setCascadingStore(update);
          ord.setCascadingDelete(delete);
          ord.setLazy(proxy);
      }
  
      public static class Person
      {
          Integer id;
          String name;
          PersonDetailIF detail;
  
          public Person()
          {
          }
  
          public Person(String name, PersonDetailIF detail)
          {
              this.name = name;
              this.detail = detail;
          }
  
          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 PersonDetailIF getDetail()
          {
              return detail;
          }
  
          public void setDetail(PersonDetailIF detail)
          {
              this.detail = detail;
          }
      }
  
      public static interface PersonDetailIF
      {
          public Integer getId();
          public void setId(Integer id);
          public String getProfile();
          public void setProfile(String profile);
          public Person getPerson();
          public void setPerson(Person person);
          public GenderIF getGender();
          public void setGender(GenderIF person);
      }
  
      public static class PersonDetail implements PersonDetailIF
      {
          Integer id;
          String profile;
          Person person;
          GenderIF gender;
  
          public PersonDetail()
          {
          }
  
          public PersonDetail(Integer id, String profile, Person person, GenderIF gender)
          {
              this.id = id;
              this.profile = profile;
              this.person = person;
              this.gender = gender;
          }
  
          public Integer getId()
          {
              return id;
          }
  
          public void setId(Integer id)
          {
              this.id = id;
          }
  
          public String getProfile()
          {
              return profile;
          }
  
          public void setProfile(String profile)
          {
              this.profile = profile;
          }
  
          public Person getPerson()
          {
              return person;
          }
  
          public void setPerson(Person person)
          {
              this.person = person;
          }
  
          public GenderIF getGender()
          {
              return gender;
          }
  
          public void setGender(GenderIF gender)
          {
              this.gender = gender;
          }
      }
  
      public static interface GenderIF
      {
          public String getType();
          public void setType(String type);
      }
  
      public static class Gender implements GenderIF
      {
          String type;
  
          public Gender()
          {
          }
  
          public Gender(String type)
          {
              this.type = type;
          }
  
          public String getType()
          {
              return type;
          }
  
          public void setType(String type)
          {
              this.type = type;
          }
      }
  }
  
  
  
  1.14      +95 -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.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- repository_junit_reference.xml	22 May 2004 16:23:45 -0000	1.13
  +++ repository_junit_reference.xml	12 Jun 2004 13:43:12 -0000	1.14
  @@ -579,6 +579,100 @@
       </class-descriptor>
   
   
  +<!-- ************************************************* -->
  +<!--      PrimaryKeyForeignKeyTest - PB-api            -->
  +<!-- ************************************************* -->
  +    <class-descriptor
  +        class="org.apache.ojb.broker.PrimaryKeyForeignKeyTest$Person"
  +        table="PKFK_PERSON">
  +
  +        <field-descriptor
  +             name="id"
  +             column="OBJ_ID"
  +             jdbc-type="INTEGER"
  +             primarykey="true"
  +             autoincrement="true"
  +        />
  +
  +        <field-descriptor
  +             name="name"
  +             column="NAME"
  +             jdbc-type="VARCHAR"
  +        />
  +
  +        <reference-descriptor
  +            name="detail"
  +            class-ref="org.apache.ojb.broker.PrimaryKeyForeignKeyTest$PersonDetail"
  +            proxy="false"
  +            auto-retrieve="true"
  +            auto-update="false"
  +            auto-delete="false">
  +                <foreignkey field-ref="id"/>
  +        </reference-descriptor>
  +    </class-descriptor>
  +
  +    <class-descriptor
  +        class="org.apache.ojb.broker.PrimaryKeyForeignKeyTest$PersonDetail"
  +        table="PKFK_PERSON_DETAIL">
  +
  +        <field-descriptor
  +             name="id"
  +             column="OBJ_ID"
  +             jdbc-type="INTEGER"
  +             primarykey="true"
  +             autoincrement="false"
  +        />
  +
  +        <field-descriptor
  +             name="profile"
  +             column="PROFILE"
  +             jdbc-type="VARCHAR"
  +        />
  +
  +        <field-descriptor
  +             name="fkGender"
  +             column="FK_GENDER"
  +             jdbc-type="VARCHAR"
  +             primarykey="false"
  +             autoincrement="false"
  +             access="anonymous"
  +        />
  +
  +        <reference-descriptor
  +            name="gender"
  +            class-ref="org.apache.ojb.broker.PrimaryKeyForeignKeyTest$Gender"
  +            proxy="false"
  +            auto-retrieve="true"
  +            auto-update="false"
  +            auto-delete="false">
  +                <foreignkey field-ref="fkGender"/>
  +        </reference-descriptor>
  +
  +        <reference-descriptor
  +            name="person"
  +            class-ref="org.apache.ojb.broker.PrimaryKeyForeignKeyTest$Person"
  +            proxy="false"
  +            auto-retrieve="true"
  +            auto-update="false"
  +            auto-delete="false">
  +                <foreignkey field-ref="id"/>
  +        </reference-descriptor>
  +    </class-descriptor>
  +
  +    <class-descriptor
  +        class="org.apache.ojb.broker.PrimaryKeyForeignKeyTest$Gender"
  +        table="PKFK_GENDER">
  +
  +        <field-descriptor
  +             name="type"
  +             column="TYPE"
  +             jdbc-type="VARCHAR"
  +             primarykey="true"
  +             autoincrement="false"
  +        />
  +    </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