Source Code Cross Referenced for OneToManyTest.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:        /*
002:         * Created by IntelliJ IDEA.
003:         * User: Matt
004:         * Date: Jun 10, 2002
005:         * Time: 9:22:36 PM
006:         * To change template for new class use
007:         * Code Style | Class Templates options (Tools | IDE Options).
008:         */
009:        package org.apache.ojb.odmg;
010:
011:        import java.util.Collection;
012:        import java.util.Iterator;
013:        import java.util.List;
014:
015:        import org.apache.ojb.broker.Article;
016:        import org.apache.ojb.broker.InterfaceArticle;
017:        import org.apache.ojb.broker.InterfaceProductGroup;
018:        import org.apache.ojb.broker.Mammal;
019:        import org.apache.ojb.broker.ProductGroup;
020:        import org.apache.ojb.broker.Reptile;
021:        import org.apache.ojb.broker.metadata.ClassDescriptor;
022:        import org.apache.ojb.broker.metadata.CollectionDescriptor;
023:        import org.apache.ojb.broker.metadata.MetadataManager;
024:        import org.apache.ojb.junit.ODMGTestCase;
025:        import org.apache.ojb.odmg.shared.ODMGZoo;
026:        import org.odmg.ODMGException;
027:        import org.odmg.OQLQuery;
028:        import org.odmg.Transaction;
029:
030:        public class OneToManyTest extends ODMGTestCase {
031:            private static final int COUNT = 10;
032:            int oldValue;
033:
034:            public static void main(String[] args) {
035:                String[] arr = { OneToManyTest.class.getName() };
036:                junit.textui.TestRunner.main(arr);
037:            }
038:
039:            public OneToManyTest(String name) {
040:                super (name);
041:            }
042:
043:            public void setUp() throws Exception {
044:                super .setUp();
045:                ClassDescriptor cld = MetadataManager.getInstance()
046:                        .getRepository().getDescriptorFor(ProductGroup.class);
047:                CollectionDescriptor cod = cld
048:                        .getCollectionDescriptorByName("allArticlesInGroup");
049:                oldValue = cod.getCascadingStore();
050:                // odmg-api need false
051:                cod.setCascadeStore(false);
052:            }
053:
054:            public void tearDown() throws Exception {
055:                ClassDescriptor cld = MetadataManager.getInstance()
056:                        .getRepository().getDescriptorFor(ProductGroup.class);
057:                cld.getCollectionDescriptorByName("allArticlesInGroup")
058:                        .setCascadingStore(oldValue);
059:                super .tearDown();
060:            }
061:
062:            /**
063:             * tests creation of new object that has a one to many relationship
064:             *
065:             * @throws Exception
066:             */
067:            public void testCreate() throws Exception {
068:                String name = "testCreate_" + System.currentTimeMillis();
069:                /**
070:                 * 1. create the article.
071:                 */
072:                Transaction tx = odmg.newTransaction();
073:                tx.begin();
074:                ProductGroup group = new ProductGroup();
075:                group.setGroupName(name);
076:                tx.lock(group, Transaction.WRITE);
077:                for (int i = 0; i < COUNT; i++) {
078:                    Article article = createArticle(name);
079:                    group.add(article);
080:                }
081:                tx.commit();
082:                /**
083:                 * 2. query on the article to make sure it is everything we set it up to be.
084:                 */
085:                tx.begin();
086:                OQLQuery query = odmg.newOQLQuery();
087:                query.create("select productGroup from "
088:                        + ProductGroup.class.getName() + " where groupName=$1");
089:                query.bind(name);
090:                Collection results = (Collection) query.execute();
091:                tx.commit();
092:                Iterator it = results.iterator();
093:                assertTrue(it.hasNext());
094:                InterfaceProductGroup pg = (InterfaceProductGroup) it.next();
095:                assertFalse(it.hasNext());
096:                assertNotNull(pg.getAllArticles());
097:                assertEquals(COUNT, pg.getAllArticles().size());
098:            }
099:
100:            /**
101:             * tests creation of new object that has a one to many relationship.
102:             * thma: this test will not work, because ODMG is no able to track
103:             * modifictations to normal collections.
104:             * Only Odmg Collections like DList will be treated properly.
105:             *
106:             * @throws Exception
107:             */
108:            public void testUpdateWithProxy() throws Exception {
109:                // arminw: fixed
110:                // if(ojbSkipKnownIssueProblem()) return;
111:                String name = "testUpdateWithProxy_"
112:                        + System.currentTimeMillis();
113:
114:                ProductGroup pg1 = new ProductGroup(null, name + "_1",
115:                        "a group");
116:                ProductGroup pg2 = new ProductGroup(null, name + "_2",
117:                        "a group");
118:                Article a1 = createArticle(name);
119:                a1.setProductGroup(pg1);
120:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
121:                tx.begin();
122:                database.makePersistent(a1);
123:                database.makePersistent(pg1);
124:                database.makePersistent(pg2);
125:                tx.commit();
126:
127:                tx.begin();
128:                tx.getBroker().clearCache();
129:                /**
130:                 * 1. get all articles from groups and add a new article
131:                 */
132:                OQLQuery query = odmg.newOQLQuery();
133:                query.create("select productGroup from "
134:                        + ProductGroup.class.getName()
135:                        + " where groupName like $1");
136:                query.bind(name + "%");
137:                Collection results = (Collection) query.execute();
138:                assertEquals(2, results.size());
139:                Iterator it = results.iterator();
140:                InterfaceProductGroup temp = null;
141:                /**
142:                 * to each productgroup add an article with a pre-determined name.
143:                 */
144:                while (it.hasNext()) {
145:                    Article article = createArticle(name);
146:                    temp = (InterfaceProductGroup) it.next();
147:                    tx.lock(temp, Transaction.WRITE);
148:                    temp.add(article);
149:                    article.setProductGroup(temp);
150:                }
151:                tx.commit();
152:
153:                /**
154:                 * 2. requery and find the articles we added.
155:                 */
156:                tx.begin();
157:                query = odmg.newOQLQuery();
158:                query.create("select productGroup from "
159:                        + ProductGroup.class.getName()
160:                        + " where groupName like $1");
161:                query.bind(name + "%");
162:                results = (Collection) query.execute();
163:                tx.commit();
164:
165:                assertEquals(2, results.size());
166:                it = results.iterator();
167:                int counter = 0;
168:                temp = null;
169:                while (it.hasNext()) {
170:                    temp = (InterfaceProductGroup) it.next();
171:                    Collection articles = temp.getAllArticles();
172:                    counter = counter + articles.size();
173:                    Iterator it2 = articles.iterator();
174:                    while (it2.hasNext()) {
175:                        InterfaceArticle art = (InterfaceArticle) it2.next();
176:                        if (art.getArticleName() != null) {
177:                            assertEquals(name, art.getArticleName());
178:                        }
179:                    }
180:                }
181:                assertEquals(3, counter);
182:            }
183:
184:            /**
185:             * this tests if polymorph collections (i.e. collections of objects
186:             * implementing a common interface) are treated correctly
187:             */
188:            public void testPolymorphOneToMany() {
189:
190:                ODMGZoo myZoo = new ODMGZoo("London");
191:                Mammal elephant = new Mammal(37, "Jumbo", 4);
192:                Mammal cat = new Mammal(11, "Silvester", 4);
193:                Reptile snake = new Reptile(3, "Kaa", "green");
194:
195:                myZoo.addAnimal(snake);
196:                myZoo.addAnimal(elephant);
197:                myZoo.addAnimal(cat);
198:
199:                try {
200:                    Transaction tx = odmg.newTransaction();
201:                    tx.begin();
202:                    database.makePersistent(myZoo);
203:                    tx.commit();
204:
205:                    int id = myZoo.getZooId();
206:
207:                    tx = odmg.newTransaction();
208:                    tx.begin();
209:                    OQLQuery query = odmg.newOQLQuery();
210:                    query.create("select zoos from " + ODMGZoo.class.getName()
211:                            + " where zooId=$1");
212:                    query.bind(new Integer(id));
213:                    List zoos = (List) query.execute();
214:                    assertEquals(1, zoos.size());
215:                    ODMGZoo zoo = (ODMGZoo) zoos.get(0);
216:                    tx.commit();
217:                    assertEquals(3, zoo.getAnimals().size());
218:
219:                } catch (ODMGException e) {
220:                    e.printStackTrace();
221:                    fail("ODMGException thrown " + e.getMessage());
222:                }
223:            }
224:
225:            /**
226:             * Create an article with 4 product groups related to it in a 1-N relationship
227:             */
228:            protected Article createArticle(String name) {
229:                Article a = Article.createInstance();
230:                a.setArticleName(name);
231:                a.setIsSelloutArticle(true);
232:                a.setMinimumStock(100);
233:                a.setOrderedUnits(17);
234:                a.setPrice(0.45);
235:                //a.setProductGroupId(1);
236:                a.setStock(234);
237:                a.setSupplierId(4);
238:                a.setUnit("bottle");
239:                return a;
240:            }
241:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.