1 package org.apache.ojb.broker; 2 3 import java.util.Collection ; 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 12 public class PolymorphicExtents extends PBTestCase 13 { 14 public static void main(String [] args) 15 { 16 String [] arr = {PolymorphicExtents.class.getName()}; 17 junit.textui.TestRunner.main(arr); 18 } 19 20 public PolymorphicExtents(String name) 21 { 22 super(name); 23 } 24 25 protected Article createArticle(String 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 (1)); 34 a.setStock(234); 35 a.setSupplierId(4); 36 a.setUnit("bottle"); 37 ProductGroup tmpPG = new ProductGroup(); 38 tmpPG.setId(new Integer (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 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 result = broker.getCollectionByQuery(q); 53 54 56 assertNotNull("should return at least one item", result); 57 assertTrue("should return at least one item", result.size() > 0); 58 } 59 60 66 public void testCollectionRetrieval() 67 { 68 try 69 { 70 ProductGroup example = new ProductGroup(); 71 example.setId(new Integer (5)); 72 73 ProductGroup group = 74 (ProductGroup) broker.getObjectByQuery(QueryFactory.newQuery(example)); 75 76 assertEquals("check size",group.getAllArticles().size(),12); 78 79 } 80 catch (Throwable t) 81 { 82 fail(t.getMessage()); 83 } 84 85 } 86 87 88 public void testExtentByQuery() throws Exception 89 { 90 Criteria selectAll = null; 92 Query q = QueryFactory.newQuery(InterfaceArticle.class, selectAll); 93 94 Collection result = broker.getCollectionByQuery(q); 95 96 98 assertNotNull("should return at least one item", result); 99 assertTrue("should return at least one item", result.size() > 0); 100 } 101 102 103 public void testRetrieveObjectByIdentity() 104 { 105 String 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 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 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 137 public void testRetrieveReferences() throws Exception 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 |