KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.odmg;
2
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6
7 import org.apache.ojb.junit.ODMGTestCase;
8 import org.apache.ojb.odmg.shared.Article;
9 import org.apache.ojb.odmg.shared.ProductGroup;
10 import org.odmg.DList;
11 import org.odmg.ObjectNameNotFoundException;
12 import org.odmg.ObjectNameNotUniqueException;
13 import org.odmg.Transaction;
14
15 /**
16  * Demo Application that shows basic concepts for Applications using the OJB ODMG
17  * implementation as an transactional object server.
18  *
19  * @version $Id: NamedRootsTest.java,v 1.1.2.4 2005/08/16 15:04:38 aclute Exp $
20  */

21 public class NamedRootsTest extends ODMGTestCase
22 {
23     private ProductGroup testProductGroup;
24
25     public static void main(String JavaDoc[] args)
26     {
27         String JavaDoc[] arr = {NamedRootsTest.class.getName()};
28         junit.textui.TestRunner.main(arr);
29     }
30
31     public NamedRootsTest(String JavaDoc name)
32     {
33         super(name);
34     }
35
36     protected void setUp() throws Exception JavaDoc
37     {
38         super.setUp();
39         Transaction tx = odmg.newTransaction();
40         tx.begin();
41         testProductGroup = new ProductGroup();
42         testProductGroup.setGroupName("NamedRootsTest_" + System.currentTimeMillis());
43         database.makePersistent(testProductGroup);
44         tx.commit();
45     }
46
47     private Article createArticle()
48     {
49         Article example = new Article();
50         example.setArticleName(testProductGroup.getName());
51         example.setProductGroupId(testProductGroup.getId());
52         return example;
53     }
54
55     public void testBindPersistentCapableObjectCollection() throws Exception JavaDoc
56     {
57         String JavaDoc bindingName = "testBindPersistentCapableObjectCollection_" + System.currentTimeMillis();
58
59         TransactionExt tx = (TransactionExt) odmg.newTransaction();
60         //bind object to name
61
tx.begin();
62
63         // get new DList instance
64
DList dlist = odmg.newDList();
65         Article a1 = createArticle();
66         Article a2 = createArticle();
67         Article a3 = createArticle();
68         dlist.add(a1);
69         dlist.add(a2);
70         dlist.add(a3);
71         database.bind(dlist, bindingName);
72         // lookup the named object - DList
73
List JavaDoc value = (List JavaDoc) database.lookup(bindingName);
74         assertNotNull("Could not lookup object for binding name: "+bindingName, value);
75         tx.commit();
76
77         try
78         {
79             tx.begin();
80             database.bind(dlist, bindingName);
81             tx.commit();
82             fail("We expected a ObjectNameNotUniqueException, but was not thrown");
83         }
84         catch (ObjectNameNotUniqueException ex)
85         {
86             // we wait for this exception
87
assertTrue(true);
88             tx.abort();
89         }
90
91         try
92         {
93             tx.begin();
94             tx.getBroker().clearCache();
95             database.bind(dlist, bindingName);
96             tx.commit();
97             fail("We expected a ObjectNameNotUniqueException, but was not thrown");
98         }
99         catch (ObjectNameNotUniqueException ex)
100         {
101             // we wait for this exception
102
assertTrue(true);
103             tx.abort();
104         }
105
106         tx.begin();
107         List JavaDoc result = (List JavaDoc) database.lookup(bindingName);
108         assertNotNull(result);
109         assertEquals(3, result.size());
110         Article newA1 = (Article) result.get(0);
111         assertNotNull(newA1);
112         assertEquals(a1.getArticleName(), newA1.getArticleName());
113         tx.commit();
114
115         tx.begin();
116         // we want to completely remove the named object
117
// the persisted DList with all DList entries,
118
// but the Article objects itself shouldn't be deleted:
119
// 1. mandatory, clear the list to remove all entries
120
result.clear();
121         // 2. unbind named object
122
database.unbind(bindingName);
123
124         // alternative can be used
125
//tx.setCascadingDelete(DListImpl.class, true);
126
//database.unbind(bindingName);
127
tx.commit();
128
129         tx.begin();
130         try
131         {
132             database.lookup(bindingName);
133         }
134         catch(ObjectNameNotFoundException e)
135         {
136             // expected exception
137
assertTrue(true);
138         }
139         tx.commit();
140     }
141
142     public void testBindPersistentCapableObject() throws Exception JavaDoc
143     {
144         String JavaDoc bindingName = "testBindPersistentCapableObject_" + System.currentTimeMillis();
145         TransactionImpl tx = (TransactionImpl) odmg.newTransaction();
146         //bind object to name
147
tx.begin();
148         Article example = createArticle();
149         database.bind(example, bindingName);
150         Article value = (Article) database.lookup(bindingName);
151         assertTrue("Could not lookup object for binding name: "+bindingName, value != null);
152         tx.commit();
153
154         try
155         {
156             tx.begin();
157             database.bind(example, bindingName);
158             tx.commit();
159             fail("We expected a ObjectNameNotUniqueException, but was not thrown");
160         }
161         catch (ObjectNameNotUniqueException ex)
162         {
163             // we wait for this exception
164
assertTrue(true);
165             tx.abort();
166         }
167
168         tx.begin();
169         // this only remove the named object link, the Article object
170
// itself will not be touched
171
database.unbind(bindingName);
172         tx.commit();
173     }
174
175     public void testBindPersistentSerialzableObject() throws Exception JavaDoc
176     {
177         String JavaDoc bindingName = "testBindPersistentSerialzableObject_" + System.currentTimeMillis();
178         TransactionImpl tx = (TransactionImpl) odmg.newTransaction();
179         //bind object to name
180
tx.begin();
181         List JavaDoc example = new ArrayList JavaDoc();
182         example.add("Merkur");
183         example.add("Venus");
184         database.bind(example, bindingName);
185         List JavaDoc value = (List JavaDoc) database.lookup(bindingName);
186         assertNotNull("Could not lookup object for binding name: "+bindingName, value);
187         assertEquals(2, value.size());
188         tx.commit();
189
190         try
191         {
192             tx.begin();
193             database.bind(example, bindingName);
194             tx.commit();
195             fail("We expected a ObjectNameNotUniqueException, but was not thrown");
196         }
197         catch (ObjectNameNotUniqueException ex)
198         {
199             // we wait for this exception
200
assertTrue(true);
201             tx.abort();
202         }
203
204         tx.begin();
205         database.unbind(bindingName);
206         tx.commit();
207
208         tx.begin();
209         example.add("earth");
210         example.add("mars");
211         database.bind(example, bindingName);
212         value = (List JavaDoc) database.lookup(bindingName);
213         assertNotNull("Could not lookup object for binding name: "+bindingName, value);
214         assertEquals(4, value.size());
215         tx.commit();
216
217         tx.begin();
218         database.unbind(bindingName);
219         tx.commit();
220     }
221
222     public void testDoubleBindInOneTx() throws Exception JavaDoc
223     {
224         String JavaDoc bindingName = "testDoubleBindInOneTx_" + System.currentTimeMillis();
225
226         Article article = createArticle();
227         Article foundArticle = null;
228
229         Transaction tx = odmg.newTransaction();
230         tx.begin();
231         database.bind(article, bindingName);
232
233         foundArticle = (Article) database.lookup(bindingName);
234         assertNotNull(foundArticle);
235
236         foundArticle = null;
237         database.unbind(bindingName);
238         try
239         {
240             foundArticle = (Article) database.lookup(bindingName);
241             fail("Found unbound DList");
242         }
243         catch (ObjectNameNotFoundException ex)
244         {
245             // expected exception
246
assertTrue(true);
247         }
248
249         database.bind(article, bindingName);
250         foundArticle = (Article) database.lookup(bindingName);
251
252         foundArticle = null;
253         tx.commit();
254
255         tx = odmg.newTransaction();
256         tx.begin();
257         foundArticle = (Article) database.lookup(bindingName);
258         assertNotNull(foundArticle);
259         database.unbind(bindingName);
260         tx.commit();
261     }
262
263
264     public void testLookup() throws Exception JavaDoc
265     {
266         String JavaDoc bindingName = "testLookup_" + System.currentTimeMillis();
267         // clear named roots.
268
TransactionImpl tx = (TransactionImpl) odmg.newTransaction();
269         //bind object to name
270
tx.begin();
271         Article example = createArticle();
272         database.makePersistent(example);
273         tx.commit();
274
275         tx.begin();
276         database.bind(example, bindingName);
277         tx.commit();
278
279         // TestThreadsNLocks look up
280
Article lookedUp1 = null;
281         tx = (TransactionImpl) odmg.newTransaction();
282         tx.begin();
283         // lookup by name binding
284
lookedUp1 = (Article) database.lookup(bindingName);
285         tx.commit();
286
287         // looking up object by OID should return same Object as by name
288
assertEquals("lookups should return identical object", example, lookedUp1);
289
290         tx.begin();
291         database.unbind(bindingName);
292         tx.commit();
293     }
294
295     public void testUnBind() throws Exception JavaDoc
296     {
297         String JavaDoc name = "testUnBind_" + System.currentTimeMillis();
298
299         Transaction tx = odmg.newTransaction();
300         //bind object to name
301
tx.begin();
302         Article example = createArticle();
303         database.makePersistent(example);
304         tx.commit();
305
306         // 1. perform binding
307
tx.begin();
308         try
309         {
310             database.bind(example, name);
311             tx.commit();
312         }
313         catch (ObjectNameNotUniqueException ex)
314         {
315             tx.abort();
316             fail(ex.getMessage());
317         }
318
319         // 2. perform unbind
320
tx = odmg.newTransaction();
321         tx.begin();
322         try
323         {
324             database.unbind(name);
325             tx.commit();
326         }
327         catch (ObjectNameNotFoundException ex)
328         {
329             tx.abort();
330             fail("name " + name + "should be known");
331         }
332
333         // 3. check if name is really unknown now
334
tx = odmg.newTransaction();
335         tx.begin();
336         try
337         {
338             Article value = (Article) database.lookup(name);
339             assertNotNull("Should not find unbind name '" + name+"'", value);
340             fail("name " + name + " should not be known after unbind");
341         }
342         catch (ObjectNameNotFoundException ex)
343         {
344             // OK, expected
345
assertTrue(true);
346         }
347         tx.abort();
348     }
349 }
350
Popular Tags