KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.odmg;
2
3 import java.util.List JavaDoc;
4
5 import org.apache.ojb.broker.Identity;
6 import org.apache.ojb.broker.PersistenceBroker;
7 import org.apache.ojb.broker.TestHelper;
8 import org.apache.ojb.broker.metadata.ClassDescriptor;
9 import org.apache.ojb.junit.OJBTestCase;
10 import org.apache.ojb.odmg.collections.DListImpl;
11 import org.apache.ojb.odmg.shared.Article;
12 import org.apache.ojb.odmg.shared.ProductGroup;
13 import org.apache.ojb.odmg.states.ModificationState;
14 import org.apache.ojb.odmg.states.StateNewClean;
15 import org.apache.ojb.odmg.states.StateNewDirty;
16 import org.odmg.DCollection;
17 import org.odmg.Database;
18 import org.odmg.Implementation;
19 import org.odmg.ODMGException;
20 import org.odmg.OQLQuery;
21 import org.odmg.Transaction;
22
23 /** Demo Application that shows basic concepts for Applications using the OJB ODMG
24  * implementation as an transactional object server.
25  */

26 public class OdmgExamples extends OJBTestCase
27 {
28     public static void main(String JavaDoc[] args)
29     {
30         String JavaDoc[] arr = {OdmgExamples.class.getName()};
31         junit.textui.TestRunner.main(arr);
32     }
33
34     private String JavaDoc databaseName;
35
36     public OdmgExamples(String JavaDoc name)
37     {
38         super(name);
39     }
40
41     public void setUp()
42     {
43         databaseName = TestHelper.DEF_DATABASE_NAME;
44     }
45
46     public void tearDown()
47     {
48         databaseName = null;
49     }
50
51     /**TestThreadsNLocks state transition of modification states*/
52     public void testModificationStates()
53     {
54         // get facade instance
55
Implementation odmg = OJB.getInstance();
56         Database db = odmg.newDatabase();
57         //open database
58
try
59         {
60             db.open(databaseName, Database.OPEN_READ_WRITE);
61         }
62         catch (ODMGException ex)
63         {
64             fail("ODMGException: " + ex.getMessage());
65         }
66         ModificationState oldState = StateNewClean.getInstance();
67         ModificationState newState = oldState.markDirty();
68         assertEquals(StateNewDirty.getInstance(), newState);
69
70         oldState = newState;
71         newState = oldState.markDirty();
72         assertEquals(oldState, newState);
73
74         // close database
75
try
76         {
77             db.close();
78         }
79         catch (ODMGException ex)
80         {
81             fail("ODMGException: " + ex.getMessage());
82         }
83
84     }
85
86     public void testOdmgSession()
87     {
88         // get facade instance
89
Implementation odmg = OJB.getInstance();
90         Database db = odmg.newDatabase();
91         //open database
92
try
93         {
94             db.open(databaseName, Database.OPEN_READ_WRITE);
95         }
96         catch (ODMGException ex)
97         {
98             fail("ODMGException: " + ex.getMessage());
99         }
100         Transaction tx = odmg.newTransaction();
101
102         //perform transaction
103
try
104         {
105             tx.begin();
106
107             ProductGroup pg = new ProductGroup();
108             pg.setName("PG A");
109             Article example = new Article();
110             example.setProductGroup(pg);
111             pg.addArticle(example);
112             db.makePersistent(pg);
113
114             // modify Object after persist call is allowed
115
example.setStock(333);
116             example.addToStock(47);
117             example.addToStock(7);
118             example.addToStock(4);
119
120             //System.out.println("now commit all changes...");
121
tx.commit();
122         }
123         catch (Exception JavaDoc ex)
124         {
125             tx.abort();
126         }
127
128         // close database
129
try
130         {
131             db.close();
132         }
133         catch (ODMGException ex)
134         {
135             fail("ODMGException: " + ex.getMessage());
136         }
137     }
138
139     public void testOQLQuery()
140     {
141         // get facade instance
142
Implementation odmg = OJB.getInstance();
143         Database db = odmg.newDatabase();
144         //open database
145
try
146         {
147             db.open(databaseName, Database.OPEN_READ_WRITE);
148         }
149         catch (ODMGException ex)
150         {
151             fail("ODMGException: " + ex.getMessage());
152         }
153         Transaction tx = odmg.newTransaction();
154
155         //perform transaction
156
try
157         {
158             tx.begin();
159
160             OQLQuery query = odmg.newOQLQuery();
161             query.create("select anArticle from " + Article.class.getName() + " where articleId = 60");
162             List JavaDoc results = (List JavaDoc) query.execute();
163
164             Article a = (Article) results.get(0);
165
166             // cross check with PersistenceBroker lookup
167
// 1. get OID
168
Article example = new Article();
169             example.setArticleId(60);
170             Identity oid = new Identity(example, ((TransactionImpl) tx).getBroker());
171             // 2. lookup object by OID
172
PersistenceBroker broker = ((TransactionImpl) tx).getBroker();
173             broker.clearCache();
174             Article b = (Article) broker.getObjectByIdentity(oid);
175
176             assertEquals("should be same object", a, b);
177
178             //System.out.println("now commit all changes...");
179
tx.commit();
180         }
181         catch (Exception JavaDoc ex)
182         {
183             tx.abort();
184             fail("ODMGException: " + ex.getMessage());
185         }
186
187         // close database
188
try
189         {
190             db.close();
191         }
192         catch (ODMGException ex)
193         {
194             fail("ODMGException: " + ex.getMessage());
195         }
196     }
197
198     public void testPathExpressionOqlQuery() throws Exception JavaDoc
199     {
200         // get facade instance
201
Implementation odmg = OJB.getInstance();
202         Database db = odmg.newDatabase();
203         //open database
204
try
205         {
206             db.open(databaseName, Database.OPEN_READ_WRITE);
207         }
208         catch (ODMGException ex)
209         {
210             fail("ODMGException: " + ex.getMessage());
211         }
212         Transaction tx = odmg.newTransaction();
213
214         // perform transaction
215
tx.begin();
216
217         OQLQuery query = odmg.newOQLQuery();
218         // use 'like' instead of '=' when perform query with wildcards
219
query.create(
220                 "select anArticle from " + Article.class.getName() + " where productGroup.groupName like \"Fruit*\"");
221         List JavaDoc results = (List JavaDoc) query.execute();
222
223         // crosscheck
224
query = odmg.newOQLQuery();
225         query.create("select aPG from " + ProductGroup.class.getName() + " where groupName like \"Fruit*\"");
226         List JavaDoc check = (List JavaDoc) query.execute();
227         if (check.size() < 1)
228             fail("Could not found ProductGroup's for: " +
229                     "select aPG from " + ProductGroup.class.getName() + " where groupName like \"Fruit*\"");
230         ProductGroup pg = (ProductGroup) check.get(0);
231
232         assertEquals(pg.getAllArticlesInGroup().size(), results.size());
233         assertTrue((results.size() > 0));
234
235         tx.commit();
236
237
238         // close database
239

240         db.close();
241     }
242
243     public void testNrmAndDlists() throws Exception JavaDoc
244     {
245         // get facade instance
246
Implementation odmg = OJB.getInstance();
247         Database db = odmg.newDatabase();
248         //open database
249
try
250         {
251             db.open(databaseName, Database.OPEN_READ_WRITE);
252         }
253         catch (ODMGException ex)
254         {
255             fail("ODMGException: " + ex.getMessage());
256         }
257         Transaction tx = odmg.newTransaction();
258
259         //perform transaction
260
try
261         {
262             //=============================
263
// this test needs DList impl as oql query collection class
264
((ImplementationImpl) odmg).setOqlCollectionClass(DListImpl.class);
265             //=============================
266

267             tx.begin();
268
269             OQLQuery query = odmg.newOQLQuery();
270             query.create("select x from " + Article.class.getName() + " where productGroupId = 7");
271             List JavaDoc results = (List JavaDoc) query.execute();
272
273             int originalSize = results.size();
274             assertTrue("result count have to be > 0", originalSize > 0);
275
276 // OJB.getLogger().debug(results);
277

278             String JavaDoc name = "gimme fruits_" + System.currentTimeMillis();
279
280             db.bind(results, name);
281             tx.commit();
282
283             tx = odmg.newTransaction();
284             tx.begin();
285
286             ((TransactionImpl) tx).getBroker().clearCache();
287
288             // look it up again
289
List JavaDoc newResults = (List JavaDoc) db.lookup(name);
290
291             assertEquals(originalSize, newResults.size());
292             Article art = (Article) newResults.get(0);
293             assertNotNull(art);
294 // OJB.getLogger().info(results);
295

296             tx.commit();
297
298         }
299         catch (Exception JavaDoc e)
300
301         {
302             tx.abort();
303             throw e;
304         }
305
306         // close database
307
try
308         {
309             db.close();
310         }
311         catch (ODMGException ex)
312         {
313             fail("ODMGException: " + ex.getMessage());
314         }
315     }
316
317     public void testOQLQueryBind()
318     {
319         // get facade instance
320
Implementation odmg = OJB.getInstance();
321         Database db = odmg.newDatabase();
322         //open database
323
try
324         {
325             db.open(databaseName, Database.OPEN_READ_WRITE);
326         }
327         catch (ODMGException ex)
328         {
329             fail("ODMGException: " + ex.getMessage());
330         }
331         Transaction tx = odmg.newTransaction();
332
333         //perform transaction
334
try
335         {
336             tx.begin();
337
338             OQLQuery query = odmg.newOQLQuery();
339             query.create("select anArticle from " + Article.class.getName() + " where articleId = $678");
340             query.bind(new Integer JavaDoc(30));
341
342             List JavaDoc results = (List JavaDoc) query.execute();
343
344             Article a = (Article) results.get(0);
345
346             //crosscheck with PersistenceBroker lookup
347
// 1. get OID
348
Article example = new Article();
349             example.setArticleId(30);
350             Identity oid = new Identity(example, ((TransactionImpl) tx).getBroker());
351
352             // 2. lookup object by OID
353
PersistenceBroker broker = ((TransactionImpl) tx).getBroker();
354             broker.clearCache();
355             Article b = (Article) broker.getObjectByIdentity(oid);
356
357             assertEquals("should be same object", a, b);
358
359             //System.out.println("now commit all changes...");
360
tx.commit();
361         }
362         catch (Exception JavaDoc ex)
363
364         {
365             tx.abort();
366             fail("ODMGException: " + ex.getMessage());
367         }
368
369         // close database
370
try
371         {
372             db.close();
373         }
374         catch (ODMGException ex)
375         {
376             fail("ODMGException: " + ex.getMessage());
377         }
378     }
379
380     public void YYYtestOQLQueryOnCollections() throws Exception JavaDoc
381     {
382         // get facade instance
383
Implementation odmg = OJB.getInstance();
384         Database db = odmg.newDatabase();
385         //open database
386
db.open(databaseName, Database.OPEN_READ_WRITE);
387         Transaction tx = odmg.newTransaction();
388
389         //perform transaction
390
try
391         {
392             tx.begin();
393             OQLQuery query = odmg.newOQLQuery();
394             query.create("select aLotOfArticles from " + Article.class.getName() + " where productGroupId = 4");
395
396             DCollection results = (DCollection) query.execute();
397             results = results.query("price > 35");
398
399             // now perform control query
400
query = odmg.newOQLQuery();
401             query.create(
402                     "select aLotOfArticles from "
403                     + Article.class.getName()
404                     + " where productGroupId = 4 and price > 35");
405
406             DCollection check = (DCollection) query.execute();
407
408             assertEquals(results, check);
409
410             tx.commit();
411         }
412                 // close database
413
finally
414         {
415             db.close();
416         }
417     }
418
419     /**try to open non-existing db*/
420     public void YYYtestWrongDbName()
421     {
422         // get facade instance
423
Implementation objectserver = OJB.getInstance();
424         Database db = objectserver.newDatabase();
425
426         //try open database with non existing repository file:
427
String JavaDoc wrongDatabaseName = "ThereIsNoSuchFile";
428         try
429         {
430             db.open(wrongDatabaseName, Database.OPEN_READ_WRITE);
431             fail("should not be able to open database " + wrongDatabaseName);
432         }
433         catch (ODMGException ex)
434         {
435             return;
436         }
437     }
438
439     /**try to crash odmg and broker tx*/
440     public void YYYtestBrokerCrash()
441     {
442         // get facade instance
443
Implementation odmg = OJB.getInstance();
444         Database db = odmg.newDatabase();
445         PersistenceBroker broker = null;
446         ClassDescriptor cld = null;
447         String JavaDoc tablename = null;
448
449         //open database
450
try
451         {
452             db.open(databaseName, Database.OPEN_READ_WRITE);
453         }
454         catch (ODMGException ex)
455         {
456             fail("ODMGException: " + ex.getMessage());
457         }
458         try
459         {
460             Transaction tx = odmg.newTransaction();
461             tx.begin();
462
463             // retrieve an Article
464
OQLQuery query = odmg.newOQLQuery();
465             query.create("select anArticle from " + Article.class.getName() + " where articleId = $678");
466             query.bind(new Integer JavaDoc(30));
467             List JavaDoc results = (List JavaDoc) query.execute();
468             Article a = (Article) results.get(0);
469
470             // manipulate metadata
471
broker = ((TransactionImpl) tx).getBroker();
472             cld = broker.getClassDescriptor(Article.class);
473             tablename = cld.getFullTableName();
474             cld.setTableName("ELVIS");
475             broker.getDescriptorRepository().setClassDescriptor(cld);
476
477             //broker will crash as metadata is corrupt
478
a.addToStock(5);
479             tx.commit();
480             fail("Can commit tx with corrupt metadata");
481         }
482         catch (Throwable JavaDoc t)
483         {
484             //ignore
485
}
486         finally
487         {
488             cld.setTableName(tablename);
489             broker.getDescriptorRepository().setClassDescriptor(cld);
490         }
491
492     }
493 }
494
Popular Tags