KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.apache.ojb.broker;
2
3 import java.util.Collection JavaDoc;
4
5 import org.apache.ojb.broker.query.Criteria;
6 import org.apache.ojb.broker.query.Query;
7 import org.apache.ojb.broker.query.QueryFactory;
8 import org.apache.ojb.junit.PBTestCase;
9
10 /** This TestClass tests OJB facilities to work with polymorphism.
11  */

12 public class PolymorphicExtents extends PBTestCase
13 {
14     public static void main(String JavaDoc[] args)
15     {
16         String JavaDoc[] arr = {PolymorphicExtents.class.getName()};
17         junit.textui.TestRunner.main(arr);
18     }
19
20     public PolymorphicExtents(String JavaDoc name)
21     {
22         super(name);
23     }
24
25     protected Article createArticle(String JavaDoc name)
26     {
27         Article a = new Article();
28         a.setArticleName(name);
29         a.setIsSelloutArticle(true);
30         a.setMinimumStock(100);
31         a.setOrderedUnits(17);
32         a.setPrice(0.45);
33         a.setProductGroupId(new Integer JavaDoc(1));
34         a.setStock(234);
35         a.setSupplierId(4);
36         a.setUnit("bottle");
37         ProductGroup tmpPG = new ProductGroup();
38         tmpPG.setId(new Integer JavaDoc(1));
39         Identity pgID = new Identity(tmpPG, broker);
40         ProductGroupProxy pgProxy = new ProductGroupProxy(broker.getPBKey(),pgID);
41         a.setProductGroup(pgProxy);
42         return a;
43     }
44
45     /** TestThreadsNLocks query support for polymorphic extents*/
46     public void testCollectionByQuery()
47     {
48         Criteria crit = new Criteria();
49         crit.addEqualTo("articleName", "Hamlet");
50         Query q = QueryFactory.newQuery(InterfaceArticle.class, crit);
51
52         Collection JavaDoc result = broker.getCollectionByQuery(q);
53
54         //System.out.println(result);
55

56         assertNotNull("should return at least one item", result);
57         assertTrue("should return at least one item", result.size() > 0);
58 }
59
60     /**
61      * try to retrieve a polymorphic collection attribute
62      * (ProductGroup.allArticlesInGroup contains items
63      * of type TestThreadsNLocks.org.apache.ojb.broker.Article which forms an extent)
64      * ProductGroup 5 contain items from table Artikel, BOOKS and CDS
65      */

66     public void testCollectionRetrieval()
67     {
68         try
69         {
70             ProductGroup example = new ProductGroup();
71             example.setId(new Integer JavaDoc(5));
72
73             ProductGroup group =
74                 (ProductGroup) broker.getObjectByQuery(QueryFactory.newQuery(example));
75
76             // 7 Articles, 2 Books, 3 Cds
77
assertEquals("check size",group.getAllArticles().size(),12);
78
79         }
80         catch (Throwable JavaDoc t)
81         {
82             fail(t.getMessage());
83         }
84
85     }
86
87     /** TestThreadsNLocks EXTENT lookup: a collection with ALL objects in the Article extent*/
88     public void testExtentByQuery() throws Exception JavaDoc
89     {
90         // no criteria signals to omit a WHERE clause
91
Criteria selectAll = null;
92         Query q = QueryFactory.newQuery(InterfaceArticle.class, selectAll);
93
94         Collection JavaDoc result = broker.getCollectionByQuery(q);
95
96         //System.out.println("OJB proudly presents: The InterfaceArticle Extent\n" + result);
97

98         assertNotNull("should return at least one item", result);
99         assertTrue("should return at least one item", result.size() > 0);
100     }
101
102     /** TestThreadsNLocks to lookup items from extent classes*/
103     public void testRetrieveObjectByIdentity()
104     {
105         String JavaDoc name = "testRetrieveObjectByIdentity_" + System.currentTimeMillis();
106         BookArticle book = new BookArticle();
107         book.setArticleName(name);
108         CdArticle cd = new CdArticle();
109         cd.setArticleName(name);
110
111         broker.beginTransaction();
112         broker.store(book);
113         broker.store(cd);
114         broker.commitTransaction();
115
116         Article example = new Article();
117         example.setArticleId(cd.getArticleId());
118         // id not present in table ARTICLES but int table CDS
119
Identity oid = broker.serviceIdentity().buildIdentity(example);
120         InterfaceArticle result = (InterfaceArticle) broker.getObjectByIdentity(oid);
121         assertNotNull("should find a CD-article", result);
122         assertTrue("should be of type CdArticle", (result instanceof CdArticle));
123
124         example = new Article();
125         example.setArticleId(book.getArticleId());
126         // id not present in table ARTICLES but int table BOOKS
127
oid = broker.serviceIdentity().buildIdentity(example);
128         result = (InterfaceArticle) broker.getObjectByIdentity(oid);
129         assertNotNull("should find a Book-article", result);
130         assertTrue("should be of type BookArticle", (result instanceof BookArticle));
131     }
132
133     /**
134      * try to load polymorphic references
135      * (OrderPosition.article is of type InterfaceArticle)
136      */

137     public void testRetrieveReferences() throws Exception JavaDoc
138     {
139         for (int i = 1; i < 4; i++)
140         {
141             OrderPosition tmp = new OrderPosition();
142             tmp.setId(i);
143             Identity oid = new Identity(tmp, broker);
144             OrderPosition pos = (OrderPosition) broker.getObjectByIdentity(oid);
145             assertNotNull(pos);
146         }
147     }
148 }
149
Popular Tags