1 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 ; 35 36 public class CollectionQueryTestCase extends JalistoQueryTestCase { 37 38 public CollectionQueryTestCase() { 39 } 40 41 public CollectionQueryTestCase(String 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 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 message, int nbr) { 88 String 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 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 clientLastName = Client.lastNames[0]; 136 String 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 authorLastName = Author.lastNames[0]; 157 String 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 books = library.getBooks(); 181 ArrayList 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 clientOid = session.createObject(client.toArray(), Client.class); 192 clients.add(clientOid); 193 } 194 Author a = Author.newAuthor(); 195 Object [] 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 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 aClass) { 218 super.cleanUp(aClass); 219 } 220 221 public void finishTests() { 222 super.finishTests(); 223 } 224 } 225 | Popular Tags |