KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > test > query > QueryTestCase


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.test.query;
25
26 import junit.framework.Test;
27 import org.objectweb.jalisto.se.api.ClassDescription;
28 import org.objectweb.jalisto.se.api.query.Query;
29 import org.objectweb.jalisto.se.api.query.Constraint;
30 import org.objectweb.jalisto.se.test.data.BookWithAuthor;
31 import org.objectweb.jalisto.se.test.workbench.JalistoTestSuite;
32 import org.objectweb.jalisto.se.test.workbench.JalistoTimer;
33
34 public class QueryTestCase extends JalistoQueryTestCase {
35
36     public QueryTestCase() {
37     }
38
39     public QueryTestCase(String JavaDoc name) {
40         super(name);
41     }
42
43     public static Test suite() {
44         JalistoTestSuite suite = new JalistoTestSuite();
45         QueryTestCase tc = (QueryTestCase) newTestCase(suite, new QueryTestCase());
46
47         tc.prepare();
48
49         tc.executeQueryTests("Without index", numberOfQueryExecute);
50
51         tc.buildIndex(BookWithAuthor.class, "title");
52         tc.buildIndex(BookWithAuthor.class, "price");
53
54         tc.executeQueryTests("With index", numberOfQueryExecute);
55
56         tc.deleteIndex(BookWithAuthor.class, "title");
57         tc.deleteIndex(BookWithAuthor.class, "price");
58
59         tc.finishTests();
60
61         return suite;
62     }
63
64     /**
65      * **************************************************************************************
66      */

67
68     public void prepare() {
69         initSession();
70         BookWithAuthor.counter = -1;
71         define(BookWithAuthor.getMetaDescription());
72         deleteIndex(BookWithAuthor.class, "title");
73         deleteIndex(BookWithAuthor.class, "price");
74         cleanUp(BookWithAuthor.class);
75         populateBookWithAuthor(1000);
76     }
77
78     public void executeQueryTests(String JavaDoc message, int nbr) {
79         executeQuery(getQuery(), message, nbr, 16);
80         executeQuery(getQueryWithOrBranch(), message + " or branch", nbr, 700);
81
82 // evolutionExecutionTime(getQuery(), message, 10, nbr);
83
}
84
85     public void executeQuery(Query query, String JavaDoc message, int nbr, int expected) {
86         String JavaDoc timerName = String.valueOf(nbr) + " executions " + message;
87
88         int resultSize = -1;
89
90         JalistoTimer.timerStart(timerName);
91         for (int i = 0; i < nbr; i++) {
92             tx.begin();
93             resultSize = query.execute().size();
94             tx.commit();
95         }
96         JalistoTimer.timerStop(timerName, false);
97         assertEquals("Should be equals", expected, resultSize);
98
99         JalistoTimer.summary(timerName);
100         JalistoTimer.clean(timerName);
101     }
102
103     public void evolutionExecutionTime(Query query, String JavaDoc message, int nbrExec, int nbrTest) {
104         String JavaDoc timerName = "Evolution of the " + nbrExec + " first execution time (average on " + nbrTest + " tests) - " + message;
105
106         for (int j = 0; j < nbrTest; j++) {
107 // Query query = getQuery();
108
for (int i = 0; i < nbrExec; i++) {
109                 JalistoTimer.timerStart(timerName + " - " + i);
110                 tx.begin();
111                 query.execute();
112                 tx.commit();
113                 JalistoTimer.timerStop(timerName + " - " + i, false);
114             }
115         }
116
117         JalistoTimer.summary();
118         JalistoTimer.deleteAllTimers();
119     }
120
121     private Query getQuery() {
122         String JavaDoc title1 = BookWithAuthor.titles[0];
123         String JavaDoc title2 = BookWithAuthor.titles[1];
124         String JavaDoc author1 = BookWithAuthor.authors[0];
125         String JavaDoc author2 = BookWithAuthor.authors[1];
126         Integer JavaDoc page1 = new Integer JavaDoc(500);
127         Integer JavaDoc price1 = new Integer JavaDoc(9);
128         Integer JavaDoc price2 = new Integer JavaDoc(26);
129
130
131         Query query = queryManager.query();
132         query.constrain(BookWithAuthor.class);
133         Constraint c1 = query.descend("title").constrain(title1).equal();
134         Constraint c2 = query.descend("title").constrain(title2).equal();
135         Constraint c3 = query.descend("author").constrain(author1).equal();
136         Constraint c4 = query.descend("author").constrain(author2).equal();
137         Constraint c5 = query.descend("price").constrain(price1).smaller();
138         Constraint c6 = query.descend("price").constrain(price2).greater();
139         Constraint c7 = query.descend("pages").constrain(page1).greater();
140         ((c1.and(c3)).or(c2.and(c4)))
141                 .and(
142                         (c5.or(c6)).and(c7));
143
144         return query;
145     }
146
147     private Query getQueryWithOrBranch() {
148         String JavaDoc title1 = BookWithAuthor.titles[0];
149         String JavaDoc title2 = BookWithAuthor.titles[1];
150         String JavaDoc author1 = BookWithAuthor.authors[0];
151         String JavaDoc author2 = BookWithAuthor.authors[1];
152         Integer JavaDoc page1 = new Integer JavaDoc(500);
153         Integer JavaDoc price1 = new Integer JavaDoc(9);
154         Integer JavaDoc price2 = new Integer JavaDoc(26);
155
156
157         Query query = queryManager.query();
158         query.constrain(BookWithAuthor.class);
159         Constraint c1 = query.descend("title").constrain(title1).equal();
160         Constraint c2 = query.descend("title").constrain(title2).equal();
161         Constraint c3 = query.descend("author").constrain(author1).equal();
162         Constraint c4 = query.descend("author").constrain(author2).equal();
163         Constraint c5 = query.descend("price").constrain(price1).smaller();
164         Constraint c6 = query.descend("price").constrain(price2).greater();
165         Constraint c7 = query.descend("pages").constrain(page1).greater();
166         ((c1.and(c3)).or(c2.and(c4)))
167                 .or(
168                         (c5.or(c6)).or(c7));
169
170         return query;
171     }
172
173
174     /**
175      * ************************* need to overwrite for suite() method ********************************************
176      */

177
178     public void initSession() {
179         super.initSession(false);
180     }
181
182     public void define(ClassDescription classDescription) {
183         super.define(classDescription);
184     }
185
186     public void cleanUp(Class JavaDoc aClass) {
187         super.cleanUp(aClass);
188     }
189
190     public void populateBookWithAuthor(int nbr) {
191         super.populateBookWithAuthor(nbr);
192     }
193
194     public void buildIndex(Class JavaDoc aClass, String JavaDoc fieldName) {
195         super.buildIndex(aClass, fieldName);
196     }
197
198     public void deleteIndex(Class JavaDoc aClass, String JavaDoc fieldName) {
199         super.deleteIndex(aClass, fieldName);
200     }
201
202     public void printIndex(Class JavaDoc aClass, String JavaDoc fieldName) {
203         super.printIndex(aClass, fieldName);
204     }
205
206     public void finishTests() {
207         super.finishTests();
208     }
209 }
210
Popular Tags