KickJava   Java API By Example, From Geeks To Geeks.

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


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.exception.JalistoException;
31 import org.objectweb.jalisto.se.test.data.*;
32 import org.objectweb.jalisto.se.test.workbench.JalistoTestSuite;
33 import org.objectweb.jalisto.se.test.workbench.JalistoTimer;
34
35 import java.util.ArrayList JavaDoc;
36
37 public class AdvancedQueryTestCase extends JalistoQueryTestCase {
38     public AdvancedQueryTestCase() {
39     }
40
41     public AdvancedQueryTestCase(String JavaDoc name) {
42         super(name);
43     }
44
45     public static Test suite() {
46         JalistoTestSuite suite = new JalistoTestSuite();
47         AdvancedQueryTestCase tc = (AdvancedQueryTestCase) newTestCase(suite, new AdvancedQueryTestCase());
48
49         tc.prepare();
50
51         tc.defineInvalidIndex();
52
53         tc.complexQuery("Without index", 50);
54
55         tc.buildIndex(Author.class, "lastName");
56
57         tc.complexQuery("With index on lastname of Author", 50);
58
59         tc.deleteIndex(Author.class, "lastName");
60
61         tc.finishTests();
62
63         return suite;
64     }
65
66     /**
67      * **************************************************************************************
68      */

69
70     public void prepare() {
71         initSession();
72         define(BookWithAuthor.getMetaDescription());
73         define(BookWithFormat.getMetaDescription());
74         define(Author.getMetaDescription());
75         define(Library.getMetaDescription());
76         define(Client.getMetaDescription());
77
78         BookWithFormat.counter = -1;
79         Library.counter = -1;
80         Client.counter = -1;
81         Author.counter = -1;
82
83         cleanUp(Library.class);
84         cleanUp(Client.class);
85         cleanUp(Author.class);
86         cleanUp(BookWithFormat.class);
87         populateTestLibrary(25, 2, 10);
88     }
89
90     public void defineInvalidIndex() {
91         try {
92             ClassDescription meta = session.getMetaRepository().getMetaData(Library.class.getName());
93             int fieldIndex = meta.getIndex("books");
94             tx.begin();
95             indexManager.buildIndexOnField(meta, fieldIndex);
96             assertTrue("must raise error", false);
97         } catch (JalistoException jalistoExc) {
98         } finally {
99             tx.commit();
100         }
101     }
102
103     public void complexQuery(String JavaDoc message, int nbr) {
104         String JavaDoc timerName = String.valueOf(nbr) + " executions " + message;
105         Query query = getQuery();
106         int resultSize = -1;
107         JalistoTimer.timerStart(timerName);
108         for (int i = 0; i < nbr; i++) {
109             tx.begin();
110             resultSize = query.execute().size();
111             tx.commit();
112         }
113         JalistoTimer.timerStop(timerName, false);
114         assertEquals("must be equals", 13, resultSize);
115         JalistoTimer.summary(timerName);
116     }
117
118     private Query getQuery() {
119         String JavaDoc authorLastName1 = Author.lastNames[0];
120         String JavaDoc authorLastName2 = Author.lastNames[1];
121         String JavaDoc authorFirstName = Author.firstNames[0];
122
123         Query query = queryManager.query();
124         query.constrain(Library.class);
125         Constraint c1 = query.descend("books").descend("author").descend("lastName").constrain(authorLastName1).equal();
126         Constraint c2 = query.descend("books").descend("author").descend("lastName").constrain(authorLastName2).equal();
127         Constraint c3 = query.descend("books").descend("author").descend("firstName").constrain(authorFirstName).equal();
128         (c1.or(c2)).and(c3);
129
130         return query;
131     }
132
133     public void populateTestLibrary(int nbrLibrary, int nbrBook, int nbrClient) {
134         tx.begin();
135         for (int i = 0; i < nbrLibrary; i++) {
136             Library library = Library.newLibrary();
137             ArrayList JavaDoc books = library.getBooks();
138             ArrayList JavaDoc clients = library.getClients();
139             for (int j = 0; j < nbrBook; j++) {
140                 Object JavaDoc[] book = BookWithFormat.newBook().toArray();
141                 Author author = Author.newAuthor();
142                 book[3] = session.createObject(author.toArray(), Author.class);
143                 books.add(session.createObject(book, BookWithFormat.class));
144             }
145             for (int j = 0; j < nbrClient; j++) {
146                 Client client = Client.newClient();
147                 Object JavaDoc clientOid = session.createObject(client.toArray(), Client.class);
148                 clients.add(clientOid);
149             }
150             Author a = Author.newAuthor();
151             Object JavaDoc[] author = a.toArray();
152             library.setAuthorMostWanted(session.createObject(author, Author.class));
153             session.createObject(library.toArray(), Library.class);
154         }
155         tx.commit();
156     }
157
158
159     /**
160      * ************************* need to overwrite for suite() method ********************************************
161      */

162
163     public void initSession() {
164         super.initSession(false);
165     }
166
167     public void finishTests() {
168         super.finishTests();
169     }
170
171     public void define(ClassDescription classDescription) {
172         super.define(classDescription);
173     }
174
175     public void cleanUp(Class JavaDoc aClass) {
176         super.cleanUp(aClass);
177     }
178
179     public void buildIndex(Class JavaDoc aClass, String JavaDoc fieldName) {
180         super.buildIndex(aClass, fieldName);
181     }
182
183     public void deleteIndex(Class JavaDoc aClass, String JavaDoc fieldName) {
184         super.deleteIndex(aClass, fieldName);
185     }
186
187     public void printIndex(Class JavaDoc aClass, String JavaDoc fieldName) {
188         super.printIndex(aClass, fieldName);
189     }
190 }
191
Popular Tags