Source Code Cross Referenced for BrokerExamples.java in  » Database-ORM » db-ojb » org » apache » ojb » broker » 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.broker 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.ojb.broker;
002:
003:        import java.util.List;
004:
005:        import org.apache.ojb.broker.metadata.ClassDescriptor;
006:        import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
007:        import org.apache.ojb.broker.query.QueryFactory;
008:        import org.apache.ojb.junit.PBTestCase;
009:
010:        /**
011:         * Demo Application that shows basic concepts for Applications using the PersistenceBroker
012:         * as a mediator for persistence
013:         */
014:        public class BrokerExamples extends PBTestCase {
015:            public static void main(String[] args) {
016:                String[] arr = { BrokerExamples.class.getName() };
017:                junit.textui.TestRunner.main(arr);
018:            }
019:
020:            public BrokerExamples(String name) {
021:                super (name);
022:            }
023:
024:            Article createArticle(String name) {
025:                Article a = new Article();
026:                a.setArticleName(name);
027:                a.setIsSelloutArticle(true);
028:                a.setMinimumStock(100);
029:                a.setOrderedUnits(17);
030:                a.setPrice(0.45);
031:                a.setStock(234);
032:                a.setSupplierId(4);
033:                a.setUnit("bottle");
034:                return a;
035:            }
036:
037:            ProductGroup createProductGroup(String name) {
038:                ProductGroup tmpPG = new ProductGroup();
039:                tmpPG.setGroupName(name);
040:                return tmpPG;
041:            }
042:
043:            public void testCollectionRetrieval() throws Exception {
044:                // Use PersistenceBroker to lookup persistent objects.
045:                // Loop through categories with id 1 to 9
046:                // A ProductGroup holds a List of all Article Objects in the specific category
047:                // the repository.xml specifies that the List of Artikels has to be
048:                // materialized immediately.
049:                for (int i = 1; i < 9; i++) {
050:                    ProductGroup example = new ProductGroup();
051:                    example.setId(new Integer(i));
052:
053:                    ProductGroup group = (ProductGroup) broker
054:                            .getObjectByQuery(QueryFactory.newQuery(example));
055:                    assertNotNull("Expect a ProductGroup with id " + i, group);
056:                    assertEquals("should be equal", i, group.getId().intValue());
057:                    List articleList = group.getAllArticles();
058:                    for (int j = 0; j < articleList.size(); j++) {
059:                        Object o = articleList.get(j);
060:                        assertNotNull(o);
061:                    }
062:                }
063:            }
064:
065:            public void testModifications() throws Exception {
066:                String name = "testModifications_" + System.currentTimeMillis();
067:
068:                //create a new Article and play with it
069:                Article article = createArticle(name);
070:
071:                Identity oid = null;
072:                broker.beginTransaction();
073:                for (int i = 1; i < 50; i++) {
074:                    article.addToStock(10);
075:                    broker.store(article);
076:                    broker.delete(article);
077:                    broker.store(article);
078:                    if (i == 1) {
079:                        // lookup identity
080:                        oid = broker.serviceIdentity().buildIdentity(article);
081:                    }
082:                }
083:                broker.commitTransaction();
084:
085:                Article result = (Article) broker.getObjectByIdentity(oid);
086:                assertNotNull(result);
087:                assertEquals(article.getArticleName(), result.getArticleName());
088:            }
089:
090:            public void testShallowAndDeepRetrieval() throws Exception {
091:                String name = "testShallowAndDeepRetrieval_"
092:                        + System.currentTimeMillis();
093:
094:                ObjectReferenceDescriptor ord = null;
095:
096:                try {
097:                    // prepare test, create article with ProductGroup
098:                    Article tmpArticle = createArticle(name);
099:                    ProductGroup pg = createProductGroup(name);
100:                    tmpArticle.setProductGroup(pg);
101:                    pg.add(tmpArticle);
102:
103:                    broker.beginTransaction();
104:                    // in repository Article 1:1 refererence to PG hasn't enabled auto-update,
105:                    // so first store the PG. PG has enabled auto-update and will store the
106:                    // article automatic
107:                    broker.store(pg);
108:                    broker.commitTransaction();
109:                    // after insert we can build the Article identity
110:                    Identity tmpOID = broker.serviceIdentity().buildIdentity(
111:                            tmpArticle);
112:                    broker.clearCache();
113:
114:                    // switch to shallow retrieval
115:                    ClassDescriptor cld = broker
116:                            .getClassDescriptor(Article.class);
117:                    ord = cld
118:                            .getObjectReferenceDescriptorByName("productGroup");
119:                    ord.setCascadeRetrieve(false);
120:
121:                    Article article = (Article) broker
122:                            .getObjectByIdentity(tmpOID);
123:                    assertNull("now reference should be null", article
124:                            .getProductGroup());
125:
126:                    // now switch to deep retrieval
127:                    ord.setCascadeRetrieve(true);
128:                    // should work without setting cld
129:                    // broker.setClassDescriptor(cld);
130:                    broker.clearCache();
131:                    article = (Article) broker.getObjectByIdentity(tmpOID);
132:                    assertNotNull("now reference should NOT be null", article
133:                            .getProductGroup());
134:                } finally {
135:                    // restore old value
136:                    if (ord != null)
137:                        ord.setCascadeRetrieve(true);
138:                }
139:            }
140:
141:            /**
142:             * tests the PB.retrieveReference() feature
143:             */
144:            public void testRetrieveReference() throws Exception {
145:                String name = "testRetrieveReference_"
146:                        + System.currentTimeMillis();
147:
148:                // ensure there is an item to find
149:                Article tmpArticle = createArticle(name);
150:                ProductGroup pg = createProductGroup(name);
151:                tmpArticle.setProductGroup(pg);
152:                broker.beginTransaction();
153:                broker.store(pg);
154:                broker.store(tmpArticle);
155:                broker.commitTransaction();
156:                Identity tmpOID = broker.serviceIdentity().buildIdentity(
157:                        tmpArticle);
158:                broker.clearCache();
159:
160:                ObjectReferenceDescriptor ord = null;
161:                try {
162:                    // switch to shallow retrieval
163:                    ClassDescriptor cld = broker
164:                            .getClassDescriptor(Article.class);
165:                    // article only has one ord
166:                    ord = cld
167:                            .getObjectReferenceDescriptorByName("productGroup");
168:                    ord.setCascadeRetrieve(false);
169:
170:                    Article article = (Article) broker
171:                            .getObjectByIdentity(tmpOID);
172:                    assertNull("now reference should be null", article
173:                            .getProductGroup());
174:
175:                    // now force loading:
176:                    broker.retrieveReference(article, "productGroup");
177:                    assertNotNull("now reference should NOT be null", article
178:                            .getProductGroup());
179:
180:                    // repair cld
181:                    ord.setCascadeRetrieve(true);
182:                    // should work without setting cld
183:                    // broker.setClassDescriptor(cld);
184:                } finally {
185:                    // restore old value
186:                    if (ord != null)
187:                        ord.setCascadeRetrieve(true);
188:                }
189:            }
190:
191:            /**
192:             * tests the PB.retrieveAllReferences() feature
193:             */
194:            public void testRetrieveAllReferences() {
195:                String name = "testRetrieveAllReferences_"
196:                        + System.currentTimeMillis();
197:
198:                // ensure there is an item to find
199:                Article tmpArticle = createArticle(name);
200:                ProductGroup pg = createProductGroup(name);
201:                tmpArticle.setProductGroup(pg);
202:
203:                broker.beginTransaction();
204:                broker.store(pg);
205:                broker.store(tmpArticle);
206:                broker.commitTransaction();
207:                Identity tmpOID = broker.serviceIdentity().buildIdentity(
208:                        tmpArticle);
209:                broker.clearCache();
210:                ObjectReferenceDescriptor ord = null;
211:                try {
212:                    // switch to shallow retrieval
213:                    ClassDescriptor cld = broker
214:                            .getClassDescriptor(Article.class);
215:                    ord = (ObjectReferenceDescriptor) cld
216:                            .getObjectReferenceDescriptors().get(0);
217:                    ord.setCascadeRetrieve(false);
218:
219:                    Article article = (Article) broker
220:                            .getObjectByIdentity(tmpOID);
221:                    assertNull("now reference should be null", article
222:                            .getProductGroup());
223:
224:                    // now force loading:
225:                    broker.retrieveAllReferences(article);
226:                    assertNotNull("now reference should NOT be null", article
227:                            .getProductGroup());
228:
229:                    // clean up cld
230:                    ord.setCascadeRetrieve(true);
231:                } finally {
232:                    // restore old value
233:                    if (ord != null)
234:                        ord.setCascadeRetrieve(true);
235:                }
236:
237:            }
238:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.