KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created by IntelliJ IDEA.
3  * User: Matt
4  * Date: Jun 10, 2002
5  * Time: 9:22:36 PM
6  * To change template for new class use
7  * Code Style | Class Templates options (Tools | IDE Options).
8  */

9 package org.apache.ojb.odmg;
10
11 import java.util.Collection JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14
15 import org.apache.ojb.broker.Article;
16 import org.apache.ojb.broker.InterfaceArticle;
17 import org.apache.ojb.broker.InterfaceProductGroup;
18 import org.apache.ojb.broker.Mammal;
19 import org.apache.ojb.broker.ProductGroup;
20 import org.apache.ojb.broker.Reptile;
21 import org.apache.ojb.broker.metadata.ClassDescriptor;
22 import org.apache.ojb.broker.metadata.CollectionDescriptor;
23 import org.apache.ojb.broker.metadata.MetadataManager;
24 import org.apache.ojb.junit.ODMGTestCase;
25 import org.apache.ojb.odmg.shared.ODMGZoo;
26 import org.odmg.ODMGException;
27 import org.odmg.OQLQuery;
28 import org.odmg.Transaction;
29
30 public class OneToManyTest extends ODMGTestCase
31 {
32     private static final int COUNT = 10;
33     int oldValue;
34
35     public static void main(String JavaDoc[] args)
36     {
37         String JavaDoc[] arr = {OneToManyTest.class.getName()};
38         junit.textui.TestRunner.main(arr);
39     }
40
41     public OneToManyTest(String JavaDoc name)
42     {
43         super(name);
44     }
45
46     public void setUp() throws Exception JavaDoc
47     {
48         super.setUp();
49         ClassDescriptor cld = MetadataManager.getInstance().getRepository().getDescriptorFor(ProductGroup.class);
50         CollectionDescriptor cod = cld.getCollectionDescriptorByName("allArticlesInGroup");
51         oldValue = cod.getCascadingStore();
52         // odmg-api need false
53
cod.setCascadeStore(false);
54     }
55
56     public void tearDown() throws Exception JavaDoc
57     {
58         ClassDescriptor cld = MetadataManager.getInstance().getRepository().getDescriptorFor(ProductGroup.class);
59         cld.getCollectionDescriptorByName("allArticlesInGroup").setCascadingStore(oldValue);
60         super.tearDown();
61     }
62
63     /**
64      * tests creation of new object that has a one to many relationship
65      *
66      * @throws Exception
67      */

68     public void testCreate() throws Exception JavaDoc
69     {
70         String JavaDoc name = "testCreate_" + System.currentTimeMillis();
71         /**
72          * 1. create the article.
73          */

74         Transaction tx = odmg.newTransaction();
75         tx.begin();
76         ProductGroup group = new ProductGroup();
77         group.setGroupName(name);
78         tx.lock(group, Transaction.WRITE);
79         for (int i = 0; i < COUNT; i++)
80         {
81             Article article = createArticle(name);
82             group.add(article);
83         }
84         tx.commit();
85         /**
86          * 2. query on the article to make sure it is everything we set it up to be.
87          */

88         tx.begin();
89         OQLQuery query = odmg.newOQLQuery();
90         query.create("select productGroup from " + ProductGroup.class.getName() + " where groupName=$1");
91         query.bind(name);
92         Collection JavaDoc results = (Collection JavaDoc) query.execute();
93         tx.commit();
94         Iterator JavaDoc it = results.iterator();
95         assertTrue(it.hasNext());
96         InterfaceProductGroup pg = (InterfaceProductGroup) it.next();
97         assertFalse(it.hasNext());
98         assertNotNull(pg.getAllArticles());
99         assertEquals(COUNT, pg.getAllArticles().size());
100     }
101
102     /**
103      * tests creation of new object that has a one to many relationship.
104      * thma: this test will not work, because ODMG is no able to track
105      * modifictations to normal collections.
106      * Only Odmg Collections like DList will be treated properly.
107      *
108      * @throws Exception
109      */

110     public void testUpdateWithProxy() throws Exception JavaDoc
111     {
112         // arminw: fixed
113
// if(ojbSkipKnownIssueProblem()) return;
114
String JavaDoc name = "testUpdateWithProxy_" + System.currentTimeMillis();
115
116         ProductGroup pg1 = new ProductGroup(null, name + "_1", "a group");
117         ProductGroup pg2 = new ProductGroup(null, name + "_2", "a group");
118         Article a1 = createArticle(name);
119         a1.setProductGroup(pg1);
120         TransactionExt tx = (TransactionExt) odmg.newTransaction();
121         tx.begin();
122         database.makePersistent(a1);
123         database.makePersistent(pg1);
124         database.makePersistent(pg2);
125         tx.commit();
126
127         tx.begin();
128         tx.getBroker().clearCache();
129         /**
130          * 1. get all articles from groups and add a new article
131          */

132         OQLQuery query = odmg.newOQLQuery();
133         query.create("select productGroup from " + ProductGroup.class.getName() + " where groupName like $1");
134         query.bind(name + "%");
135         Collection JavaDoc results = (Collection JavaDoc) query.execute();
136         assertEquals(2, results.size());
137         Iterator JavaDoc it = results.iterator();
138         InterfaceProductGroup temp = null;
139         /**
140          * to each productgroup add an article with a pre-determined name.
141          */

142         while (it.hasNext())
143         {
144             Article article = createArticle(name);
145             temp = (InterfaceProductGroup) it.next();
146             tx.lock(temp, Transaction.WRITE);
147             temp.add(article);
148             article.setProductGroup(temp);
149         }
150         tx.commit();
151
152         /**
153          * 2. requery and find the articles we added.
154          */

155         tx.begin();
156         query = odmg.newOQLQuery();
157         query.create("select productGroup from " + ProductGroup.class.getName() + " where groupName like $1");
158         query.bind(name + "%");
159         results = (Collection JavaDoc) query.execute();
160         tx.commit();
161
162         assertEquals(2, results.size());
163         it = results.iterator();
164         int counter = 0;
165         temp = null;
166         while (it.hasNext())
167         {
168             temp = (InterfaceProductGroup) it.next();
169             Collection JavaDoc articles = temp.getAllArticles();
170             counter = counter + articles.size();
171             Iterator JavaDoc it2 = articles.iterator();
172             while (it2.hasNext())
173             {
174                 InterfaceArticle art = (InterfaceArticle) it2.next();
175                 if (art.getArticleName() != null)
176                 {
177                     assertEquals(name, art.getArticleName());
178                 }
179             }
180         }
181         assertEquals(3, counter);
182     }
183
184     /**
185      * this tests if polymorph collections (i.e. collections of objects
186      * implementing a common interface) are treated correctly
187      */

188     public void testPolymorphOneToMany()
189     {
190
191         ODMGZoo myZoo = new ODMGZoo("London");
192         Mammal elephant = new Mammal(37, "Jumbo", 4);
193         Mammal cat = new Mammal(11, "Silvester", 4);
194         Reptile snake = new Reptile(3, "Kaa", "green");
195
196         myZoo.addAnimal(snake);
197         myZoo.addAnimal(elephant);
198         myZoo.addAnimal(cat);
199
200         try
201         {
202             Transaction tx = odmg.newTransaction();
203             tx.begin();
204             database.makePersistent(myZoo);
205             tx.commit();
206
207             int id = myZoo.getZooId();
208
209             tx = odmg.newTransaction();
210             tx.begin();
211             OQLQuery query = odmg.newOQLQuery();
212             query.create("select zoos from " + ODMGZoo.class.getName() +
213                     " where zooId=$1");
214             query.bind(new Integer JavaDoc(id));
215             List JavaDoc zoos = (List JavaDoc) query.execute();
216             assertEquals(1, zoos.size());
217             ODMGZoo zoo = (ODMGZoo) zoos.get(0);
218             tx.commit();
219             assertEquals(3, zoo.getAnimals().size());
220
221
222         }
223         catch (ODMGException e)
224         {
225             e.printStackTrace();
226             fail("ODMGException thrown " + e.getMessage());
227         }
228     }
229
230     /**
231      * Create an article with 4 product groups related to it in a 1-N relationship
232      */

233     protected Article createArticle(String JavaDoc name)
234     {
235         Article a = Article.createInstance();
236         a.setArticleName(name);
237         a.setIsSelloutArticle(true);
238         a.setMinimumStock(100);
239         a.setOrderedUnits(17);
240         a.setPrice(0.45);
241         //a.setProductGroupId(1);
242
a.setStock(234);
243         a.setSupplierId(4);
244         a.setUnit("bottle");
245         return a;
246     }
247 }
248
Popular Tags