Source Code Cross Referenced for OdmgExamples.java in  » Database-ORM » db-ojb » org » apache » ojb » odmg » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » db ojb » org.apache.ojb.odmg 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.ojb.odmg;
002:
003:        import java.util.List;
004:
005:        import org.apache.ojb.broker.Identity;
006:        import org.apache.ojb.broker.PersistenceBroker;
007:        import org.apache.ojb.broker.TestHelper;
008:        import org.apache.ojb.broker.metadata.ClassDescriptor;
009:        import org.apache.ojb.junit.OJBTestCase;
010:        import org.apache.ojb.odmg.collections.DListImpl;
011:        import org.apache.ojb.odmg.shared.Article;
012:        import org.apache.ojb.odmg.shared.ProductGroup;
013:        import org.apache.ojb.odmg.states.ModificationState;
014:        import org.apache.ojb.odmg.states.StateNewClean;
015:        import org.apache.ojb.odmg.states.StateNewDirty;
016:        import org.odmg.DCollection;
017:        import org.odmg.Database;
018:        import org.odmg.Implementation;
019:        import org.odmg.ODMGException;
020:        import org.odmg.OQLQuery;
021:        import org.odmg.Transaction;
022:
023:        /** Demo Application that shows basic concepts for Applications using the OJB ODMG
024:         * implementation as an transactional object server.
025:         */
026:        public class OdmgExamples extends OJBTestCase {
027:            public static void main(String[] args) {
028:                String[] arr = { OdmgExamples.class.getName() };
029:                junit.textui.TestRunner.main(arr);
030:            }
031:
032:            private String databaseName;
033:
034:            public OdmgExamples(String name) {
035:                super (name);
036:            }
037:
038:            public void setUp() {
039:                databaseName = TestHelper.DEF_DATABASE_NAME;
040:            }
041:
042:            public void tearDown() {
043:                databaseName = null;
044:            }
045:
046:            /**TestThreadsNLocks state transition of modification states*/
047:            public void testModificationStates() {
048:                // get facade instance
049:                Implementation odmg = OJB.getInstance();
050:                Database db = odmg.newDatabase();
051:                //open database
052:                try {
053:                    db.open(databaseName, Database.OPEN_READ_WRITE);
054:                } catch (ODMGException ex) {
055:                    fail("ODMGException: " + ex.getMessage());
056:                }
057:                ModificationState oldState = StateNewClean.getInstance();
058:                ModificationState newState = oldState.markDirty();
059:                assertEquals(StateNewDirty.getInstance(), newState);
060:
061:                oldState = newState;
062:                newState = oldState.markDirty();
063:                assertEquals(oldState, newState);
064:
065:                // close database
066:                try {
067:                    db.close();
068:                } catch (ODMGException ex) {
069:                    fail("ODMGException: " + ex.getMessage());
070:                }
071:
072:            }
073:
074:            public void testOdmgSession() {
075:                // get facade instance
076:                Implementation odmg = OJB.getInstance();
077:                Database db = odmg.newDatabase();
078:                //open database
079:                try {
080:                    db.open(databaseName, Database.OPEN_READ_WRITE);
081:                } catch (ODMGException ex) {
082:                    fail("ODMGException: " + ex.getMessage());
083:                }
084:                Transaction tx = odmg.newTransaction();
085:
086:                //perform transaction
087:                try {
088:                    tx.begin();
089:
090:                    ProductGroup pg = new ProductGroup();
091:                    pg.setName("PG A");
092:                    Article example = new Article();
093:                    example.setProductGroup(pg);
094:                    pg.addArticle(example);
095:                    db.makePersistent(pg);
096:
097:                    // modify Object after persist call is allowed
098:                    example.setStock(333);
099:                    example.addToStock(47);
100:                    example.addToStock(7);
101:                    example.addToStock(4);
102:
103:                    //System.out.println("now commit all changes...");
104:                    tx.commit();
105:                } catch (Exception ex) {
106:                    tx.abort();
107:                }
108:
109:                // close database
110:                try {
111:                    db.close();
112:                } catch (ODMGException ex) {
113:                    fail("ODMGException: " + ex.getMessage());
114:                }
115:            }
116:
117:            public void testOQLQuery() {
118:                // get facade instance
119:                Implementation odmg = OJB.getInstance();
120:                Database db = odmg.newDatabase();
121:                //open database
122:                try {
123:                    db.open(databaseName, Database.OPEN_READ_WRITE);
124:                } catch (ODMGException ex) {
125:                    fail("ODMGException: " + ex.getMessage());
126:                }
127:                Transaction tx = odmg.newTransaction();
128:
129:                //perform transaction
130:                try {
131:                    tx.begin();
132:
133:                    OQLQuery query = odmg.newOQLQuery();
134:                    query
135:                            .create("select anArticle from "
136:                                    + Article.class.getName()
137:                                    + " where articleId = 60");
138:                    List results = (List) query.execute();
139:
140:                    Article a = (Article) results.get(0);
141:
142:                    // cross check with PersistenceBroker lookup
143:                    // 1. get OID
144:                    Article example = new Article();
145:                    example.setArticleId(60);
146:                    Identity oid = new Identity(example, ((TransactionImpl) tx)
147:                            .getBroker());
148:                    // 2. lookup object by OID
149:                    PersistenceBroker broker = ((TransactionImpl) tx)
150:                            .getBroker();
151:                    broker.clearCache();
152:                    Article b = (Article) broker.getObjectByIdentity(oid);
153:
154:                    assertEquals("should be same object", a, b);
155:
156:                    //System.out.println("now commit all changes...");
157:                    tx.commit();
158:                } catch (Exception ex) {
159:                    tx.abort();
160:                    fail("ODMGException: " + ex.getMessage());
161:                }
162:
163:                // close database
164:                try {
165:                    db.close();
166:                } catch (ODMGException ex) {
167:                    fail("ODMGException: " + ex.getMessage());
168:                }
169:            }
170:
171:            public void testPathExpressionOqlQuery() throws Exception {
172:                // get facade instance
173:                Implementation odmg = OJB.getInstance();
174:                Database db = odmg.newDatabase();
175:                //open database
176:                try {
177:                    db.open(databaseName, Database.OPEN_READ_WRITE);
178:                } catch (ODMGException ex) {
179:                    fail("ODMGException: " + ex.getMessage());
180:                }
181:                Transaction tx = odmg.newTransaction();
182:
183:                // perform transaction
184:                tx.begin();
185:
186:                OQLQuery query = odmg.newOQLQuery();
187:                // use 'like' instead of '=' when perform query with wildcards
188:                query.create("select anArticle from " + Article.class.getName()
189:                        + " where productGroup.groupName like \"Fruit*\"");
190:                List results = (List) query.execute();
191:
192:                // crosscheck
193:                query = odmg.newOQLQuery();
194:                query.create("select aPG from " + ProductGroup.class.getName()
195:                        + " where groupName like \"Fruit*\"");
196:                List check = (List) query.execute();
197:                if (check.size() < 1)
198:                    fail("Could not found ProductGroup's for: "
199:                            + "select aPG from " + ProductGroup.class.getName()
200:                            + " where groupName like \"Fruit*\"");
201:                ProductGroup pg = (ProductGroup) check.get(0);
202:
203:                assertEquals(pg.getAllArticlesInGroup().size(), results.size());
204:                assertTrue((results.size() > 0));
205:
206:                tx.commit();
207:
208:                // close database
209:
210:                db.close();
211:            }
212:
213:            public void testNrmAndDlists() throws Exception {
214:                // get facade instance
215:                Implementation odmg = OJB.getInstance();
216:                Database db = odmg.newDatabase();
217:                //open database
218:                try {
219:                    db.open(databaseName, Database.OPEN_READ_WRITE);
220:                } catch (ODMGException ex) {
221:                    fail("ODMGException: " + ex.getMessage());
222:                }
223:                Transaction tx = odmg.newTransaction();
224:
225:                //perform transaction
226:                try {
227:                    //=============================
228:                    // this test needs DList impl as oql query collection class
229:                    ((ImplementationImpl) odmg)
230:                            .setOqlCollectionClass(DListImpl.class);
231:                    //=============================
232:
233:                    tx.begin();
234:
235:                    OQLQuery query = odmg.newOQLQuery();
236:                    query.create("select x from " + Article.class.getName()
237:                            + " where productGroupId = 7");
238:                    List results = (List) query.execute();
239:
240:                    int originalSize = results.size();
241:                    assertTrue("result count have to be > 0", originalSize > 0);
242:
243:                    //            OJB.getLogger().debug(results);
244:
245:                    String name = "gimme fruits_" + System.currentTimeMillis();
246:
247:                    db.bind(results, name);
248:                    tx.commit();
249:
250:                    tx = odmg.newTransaction();
251:                    tx.begin();
252:
253:                    ((TransactionImpl) tx).getBroker().clearCache();
254:
255:                    // look it up again
256:                    List newResults = (List) db.lookup(name);
257:
258:                    assertEquals(originalSize, newResults.size());
259:                    Article art = (Article) newResults.get(0);
260:                    assertNotNull(art);
261:                    //            OJB.getLogger().info(results);
262:
263:                    tx.commit();
264:
265:                } catch (Exception e)
266:
267:                {
268:                    tx.abort();
269:                    throw e;
270:                }
271:
272:                // close database
273:                try {
274:                    db.close();
275:                } catch (ODMGException ex) {
276:                    fail("ODMGException: " + ex.getMessage());
277:                }
278:            }
279:
280:            public void testOQLQueryBind() {
281:                // get facade instance
282:                Implementation odmg = OJB.getInstance();
283:                Database db = odmg.newDatabase();
284:                //open database
285:                try {
286:                    db.open(databaseName, Database.OPEN_READ_WRITE);
287:                } catch (ODMGException ex) {
288:                    fail("ODMGException: " + ex.getMessage());
289:                }
290:                Transaction tx = odmg.newTransaction();
291:
292:                //perform transaction
293:                try {
294:                    tx.begin();
295:
296:                    OQLQuery query = odmg.newOQLQuery();
297:                    query.create("select anArticle from "
298:                            + Article.class.getName()
299:                            + " where articleId = $678");
300:                    query.bind(new Integer(30));
301:
302:                    List results = (List) query.execute();
303:
304:                    Article a = (Article) results.get(0);
305:
306:                    //crosscheck with PersistenceBroker lookup
307:                    // 1. get OID
308:                    Article example = new Article();
309:                    example.setArticleId(30);
310:                    Identity oid = new Identity(example, ((TransactionImpl) tx)
311:                            .getBroker());
312:
313:                    // 2. lookup object by OID
314:                    PersistenceBroker broker = ((TransactionImpl) tx)
315:                            .getBroker();
316:                    broker.clearCache();
317:                    Article b = (Article) broker.getObjectByIdentity(oid);
318:
319:                    assertEquals("should be same object", a, b);
320:
321:                    //System.out.println("now commit all changes...");
322:                    tx.commit();
323:                } catch (Exception ex)
324:
325:                {
326:                    tx.abort();
327:                    fail("ODMGException: " + ex.getMessage());
328:                }
329:
330:                // close database
331:                try {
332:                    db.close();
333:                } catch (ODMGException ex) {
334:                    fail("ODMGException: " + ex.getMessage());
335:                }
336:            }
337:
338:            public void YYYtestOQLQueryOnCollections() throws Exception {
339:                // get facade instance
340:                Implementation odmg = OJB.getInstance();
341:                Database db = odmg.newDatabase();
342:                //open database
343:                db.open(databaseName, Database.OPEN_READ_WRITE);
344:                Transaction tx = odmg.newTransaction();
345:
346:                //perform transaction
347:                try {
348:                    tx.begin();
349:                    OQLQuery query = odmg.newOQLQuery();
350:                    query.create("select aLotOfArticles from "
351:                            + Article.class.getName()
352:                            + " where productGroupId = 4");
353:
354:                    DCollection results = (DCollection) query.execute();
355:                    results = results.query("price > 35");
356:
357:                    // now perform control query
358:                    query = odmg.newOQLQuery();
359:                    query.create("select aLotOfArticles from "
360:                            + Article.class.getName()
361:                            + " where productGroupId = 4 and price  > 35");
362:
363:                    DCollection check = (DCollection) query.execute();
364:
365:                    assertEquals(results, check);
366:
367:                    tx.commit();
368:                }
369:                // close database
370:                finally {
371:                    db.close();
372:                }
373:            }
374:
375:            /**try to open non-existing db*/
376:            public void YYYtestWrongDbName() {
377:                // get facade instance
378:                Implementation objectserver = OJB.getInstance();
379:                Database db = objectserver.newDatabase();
380:
381:                //try open database with non existing repository file:
382:                String wrongDatabaseName = "ThereIsNoSuchFile";
383:                try {
384:                    db.open(wrongDatabaseName, Database.OPEN_READ_WRITE);
385:                    fail("should not be able to open database "
386:                            + wrongDatabaseName);
387:                } catch (ODMGException ex) {
388:                    return;
389:                }
390:            }
391:
392:            /**try to crash odmg and broker tx*/
393:            public void YYYtestBrokerCrash() {
394:                // get facade instance
395:                Implementation odmg = OJB.getInstance();
396:                Database db = odmg.newDatabase();
397:                PersistenceBroker broker = null;
398:                ClassDescriptor cld = null;
399:                String tablename = null;
400:
401:                //open database
402:                try {
403:                    db.open(databaseName, Database.OPEN_READ_WRITE);
404:                } catch (ODMGException ex) {
405:                    fail("ODMGException: " + ex.getMessage());
406:                }
407:                try {
408:                    Transaction tx = odmg.newTransaction();
409:                    tx.begin();
410:
411:                    // retrieve an Article
412:                    OQLQuery query = odmg.newOQLQuery();
413:                    query.create("select anArticle from "
414:                            + Article.class.getName()
415:                            + " where articleId = $678");
416:                    query.bind(new Integer(30));
417:                    List results = (List) query.execute();
418:                    Article a = (Article) results.get(0);
419:
420:                    // manipulate metadata
421:                    broker = ((TransactionImpl) tx).getBroker();
422:                    cld = broker.getClassDescriptor(Article.class);
423:                    tablename = cld.getFullTableName();
424:                    cld.setTableName("ELVIS");
425:                    broker.getDescriptorRepository().setClassDescriptor(cld);
426:
427:                    //broker will crash as metadata is corrupt
428:                    a.addToStock(5);
429:                    tx.commit();
430:                    fail("Can commit tx with corrupt metadata");
431:                } catch (Throwable t) {
432:                    //ignore
433:                } finally {
434:                    cld.setTableName(tablename);
435:                    broker.getDescriptorRepository().setClassDescriptor(cld);
436:                }
437:
438:            }
439:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.