KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > BrokerExamples


1 package org.apache.ojb.broker;
2
3 import java.util.List JavaDoc;
4
5 import org.apache.ojb.broker.metadata.ClassDescriptor;
6 import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
7 import org.apache.ojb.broker.query.QueryFactory;
8 import org.apache.ojb.junit.PBTestCase;
9
10 /**
11  * Demo Application that shows basic concepts for Applications using the PersistenceBroker
12  * as a mediator for persistence
13  */

14 public class BrokerExamples extends PBTestCase
15 {
16     public static void main(String JavaDoc[] args)
17     {
18         String JavaDoc[] arr = {BrokerExamples.class.getName()};
19         junit.textui.TestRunner.main(arr);
20     }
21
22     public BrokerExamples(String JavaDoc name)
23     {
24         super(name);
25     }
26
27     Article createArticle(String JavaDoc name)
28     {
29         Article a = new Article();
30         a.setArticleName(name);
31         a.setIsSelloutArticle(true);
32         a.setMinimumStock(100);
33         a.setOrderedUnits(17);
34         a.setPrice(0.45);
35         a.setStock(234);
36         a.setSupplierId(4);
37         a.setUnit("bottle");
38         return a;
39     }
40
41     ProductGroup createProductGroup(String JavaDoc name)
42     {
43         ProductGroup tmpPG = new ProductGroup();
44         tmpPG.setGroupName(name);
45         return tmpPG;
46     }
47
48     public void testCollectionRetrieval() throws Exception JavaDoc
49     {
50         // Use PersistenceBroker to lookup persistent objects.
51
// Loop through categories with id 1 to 9
52
// A ProductGroup holds a List of all Article Objects in the specific category
53
// the repository.xml specifies that the List of Artikels has to be
54
// materialized immediately.
55
for (int i = 1; i < 9; i++)
56         {
57             ProductGroup example = new ProductGroup();
58             example.setId(new Integer JavaDoc(i));
59
60             ProductGroup group =
61                     (ProductGroup) broker.getObjectByQuery(QueryFactory.newQuery(example));
62             assertNotNull("Expect a ProductGroup with id " + i, group);
63             assertEquals("should be equal", i, group.getId().intValue());
64             List JavaDoc articleList = group.getAllArticles();
65             for(int j = 0; j < articleList.size(); j++)
66             {
67                 Object JavaDoc o = articleList.get(j);
68                 assertNotNull(o);
69             }
70         }
71     }
72
73     public void testModifications() throws Exception JavaDoc
74     {
75         String JavaDoc name = "testModifications_" + System.currentTimeMillis();
76
77         //create a new Article and play with it
78
Article article = createArticle(name);
79
80         Identity oid = null;
81         broker.beginTransaction();
82         for (int i = 1; i < 50; i++)
83         {
84             article.addToStock(10);
85             broker.store(article);
86             broker.delete(article);
87             broker.store(article);
88             if(i == 1)
89             {
90                 // lookup identity
91
oid = broker.serviceIdentity().buildIdentity(article);
92             }
93         }
94         broker.commitTransaction();
95
96         Article result = (Article) broker.getObjectByIdentity(oid);
97         assertNotNull(result);
98         assertEquals(article.getArticleName(), result.getArticleName());
99     }
100
101     public void testShallowAndDeepRetrieval() throws Exception JavaDoc
102     {
103         String JavaDoc name = "testShallowAndDeepRetrieval_" + System.currentTimeMillis();
104
105         ObjectReferenceDescriptor ord = null;
106
107         try
108         {
109             // prepare test, create article with ProductGroup
110
Article tmpArticle = createArticle(name);
111             ProductGroup pg = createProductGroup(name);
112             tmpArticle.setProductGroup(pg);
113             pg.add(tmpArticle);
114
115             broker.beginTransaction();
116             // in repository Article 1:1 refererence to PG hasn't enabled auto-update,
117
// so first store the PG. PG has enabled auto-update and will store the
118
// article automatic
119
broker.store(pg);
120             broker.commitTransaction();
121             // after insert we can build the Article identity
122
Identity tmpOID = broker.serviceIdentity().buildIdentity(tmpArticle);
123             broker.clearCache();
124
125             // switch to shallow retrieval
126
ClassDescriptor cld = broker.getClassDescriptor(Article.class);
127             ord = cld.getObjectReferenceDescriptorByName("productGroup");
128             ord.setCascadeRetrieve(false);
129
130             Article article = (Article) broker.getObjectByIdentity(tmpOID);
131             assertNull("now reference should be null", article.getProductGroup());
132
133             // now switch to deep retrieval
134
ord.setCascadeRetrieve(true);
135             // should work without setting cld
136
// broker.setClassDescriptor(cld);
137
broker.clearCache();
138             article = (Article) broker.getObjectByIdentity(tmpOID);
139             assertNotNull("now reference should NOT be null", article.getProductGroup());
140         }
141         finally
142         {
143             // restore old value
144
if(ord != null) ord.setCascadeRetrieve(true);
145         }
146     }
147
148
149     /**
150      * tests the PB.retrieveReference() feature
151      */

152     public void testRetrieveReference() throws Exception JavaDoc
153     {
154         String JavaDoc name = "testRetrieveReference_" + System.currentTimeMillis();
155
156         // ensure there is an item to find
157
Article tmpArticle = createArticle(name);
158         ProductGroup pg = createProductGroup(name);
159         tmpArticle.setProductGroup(pg);
160         broker.beginTransaction();
161         broker.store(pg);
162         broker.store(tmpArticle);
163         broker.commitTransaction();
164         Identity tmpOID = broker.serviceIdentity().buildIdentity(tmpArticle);
165         broker.clearCache();
166
167         ObjectReferenceDescriptor ord = null;
168         try
169         {
170             // switch to shallow retrieval
171
ClassDescriptor cld = broker.getClassDescriptor(Article.class);
172             // article only has one ord
173
ord = cld.getObjectReferenceDescriptorByName("productGroup");
174             ord.setCascadeRetrieve(false);
175
176             Article article = (Article) broker.getObjectByIdentity(tmpOID);
177             assertNull("now reference should be null", article.getProductGroup());
178
179             // now force loading:
180
broker.retrieveReference(article, "productGroup");
181             assertNotNull("now reference should NOT be null", article.getProductGroup());
182
183             // repair cld
184
ord.setCascadeRetrieve(true);
185             // should work without setting cld
186
// broker.setClassDescriptor(cld);
187
}
188         finally
189         {
190             // restore old value
191
if(ord != null) ord.setCascadeRetrieve(true);
192         }
193     }
194
195     /**
196      * tests the PB.retrieveAllReferences() feature
197      */

198     public void testRetrieveAllReferences()
199     {
200         String JavaDoc name = "testRetrieveAllReferences_" + System.currentTimeMillis();
201
202         // ensure there is an item to find
203
Article tmpArticle = createArticle(name);
204         ProductGroup pg = createProductGroup(name);
205         tmpArticle.setProductGroup(pg);
206
207         broker.beginTransaction();
208         broker.store(pg);
209         broker.store(tmpArticle);
210         broker.commitTransaction();
211         Identity tmpOID = broker.serviceIdentity().buildIdentity(tmpArticle);
212         broker.clearCache();
213         ObjectReferenceDescriptor ord = null;
214         try
215         {
216             // switch to shallow retrieval
217
ClassDescriptor cld = broker.getClassDescriptor(Article.class);
218             ord = (ObjectReferenceDescriptor) cld.getObjectReferenceDescriptors().get(0);
219             ord.setCascadeRetrieve(false);
220
221             Article article = (Article) broker.getObjectByIdentity(tmpOID);
222             assertNull("now reference should be null", article.getProductGroup());
223
224             // now force loading:
225
broker.retrieveAllReferences(article);
226             assertNotNull("now reference should NOT be null", article.getProductGroup());
227
228             // clean up cld
229
ord.setCascadeRetrieve(true);
230         }
231         finally
232         {
233             // restore old value
234
if(ord != null) ord.setCascadeRetrieve(true);
235         }
236
237     }
238 }
239
Popular Tags