KickJava   Java API By Example, From Geeks To Geeks.

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


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.Constraint;
29 import org.objectweb.jalisto.se.api.query.Query;
30 import org.objectweb.jalisto.se.query.parameter.QueryParameter;
31 import org.objectweb.jalisto.se.query.result.ObjectSetImpl;
32 import org.objectweb.jalisto.se.test.data.*;
33 import org.objectweb.jalisto.se.test.workbench.JalistoTestSuite;
34 import org.objectweb.jalisto.se.test.workbench.JalistoTimer;
35
36 public class LinkQueryTestCase extends JalistoQueryTestCase {
37
38     public LinkQueryTestCase() {
39     }
40
41     public LinkQueryTestCase(String JavaDoc name) {
42         super(name);
43     }
44
45     public static Test suite() {
46         JalistoTestSuite suite = new JalistoTestSuite();
47         LinkQueryTestCase tc = (LinkQueryTestCase) newTestCase(suite, new LinkQueryTestCase());
48
49         tc.prepare();
50
51         tc.testQueryWithLink("without index and parameters", false, numberOfQueryExecute);
52         tc.testQueryWithLink("without index, with parameters", true, numberOfQueryExecute);
53
54         tc.buildIndex(BookWithFormat.class, "title");
55
56         tc.testQueryWithLink("with index, without parameters", false, numberOfQueryExecute);
57         tc.testQueryWithLink("with index, with parameters", true, numberOfQueryExecute);
58
59         tc.deleteIndex(BookWithFormat.class, "title");
60
61         tc.finishTests();
62
63         return suite;
64     }
65
66     /**
67      * ************************* need to overwrite for suite() method ********************************************
68      */

69
70     public void prepare() {
71         initSession();
72         define(BookWithAuthor.getMetaDescription());
73         define(BookWithFormat.getMetaDescription());
74         define(Author.getMetaDescription());
75
76         resetFloidValue();
77         BookWithFormat.counter = -1;
78         Author.counter = -1;
79         Format.counter = -1;
80
81         deleteIndex(BookWithFormat.class, "title");
82         cleanUp(BookWithFormat.class);
83         cleanUp(Author.class);
84
85         populateBookWithFormat(1000);
86     }
87
88     public void testQueryWithLink(String JavaDoc message, boolean withParameters, int nbr) {
89         String JavaDoc timerName = String.valueOf(nbr) + " execution " + message;
90
91         Query query;
92         if (withParameters) {
93             query = getQueryWithParameters();
94         } else {
95             query = getQueryWithoutParameters();
96         }
97
98         int resultSize = -1;
99         ObjectSetImpl resultSet = null;
100
101         JalistoTimer.timerStart(timerName);
102         for (int i = 0; i < nbr; i++) {
103             tx.begin();
104             resultSet = (ObjectSetImpl) query.execute();
105             resultSize = resultSet.size();
106             tx.commit();
107         }
108         JalistoTimer.timerStop(timerName, false);
109
110         assertEquals("shoulb be equal", 8, resultSize);
111         resultSet.sort();
112         tx.begin();
113         String JavaDoc r1 = "((1.0,[Death Off Stage,0,5,[James,McManus],18x13],(1.140,[Death Off Stage,140,15,[James,McManus],18x13],(1.280,[Death Off Stage,280,25,[James,McManus],18x13],(1.420,[Death Off Stage,420,5,[James,McManus],18x13],(1.560,[Death Off Stage,560,15,[James,McManus],18x13],(1.700,[Death Off Stage,700,25,[James,McManus],18x13],(1.840,[Death Off Stage,840,5,[James,McManus],18x13],(1.980,[Death Off Stage,980,15,[James,McManus],18x13])";
114         assertEquals("should be equal", r1, resultSet.toStringFull(session));
115         tx.commit();
116
117         JalistoTimer.summary(timerName);
118         JalistoTimer.clean(timerName);
119     }
120
121     private Query getQueryWithoutParameters() {
122         String JavaDoc title1 = BookWithFormat.titles[0];
123         String JavaDoc authorLastName = Author.lastNames[0];
124         Format format1 = Format.availableFormat[0];
125
126         Query query = queryManager.query();
127         query.constrain(BookWithFormat.class);
128         Constraint c1 = query.descend("title").constrain(title1).equal();
129         Constraint c2 = query.descend("author").descend("lastName").constrain(authorLastName).equal();
130         Constraint c3 = query.descend("format").constrain(format1).equal();
131         c1.and(c2).and(c3);
132
133         return query;
134     }
135
136     private Query getQueryWithParameters() {
137         String JavaDoc title1 = BookWithFormat.titles[0];
138         String JavaDoc authorLastName = Author.lastNames[0];
139         Format format1 = Format.availableFormat[0];
140
141         QueryParameter titleParameter = new QueryParameter("title");
142         QueryParameter lastNameParameter = new QueryParameter("lastName");
143
144         Query query = queryManager.query();
145         query.constrain(BookWithFormat.class);
146         Constraint c1 = query.descend("title").constrain(titleParameter).equal();
147         Constraint c2 = query.descend("author").descend("lastName").constrain(lastNameParameter).equal();
148         Constraint c3 = query.descend("format").constrain(format1).equal();
149         c1.and(c2).and(c3);
150
151         query.bind("title", title1);
152         query.bind("lastName", authorLastName);
153
154         return query;
155     }
156
157     public void resetFloidValue() {
158         tx.begin();
159         session.getInternalSession().getOidTable().getIdentityProvider().resetFloidCounters();
160         tx.commit();
161     }
162
163
164     /**
165      * ********************************************************************************
166      */

167
168     public void initSession() {
169         super.initSession(false);
170     }
171
172     public void define(ClassDescription classDescription) {
173         super.define(classDescription);
174     }
175
176     public void cleanUp(Class JavaDoc aClass) {
177         super.cleanUp(aClass);
178     }
179
180     public void finishTests() {
181         super.finishTests();
182     }
183
184     public void populateBookWithFormat(int nbr) {
185         tx.begin();
186         for (int i = 0; i < nbr; i++) {
187             Object JavaDoc authorOid = session.createObject(Author.newAuthor().toArray(), Author.class);
188             Object JavaDoc[] book = BookWithFormat.newBook().toArray();
189             book[3] = authorOid;
190             session.createObject(book, BookWithFormat.class);
191         }
192         tx.commit();
193     }
194
195     public void buildIndex(Class JavaDoc aClass, String JavaDoc fieldName) {
196         super.buildIndex(aClass, fieldName);
197     }
198
199     public void deleteIndex(Class JavaDoc aClass, String JavaDoc fieldName) {
200         super.deleteIndex(aClass, fieldName);
201     }
202
203     public void printIndex(Class JavaDoc aClass, String JavaDoc fieldName) {
204         super.printIndex(aClass, fieldName);
205     }
206 }
207
Popular Tags