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.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 ; 36 37 public class AdvancedQueryTestCase extends JalistoQueryTestCase { 38 public AdvancedQueryTestCase() { 39 } 40 41 public AdvancedQueryTestCase(String 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 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 message, int nbr) { 104 String 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 authorLastName1 = Author.lastNames[0]; 120 String authorLastName2 = Author.lastNames[1]; 121 String 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 books = library.getBooks(); 138 ArrayList clients = library.getClients(); 139 for (int j = 0; j < nbrBook; j++) { 140 Object [] 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 clientOid = session.createObject(client.toArray(), Client.class); 148 clients.add(clientOid); 149 } 150 Author a = Author.newAuthor(); 151 Object [] 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 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 aClass) { 176 super.cleanUp(aClass); 177 } 178 179 public void buildIndex(Class aClass, String fieldName) { 180 super.buildIndex(aClass, fieldName); 181 } 182 183 public void deleteIndex(Class aClass, String fieldName) { 184 super.deleteIndex(aClass, fieldName); 185 } 186 187 public void printIndex(Class aClass, String fieldName) { 188 super.printIndex(aClass, fieldName); 189 } 190 } 191 | Popular Tags |