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.BookWithAuthor; 31 import org.objectweb.jalisto.se.test.workbench.JalistoTestSuite; 32 import org.objectweb.jalisto.se.test.workbench.JalistoTimer; 33 34 public class QueryTestCase extends JalistoQueryTestCase { 35 36 public QueryTestCase() { 37 } 38 39 public QueryTestCase(String name) { 40 super(name); 41 } 42 43 public static Test suite() { 44 JalistoTestSuite suite = new JalistoTestSuite(); 45 QueryTestCase tc = (QueryTestCase) newTestCase(suite, new QueryTestCase()); 46 47 tc.prepare(); 48 49 tc.executeQueryTests("Without index", numberOfQueryExecute); 50 51 tc.buildIndex(BookWithAuthor.class, "title"); 52 tc.buildIndex(BookWithAuthor.class, "price"); 53 54 tc.executeQueryTests("With index", numberOfQueryExecute); 55 56 tc.deleteIndex(BookWithAuthor.class, "title"); 57 tc.deleteIndex(BookWithAuthor.class, "price"); 58 59 tc.finishTests(); 60 61 return suite; 62 } 63 64 67 68 public void prepare() { 69 initSession(); 70 BookWithAuthor.counter = -1; 71 define(BookWithAuthor.getMetaDescription()); 72 deleteIndex(BookWithAuthor.class, "title"); 73 deleteIndex(BookWithAuthor.class, "price"); 74 cleanUp(BookWithAuthor.class); 75 populateBookWithAuthor(1000); 76 } 77 78 public void executeQueryTests(String message, int nbr) { 79 executeQuery(getQuery(), message, nbr, 16); 80 executeQuery(getQueryWithOrBranch(), message + " or branch", nbr, 700); 81 82 } 84 85 public void executeQuery(Query query, String message, int nbr, int expected) { 86 String timerName = String.valueOf(nbr) + " executions " + message; 87 88 int resultSize = -1; 89 90 JalistoTimer.timerStart(timerName); 91 for (int i = 0; i < nbr; i++) { 92 tx.begin(); 93 resultSize = query.execute().size(); 94 tx.commit(); 95 } 96 JalistoTimer.timerStop(timerName, false); 97 assertEquals("Should be equals", expected, resultSize); 98 99 JalistoTimer.summary(timerName); 100 JalistoTimer.clean(timerName); 101 } 102 103 public void evolutionExecutionTime(Query query, String message, int nbrExec, int nbrTest) { 104 String timerName = "Evolution of the " + nbrExec + " first execution time (average on " + nbrTest + " tests) - " + message; 105 106 for (int j = 0; j < nbrTest; j++) { 107 for (int i = 0; i < nbrExec; i++) { 109 JalistoTimer.timerStart(timerName + " - " + i); 110 tx.begin(); 111 query.execute(); 112 tx.commit(); 113 JalistoTimer.timerStop(timerName + " - " + i, false); 114 } 115 } 116 117 JalistoTimer.summary(); 118 JalistoTimer.deleteAllTimers(); 119 } 120 121 private Query getQuery() { 122 String title1 = BookWithAuthor.titles[0]; 123 String title2 = BookWithAuthor.titles[1]; 124 String author1 = BookWithAuthor.authors[0]; 125 String author2 = BookWithAuthor.authors[1]; 126 Integer page1 = new Integer (500); 127 Integer price1 = new Integer (9); 128 Integer price2 = new Integer (26); 129 130 131 Query query = queryManager.query(); 132 query.constrain(BookWithAuthor.class); 133 Constraint c1 = query.descend("title").constrain(title1).equal(); 134 Constraint c2 = query.descend("title").constrain(title2).equal(); 135 Constraint c3 = query.descend("author").constrain(author1).equal(); 136 Constraint c4 = query.descend("author").constrain(author2).equal(); 137 Constraint c5 = query.descend("price").constrain(price1).smaller(); 138 Constraint c6 = query.descend("price").constrain(price2).greater(); 139 Constraint c7 = query.descend("pages").constrain(page1).greater(); 140 ((c1.and(c3)).or(c2.and(c4))) 141 .and( 142 (c5.or(c6)).and(c7)); 143 144 return query; 145 } 146 147 private Query getQueryWithOrBranch() { 148 String title1 = BookWithAuthor.titles[0]; 149 String title2 = BookWithAuthor.titles[1]; 150 String author1 = BookWithAuthor.authors[0]; 151 String author2 = BookWithAuthor.authors[1]; 152 Integer page1 = new Integer (500); 153 Integer price1 = new Integer (9); 154 Integer price2 = new Integer (26); 155 156 157 Query query = queryManager.query(); 158 query.constrain(BookWithAuthor.class); 159 Constraint c1 = query.descend("title").constrain(title1).equal(); 160 Constraint c2 = query.descend("title").constrain(title2).equal(); 161 Constraint c3 = query.descend("author").constrain(author1).equal(); 162 Constraint c4 = query.descend("author").constrain(author2).equal(); 163 Constraint c5 = query.descend("price").constrain(price1).smaller(); 164 Constraint c6 = query.descend("price").constrain(price2).greater(); 165 Constraint c7 = query.descend("pages").constrain(page1).greater(); 166 ((c1.and(c3)).or(c2.and(c4))) 167 .or( 168 (c5.or(c6)).or(c7)); 169 170 return query; 171 } 172 173 174 177 178 public void initSession() { 179 super.initSession(false); 180 } 181 182 public void define(ClassDescription classDescription) { 183 super.define(classDescription); 184 } 185 186 public void cleanUp(Class aClass) { 187 super.cleanUp(aClass); 188 } 189 190 public void populateBookWithAuthor(int nbr) { 191 super.populateBookWithAuthor(nbr); 192 } 193 194 public void buildIndex(Class aClass, String fieldName) { 195 super.buildIndex(aClass, fieldName); 196 } 197 198 public void deleteIndex(Class aClass, String fieldName) { 199 super.deleteIndex(aClass, fieldName); 200 } 201 202 public void printIndex(Class aClass, String fieldName) { 203 super.printIndex(aClass, fieldName); 204 } 205 206 public void finishTests() { 207 super.finishTests(); 208 } 209 } 210 | Popular Tags |