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: }
|