001: /*
002: * (C) 2003 ppi Media
003: * User: om
004: */
005:
006: package org.apache.ojb.broker.sqlcount;
007:
008: import java.util.ArrayList;
009: import java.util.Arrays;
010: import java.util.Collection;
011: import java.util.Iterator;
012: import java.util.List;
013:
014: import org.apache.ojb.broker.Article;
015: import org.apache.ojb.broker.Identity;
016: import org.apache.ojb.broker.InterfaceArticle;
017: import org.apache.ojb.broker.InterfaceProductGroup;
018: import org.apache.ojb.broker.PersistenceBroker;
019: import org.apache.ojb.broker.PersistenceBrokerFactory;
020: import org.apache.ojb.broker.Person;
021: import org.apache.ojb.broker.ProductGroup;
022: import org.apache.ojb.broker.Project;
023: import org.apache.ojb.broker.metadata.ClassDescriptor;
024: import org.apache.ojb.broker.query.Criteria;
025: import org.apache.ojb.broker.query.Query;
026: import org.apache.ojb.broker.query.QueryByCriteria;
027: import org.apache.ojb.broker.query.QueryFactory;
028: import org.apache.ojb.broker.util.ObjectModification;
029:
030: /**
031: * @author <a href="mailto:om@ppi.de">Oliver Matz</a>
032: * @version $Id: CollectionCountTest.java,v 1.9.2.1 2005/10/10 00:35:43 arminw Exp $
033: */
034: public class CollectionCountTest extends AbstractCountTest {
035: protected PersistenceBroker myPB;
036:
037: protected void setUp() throws Exception {
038: super .setUp();
039: myPB = PersistenceBrokerFactory.defaultPersistenceBroker();
040: }
041:
042: protected void tearDown() throws Exception {
043: if ((myPB != null) && !myPB.isClosed()) {
044: myPB.close();
045: }
046: super .tearDown();
047: }
048:
049: /**
050: * retrieve all product groups.
051: */
052: public void testAllProductGroups() {
053: resetStmtCount();
054: myPB.clearCache();
055: myPB.beginTransaction();
056: Query qry = new QueryByCriteria(ProductGroup.class, null);
057: assertStmtCount("preparation", 0);
058: Iterator iter = myPB.getIteratorByQuery(qry);
059: assertStmtCount("getIteratorByQuery", 1);
060: while (iter.hasNext()) {
061: resetStmtCount();
062: InterfaceProductGroup next = (InterfaceProductGroup) iter
063: .next();
064: assertStmtCount("next", 0);
065: List articles = next.getAllArticles();
066: // SELECT ... FROM Kategorien
067: // SELECT ... FROM Artikel
068: // SELECT ... FROM BOOKS
069: // SELECT ... FROM CDS
070: assertStmtCount("getAllArticles", 4);
071: }
072: myPB.commitTransaction();
073: }
074:
075: /**
076: * retrieve Product group number 5 and its 12 articles
077: */
078: public void testProductGroup5() {
079: resetStmtCount();
080: myPB.clearCache();
081: myPB.beginTransaction();
082: ProductGroup pg = (ProductGroup) myPB
083: .getObjectByIdentity(new Identity(null,
084: ProductGroup.class, new Object[] { new Integer(
085: 5) }));
086: assertStmtCount("getObjectByIdentity", 4);
087: resetStmtCount();
088: List articles = pg.getAllArticles();
089: assertEquals(12, articles.size());
090: assertStmtCount("getAllArticles", 0);
091: resetStmtCount();
092: for (Iterator articleIterator = articles.iterator(); articleIterator
093: .hasNext();) {
094: InterfaceArticle article = (InterfaceArticle) articleIterator
095: .next();
096: logger.info("articleId " + article.getArticleId());
097: // SELECT ... FROM Artikel WHERE Artikel_Nr = ...
098: }
099: assertStmtCount("collect ids: ", 1); // batch retrieval!
100: resetStmtCount();
101: String str = pg.toString();
102: // SELECT ... FROM Kategorien WHERE Kategorie_Nr = '5'
103: // SELECT ... FROM Artikel A0 WHERE Kategorie_Nr = '5'
104: // SELECT ... FROM BOOKS A0 WHERE Kategorie_Nr = '5'
105: // SELECT ... FROM CDS A0 WHERE Kategorie_Nr = '5'
106: assertStmtCount("toString", 4);
107: logger.info(str);
108: myPB.commitTransaction();
109: }
110:
111: /*
112: * insert a person with an empty project collection.
113: * note: the <em>first</em> Person and Project require extra lookups
114: * in the table OJB_HL_SEQ.
115: */
116: public void testPersonEmptyProjectsInsert() {
117: resetStmtCount();
118: myPB.clearCache();
119: myPB.beginTransaction();
120: Person pers = new Person();
121: myPB.store(pers);
122: //SELECT A0.VERSION,A0.GRAB_SIZE,A0.MAX_KEY,A0.FIELDNAME,A0.TABLENAME FROM OJB_HL_SEQ A0 WHERE (A0.TABLENAME LIKE 'SEQ_PERSON' ) AND A0.FIELDNAME LIKE 'ID'
123: //SELECT VERSION,GRAB_SIZE,MAX_KEY,FIELDNAME,TABLENAME FROM OJB_HL_SEQ WHERE TABLENAME = 'SEQ_PERSON' AND FIELDNAME = 'ID'
124: //UPDATE OJB_HL_SEQ SET MAX_KEY='150',GRAB_SIZE='20',VERSION='7' WHERE TABLENAME = 'SEQ_PERSON' AND FIELDNAME = 'ID' AND VERSION = '6'
125: // commit|
126: //SELECT LASTNAME,FIRSTNAME,ID FROM PERSON WHERE ID = '131'
127: //INSERT INTO PERSON (ID,FIRSTNAME,LASTNAME) VALUES ('131','','')
128: logStmtCount("Storing first person"); // 6. oma: why so many? double lookup in OJB_HL_SEQ !
129: resetStmtCount();
130: pers = new Person();
131: myPB.store(pers, ObjectModification.INSERT);
132: myPB.commitTransaction();
133: // INSERT INTO PERSON (ID,FIRSTNAME,LASTNAME) VALUES ('172','','')
134: // commit
135: assertStmtCount("insert second Person with empty collection.",
136: 2);
137: }
138:
139: /**
140: * insert a person with a project collection with one fresh element.
141: * note: the <em>first</em> Person and Project require extra lookups
142: * in the table OJB_HL_SEQ.
143: */
144: public void testPersonSingleProjectInsert() {
145: resetStmtCount();
146: myPB.clearCache();
147: myPB.beginTransaction();
148: Person pers = new Person();
149: pers.setFirstname("testPersonSingleProjectInsert(1)");
150: Project proj = new Project();
151: proj.setTitle("testPersonSingleProjectInsert(1)");
152: myPB.store(pers);
153: myPB.store(proj);
154: logStmtCount("Storing first person and first project");
155: // 12. oma: why so many? double lookup in OJB_HL_SEQ !
156: resetStmtCount();
157: pers = new Person();
158: Project proj2 = new Project();
159: proj2.setTitle("proj2");
160: Collection projects = Arrays.asList(new Project[] { proj2 });
161: pers.setProjects(projects);
162: myPB.store(pers, ObjectModification.INSERT);
163: myPB.commitTransaction();
164: // INSERT INTO PERSON (ID,FIRSTNAME,LASTNAME) VALUES ('292','','')
165: // SELECT TITLE,DESCRIPTION,ID FROM PROJECT WHERE ID = '88'
166: // INSERT INTO PROJECT (ID,TITLE,DESCRIPTION) VALUES ('88','proj2','')
167: // SELECT PROJECT_ID FROM PERSON_PROJECT WHERE PERSON_ID='292' // BRJ: check mn-implementor
168: // INSERT INTO PERSON_PROJECT (PERSON_ID,PROJECT_ID) VALUES ('292','88')
169: // commit|
170: assertStmtCount("insert second Person, singleton collection.",
171: 6);
172: }
173:
174: public void testPrefetched() {
175: ClassDescriptor cldProductGroup = myPB
176: .getClassDescriptor(ProductGroup.class);
177: ClassDescriptor cldArticle = myPB
178: .getClassDescriptor(Article.class);
179: Class productGroupProxy = cldProductGroup.getProxyClass();
180: Class articleProxy = cldArticle.getProxyClass();
181:
182: //
183: // use ProductGroup and Articles with disabled Proxy
184: //
185: cldProductGroup.setProxyClass(null);
186: cldProductGroup.setProxyClassName(null);
187: cldArticle.setProxyClass(null);
188: cldArticle.setProxyClassName(null);
189:
190: resetStmtCount();
191: myPB.clearCache();
192:
193: myPB.beginTransaction();
194:
195: Criteria crit = new Criteria();
196: crit.addLessOrEqualThan("groupId", new Integer(5));
197: QueryByCriteria q = QueryFactory.newQuery(ProductGroup.class,
198: crit);
199: q.addOrderByDescending("groupId");
200: q.addPrefetchedRelationship("allArticlesInGroup");
201:
202: Collection results = myPB.getCollectionByQuery(q);
203: assertEquals("Number of ProductGroups", 5, results.size());
204: Collection articles = new ArrayList();
205: for (Iterator it = results.iterator(); it.hasNext();) {
206: ProductGroup p = (ProductGroup) it.next();
207: articles.addAll(p.getAllArticles());
208: }
209: assertEquals("Total number of Articles", 59, articles.size());
210:
211: //SELECT A0.KategorieName,A0.Kategorie_Nr,A0.Beschreibung FROM Kategorien A0 WHERE A0.Kategorie_Nr <= '5'
212: //SELECT ... FROM CDS A0 WHERE A0.Kategorie_Nr IN ( '1' , '4' , '2' , '5' , '3' )
213: //SELECT ... FROM Artikel A0 WHERE A0.Kategorie_Nr IN ( '1' , '4' , '2' , '5' , '3' )
214: //SELECT ... FROM BOOKS A0 WHERE A0.Kategorie_Nr IN ( '1' , '4' , '2' , '5' , '3' )
215: assertStmtCount("Read Prefetched.", 4);
216: myPB.commitTransaction();
217:
218: //
219: // Reset original Proxy settings
220: //
221: cldProductGroup.setProxyClass(productGroupProxy);
222: cldProductGroup.setProxyClassName(productGroupProxy.getName());
223: cldArticle.setProxyClass(articleProxy);
224: cldArticle.setProxyClassName(articleProxy.getName());
225:
226: }
227:
228: public void testNonPrefetched() {
229: ClassDescriptor cldProductGroup = myPB
230: .getClassDescriptor(ProductGroup.class);
231: ClassDescriptor cldArticle = myPB
232: .getClassDescriptor(Article.class);
233: Class productGroupProxy = cldProductGroup.getProxyClass();
234: Class articleProxy = cldArticle.getProxyClass();
235:
236: //
237: // use ProductGroup and Articles with disabled Proxy
238: //
239: cldProductGroup.setProxyClass(null);
240: cldProductGroup.setProxyClassName(null);
241: cldArticle.setProxyClass(null);
242: cldArticle.setProxyClassName(null);
243:
244: resetStmtCount();
245: myPB.clearCache();
246:
247: myPB.beginTransaction();
248:
249: Criteria crit = new Criteria();
250: crit.addLessOrEqualThan("groupId", new Integer(5));
251: QueryByCriteria q = QueryFactory.newQuery(ProductGroup.class,
252: crit);
253: q.addOrderByDescending("groupId");
254:
255: Collection results = myPB.getCollectionByQuery(q);
256: assertEquals("Number of ProductGroups", 5, results.size());
257: Collection articles = new ArrayList();
258: for (Iterator it = results.iterator(); it.hasNext();) {
259: ProductGroup p = (ProductGroup) it.next();
260: articles.addAll(p.getAllArticles());
261: }
262: assertEquals("Total number of Articles", 59, articles.size());
263:
264: //SELECT A0.KategorieName,A0.Kategorie_Nr,A0.Beschreibung FROM Kategorien A0 WHERE A0.Kategorie_Nr <= '5'
265: //SELECT ... FROM CDS A0 WHERE A0.Kategorie_Nr = '5'
266: //SELECT ... FROM Artikel A0 WHERE A0.Kategorie_Nr = '5'
267: //SELECT ... FROM BOOKS A0 WHERE A0.Kategorie_Nr = '5'
268: //SELECT ... FROM CDS A0 WHERE A0.Kategorie_Nr = '4'
269: //SELECT ... FROM Artikel A0 WHERE A0.Kategorie_Nr = '4'
270: //SELECT ... FROM BOOKS A0 WHERE A0.Kategorie_Nr = '4'
271: //...
272: //SELECT ... FROM CDS A0 WHERE A0.Kategorie_Nr = '1'
273: //SELECT ... FROM Artikel A0 WHERE A0.Kategorie_Nr = '1'
274: //SELECT ... FROM BOOKS A0 WHERE A0.Kategorie_Nr = '1'
275: assertStmtCount("Read Non-Prefetched.", 16);
276: myPB.commitTransaction();
277:
278: //
279: // Reset original Proxy settings
280: //
281: cldProductGroup.setProxyClass(productGroupProxy);
282: cldProductGroup.setProxyClassName(productGroupProxy.getName());
283: cldArticle.setProxyClass(articleProxy);
284: cldArticle.setProxyClassName(articleProxy.getName());
285:
286: }
287:
288: }
|