1 package org.apache.ojb.broker; 2 3 4 import java.util.Collection ; 5 import java.util.Iterator ; 6 import java.util.Vector ; 7 8 import org.apache.ojb.broker.query.Query; 9 import org.apache.ojb.broker.query.QueryFactory; 10 import org.apache.ojb.junit.PBTestCase; 11 12 16 public class ProxyExamples extends PBTestCase 17 { 18 public static void main(String [] args) 19 { 20 String [] arr = {ProxyExamples.class.getName()}; 21 junit.textui.TestRunner.main(arr); 22 } 23 24 public ProxyExamples(String name) 25 { 26 super(name); 27 } 28 29 42 public void testProgrammedProxies() throws Exception 43 { 44 String name = "testDynamicProxies_" + System.currentTimeMillis(); 45 Vector myArticles = new Vector (); 46 ProductGroup pg = new ProductGroup(); 48 pg.setGroupName(name); 49 broker.beginTransaction(); 50 broker.store(pg); 51 broker.commitTransaction(); 52 53 for(int i = 1; i < 10; i++) 54 { 55 Article a = new Article(); 56 a.setArticleName(name); 57 a.setProductGroup(pg); 58 broker.beginTransaction(); 59 broker.store(a); 60 broker.commitTransaction(); 61 Identity id = broker.serviceIdentity().buildIdentity(a); 62 InterfaceArticle A = 63 (InterfaceArticle) ((PersistenceBrokerInternal)broker).createProxy(Article.class, id); 64 myArticles.add(A); 65 } 67 for(int i = 0; i < 9; i++) 72 { 73 InterfaceArticle a = (InterfaceArticle) myArticles.get(i); 74 assertNotNull(a); 76 } 77 for(int i = 0; i < 9; i++) 80 { 81 InterfaceArticle a = (InterfaceArticle) myArticles.get(i); 82 assertNotNull(a.getProductGroup()); 83 assertNotNull(a.getProductGroup().getName()); 84 85 } 87 Object [] pkvals = new Object [1]; 91 pkvals[0] = new Integer (2); 92 Identity id = new Identity(ProductGroup.class, ProductGroup.class, pkvals); 93 InterfaceProductGroup group2 = null; 94 try 95 { 96 group2 = (InterfaceProductGroup) ((PersistenceBrokerInternal)broker).createProxy(ProductGroupProxy.class, id); 97 } 98 catch(Exception ignored) 99 { 100 } 101 broker.beginTransaction(); 103 for(int i = 0; i < group2.getAllArticles().size(); i++) 104 { 105 InterfaceArticle a = (InterfaceArticle) group2.getAllArticles().get(i); 106 assertNotNull(a); 108 broker.store(a); 109 } 110 broker.store(group2); 111 broker.commitTransaction(); 112 } 113 114 130 143 public void testDynamicProxies() 144 { 145 String name = "testDynamicProxies_" + System.currentTimeMillis(); 146 Vector myArticles = new Vector (); 147 ProductGroup pg = new ProductGroup(); 149 pg.setGroupName(name); 150 broker.beginTransaction(); 151 broker.store(pg); 152 broker.commitTransaction(); 153 154 for(int i = 1; i < 10; i++) 155 { 156 Article a = new Article(); 157 a.setArticleName(name); 158 a.setProductGroup(pg); 159 broker.beginTransaction(); 160 broker.store(a); 161 broker.commitTransaction(); 162 Identity id = broker.serviceIdentity().buildIdentity(a); 163 InterfaceArticle A = 164 (InterfaceArticle) ((PersistenceBrokerInternal)broker).createProxy(Article.class, id); 165 myArticles.add(A); 166 } 168 for(int i = 0; i < 9; i++) 173 { 174 InterfaceArticle a = (InterfaceArticle) myArticles.get(i); 175 } 177 for(int i = 0; i < 9; i++) 180 { 181 InterfaceArticle a = (InterfaceArticle) myArticles.get(i); 182 } 184 } 185 186 public void testCollectionProxies() throws Exception 187 { 188 ProductGroupWithCollectionProxy org_pg = new ProductGroupWithCollectionProxy(); 189 org_pg.setId(new Integer (7)); 190 Identity pgOID = broker.serviceIdentity().buildIdentity(org_pg); 191 192 ProductGroupWithCollectionProxy pg = (ProductGroupWithCollectionProxy) broker.getObjectByIdentity(pgOID); 193 assertEquals(org_pg.getId(), pg.getId()); 194 195 Collection col = pg.getAllArticles(); 196 int countedSize = col.size(); Iterator iter = col.iterator(); 198 while(iter.hasNext()) 199 { 200 InterfaceArticle a = (InterfaceArticle) iter.next(); 201 } 202 203 assertEquals("compare counted and loaded size", countedSize, col.size()); 204 } 205 206 public void testCollectionProxiesAndExtents() throws Exception 207 { 208 ProductGroupWithCollectionProxy pg = new ProductGroupWithCollectionProxy(); 209 pg.setId(new Integer (5)); 210 Identity pgOID = broker.serviceIdentity().buildIdentity(pg); 211 212 pg = (ProductGroupWithCollectionProxy) broker.getObjectByIdentity(pgOID); 213 assertEquals(5, pg.getId().intValue()); 214 215 Collection col = pg.getAllArticles(); 216 int countedSize = col.size(); Iterator iter = col.iterator(); 218 while(iter.hasNext()) 219 { 220 InterfaceArticle a = (InterfaceArticle) iter.next(); 221 } 222 223 assertEquals("compare counted and loaded size", countedSize, col.size()); 224 225 assertEquals("check size", col.size(), 12); 227 } 228 229 public void testReferenceProxies() 230 { 231 ArticleWithReferenceProxy a = new ArticleWithReferenceProxy(); 232 a.setArticleName("ProxyExamples.testReferenceProxy article"); 234 235 Query q = QueryFactory.newQuery(a); 236 237 ProductGroup pg = new ProductGroup(); 238 pg.setGroupName("ProxyExamples test group"); 240 241 a.setProductGroup(pg); 242 broker.beginTransaction(); 243 broker.store(a); 244 broker.commitTransaction(); 245 int id = pg.getGroupId().intValue(); 246 247 broker.clearCache(); 248 ArticleWithReferenceProxy ar = (ArticleWithReferenceProxy) broker.getObjectByQuery(q); 249 250 assertEquals(id, ar.getProductGroup().getId().intValue()); 251 } 252 253 259 public void testProxiesAndJDBCTransactionIsolation() 260 { 261 boolean commit = false; 262 try 263 { 264 broker.beginTransaction(); 266 267 ProductGroupWithCollectionProxy pg = new ProductGroupWithCollectionProxy(); 269 pg.setGroupName("TESTPRODUCTGROUP"); 270 broker.store(pg); 271 272 for(int j = 1; j <= 2; j++) 274 { 275 Article ar = new Article(); 276 ar.setArticleName("ARTICLE " + j); 277 ar.setProductGroup(pg); 278 broker.store(ar); 279 } 280 281 broker.clearCache(); 283 pg = (ProductGroupWithCollectionProxy) broker.getObjectByQuery(QueryFactory.newQuery(pg)); 284 assertTrue(pg != null); 285 286 Collection articles = pg.getAllArticlesInGroup(); 291 assertEquals(2, articles.size()); 292 293 broker.commitTransaction(); 295 commit = true; 296 } 297 finally 298 { 299 if(!commit) 300 broker.abortTransaction(); 301 } 302 } 303 304 } 305 | Popular Tags |