KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > sqlcount > CollectionCountTest


1 /*
2  * (C) 2003 ppi Media
3  * User: om
4  */

5
6 package org.apache.ojb.broker.sqlcount;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.Arrays JavaDoc;
10 import java.util.Collection JavaDoc;
11 import java.util.Iterator JavaDoc;
12 import java.util.List JavaDoc;
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 /**
31  * @author <a HREF="mailto:om@ppi.de">Oliver Matz</a>
32  * @version $Id: CollectionCountTest.java,v 1.9.2.1 2005/10/10 00:35:43 arminw Exp $
33  */

34 public class CollectionCountTest extends AbstractCountTest
35 {
36   protected PersistenceBroker myPB;
37
38   protected void setUp() throws Exception JavaDoc
39   {
40     super.setUp();
41     myPB = PersistenceBrokerFactory.defaultPersistenceBroker();
42   }
43
44   protected void tearDown() throws Exception JavaDoc
45   {
46     if ((myPB != null) && !myPB.isClosed())
47     {
48       myPB.close();
49     }
50     super.tearDown();
51   }
52
53   /**
54    * retrieve all product groups.
55    */

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 JavaDoc 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 JavaDoc articles = next.getAllArticles();
71       // SELECT ... FROM Kategorien
72
// SELECT ... FROM Artikel
73
// SELECT ... FROM BOOKS
74
// SELECT ... FROM CDS
75
assertStmtCount("getAllArticles", 4);
76     }
77     myPB.commitTransaction();
78   }
79
80   /**
81    * retrieve Product group number 5 and its 12 articles
82    */

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 JavaDoc[] {new Integer JavaDoc(5)}));
90     assertStmtCount("getObjectByIdentity", 4);
91     resetStmtCount();
92     List JavaDoc articles = pg.getAllArticles();
93     assertEquals(12, articles.size());
94     assertStmtCount("getAllArticles", 0);
95     resetStmtCount();
96     for (Iterator JavaDoc articleIterator = articles.iterator(); articleIterator.hasNext(); )
97     {
98       InterfaceArticle article = (InterfaceArticle)articleIterator.next();
99       logger.info("articleId " + article.getArticleId());
100       // SELECT ... FROM Artikel WHERE Artikel_Nr = ...
101
}
102     assertStmtCount("collect ids: ", 1); // batch retrieval!
103
resetStmtCount();
104     String JavaDoc str = pg.toString();
105     // SELECT ... FROM Kategorien WHERE Kategorie_Nr = '5'
106
// SELECT ... FROM Artikel A0 WHERE Kategorie_Nr = '5'
107
// SELECT ... FROM BOOKS A0 WHERE Kategorie_Nr = '5'
108
// SELECT ... FROM CDS A0 WHERE Kategorie_Nr = '5'
109
assertStmtCount("toString", 4);
110     logger.info(str);
111     myPB.commitTransaction();
112   }
113
114   /*
115   * insert a person with an empty project collection.
116   * note: the <em>first</em> Person and Project require extra lookups
117   * in the table OJB_HL_SEQ.
118   */

119   public void testPersonEmptyProjectsInsert()
120   {
121     resetStmtCount();
122     myPB.clearCache();
123     myPB.beginTransaction();
124     Person pers = new Person();
125     myPB.store(pers);
126     //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'
127
//SELECT VERSION,GRAB_SIZE,MAX_KEY,FIELDNAME,TABLENAME FROM OJB_HL_SEQ WHERE TABLENAME = 'SEQ_PERSON' AND FIELDNAME = 'ID'
128
//UPDATE OJB_HL_SEQ SET MAX_KEY='150',GRAB_SIZE='20',VERSION='7' WHERE TABLENAME = 'SEQ_PERSON' AND FIELDNAME = 'ID' AND VERSION = '6'
129
// commit|
130
//SELECT LASTNAME,FIRSTNAME,ID FROM PERSON WHERE ID = '131'
131
//INSERT INTO PERSON (ID,FIRSTNAME,LASTNAME) VALUES ('131','','')
132
logStmtCount("Storing first person"); // 6. oma: why so many? double lookup in OJB_HL_SEQ !
133
resetStmtCount();
134     pers = new Person();
135     myPB.store(pers, ObjectModification.INSERT);
136     myPB.commitTransaction();
137     // INSERT INTO PERSON (ID,FIRSTNAME,LASTNAME) VALUES ('172','','')
138
// commit
139
assertStmtCount("insert second Person with empty collection.", 2);
140   }
141
142   /**
143    * insert a person with a project collection with one fresh element.
144    * note: the <em>first</em> Person and Project require extra lookups
145    * in the table OJB_HL_SEQ.
146    */

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     // 12. oma: why so many? double lookup in OJB_HL_SEQ !
160
resetStmtCount();
161     pers = new Person();
162     Project proj2 = new Project();
163     proj2.setTitle("proj2");
164     Collection JavaDoc projects = Arrays.asList(new Project[] {proj2});
165     pers.setProjects(projects);
166     myPB.store(pers, ObjectModification.INSERT);
167     myPB.commitTransaction();
168     // INSERT INTO PERSON (ID,FIRSTNAME,LASTNAME) VALUES ('292','','')
169
// SELECT TITLE,DESCRIPTION,ID FROM PROJECT WHERE ID = '88'
170
// INSERT INTO PROJECT (ID,TITLE,DESCRIPTION) VALUES ('88','proj2','')
171
// SELECT PROJECT_ID FROM PERSON_PROJECT WHERE PERSON_ID='292' // BRJ: check mn-implementor
172
// INSERT INTO PERSON_PROJECT (PERSON_ID,PROJECT_ID) VALUES ('292','88')
173
// commit|
174
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 JavaDoc productGroupProxy = cldProductGroup.getProxyClass();
183       Class JavaDoc articleProxy = cldArticle.getProxyClass();
184
185       //
186
// use ProductGroup and Articles with disabled Proxy
187
//
188
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 JavaDoc(5));
201       QueryByCriteria q = QueryFactory.newQuery(ProductGroup.class, crit);
202       q.addOrderByDescending("groupId");
203       q.addPrefetchedRelationship("allArticlesInGroup");
204
205       Collection JavaDoc results = myPB.getCollectionByQuery(q);
206       assertEquals("Number of ProductGroups", 5, results.size());
207       Collection JavaDoc articles = new ArrayList JavaDoc();
208       for (Iterator JavaDoc 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       //SELECT A0.KategorieName,A0.Kategorie_Nr,A0.Beschreibung FROM Kategorien A0 WHERE A0.Kategorie_Nr <= '5'
216
//SELECT ... FROM CDS A0 WHERE A0.Kategorie_Nr IN ( '1' , '4' , '2' , '5' , '3' )
217
//SELECT ... FROM Artikel A0 WHERE A0.Kategorie_Nr IN ( '1' , '4' , '2' , '5' , '3' )
218
//SELECT ... FROM BOOKS A0 WHERE A0.Kategorie_Nr IN ( '1' , '4' , '2' , '5' , '3' )
219
assertStmtCount("Read Prefetched.", 4);
220       myPB.commitTransaction();
221
222       //
223
// Reset original Proxy settings
224
//
225
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 JavaDoc productGroupProxy = cldProductGroup.getProxyClass();
237       Class JavaDoc articleProxy = cldArticle.getProxyClass();
238
239       //
240
// use ProductGroup and Articles with disabled Proxy
241
//
242
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 JavaDoc(5));
254       QueryByCriteria q = QueryFactory.newQuery(ProductGroup.class, crit);
255       q.addOrderByDescending("groupId");
256
257       Collection JavaDoc results = myPB.getCollectionByQuery(q);
258       assertEquals("Number of ProductGroups", 5, results.size());
259       Collection JavaDoc articles = new ArrayList JavaDoc();
260       for (Iterator JavaDoc 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       //SELECT A0.KategorieName,A0.Kategorie_Nr,A0.Beschreibung FROM Kategorien A0 WHERE A0.Kategorie_Nr <= '5'
268
//SELECT ... FROM CDS A0 WHERE A0.Kategorie_Nr = '5'
269
//SELECT ... FROM Artikel A0 WHERE A0.Kategorie_Nr = '5'
270
//SELECT ... FROM BOOKS A0 WHERE A0.Kategorie_Nr = '5'
271
//SELECT ... FROM CDS A0 WHERE A0.Kategorie_Nr = '4'
272
//SELECT ... FROM Artikel A0 WHERE A0.Kategorie_Nr = '4'
273
//SELECT ... FROM BOOKS A0 WHERE A0.Kategorie_Nr = '4'
274
//...
275
//SELECT ... FROM CDS A0 WHERE A0.Kategorie_Nr = '1'
276
//SELECT ... FROM Artikel A0 WHERE A0.Kategorie_Nr = '1'
277
//SELECT ... FROM BOOKS A0 WHERE A0.Kategorie_Nr = '1'
278
assertStmtCount("Read Non-Prefetched.", 16);
279       myPB.commitTransaction();
280
281       //
282
// Reset original Proxy settings
283
//
284
cldProductGroup.setProxyClass(productGroupProxy);
285       cldProductGroup.setProxyClassName(productGroupProxy.getName());
286       cldArticle.setProxyClass(articleProxy);
287       cldArticle.setProxyClassName(articleProxy.getName());
288
289   }
290
291 }
292
Popular Tags