db-ojb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From b..@apache.org
Subject cvs commit: db-ojb/src/test/org/apache/ojb/broker MtoNMapping.java
Date Wed, 07 Jan 2004 21:18:16 GMT
brj         2004/01/07 13:18:16

  Modified:    src/test/org/apache/ojb/broker Tag: OJB_BRANCH_1_0
                        MtoNMapping.java
  Log:
   test storing of Project-Persons when both relationships use auto-update=true
  
  Revision  Changes    Path
  No                   revision
  No                   revision
  1.13.2.3  +90 -7     db-ojb/src/test/org/apache/ojb/broker/MtoNMapping.java
  
  Index: MtoNMapping.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/MtoNMapping.java,v
  retrieving revision 1.13.2.2
  retrieving revision 1.13.2.3
  diff -u -r1.13.2.2 -r1.13.2.3
  --- MtoNMapping.java	7 Jan 2004 13:02:02 -0000	1.13.2.2
  +++ MtoNMapping.java	7 Jan 2004 21:18:16 -0000	1.13.2.3
  @@ -8,11 +8,12 @@
   
   import junit.framework.TestCase;
   
  +import org.apache.ojb.broker.metadata.ClassDescriptor;
  +import org.apache.ojb.broker.metadata.CollectionDescriptor;
   import org.apache.ojb.broker.query.Criteria;
   import org.apache.ojb.broker.query.Query;
   import org.apache.ojb.broker.query.QueryByCriteria;
   import org.apache.ojb.broker.query.QueryFactory;
  -import org.apache.ojb.broker.query.QueryByIdentity;
   
   /** This TestClass tests OJB facilities to work with polymorphism.
    */
  @@ -370,10 +371,17 @@
   
       }
   
  -    private Project createProjectWithAssignedPersons(String title) throws Exception
  +    /**
  +     * Create a project with two persons
  +     * @param title
  +     * @return
  +     * @throws Exception
  +     */
  +    private Project createProjectWithAssignedPersons_1(String title) throws Exception
       {
           /*
  -        the order of store statements is crucial
  +        the order of store statements is crucial because the relationship
  +        pointing from Project to Person has auto-update=false 
           */
           // create new project
           Project project = new Project();
  @@ -410,6 +418,63 @@
       }
   
       /**
  +     * Create a project with two persons
  +     * both relationships are set to auto-update=true
  +     * @param title
  +     * @return
  +     * @throws Exception
  +     */
  +    private Project createProjectWithAssignedPersons_2(String title) throws Exception
  +    {
  +        ClassDescriptor cldProject = broker.getClassDescriptor(Project.class);
  +        CollectionDescriptor codPersons =cldProject.getCollectionDescriptorByName("persons");
  +        boolean cascadeStorePersons = codPersons.getCascadeStore();
  +
  +        ClassDescriptor cldPerson = broker.getClassDescriptor(Person.class);
  +        CollectionDescriptor codProjects =cldPerson.getCollectionDescriptorByName("projects");
  +        boolean cascadeStoreProjects = codProjects.getCascadeStore();
  +        
  +        // temporarily set auto-update = true
  +        codPersons.setCascadeStore(true);
  +        codProjects.setCascadeStore(true);
  +        
  +        // create new project
  +        Project project = new Project();
  +        project.setTitle(title);
  +
  +        // create two persons and assign project
  +        // and assign persons with project
  +        Person p1 = new Person();
  +        p1.setFirstname(title);
  +
  +        List projects_1 = new ArrayList();
  +        projects_1.add(project);
  +        p1.setProjects(projects_1); // connect project to person
  +
  +        Person p2 = new Person();
  +        p2.setFirstname(title);
  +
  +        List projects_2 = new ArrayList();
  +        projects_2.add(project);
  +        p2.setProjects(projects_2); // connect project to person
  +
  +        ArrayList persons = new ArrayList();
  +        persons.add(p1);
  +        persons.add(p2);
  +        project.setPersons(persons);    // connect persons to project
  +
  +        broker.beginTransaction();
  +        broker.store(project);
  +        broker.commitTransaction();
  +
  +        // reset original value
  +        codPersons.setCascadeStore(cascadeStorePersons);
  +        codProjects.setCascadeStore(cascadeStoreProjects);
  +        
  +        return project;
  +    }
  +    
  +    /**
        * Add two new persons and one new project. Assign persons with the
        * new project and vice versa.
        */
  @@ -417,8 +482,27 @@
       {
           String title = "testInsertWithIndirectionTable_1_" + System.currentTimeMillis();
   
  -        Project project = createProjectWithAssignedPersons(title);
  +        Project project = createProjectWithAssignedPersons_1(title);
   
  +        verifyProjectWithAssignedPersons(title, project);
  +    }
  +
  +    /**
  +     * Add two new persons and one new project. Assign persons with the
  +     * new project and vice versa. 
  +     * both relationships are set to auto-update=true
  +     */
  +    public void testInsertWithIndirectionTable_2() throws Exception
  +    {
  +        String title = "testInsertWithIndirectionTable_2_" + System.currentTimeMillis();
  +
  +        Project project = createProjectWithAssignedPersons_2(title);
  +
  +        verifyProjectWithAssignedPersons(title, project);
  +    }
  +    
  +    private void verifyProjectWithAssignedPersons(String title, Project project)
  +    {
           /*
           Now I expect two entries in PERSON_PROJECT table
           with same project id, two new Person and one Project
  @@ -461,7 +545,7 @@
   
           // first we create an project with assigned persons
           // create new project with two assigned persons
  -        Project tempProject = createProjectWithAssignedPersons(title);
  +        Project tempProject = createProjectWithAssignedPersons_1(title);
   
           // now the real update test begins
           broker.clearCache();
  @@ -510,7 +594,6 @@
           project.getPersons().add(p1);
           project.getPersons().add(p2);
   
  -
           broker.beginTransaction();
           broker.store(project);
           broker.commitTransaction();
  @@ -537,7 +620,7 @@
           assertEquals("We expect 1 project instance", 1, result.size());
           Project newProject = (Project) result.iterator().next();
           assertNotNull(newProject.getRoles());
  -        assertEquals("We expect 2 new Role objects", roleCount +  2, newProject.getRoles().size());
  +        assertEquals("We expect 2 new Role objects", roleCount + 2, newProject.getRoles().size());
   
           // query for role objects representing PERSON_PROJECT entries
           crit = new Criteria();
  
  
  

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