Source Code Cross Referenced for OQLTest.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) 


0001:        package org.apache.ojb.odmg;
0002:
0003:        import java.io.Serializable;
0004:        import java.util.ArrayList;
0005:        import java.util.Collection;
0006:        import java.util.Iterator;
0007:        import java.util.List;
0008:
0009:        import org.apache.commons.lang.SerializationUtils;
0010:        import org.apache.ojb.junit.ODMGTestCase;
0011:        import org.apache.ojb.odmg.shared.Article;
0012:        import org.apache.ojb.odmg.shared.Person;
0013:        import org.apache.ojb.odmg.shared.PersonImpl;
0014:        import org.apache.ojb.odmg.shared.ProductGroup;
0015:        import org.odmg.OQLQuery;
0016:        import org.odmg.Transaction;
0017:
0018:        /**
0019:         * OQLQuery tests.
0020:         *
0021:         * @version $Id: OQLTest.java,v 1.16.2.3 2005/03/09 16:30:36 arminw Exp $
0022:         */
0023:        public class OQLTest extends ODMGTestCase {
0024:            private int COUNT = 10;
0025:            private int id_filter = 2;
0026:
0027:            public static void main(String[] args) {
0028:                String[] arr = { OQLTest.class.getName() };
0029:                junit.textui.TestRunner.main(arr);
0030:            }
0031:
0032:            public OQLTest(String name) {
0033:                super (name);
0034:            }
0035:
0036:            private void createPersons() throws Exception {
0037:                Transaction tx = odmg.newTransaction();
0038:                tx.begin();
0039:                for (int i = 0; i < COUNT; i++) {
0040:                    Person aPerson = new PersonImpl();
0041:                    aPerson.setFirstname("firstname" + i);
0042:                    aPerson.setLastname("lastname" + i);
0043:                    database.makePersistent(aPerson);
0044:                }
0045:                tx.commit();
0046:            }
0047:
0048:            private void deleteData(Class target) throws Exception {
0049:                Transaction tx = odmg.newTransaction();
0050:                tx.begin();
0051:                OQLQuery query = odmg.newOQLQuery();
0052:                query.create("select allStuff from " + target.getName());
0053:                Collection allTargets = (Collection) query.execute();
0054:                Iterator it = allTargets.iterator();
0055:                while (it.hasNext()) {
0056:                    database.deletePersistent(it.next());
0057:                }
0058:                tx.commit();
0059:            }
0060:
0061:            public void testMultipleLoad() throws Exception {
0062:                String name = "testMultipleLoad_" + System.currentTimeMillis();
0063:                Fish fish = new Fish();
0064:                fish.setName(name);
0065:                fish.setTypeOfWater("normal");
0066:
0067:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
0068:                tx.begin();
0069:                odmg.getDatabase(null).makePersistent(fish);
0070:                tx.commit();
0071:
0072:                Fish newFish = (Fish) SerializationUtils.clone(fish);
0073:                newFish.setTypeOfWater("salty");
0074:
0075:                tx = (TransactionExt) odmg.newTransaction();
0076:                tx.begin();
0077:                tx.getBroker().clearCache();
0078:
0079:                OQLQuery query = odmg.newOQLQuery();
0080:
0081:                // Query current fish for comparison
0082:                query.create("select object from " + Fish.class.getName()
0083:                        + " where foodId=$1");
0084:                query.bind(new Integer(newFish.getFoodId()));
0085:                List result = (List) query.execute();
0086:
0087:                assertNotNull(result);
0088:                assertEquals(1, result.size());
0089:                Fish current = (Fish) result.get(0);
0090:
0091:                assertEquals(name, current.getName());
0092:                assertEquals("normal", current.getTypeOfWater());
0093:
0094:                assertEquals("salty", newFish.getTypeOfWater());
0095:                tx.lock(newFish, Transaction.WRITE);
0096:                tx.markDirty(newFish);
0097:                tx.commit();
0098:
0099:                // check for changes allow using cache
0100:                tx = (TransactionExt) odmg.newTransaction();
0101:                tx.begin();
0102:                query = odmg.newOQLQuery();
0103:
0104:                // Query current fish for comparison
0105:                query.create("select object from " + Fish.class.getName()
0106:                        + " where foodId=$1");
0107:                query.bind(new Integer(newFish.getFoodId()));
0108:                result = (List) query.execute();
0109:                tx.commit();
0110:
0111:                assertNotNull(result);
0112:                assertEquals(1, result.size());
0113:                current = (Fish) result.get(0);
0114:                assertEquals(name, current.getName());
0115:                assertEquals("salty", current.getTypeOfWater());
0116:
0117:                // check for changes without using cache
0118:                tx = (TransactionExt) odmg.newTransaction();
0119:                tx.begin();
0120:                // clear cache to lookup object from DB
0121:                tx.getBroker().clearCache();
0122:                query = odmg.newOQLQuery();
0123:
0124:                // Query current fish for comparison
0125:                query.create("select object from " + Fish.class.getName()
0126:                        + " where foodId=$1");
0127:                query.bind(new Integer(newFish.getFoodId()));
0128:                result = (List) query.execute();
0129:                tx.commit();
0130:
0131:                assertNotNull(result);
0132:                assertEquals(1, result.size());
0133:                current = (Fish) result.get(0);
0134:                assertEquals(name, current.getName());
0135:                assertEquals("salty", current.getTypeOfWater());
0136:            }
0137:
0138:            /**
0139:             * test the following conditions:
0140:             * != support
0141:             * literal support at binding.
0142:             * A prior bug where criteria was reporting as bindable when it was in fact a literal.
0143:             */
0144:            public void testGetWithLiteral() throws Exception {
0145:                createPersons();
0146:
0147:                // 3. Get a list of some articles
0148:                Transaction tx = odmg.newTransaction();
0149:                tx.begin();
0150:
0151:                OQLQuery query = odmg.newOQLQuery();
0152:                String sql = "select allPersons from " + Person.class.getName()
0153:                        + " where id != 5 and id > $1";
0154:                query.create(sql);
0155:                query.bind(new Integer(id_filter));
0156:
0157:                Collection allPersons = (Collection) query.execute();
0158:
0159:                // Iterator over the restricted articles objects
0160:                java.util.Iterator it = allPersons.iterator();
0161:
0162:                while (it.hasNext()) {
0163:                    /**
0164:                     * just make sure it's a string.
0165:                     */
0166:                    Object result = it.next();
0167:                    Person value = (Person) result;
0168:                    if (value.getId() <= id_filter)
0169:                        fail("oql didn't filter, got id (" + value.getId()
0170:                                + " where it should have been over "
0171:                                + id_filter);
0172:                }
0173:                tx.commit();
0174:            }
0175:
0176:            public void testQueryIn() throws Exception {
0177:                // deleteData(database, odmg, Article.class);
0178:
0179:                Transaction tx = odmg.newTransaction();
0180:                tx.begin();
0181:
0182:                OQLQuery query1 = odmg.newOQLQuery();
0183:                query1.create("select anArticle from "
0184:                        + Article.class.getName()
0185:                        + " where articleId in(30, 31, 32) order by articleId");
0186:                List result1 = (List) query1.execute();
0187:
0188:                Collection ids = new ArrayList();
0189:                ids.add(new Integer(30));
0190:                ids.add(new Integer(31));
0191:                ids.add(new Integer(32));
0192:                OQLQuery query2 = odmg.newOQLQuery();
0193:                query2.create("select anArticle from "
0194:                        + Article.class.getName()
0195:                        + " where articleId in($1) order by articleId");
0196:                query2.bind(ids);
0197:                List result2 = (List) query2.execute();
0198:
0199:                assertEquals(result1.size(), result2.size());
0200:                tx.commit();
0201:            }
0202:
0203:            public void testQueryNull() throws Exception {
0204:                Transaction tx = odmg.newTransaction();
0205:                tx.begin();
0206:
0207:                OQLQuery query1 = odmg.newOQLQuery();
0208:                query1.create("select anArticle from "
0209:                        + Article.class.getName()
0210:                        + " where is_undefined(articleName)");
0211:                List result1 = (List) query1.execute();
0212:
0213:                OQLQuery query2 = odmg.newOQLQuery();
0214:                query2.create("select anArticle from "
0215:                        + Article.class.getName() + " where articleName = nil");
0216:                List result2 = (List) query2.execute();
0217:
0218:                assertEquals(result1.size(), result2.size());
0219:                tx.commit();
0220:            }
0221:
0222:            public void testQueryNotNull() throws Exception {
0223:                Transaction tx = odmg.newTransaction();
0224:                tx.begin();
0225:
0226:                OQLQuery query1 = odmg.newOQLQuery();
0227:                query1.create("select anArticle from "
0228:                        + Article.class.getName()
0229:                        + " where is_defined(articleName)");
0230:                List result1 = (List) query1.execute();
0231:
0232:                OQLQuery query2 = odmg.newOQLQuery();
0233:                query2
0234:                        .create("select anArticle from "
0235:                                + Article.class.getName()
0236:                                + " where articleName <> nil");
0237:                List result2 = (List) query2.execute();
0238:
0239:                assertEquals(result1.size(), result2.size());
0240:                tx.commit();
0241:            }
0242:
0243:            public void testQueryBetween() throws Exception {
0244:                Transaction tx = odmg.newTransaction();
0245:
0246:                tx.begin();
0247:
0248:                OQLQuery query1 = odmg.newOQLQuery();
0249:                query1
0250:                        .create("select anArticle from "
0251:                                + Article.class.getName()
0252:                                + " where articleId between 30 and 32 order by articleId desc");
0253:                List result1 = (List) query1.execute();
0254:
0255:                OQLQuery query2 = odmg.newOQLQuery();
0256:                query2
0257:                        .create("select anArticle from "
0258:                                + Article.class.getName()
0259:                                + " where articleId between $1 and $2 order by articleId asc");
0260:                query2.bind(new Integer(30));
0261:                query2.bind(new Integer(32));
0262:                List result2 = (List) query2.execute();
0263:
0264:                //        System.out.println("#### OQLTest#testQueryBetween(): Size result_1=" + result1.size()
0265:                //                + ", size result_2=" + result2.size());
0266:                //        for(int i = 0; i < result1.size(); i++)
0267:                //        {
0268:                //            Article a = (Article) result1.get(i);
0269:                //            System.out.println("Article_Query_1: articles_in_group=" + a.getProductGroup().getAllArticlesInGroup().size()
0270:                //                    + " - " + a);
0271:                //        }
0272:                //
0273:                //        for(int i = 0; i < result2.size(); i++)
0274:                //        {
0275:                //            Article a = (Article) result2.get(i);
0276:                //            System.out.println("Article_Query_2: articles_in_group=" + a.getProductGroup().getAllArticlesInGroup().size()
0277:                //                    + " - " + a);
0278:                //        }
0279:                tx.commit();
0280:
0281:                tx.begin();
0282:                OQLQuery query3 = odmg.newOQLQuery();
0283:                query3
0284:                        .create("select Article from "
0285:                                + Article.class.getName()
0286:                                + " where articleId between $1 and $2 order by articleId asc");
0287:                query3.bind(new Integer(30));
0288:                query3.bind(new Integer(32));
0289:                List result3 = (List) query3.execute();
0290:
0291:                OQLQuery query4 = odmg.newOQLQuery();
0292:                query4
0293:                        .create("select Article from "
0294:                                + Article.class.getName()
0295:                                + " where articleId between 30 and 32 order by articleId desc");
0296:                List result4 = (List) query4.execute();
0297:
0298:                //        for(int i = 0; i < result3.size(); i++)
0299:                //        {
0300:                //            Article a = (Article) result3.get(i);
0301:                //            System.out.println("Article_Query_3: articles_in_group=" + a.getProductGroup().getAllArticlesInGroup().size()
0302:                //                    + " - " + a);
0303:                //        }
0304:                //
0305:                //        for(int i = 0; i < result4.size(); i++)
0306:                //        {
0307:                //            Article a = (Article) result4.get(i);
0308:                //            System.out.println("Article_Query_4: articles_in_group=" + a.getProductGroup().getAllArticlesInGroup().size()
0309:                //                    + " - " + a);
0310:                //        }
0311:                tx.commit();
0312:
0313:                assertEquals(result1.size(), result2.size());
0314:                assertEquals(result1.size(), result3.size());
0315:                assertEquals(result2.size(), result3.size());
0316:                assertEquals(result1.size(), result4.size());
0317:                assertEquals(result2.size(), result4.size());
0318:                assertEquals(result1, result4);
0319:                assertEquals(result2, result3);
0320:            }
0321:
0322:            public void testInListQuery() throws Exception {
0323:                //objects that are part of a 1:n relation, i.e. they have fk-fields
0324:                Mammal elephant = new Mammal(4, "Minnie", 4);
0325:                Mammal cat = new Mammal(4, "Winston", 4);
0326:                Reptile snake = new Reptile(4, "Skuzzlebutt", "green");
0327:
0328:                Transaction tx = odmg.newTransaction();
0329:                tx.begin();
0330:                OQLQuery query = odmg.newOQLQuery();
0331:                query
0332:                        .create("select animals from "
0333:                                + InterfaceAnimal.class.getName()
0334:                                + " where name in list (\"Minnie\", \"Winston\", \"Skuzzlebutt\")");
0335:                int before = ((Collection) query.execute()).size();
0336:                tx.commit();
0337:
0338:                tx = odmg.newTransaction();
0339:                tx.begin();
0340:                database.makePersistent(elephant);
0341:                database.makePersistent(cat);
0342:                database.makePersistent(snake);
0343:                tx.commit();
0344:
0345:                tx = odmg.newTransaction();
0346:                tx.begin();
0347:                List animals = (List) query.execute();
0348:                tx.commit();
0349:                assertEquals(before + 3, animals.size());
0350:            }
0351:
0352:            public void testPrefetchQuery() throws Exception {
0353:                String oql = "select allProductGroups from "
0354:                        + ProductGroup.class.getName()
0355:                        + " where groupId <= $1 order by groupId prefetch allArticlesInGroup";
0356:                OQLQuery query = odmg.newOQLQuery();
0357:                query.create(oql);
0358:                query.bind(new Integer(5));
0359:                Transaction tx = odmg.newTransaction();
0360:                tx.begin();
0361:                Collection results = (Collection) query.execute();
0362:                tx.commit();
0363:                assertNotNull(results);
0364:                assertTrue(results.size() > 0);
0365:            }
0366:
0367:            public void testInterfaceQuery() throws Exception {
0368:                int age = (int) (Math.random() * Integer.MAX_VALUE);
0369:                int calories = (int) (Math.random() * Integer.MAX_VALUE);
0370:                int caloriesOther = (int) (Math.random() * Integer.MAX_VALUE);
0371:
0372:                //objects that are part of a 1:n relation, i.e. they have fk-fields
0373:                Mammal elephant = new Mammal(age, "Jumbo", 4);
0374:                Mammal cat = new Mammal(age, "Silvester", 4);
0375:                Reptile snake = new Reptile(age, "Kaa", "green");
0376:
0377:                //objects that are independent or part of m:n relations, i.e. they
0378:                //don't have fk-fields
0379:                Fish tuna = new Fish("tuna", calories, "salt");
0380:                Fish trout = new Fish("trout", calories, "fresh water");
0381:
0382:                Salad radiccio = new Salad("Radiccio", calories, "red");
0383:                Salad lolloverde = new Salad("Lollo verde", caloriesOther,
0384:                        "green");
0385:
0386:                deleteData(InterfaceAnimal.class);
0387:                deleteData(InterfaceFood.class);
0388:
0389:                Transaction tx = odmg.newTransaction();
0390:                tx.begin();
0391:                database.makePersistent(elephant);
0392:                database.makePersistent(cat);
0393:                database.makePersistent(snake);
0394:                database.makePersistent(tuna);
0395:                database.makePersistent(trout);
0396:                database.makePersistent(radiccio);
0397:                database.makePersistent(lolloverde);
0398:                tx.commit();
0399:
0400:                tx = odmg.newTransaction();
0401:                tx.begin();
0402:                OQLQuery query = odmg.newOQLQuery();
0403:                query.create("select animals from "
0404:                        + InterfaceAnimal.class.getName() + " where age=$1");
0405:                query.bind(new Integer(age));
0406:                List animals = (List) query.execute();
0407:                tx.commit();
0408:                Iterator it = animals.iterator();
0409:                while (it.hasNext()) {
0410:                    Object obj = it.next();
0411:                    // System.out.println(obj);
0412:                }
0413:                assertEquals(3, animals.size());
0414:
0415:                //test independent objects
0416:                query = odmg.newOQLQuery();
0417:                tx.begin();
0418:                query.create("select food from "
0419:                        + InterfaceFood.class.getName() + " where calories=$1");
0420:                query.bind(new Integer(calories));
0421:                List food = (List) query.execute();
0422:                tx.commit();
0423:                assertEquals(3, food.size());
0424:            }
0425:
0426:            /**
0427:             *
0428:             */
0429:            public void _testFunctions() throws Exception {
0430:                Transaction tx = odmg.newTransaction();
0431:                tx.begin();
0432:
0433:                OQLQuery query = odmg.newOQLQuery();
0434:                query.create("select anArticle from " + Article.class.getName()
0435:                        + " where upper(articleName) like \"A%\" ");
0436:
0437:                List results = (List) query.execute();
0438:                tx.commit();
0439:                assertTrue(results.size() > 0);
0440:            }
0441:
0442:            /**
0443:             * ReportQuery returning rows with summed stock and price per article group
0444:             */
0445:            public void testReportQueryGroupBy() throws Exception {
0446:                Transaction tx = odmg.newTransaction();
0447:                tx.begin();
0448:
0449:                OQLQuery query = odmg.newOQLQuery();
0450:                query
0451:                        .create("select p.groupName, p.allArticlesInGroup.stock, p.allArticlesInGroup.price"
0452:                                + " from "
0453:                                + ProductGroup.class.getName()
0454:                                + " group by groupName, allArticlesInGroup.stock, allArticlesInGroup.price");
0455:
0456:                //        query.create("select p.groupName, sum(p.allArticlesInGroup.stock), sum(p.allArticlesInGroup.price)" +
0457:                //               " from " + ProductGroup.class.getName() +
0458:                //               "by groupName, allArticlesInGroup.stock, allArticlesInGroup.price");
0459:
0460:                List results = (List) query.execute();
0461:                tx.commit();
0462:                assertTrue(results.size() > 0);
0463:            }
0464:
0465:            public void testRepeatableQuery() throws Exception {
0466:                deleteData(Person.class);
0467:                createPersons();
0468:
0469:                // 3. Get a list of some articles
0470:                Transaction tx = odmg.newTransaction();
0471:                tx.begin();
0472:
0473:                OQLQuery query = odmg.newOQLQuery();
0474:                String sql = "select allPersons from " + Person.class.getName()
0475:                        + " where id != $1 and id > $2";
0476:                query.create(sql);
0477:                query.bind(new Integer(5));
0478:                query.bind(new Integer(id_filter));
0479:
0480:                Collection allPersons = (Collection) query.execute();
0481:                // Iterator over the restricted articles objects
0482:                Iterator it = allPersons.iterator();
0483:                while (it.hasNext()) {
0484:                    /**
0485:                     * just make sure it's a string.
0486:                     */
0487:                    Object result = it.next();
0488:                    Person value = (Person) result;
0489:                    if (value.getId() <= id_filter || value.getId() == 5)
0490:                        fail("oql didn't filter, got id (" + value.getId()
0491:                                + " where it should have been over "
0492:                                + id_filter + " and not 5");
0493:                }
0494:                tx.commit();
0495:
0496:                // now we try to reuse OQLQuery
0497:                tx.begin();
0498:
0499:                query.bind(new Integer(8));
0500:                query.bind(new Integer(id_filter));
0501:                allPersons = (Collection) query.execute();
0502:                // Iterator over the restricted articles objects
0503:                it = allPersons.iterator();
0504:                while (it.hasNext()) {
0505:                    /**
0506:                     * just make sure it's a string.
0507:                     */
0508:                    Object result = it.next();
0509:                    Person value = (Person) result;
0510:                    if (value.getId() <= id_filter || value.getId() == 8)
0511:                        fail("oql didn't filter, got id (" + value.getId()
0512:                                + " where it should have been over "
0513:                                + id_filter + " and not 8");
0514:                }
0515:
0516:                // reuse OQLQuery within same tx
0517:                query.bind(new Integer(9));
0518:                query.bind(new Integer(id_filter));
0519:                allPersons = (Collection) query.execute();
0520:                // Iterator over the restricted articles objects
0521:                it = allPersons.iterator();
0522:                while (it.hasNext()) {
0523:                    /**
0524:                     * just make sure it's a string.
0525:                     */
0526:                    Object result = it.next();
0527:                    Person value = (Person) result;
0528:                    if (value.getId() <= id_filter || value.getId() == 9)
0529:                        fail("oql didn't filter, got id (" + value.getId()
0530:                                + " where it should have been over "
0531:                                + id_filter + " and not 9");
0532:                }
0533:                tx.commit();
0534:            }
0535:
0536:            /**
0537:             * test Subquery
0538:             * get all articles with price > avg(price)
0539:             * PROBLEM: avg(price) is NOT extent aware !!
0540:             *
0541:             * test may fail if db does not support sub queries
0542:             */
0543:            public void _testSubQuery1() throws Exception {
0544:                Transaction tx = odmg.newTransaction();
0545:                tx.begin();
0546:
0547:                OQLQuery query = odmg.newOQLQuery();
0548:                query.create("select anArticle from " + Article.class.getName()
0549:                        + " where " + " price >= (select avg(price) from "
0550:                        + Article.class.getName()
0551:                        + " where articleName like \"A%\") ");
0552:
0553:                List results = (List) query.execute();
0554:                tx.commit();
0555:                assertTrue(results.size() > 0);
0556:            }
0557:
0558:            //*******************************************************************
0559:            // test classes start here
0560:            //*******************************************************************
0561:            public interface InterfaceAnimal extends Serializable {
0562:                int getAge();
0563:
0564:                String getName();
0565:            }
0566:
0567:            public interface InterfaceFood extends Serializable {
0568:                String getName();
0569:
0570:                int getCalories();
0571:
0572:            }
0573:
0574:            public static abstract class AbstractAnimal {
0575:                int animalId;
0576:                String name;
0577:                Integer zooId;
0578:
0579:                public int getAnimalId() {
0580:                    return animalId;
0581:                }
0582:
0583:                public void setAnimalId(int animalId) {
0584:                    this .animalId = animalId;
0585:                }
0586:
0587:                public String getName() {
0588:                    return name;
0589:                }
0590:
0591:                public void setName(String name) {
0592:                    this .name = name;
0593:                }
0594:
0595:                public Integer getZooId() {
0596:                    return zooId;
0597:                }
0598:
0599:                public void setZooId(Integer zooId) {
0600:                    this .zooId = zooId;
0601:                }
0602:            }
0603:
0604:            public static class Mammal extends AbstractAnimal implements 
0605:                    InterfaceAnimal, Serializable {
0606:                private int age;
0607:                private int numLegs;
0608:
0609:                public Mammal() {
0610:                    super ();
0611:                }
0612:
0613:                public Mammal(int age, String name, int numLegs) {
0614:                    this .age = age;
0615:                    this .name = name;
0616:                    this .numLegs = numLegs;
0617:                }
0618:
0619:                public String toString() {
0620:                    return "Mammal: id = " + animalId + "\n name = " + name
0621:                            + "\n age = " + age + "\n Number of legs = "
0622:                            + numLegs + "\n zooId = " + zooId;
0623:                }
0624:
0625:                public int getAge() {
0626:                    return age;
0627:                }
0628:
0629:                public int getNumLegs() {
0630:                    return numLegs;
0631:                }
0632:
0633:                public void setNumLegs(int numLegs) {
0634:                    this .numLegs = numLegs;
0635:                }
0636:            }
0637:
0638:            public static class Reptile extends AbstractAnimal implements 
0639:                    InterfaceAnimal, Serializable {
0640:                private int age;
0641:                private String color;
0642:
0643:                /**
0644:                 * Constructor for Plant.
0645:                 */
0646:                public Reptile() {
0647:                    super ();
0648:                }
0649:
0650:                public Reptile(int age, String name, String color) {
0651:                    this .age = age;
0652:                    this .name = name;
0653:                    this .color = color;
0654:                }
0655:
0656:                public int getAge() {
0657:                    return age;
0658:                }
0659:
0660:                public int getAnimalId() {
0661:                    return animalId;
0662:                }
0663:
0664:                public void setAnimalId(int animalId) {
0665:                    this .animalId = animalId;
0666:                }
0667:
0668:                public String getName() {
0669:                    return name;
0670:                }
0671:
0672:                public void setName(String name) {
0673:                    this .name = name;
0674:                }
0675:
0676:                public String getColor() {
0677:                    return color;
0678:                }
0679:
0680:                public void setColor(String color) {
0681:                    this .color = color;
0682:                }
0683:
0684:                public String toString() {
0685:                    return "Reptile: id = " + animalId + "\n name = " + name
0686:                            + "\n age = " + age + "\n color = " + color
0687:                            + "\n zooId = " + zooId;
0688:                }
0689:            }
0690:
0691:            public static class Fish implements  InterfaceFood, Serializable {
0692:                int foodId;
0693:                String name;
0694:                int calories;
0695:                String typeOfWater;
0696:
0697:                /**
0698:                 * Constructor for Fish.
0699:                 */
0700:                public Fish() {
0701:                    super ();
0702:                }
0703:
0704:                public Fish(String name, int calories, String typeOfWater) {
0705:                    this .calories = calories;
0706:                    this .name = name;
0707:                    this .typeOfWater = typeOfWater;
0708:                }
0709:
0710:                /**
0711:                 * @see org.apache.ojb.broker.InterfaceFood#getName()
0712:                 */
0713:                public String getName() {
0714:                    return name;
0715:                }
0716:
0717:                /**
0718:                 * @see org.apache.ojb.broker.InterfaceFood#getCalories()
0719:                 */
0720:                public int getCalories() {
0721:                    return calories;
0722:                }
0723:
0724:                /**
0725:                 * Returns the typeOfWater.
0726:                 * @return String
0727:                 */
0728:                public String getTypeOfWater() {
0729:                    return typeOfWater;
0730:                }
0731:
0732:                /**
0733:                 * Returns the foodId.
0734:                 * @return int
0735:                 */
0736:                public int getFoodId() {
0737:                    return foodId;
0738:                }
0739:
0740:                public String toString() {
0741:                    return "Fish: id = " + foodId + "\n name = " + name
0742:                            + "\n calories = " + calories
0743:                            + "\n Type of water = " + typeOfWater;
0744:                }
0745:
0746:                /**
0747:                 * Sets the calories.
0748:                 * @param calories The calories to set
0749:                 */
0750:                public void setCalories(int calories) {
0751:                    this .calories = calories;
0752:                }
0753:
0754:                /**
0755:                 * Sets the foodId.
0756:                 * @param foodId The foodId to set
0757:                 */
0758:                public void setFoodId(int foodId) {
0759:                    this .foodId = foodId;
0760:                }
0761:
0762:                /**
0763:                 * Sets the name.
0764:                 * @param name The name to set
0765:                 */
0766:                public void setName(String name) {
0767:                    this .name = name;
0768:                }
0769:
0770:                /**
0771:                 * Sets the typeOfWater.
0772:                 * @param typeOfWater The typeOfWater to set
0773:                 */
0774:                public void setTypeOfWater(String typeOfWater) {
0775:                    this .typeOfWater = typeOfWater;
0776:                }
0777:
0778:            }
0779:
0780:            public static class Gourmet implements  Serializable {
0781:                int gourmetId;
0782:                String name;
0783:                List favoriteFood = new ArrayList();
0784:
0785:                /**
0786:                 * Constructor for Gourmet.
0787:                 */
0788:                public Gourmet() {
0789:                    super ();
0790:                }
0791:
0792:                public Gourmet(String name) {
0793:                    this .name = name;
0794:                }
0795:
0796:                public List getFavoriteFood() {
0797:                    return favoriteFood;
0798:                }
0799:
0800:                public void addFavoriteFood(InterfaceFood food) {
0801:                    favoriteFood.add(food);
0802:                }
0803:
0804:                /**
0805:                 * Returns the gourmetId.
0806:                 * @return int
0807:                 */
0808:                public int getGourmetId() {
0809:                    return gourmetId;
0810:                }
0811:
0812:                public String toString() {
0813:                    StringBuffer text = new StringBuffer("Gourmet: id = "
0814:                            + gourmetId + "\n");
0815:                    text.append("name = ");
0816:                    text.append(name);
0817:                    text.append("\nFavoriteFood:\n");
0818:                    for (Iterator it = favoriteFood.iterator(); it.hasNext();) {
0819:                        text.append(it.next().toString());
0820:                        text.append("\n-------\n");
0821:                    }
0822:                    return text.toString();
0823:                }
0824:
0825:                /**
0826:                 * Returns the name.
0827:                 * @return String
0828:                 */
0829:                public String getName() {
0830:                    return name;
0831:                }
0832:
0833:                /**
0834:                 * Sets the favoriteFood.
0835:                 * @param favoriteFood The favoriteFood to set
0836:                 */
0837:                public void setFavoriteFood(List favoriteFood) {
0838:                    this .favoriteFood = favoriteFood;
0839:                }
0840:
0841:                /**
0842:                 * Sets the gourmetId.
0843:                 * @param gourmetId The gourmetId to set
0844:                 */
0845:                public void setGourmetId(int gourmetId) {
0846:                    this .gourmetId = gourmetId;
0847:                }
0848:
0849:                /**
0850:                 * Sets the name.
0851:                 * @param name The name to set
0852:                 */
0853:                public void setName(String name) {
0854:                    this .name = name;
0855:                }
0856:
0857:            }
0858:
0859:            public static class Salad implements  InterfaceFood, Serializable {
0860:                int foodId;
0861:                String name;
0862:                int calories;
0863:                String color;
0864:
0865:                /**
0866:                 * Constructor for Salad.
0867:                 */
0868:                public Salad() {
0869:                    super ();
0870:                }
0871:
0872:                public Salad(String name, int calories, String color) {
0873:                    this .name = name;
0874:                    this .calories = calories;
0875:                    this .color = color;
0876:                }
0877:
0878:                /**
0879:                 * @see org.apache.ojb.broker.InterfaceFood#getName()
0880:                 */
0881:                public String getName() {
0882:                    return name;
0883:                }
0884:
0885:                /**
0886:                 * @see org.apache.ojb.broker.InterfaceFood#getCalories()
0887:                 */
0888:                public int getCalories() {
0889:                    return calories;
0890:                }
0891:
0892:                /**
0893:                 * Returns the color.
0894:                 * @return String
0895:                 */
0896:                public String getColor() {
0897:                    return color;
0898:                }
0899:
0900:                /**
0901:                 * Returns the foodId.
0902:                 * @return int
0903:                 */
0904:                public int getFoodId() {
0905:                    return foodId;
0906:                }
0907:
0908:                public String toString() {
0909:                    return "Salad: id = " + foodId + "\n name = " + name
0910:                            + "\n calories = " + calories + "\n Color = "
0911:                            + color;
0912:                }
0913:
0914:                /**
0915:                 * Sets the calories.
0916:                 * @param calories The calories to set
0917:                 */
0918:                public void setCalories(int calories) {
0919:                    this .calories = calories;
0920:                }
0921:
0922:                /**
0923:                 * Sets the color.
0924:                 * @param color The color to set
0925:                 */
0926:                public void setColor(String color) {
0927:                    this .color = color;
0928:                }
0929:
0930:                /**
0931:                 * Sets the foodId.
0932:                 * @param foodId The foodId to set
0933:                 */
0934:                public void setFoodId(int foodId) {
0935:                    this .foodId = foodId;
0936:                }
0937:
0938:                /**
0939:                 * Sets the name.
0940:                 * @param name The name to set
0941:                 */
0942:                public void setName(String name) {
0943:                    this .name = name;
0944:                }
0945:
0946:            }
0947:
0948:            public class Zoo implements  Serializable {
0949:                private int zooId;
0950:                private String name;
0951:                private List animals = new ArrayList();
0952:
0953:                /**
0954:                 * Constructor for Zoo.
0955:                 */
0956:                public Zoo() {
0957:                    super ();
0958:                }
0959:
0960:                public Zoo(String name) {
0961:                    this .name = name;
0962:                }
0963:
0964:                public List getAnimals() {
0965:                    return animals;
0966:                }
0967:
0968:                public void addAnimal(InterfaceAnimal animal) {
0969:                    animals.add(animal);
0970:                }
0971:
0972:                public int getZooId() {
0973:                    return zooId;
0974:                }
0975:
0976:                public String toString() {
0977:                    StringBuffer text = new StringBuffer("Zoo: id = " + zooId
0978:                            + "\n");
0979:                    text.append("name = ");
0980:                    text.append(name);
0981:                    text.append("\nAnimals:\n");
0982:                    for (Iterator it = animals.iterator(); it.hasNext();) {
0983:                        text.append(it.next().toString());
0984:                        text.append("\n-------\n");
0985:                    }
0986:                    return text.toString();
0987:                }
0988:
0989:                /**
0990:                 * Returns the name.
0991:                 * @return String
0992:                 */
0993:                public String getName() {
0994:                    return name;
0995:                }
0996:
0997:                /**
0998:                 * Sets the animals.
0999:                 * @param animals The animals to set
1000:                 */
1001:                public void setAnimals(List animals) {
1002:                    this .animals = animals;
1003:                }
1004:
1005:                /**
1006:                 * Sets the name.
1007:                 * @param name The name to set
1008:                 */
1009:                public void setName(String name) {
1010:                    this .name = name;
1011:                }
1012:
1013:                /**
1014:                 * Sets the zooId.
1015:                 * @param zooId The zooId to set
1016:                 */
1017:                public void setZooId(int zooId) {
1018:                    this.zooId = zooId;
1019:                }
1020:
1021:            }
1022:
1023:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.