You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by ar...@apache.org on 2004/02/06 13:27:03 UTC

cvs commit: db-ojb/src/test/org/apache/ojb/broker ReferenceTest.java

arminw      2004/02/06 04:27:03

  Modified:    src/test/org/apache/ojb/broker ReferenceTest.java
  Log:
  - add new test check handling of multiple PK fields
  
  Revision  Changes    Path
  1.11      +95 -17    db-ojb/src/test/org/apache/ojb/broker/ReferenceTest.java
  
  Index: ReferenceTest.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/test/org/apache/ojb/broker/ReferenceTest.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- ReferenceTest.java	28 Jan 2004 23:55:30 -0000	1.10
  +++ ReferenceTest.java	6 Feb 2004 12:27:02 -0000	1.11
  @@ -26,19 +26,11 @@
           junit.textui.TestRunner.main(arr);
       }
   
  -    /**
  -     * Insert the method's description here.
  -     * Creation date: (06.12.2000 21:58:53)
  -     */
       public void setUp() throws PBFactoryException
       {
           broker = PersistenceBrokerFactory.defaultPersistenceBroker();
       }
   
  -    /**
  -     * Insert the method's description here.
  -     * Creation date: (06.12.2000 21:59:14)
  -     */
       public void tearDown()
       {
           try
  @@ -50,11 +42,64 @@
           }
       }
   
  -    public void testStoreWithMultiplePK_1() throws Exception
  +    /**
  +     * not really a reference test, here we check handling of objects
  +     * with multiple PK fields. Such an object was used in following
  +     * reference tests.
  +     */
  +    public void testHandlingOfMultiplePKFields() throws Exception
       {
  -        String timestamp = ""+System.currentTimeMillis();
  +        String timestamp = "testLookupWithMultiplePK_"+System.currentTimeMillis();
           String regionName = "baden_"+timestamp;
           String countryName = "germany_"+timestamp;
  +        /*
  +        Wine has a 1:1 reference with Region, we set the reference object in
  +        Wine class. We don't set the FK fields in Wine, this should be done by OJB
  +        automatic
  +        */
  +        Region region = new Region(regionName, countryName, "original");
  +
  +        broker.beginTransaction();
  +        broker.store(region);
  +        broker.commitTransaction();
  +
  +        Identity oid = new Identity(region, broker);
  +        broker.clearCache();
  +        Region loadedRegion = (Region) broker.getObjectByIdentity(oid);
  +
  +        assertNotNull(loadedRegion);
  +        assertEquals(region.getName() , loadedRegion.getName());
  +
  +        loadedRegion.setDescription("update_1");
  +        broker.beginTransaction();
  +        broker.store(loadedRegion);
  +        broker.commitTransaction();
  +        broker.clearCache();
  +        loadedRegion = (Region) broker.getObjectByIdentity(oid);
  +        assertNotNull(loadedRegion);
  +        assertEquals("update_1" , loadedRegion.getDescription());
  +
  +        loadedRegion.setDescription("update_2");
  +        broker.beginTransaction();
  +        broker.store(loadedRegion);
  +        broker.commitTransaction();
  +        broker.clearCache();
  +        loadedRegion = (Region) broker.getObjectByIdentity(oid);
  +        assertNotNull(loadedRegion);
  +        assertEquals("update_2" , loadedRegion.getDescription());
  +
  +        Criteria crit = new Criteria();
  +        crit.addLike("name", regionName);
  +        Query q = QueryFactory.newQuery(Region.class, crit);
  +        Collection result = broker.getCollectionByQuery(q);
  +        assertEquals(1, result.size());
  +    }
  +
  +    public void testStoreWithMultiplePK_1() throws Exception
  +    {
  +        String timestamp = "testStoreWithMultiplePK_1_"+System.currentTimeMillis();
  +        String regionName = "baden_1"+timestamp;
  +        String countryName = "germany_1"+timestamp;
           Region region = new Region(regionName, countryName, "brrr");
           Wine wine = new Wine(timestamp, "silvaner", "2003", regionName, countryName);
   
  @@ -71,13 +116,21 @@
           broker.retrieveAllReferences(wine);
           broker.store(wine);
           broker.commitTransaction();
  +
  +        Identity oid = new Identity(wine, broker);
  +        broker.clearCache();
  +        Wine loadedWine = (Wine) broker.getObjectByIdentity(oid);
  +        assertNotNull(loadedWine);
  +        assertEquals(wine.getGrape() , loadedWine.getGrape());
  +        assertNotNull(loadedWine.getRegion());
  +        assertEquals(wine.getRegion().getCountry(), loadedWine.getRegion().getCountry());
       }
   
       public void testStoreWithMultiplePK_2() throws Exception
       {
  -        String timestamp = ""+System.currentTimeMillis();
  -        String regionName = "baden_"+timestamp;
  -        String countryName = "germany_"+timestamp;
  +        String timestamp = "testStoreWithMultiplePK_2_"+System.currentTimeMillis();
  +        String regionName = "baden_2"+timestamp;
  +        String countryName = "germany_2"+timestamp;
           /*
           Wine has a 1:1 reference with Region, we set the reference object in
           Wine class. We don't set the FK fields in Wine, this should be done by OJB
  @@ -94,13 +147,21 @@
           broker.beginTransaction();
           broker.store(wine);
           broker.commitTransaction();
  +
  +        Identity oid = new Identity(wine, broker);
  +        broker.clearCache();
  +        Wine loadedWine = (Wine) broker.getObjectByIdentity(oid);
  +        assertNotNull(loadedWine);
  +        assertEquals(wine.getGrape() , loadedWine.getGrape());
  +        assertNotNull(loadedWine.getRegion());
  +        assertEquals(wine.getRegion().getCountry(), loadedWine.getRegion().getCountry());
       }
   
       public void testStoreWithMultiplePK_3() throws Exception
       {
  -        String timestamp = ""+System.currentTimeMillis();
  -        String regionName = "baden_"+timestamp;
  -        String countryName = "germany_"+timestamp;
  +        String timestamp = "testStoreWithMultiplePK_3_"+System.currentTimeMillis();
  +        String regionName = "baden_3"+timestamp;
  +        String countryName = "germany_3"+timestamp;
           /*
           Wine has a 1:1 reference with Region, we set set the FK fields
           of an existing Region object in Wine
  @@ -108,6 +169,7 @@
           */
           Region region = new Region(regionName, countryName, "brrr");
           Wine wine = new Wine(timestamp, "silvaner", "2003", regionName, countryName);
  +        wine.setRegion(region);
   
           broker.beginTransaction();
           broker.store(region);
  @@ -116,6 +178,14 @@
           broker.beginTransaction();
           broker.store(wine);
           broker.commitTransaction();
  +
  +        Identity oid = new Identity(wine, broker);
  +        broker.clearCache();
  +        Wine loadedWine = (Wine) broker.getObjectByIdentity(oid);
  +        assertNotNull(loadedWine);
  +        assertEquals(wine.getGrape() , loadedWine.getGrape());
  +        assertNotNull(loadedWine.getRegion());
  +        assertEquals(wine.getRegion().getCountry(), loadedWine.getRegion().getCountry());
       }
   
       public void testStoreReferencesMappedToSameTable()
  @@ -1074,6 +1144,10 @@
           private String regionCountry;
           private Region region;
   
  +        private Wine()
  +        {
  +        }
  +
           public Wine(String id, String grape, String year, String regionName, String regionCountry)
           {
               this.id = id;
  @@ -1149,6 +1223,10 @@
           private String name;
           private String country;
           private String description;
  +
  +        private Region()
  +        {
  +        }
   
           public Region(String name, String country, String description)
           {
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org