001: package org.apache.ojb.broker;
002:
003: import java.util.Collection;
004:
005: import org.apache.ojb.broker.query.Criteria;
006: import org.apache.ojb.broker.query.Query;
007: import org.apache.ojb.broker.query.QueryFactory;
008: import org.apache.ojb.junit.PBTestCase;
009:
010: /** This TestClass tests OJB facilities to work with polymorphism.
011: */
012: public class PolymorphicExtents extends PBTestCase {
013: public static void main(String[] args) {
014: String[] arr = { PolymorphicExtents.class.getName() };
015: junit.textui.TestRunner.main(arr);
016: }
017:
018: public PolymorphicExtents(String name) {
019: super (name);
020: }
021:
022: protected Article createArticle(String name) {
023: Article a = new Article();
024: a.setArticleName(name);
025: a.setIsSelloutArticle(true);
026: a.setMinimumStock(100);
027: a.setOrderedUnits(17);
028: a.setPrice(0.45);
029: a.setProductGroupId(new Integer(1));
030: a.setStock(234);
031: a.setSupplierId(4);
032: a.setUnit("bottle");
033: ProductGroup tmpPG = new ProductGroup();
034: tmpPG.setId(new Integer(1));
035: Identity pgID = new Identity(tmpPG, broker);
036: ProductGroupProxy pgProxy = new ProductGroupProxy(broker
037: .getPBKey(), pgID);
038: a.setProductGroup(pgProxy);
039: return a;
040: }
041:
042: /** TestThreadsNLocks query support for polymorphic extents*/
043: public void testCollectionByQuery() {
044: Criteria crit = new Criteria();
045: crit.addEqualTo("articleName", "Hamlet");
046: Query q = QueryFactory.newQuery(InterfaceArticle.class, crit);
047:
048: Collection result = broker.getCollectionByQuery(q);
049:
050: //System.out.println(result);
051:
052: assertNotNull("should return at least one item", result);
053: assertTrue("should return at least one item", result.size() > 0);
054: }
055:
056: /**
057: * try to retrieve a polymorphic collection attribute
058: * (ProductGroup.allArticlesInGroup contains items
059: * of type TestThreadsNLocks.org.apache.ojb.broker.Article which forms an extent)
060: * ProductGroup 5 contain items from table Artikel, BOOKS and CDS
061: */
062: public void testCollectionRetrieval() {
063: try {
064: ProductGroup example = new ProductGroup();
065: example.setId(new Integer(5));
066:
067: ProductGroup group = (ProductGroup) broker
068: .getObjectByQuery(QueryFactory.newQuery(example));
069:
070: // 7 Articles, 2 Books, 3 Cds
071: assertEquals("check size", group.getAllArticles().size(),
072: 12);
073:
074: } catch (Throwable t) {
075: fail(t.getMessage());
076: }
077:
078: }
079:
080: /** TestThreadsNLocks EXTENT lookup: a collection with ALL objects in the Article extent*/
081: public void testExtentByQuery() throws Exception {
082: // no criteria signals to omit a WHERE clause
083: Criteria selectAll = null;
084: Query q = QueryFactory.newQuery(InterfaceArticle.class,
085: selectAll);
086:
087: Collection result = broker.getCollectionByQuery(q);
088:
089: //System.out.println("OJB proudly presents: The InterfaceArticle Extent\n" + result);
090:
091: assertNotNull("should return at least one item", result);
092: assertTrue("should return at least one item", result.size() > 0);
093: }
094:
095: /** TestThreadsNLocks to lookup items from extent classes*/
096: public void testRetrieveObjectByIdentity() {
097: String name = "testRetrieveObjectByIdentity_"
098: + System.currentTimeMillis();
099: BookArticle book = new BookArticle();
100: book.setArticleName(name);
101: CdArticle cd = new CdArticle();
102: cd.setArticleName(name);
103:
104: broker.beginTransaction();
105: broker.store(book);
106: broker.store(cd);
107: broker.commitTransaction();
108:
109: Article example = new Article();
110: example.setArticleId(cd.getArticleId());
111: // id not present in table ARTICLES but int table CDS
112: Identity oid = broker.serviceIdentity().buildIdentity(example);
113: InterfaceArticle result = (InterfaceArticle) broker
114: .getObjectByIdentity(oid);
115: assertNotNull("should find a CD-article", result);
116: assertTrue("should be of type CdArticle",
117: (result instanceof CdArticle));
118:
119: example = new Article();
120: example.setArticleId(book.getArticleId());
121: // id not present in table ARTICLES but int table BOOKS
122: oid = broker.serviceIdentity().buildIdentity(example);
123: result = (InterfaceArticle) broker.getObjectByIdentity(oid);
124: assertNotNull("should find a Book-article", result);
125: assertTrue("should be of type BookArticle",
126: (result instanceof BookArticle));
127: }
128:
129: /**
130: * try to load polymorphic references
131: * (OrderPosition.article is of type InterfaceArticle)
132: */
133: public void testRetrieveReferences() throws Exception {
134: for (int i = 1; i < 4; i++) {
135: OrderPosition tmp = new OrderPosition();
136: tmp.setId(i);
137: Identity oid = new Identity(tmp, broker);
138: OrderPosition pos = (OrderPosition) broker
139: .getObjectByIdentity(oid);
140: assertNotNull(pos);
141: }
142: }
143: }
|