1 package org.apache.ojb.broker; 2 3 import java.util.List ; 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 14 public class BrokerExamples extends PBTestCase 15 { 16 public static void main(String [] args) 17 { 18 String [] arr = {BrokerExamples.class.getName()}; 19 junit.textui.TestRunner.main(arr); 20 } 21 22 public BrokerExamples(String name) 23 { 24 super(name); 25 } 26 27 Article createArticle(String 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 name) 42 { 43 ProductGroup tmpPG = new ProductGroup(); 44 tmpPG.setGroupName(name); 45 return tmpPG; 46 } 47 48 public void testCollectionRetrieval() throws Exception 49 { 50 for (int i = 1; i < 9; i++) 56 { 57 ProductGroup example = new ProductGroup(); 58 example.setId(new Integer (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 articleList = group.getAllArticles(); 65 for(int j = 0; j < articleList.size(); j++) 66 { 67 Object o = articleList.get(j); 68 assertNotNull(o); 69 } 70 } 71 } 72 73 public void testModifications() throws Exception 74 { 75 String name = "testModifications_" + System.currentTimeMillis(); 76 77 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 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 102 { 103 String name = "testShallowAndDeepRetrieval_" + System.currentTimeMillis(); 104 105 ObjectReferenceDescriptor ord = null; 106 107 try 108 { 109 Article tmpArticle = createArticle(name); 111 ProductGroup pg = createProductGroup(name); 112 tmpArticle.setProductGroup(pg); 113 pg.add(tmpArticle); 114 115 broker.beginTransaction(); 116 broker.store(pg); 120 broker.commitTransaction(); 121 Identity tmpOID = broker.serviceIdentity().buildIdentity(tmpArticle); 123 broker.clearCache(); 124 125 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 ord.setCascadeRetrieve(true); 135 broker.clearCache(); 138 article = (Article) broker.getObjectByIdentity(tmpOID); 139 assertNotNull("now reference should NOT be null", article.getProductGroup()); 140 } 141 finally 142 { 143 if(ord != null) ord.setCascadeRetrieve(true); 145 } 146 } 147 148 149 152 public void testRetrieveReference() throws Exception 153 { 154 String name = "testRetrieveReference_" + System.currentTimeMillis(); 155 156 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 ClassDescriptor cld = broker.getClassDescriptor(Article.class); 172 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 broker.retrieveReference(article, "productGroup"); 181 assertNotNull("now reference should NOT be null", article.getProductGroup()); 182 183 ord.setCascadeRetrieve(true); 185 } 188 finally 189 { 190 if(ord != null) ord.setCascadeRetrieve(true); 192 } 193 } 194 195 198 public void testRetrieveAllReferences() 199 { 200 String name = "testRetrieveAllReferences_" + System.currentTimeMillis(); 201 202 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 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 broker.retrieveAllReferences(article); 226 assertNotNull("now reference should NOT be null", article.getProductGroup()); 227 228 ord.setCascadeRetrieve(true); 230 } 231 finally 232 { 233 if(ord != null) ord.setCascadeRetrieve(true); 235 } 236 237 } 238 } 239 | Popular Tags |