1 5 6 package org.apache.ojb.broker.sqlcount; 7 8 import java.util.ArrayList ; 9 import java.util.Arrays ; 10 import java.util.Collection ; 11 import java.util.Iterator ; 12 import java.util.List ; 13 14 import org.apache.ojb.broker.Article; 15 import org.apache.ojb.broker.Identity; 16 import org.apache.ojb.broker.InterfaceArticle; 17 import org.apache.ojb.broker.InterfaceProductGroup; 18 import org.apache.ojb.broker.PersistenceBroker; 19 import org.apache.ojb.broker.PersistenceBrokerFactory; 20 import org.apache.ojb.broker.Person; 21 import org.apache.ojb.broker.ProductGroup; 22 import org.apache.ojb.broker.Project; 23 import org.apache.ojb.broker.metadata.ClassDescriptor; 24 import org.apache.ojb.broker.query.Criteria; 25 import org.apache.ojb.broker.query.Query; 26 import org.apache.ojb.broker.query.QueryByCriteria; 27 import org.apache.ojb.broker.query.QueryFactory; 28 import org.apache.ojb.broker.util.ObjectModification; 29 30 34 public class CollectionCountTest extends AbstractCountTest 35 { 36 protected PersistenceBroker myPB; 37 38 protected void setUp() throws Exception 39 { 40 super.setUp(); 41 myPB = PersistenceBrokerFactory.defaultPersistenceBroker(); 42 } 43 44 protected void tearDown() throws Exception 45 { 46 if ((myPB != null) && !myPB.isClosed()) 47 { 48 myPB.close(); 49 } 50 super.tearDown(); 51 } 52 53 56 public void testAllProductGroups() 57 { 58 resetStmtCount(); 59 myPB.clearCache(); 60 myPB.beginTransaction(); 61 Query qry = new QueryByCriteria(ProductGroup.class, null); 62 assertStmtCount("preparation", 0); 63 Iterator iter = myPB.getIteratorByQuery(qry); 64 assertStmtCount("getIteratorByQuery", 1); 65 while (iter.hasNext()) 66 { 67 resetStmtCount(); 68 InterfaceProductGroup next = (InterfaceProductGroup) iter.next(); 69 assertStmtCount("next", 0); 70 List articles = next.getAllArticles(); 71 assertStmtCount("getAllArticles", 4); 76 } 77 myPB.commitTransaction(); 78 } 79 80 83 public void testProductGroup5() 84 { 85 resetStmtCount(); 86 myPB.clearCache(); 87 myPB.beginTransaction(); 88 ProductGroup pg = 89 (ProductGroup)myPB.getObjectByIdentity(new Identity(null, ProductGroup.class, new Object [] {new Integer (5)})); 90 assertStmtCount("getObjectByIdentity", 4); 91 resetStmtCount(); 92 List articles = pg.getAllArticles(); 93 assertEquals(12, articles.size()); 94 assertStmtCount("getAllArticles", 0); 95 resetStmtCount(); 96 for (Iterator articleIterator = articles.iterator(); articleIterator.hasNext(); ) 97 { 98 InterfaceArticle article = (InterfaceArticle)articleIterator.next(); 99 logger.info("articleId " + article.getArticleId()); 100 } 102 assertStmtCount("collect ids: ", 1); resetStmtCount(); 104 String str = pg.toString(); 105 assertStmtCount("toString", 4); 110 logger.info(str); 111 myPB.commitTransaction(); 112 } 113 114 119 public void testPersonEmptyProjectsInsert() 120 { 121 resetStmtCount(); 122 myPB.clearCache(); 123 myPB.beginTransaction(); 124 Person pers = new Person(); 125 myPB.store(pers); 126 logStmtCount("Storing first person"); resetStmtCount(); 134 pers = new Person(); 135 myPB.store(pers, ObjectModification.INSERT); 136 myPB.commitTransaction(); 137 assertStmtCount("insert second Person with empty collection.", 2); 140 } 141 142 147 public void testPersonSingleProjectInsert() 148 { 149 resetStmtCount(); 150 myPB.clearCache(); 151 myPB.beginTransaction(); 152 Person pers = new Person(); 153 pers.setFirstname("testPersonSingleProjectInsert(1)"); 154 Project proj = new Project(); 155 proj.setTitle("testPersonSingleProjectInsert(1)"); 156 myPB.store(pers); 157 myPB.store(proj); 158 logStmtCount("Storing first person and first project"); 159 resetStmtCount(); 161 pers = new Person(); 162 Project proj2 = new Project(); 163 proj2.setTitle("proj2"); 164 Collection projects = Arrays.asList(new Project[] {proj2}); 165 pers.setProjects(projects); 166 myPB.store(pers, ObjectModification.INSERT); 167 myPB.commitTransaction(); 168 assertStmtCount("insert second Person, singleton collection.", 6); 175 } 176 177 178 public void testPrefetched() 179 { 180 ClassDescriptor cldProductGroup = myPB.getClassDescriptor(ProductGroup.class); 181 ClassDescriptor cldArticle = myPB.getClassDescriptor(Article.class); 182 Class productGroupProxy = cldProductGroup.getProxyClass(); 183 Class articleProxy = cldArticle.getProxyClass(); 184 185 cldProductGroup.setProxyClass(null); 189 cldProductGroup.setProxyClassName(null); 190 cldArticle.setProxyClass(null); 191 cldArticle.setProxyClassName(null); 192 193 resetStmtCount(); 194 myPB.clearCache(); 195 196 197 myPB.beginTransaction(); 198 199 Criteria crit = new Criteria(); 200 crit.addLessOrEqualThan("groupId", new Integer (5)); 201 QueryByCriteria q = QueryFactory.newQuery(ProductGroup.class, crit); 202 q.addOrderByDescending("groupId"); 203 q.addPrefetchedRelationship("allArticlesInGroup"); 204 205 Collection results = myPB.getCollectionByQuery(q); 206 assertEquals("Number of ProductGroups", 5, results.size()); 207 Collection articles = new ArrayList (); 208 for (Iterator it = results.iterator();it.hasNext();) 209 { 210 ProductGroup p = (ProductGroup) it.next(); 211 articles.addAll(p.getAllArticles()); 212 } 213 assertEquals("Total number of Articles", 59, articles.size()); 214 215 assertStmtCount("Read Prefetched.", 4); 220 myPB.commitTransaction(); 221 222 cldProductGroup.setProxyClass(productGroupProxy); 226 cldProductGroup.setProxyClassName(productGroupProxy.getName()); 227 cldArticle.setProxyClass(articleProxy); 228 cldArticle.setProxyClassName(articleProxy.getName()); 229 230 } 231 232 public void testNonPrefetched() 233 { 234 ClassDescriptor cldProductGroup = myPB.getClassDescriptor(ProductGroup.class); 235 ClassDescriptor cldArticle = myPB.getClassDescriptor(Article.class); 236 Class productGroupProxy = cldProductGroup.getProxyClass(); 237 Class articleProxy = cldArticle.getProxyClass(); 238 239 cldProductGroup.setProxyClass(null); 243 cldProductGroup.setProxyClassName(null); 244 cldArticle.setProxyClass(null); 245 cldArticle.setProxyClassName(null); 246 247 resetStmtCount(); 248 myPB.clearCache(); 249 250 myPB.beginTransaction(); 251 252 Criteria crit = new Criteria(); 253 crit.addLessOrEqualThan("groupId", new Integer (5)); 254 QueryByCriteria q = QueryFactory.newQuery(ProductGroup.class, crit); 255 q.addOrderByDescending("groupId"); 256 257 Collection results = myPB.getCollectionByQuery(q); 258 assertEquals("Number of ProductGroups", 5, results.size()); 259 Collection articles = new ArrayList (); 260 for (Iterator it = results.iterator();it.hasNext();) 261 { 262 ProductGroup p = (ProductGroup) it.next(); 263 articles.addAll(p.getAllArticles()); 264 } 265 assertEquals("Total number of Articles", 59, articles.size()); 266 267 assertStmtCount("Read Non-Prefetched.", 16); 279 myPB.commitTransaction(); 280 281 cldProductGroup.setProxyClass(productGroupProxy); 285 cldProductGroup.setProxyClassName(productGroupProxy.getName()); 286 cldArticle.setProxyClass(articleProxy); 287 cldArticle.setProxyClassName(articleProxy.getName()); 288 289 } 290 291 } 292 | Popular Tags |