KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > OneToManyTest


1 package org.apache.ojb.broker;
2
3 import java.util.List JavaDoc;
4
5 import org.apache.ojb.junit.PBTestCase;
6
7 /**
8  * tests for the essential create/update/delete/read for one-to-many related objects
9  */

10
11 public class OneToManyTest extends PBTestCase
12 {
13     public static void main(String JavaDoc[] args)
14     {
15         String JavaDoc[] arr = {OneToManyTest.class.getName()};
16         junit.textui.TestRunner.main(arr);
17     }
18
19     public OneToManyTest(String JavaDoc name)
20     {
21         super(name);
22     }
23
24     /**
25      * test the removal aware functionality.
26      */

27     public void testDeleteWithRemovalAwareCollection()
28     {
29         long timestamp = System.currentTimeMillis();
30
31         ProductGroupWithRemovalAwareCollection pg = new ProductGroupWithRemovalAwareCollection();
32         // auto-increment was not enabled in repository
33
// thus we set our own
34
pg.setId((int) timestamp%Integer.MAX_VALUE);
35         pg.setGroupName("testDeleteWithRemovalAwareCollection_"+timestamp);
36
37         Identity pgId = new Identity(pg, broker);
38
39         Article a = new Article();
40         a.setArticleName("testDeleteWithRemovalAwareCollection_"+timestamp);
41
42         Article b = new Article();
43         b.setArticleName("testDeleteWithRemovalAwareCollection_"+timestamp);
44
45         Article c = new Article();
46         c.setArticleName("testDeleteWithRemovalAwareCollection_"+timestamp);
47
48         pg.add(a);
49         pg.add(b);
50         pg.add(c);
51         broker.beginTransaction();
52         broker.store(pg);
53         broker.commitTransaction();
54
55         broker.clearCache();
56         pg = (ProductGroupWithRemovalAwareCollection) broker.getObjectByIdentity(pgId);
57         assertEquals(3,pg.getAllArticles().size());
58
59         pg.getAllArticles().remove(c);
60         pg.getAllArticles().remove(0);
61         broker.beginTransaction();
62         broker.store(pg);
63         broker.commitTransaction();
64
65         broker.clearCache();
66         pg = (ProductGroupWithRemovalAwareCollection) broker.getObjectByIdentity(pgId);
67         assertEquals(1,pg.getAllArticles().size());
68     }
69
70     /**
71      * this tests if polymorph collections (i.e. collections of objects
72      * implementing a common interface) are treated correctly
73      */

74     public void testPolymorphOneToMany()
75     {
76         long timestamp = System.currentTimeMillis();
77
78         Zoo myZoo = new Zoo("London_"+timestamp);
79         Identity id = new Identity(myZoo, broker);
80
81         Mammal elephant = new Mammal(2,"Jumbo_"+timestamp,4);
82         Mammal cat = new Mammal(2,"Silvester_"+timestamp,4);
83         Reptile snake = new Reptile(2,"Kaa_"+timestamp,"green");
84
85         myZoo.addAnimal(snake);
86         myZoo.addAnimal(elephant);
87         myZoo.addAnimal(cat);
88 // System.out.println("## "+myZoo);
89
broker.beginTransaction();
90         broker.store(myZoo);
91         broker.commitTransaction();
92 // System.out.println("## "+myZoo);
93

94         broker.clearCache();
95
96         Zoo loadedZoo = (Zoo) broker.getObjectByIdentity(id);
97         List JavaDoc animals = loadedZoo.getAnimals();
98         assertEquals(3, animals.size());
99
100         broker.beginTransaction();
101         for (int i = 0; i < animals.size(); i++)
102         {
103             broker.delete(animals.get(i));
104         }
105         broker.delete(loadedZoo);
106         broker.commitTransaction();
107     }
108 }
109
Popular Tags