KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
31 import org.objectweb.jalisto.se.test.workbench.JalistoTestSuite;
32 import org.objectweb.jalisto.se.test.workbench.JalistoTimer;
33
34 import java.util.ArrayList JavaDoc;
35
36 public class CollectionQueryTestCase extends JalistoQueryTestCase {
37
38     public CollectionQueryTestCase() {
39     }
40
41     public CollectionQueryTestCase(String JavaDoc name) {
42         super(name);
43     }
44
45     public static Test suite() {
46         JalistoTestSuite suite = new JalistoTestSuite();
47         CollectionQueryTestCase tc = (CollectionQueryTestCase) newTestCase(suite, new CollectionQueryTestCase());
48
49         tc.prepare();
50
51         tc.executeQuery(0, "Without index", numberOfQueryExecute);
52         tc.executeQuery(1, "Without index", numberOfQueryExecute);
53         tc.executeQuery(2, "Without index", numberOfQueryExecute);
54
55         tc.cleanUp(Library.class);
56
57         tc.finishTests();
58
59         return suite;
60     }
61
62     /**
63      * **************************************************************************************
64      */

65
66     public void prepare() {
67         initSession();
68         define(BookWithAuthor.getMetaDescription());
69         define(BookWithFormat.getMetaDescription());
70         define(Author.getMetaDescription());
71         define(Library.getMetaDescription());
72         define(Client.getMetaDescription());
73
74         BookWithFormat.counter = -1;
75         Library.counter = -1;
76         Client.counter = -1;
77         Author.counter = -1;
78         Format.counter = -1;
79
80         cleanUp(Library.class);
81         cleanUp(Client.class);
82         cleanUp(Author.class);
83         cleanUp(BookWithFormat.class);
84         populateTestLibrary(10, 20, 10);
85     }
86
87     public void executeQuery(int q, String JavaDoc message, int nbr) {
88         String JavaDoc timerName = String.valueOf(nbr) + " executions " + message;
89
90         Query query;
91         if (q == 0) {
92             query = getQuery();
93         } else if (q == 1) {
94             query = getQueryWithSubQueryOnCollection();
95         } else {
96             query = getQueryWithSubQueryOnLink();
97         }
98
99         int resultSize = -1;
100
101         JalistoTimer.timerStart(timerName);
102         for (int i = 0; i < nbr; i++) {
103             tx.begin();
104             resultSize = query.execute().size();
105             tx.commit();
106         }
107         JalistoTimer.timerStop(timerName, false);
108         if (q == 0) {
109             assertEquals("should be equal", 8, resultSize);
110         } else if (q == 1) {
111             assertEquals("should be equal", 3, resultSize);
112         } else {
113             assertEquals("should be equal", 4, resultSize);
114         }
115
116
117         JalistoTimer.summary(timerName);
118         JalistoTimer.clean(timerName);
119     }
120
121     private Query getQuery() {
122         String JavaDoc clientLastName = Client.lastNames[0];
123
124         Query query = queryManager.query();
125         query.constrain(Library.class);
126
127         Constraint c1 = query.descend("books").constrain(bookToRetrieve).contains();
128         Constraint c2 = query.descend("clients").descend("lastName").constrain(clientLastName).equal();
129         c1.or(c2);
130
131         return query;
132     }
133
134     private Query getQueryWithSubQueryOnCollection() {
135         String JavaDoc clientLastName = Client.lastNames[0];
136         String JavaDoc clientFirstName = Client.firstNames[0];
137
138         Query query = queryManager.query();
139         query.constrain(Library.class);
140
141         Query clientQuery = queryManager.query();
142         clientQuery.constrain(Client.class);
143
144         Constraint c1 = query.descend("books").constrain(bookToRetrieve).contains();
145         Constraint c2 = query.descend("clients").constrain(clientQuery);
146         c1.or(c2);
147
148         Constraint c3 = clientQuery.descend("lastName").constrain(clientLastName).equal();
149         Constraint c4 = clientQuery.descend("firstName").constrain(clientFirstName).equal();
150         c3.and(c4);
151
152         return query;
153     }
154
155     private Query getQueryWithSubQueryOnLink() {
156         String JavaDoc authorLastName = Author.lastNames[0];
157         String JavaDoc authorFirstName = Author.firstNames[0];
158
159         Query query = queryManager.query();
160         query.constrain(Library.class);
161
162         Query authorQuery = queryManager.query();
163         authorQuery.constrain(Author.class);
164
165         Constraint c1 = query.descend("books").constrain(bookToRetrieve).contains();
166         Constraint c2 = query.descend("authorMostWanted").constrain(authorQuery);
167         c1.or(c2);
168
169         Constraint c3 = authorQuery.descend("lastName").constrain(authorLastName).equal();
170         Constraint c4 = authorQuery.descend("firstName").constrain(authorFirstName).equal();
171         c3.and(c4);
172
173         return query;
174     }
175
176     public void populateTestLibrary(int nbrLibrary, int nbrBook, int nbrClient) {
177         tx.begin();
178         for (int i = 0; i < nbrLibrary; i++) {
179             Library library = Library.newLibrary();
180             ArrayList JavaDoc books = library.getBooks();
181             ArrayList JavaDoc clients = library.getClients();
182             for (int j = 0; j < nbrBook; j++) {
183                 BookWithFormat book = BookWithFormat.newBook();
184                 books.add(book);
185                 if ((i == (nbrLibrary / 2)) && (j == (nbrBook / 2))) {
186                     bookToRetrieve = book;
187                 }
188             }
189             for (int j = 0; j < nbrClient; j++) {
190                 Client client = Client.newClient();
191                 Object JavaDoc clientOid = session.createObject(client.toArray(), Client.class);
192                 clients.add(clientOid);
193             }
194             Author a = Author.newAuthor();
195             Object JavaDoc[] author = a.toArray();
196             library.setAuthorMostWanted(session.createObject(author, Author.class));
197             session.createObject(library.toArray(), Library.class);
198         }
199         tx.commit();
200     }
201
202
203     private BookWithFormat bookToRetrieve = null;
204
205     /**
206      * ************************* need to overwrite for suite() method ********************************************
207      */

208
209     public void initSession() {
210         super.initSession(false);
211     }
212
213     public void define(ClassDescription classDescription) {
214         super.define(classDescription);
215     }
216
217     public void cleanUp(Class JavaDoc aClass) {
218         super.cleanUp(aClass);
219     }
220
221     public void finishTests() {
222         super.finishTests();
223     }
224 }
225
Popular Tags