1 /*------------------------------------------------------------------------------
2 Name: Sql92SelectorTest.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6 package org.xmlBlaster.test.classtest;
7
8 import java.io.BufferedReader;
9 import java.io.InputStreamReader;
10 import java.util.ArrayList;
11 import java.util.HashMap;
12 import java.util.Map;
13
14 import junit.framework.TestCase;
15
16 import java.util.logging.Logger;
17 import java.util.logging.Level;
18 import org.xmlBlaster.util.Global;
19 import org.xmlBlaster.util.XmlBlasterException;
20 import org.xmlBlaster.util.lexical.LikeOpWrapper;
21 import org.xmlBlaster.util.lexical.Sql92Selector;
22 import org.xmlBlaster.util.qos.ClientProperty;
23
24 /**
25 * Test ClientProperty.
26 * <p />
27 * All methods starting with 'test' and without arguments are invoked automatically
28 * <p />
29 * Invoke: java -Djava.compiler= junit.textui.TestRunner -noloading org.xmlBlaster.test.classtest.Sql92SelectorTest
30 * @see org.xmlBlaster.util.qos.ClientProperty
31 * @see <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/engine.qos.clientProperty.html">The client.qos.clientProperty requirement</a>
32 */
33 public class Sql92SelectorTest extends TestCase {
34
35 private final static String ME = "Sql92SelectorTest";
36 protected Global glob;
37 private static Logger log = Logger.getLogger(Sql92SelectorTest.class.getName());
38 private Map[] dataSet;
39 private boolean[][] resultSet;
40 private String[] querySet;
41
42 public Sql92SelectorTest(String name) {
43 this(null, name);
44 }
45
46 public Sql92SelectorTest(Global global, String name) {
47 super(name);
48 if (global == null) this.glob = Global.instance();
49 else this.glob = global;
50
51 }
52
53 protected void setUp() {
54 setupDataSets();
55 }
56
57 protected void tearDown() {
58 }
59
60 /**
61 * Change this method if you want to add a new data set to be checked
62 *
63 */
64 private void setupDataSets() {
65 ArrayList datas = new ArrayList();
66 Map map;
67 String encoding = null;
68 ClientProperty prop1, prop2, prop3;
69 prop1 = new ClientProperty("age" , "integer", encoding, "23" );
70 prop2 = new ClientProperty("city" , null, encoding, "London" );
71 prop3 = new ClientProperty("amount", "double", encoding, "100.1234567");
72
73 // set : 0 (0:0:0)
74 map = new HashMap();
75 datas.add(map);
76
77 // set : 1 (0:0:1)
78 map = new HashMap();
79 map.put("amount", prop3);
80 datas.add(map);
81
82 // set : 2 (0:1:0)
83 map = new HashMap();
84 map.put("city" , prop2);
85 datas.add(map);
86
87 // set : 3 (0:1:1)
88 map = new HashMap();
89 map.put("city" , prop2);
90 map.put("amount", prop3);
91 datas.add(map);
92
93 // set : 4 (1:0:0)
94 map = new HashMap();
95 map.put("age" , prop1);
96 datas.add(map);
97
98 // set : 5 (1:0:1)
99 map = new HashMap();
100 map.put("age" , prop1);
101 map.put("amount", prop3);
102 datas.add(map);
103
104 // set : 6 (1:1:0)
105 map = new HashMap();
106 map.put("age" , prop1);
107 map.put("city" , prop2);
108 datas.add(map);
109
110 // set : 7 (1:1:1)
111 map = new HashMap();
112 map.put("age" , prop1);
113 map.put("city" , prop2);
114 map.put("amount", prop3);
115 datas.add(map);
116 this.dataSet = (Map[])datas.toArray(new Map[datas.size()]);
117
118 }
119
120 /**
121 * Checks if the provided data sets, the queries and the results are
122 * consistent with eachother (this is invoked before starting the real testing)
123 */
124 private void consistencyCheck() {
125 int numData = this.dataSet.length;
126 int numQueries = this.querySet.length;
127 int numResults = this.resultSet.length;
128 assertEquals("The number of queries '" + numQueries + "' differes from the number of results '" + numResults + "'", numQueries, numResults);
129 for (int i=0; i < numResults; i++) {
130 assertEquals("The number of results for query '" + i + "' is wrong", numData, this.resultSet[i].length);
131 }
132 }
133
134 private String getDataAsText(int pos) {
135 Map map = this.dataSet[pos];
136 StringBuffer buffer = new StringBuffer("[");
137 Object[] keys = map.keySet().toArray();
138 for (int i=0; i < keys.length; i++) {
139 if (i != 0) buffer.append(";");
140 buffer.append(keys[i]).append("=");
141 ClientProperty cp = (ClientProperty)map.get(keys[i]);
142 String tmp = "null";
143 if (cp != null) tmp = cp.getStringValue();
144 buffer.append(tmp);
145 }
146 buffer.append("]");
147 return buffer.toString();
148 }
149
150 /**
151 * This is the fully automatized initial (general) test. Since it is
152 * difficult to predict all possible problems, additional tests should
153 * be added once a bug is encountered. For each such bug an own test
154 * method should be added.
155 */
156 private void selectorPerformTest() {
157 // for each data set one selector
158 consistencyCheck();
159 /*
160 Sql92Selector[] selectors = new Sql92Selector[this.dataSet.length];
161 for (int i=0; i < this.dataSet.length; i++) {
162 if (log.isLoggable(Level.FINE)) log.fine("testSelectorStandard: creating selector nr. " + i);
163 selectors[i] = new Sql92Selector(this.glob);
164 }
165 */
166 Sql92Selector selector = new Sql92Selector(this.glob);
167
168 for (int i=0; i < this.querySet.length; i++) {
169 String query = this.querySet[i];
170 log.info("testSelectorStandard: process query '" + query + "'");
171 boolean[] shouldAnswers = this.resultSet[i];
172 for (int j=0; j < this.dataSet.length; j++) {
173 if (log.isLoggable(Level.FINE)) log.fine("testSelectorStandard: query '" + query + "' on set '" + getDataAsText(j));
174 try {
175 boolean response = selector.select(query, this.dataSet[j]);
176 assertEquals("wrong answer for query '" + i + "'\"" + query + "\" on set '" + j + "' " + getDataAsText(j), shouldAnswers[j], response);
177 }
178 catch (XmlBlasterException ex) {
179 ex.printStackTrace();
180 assertTrue("An exception should not occur on query '" + i + "'\"" + query + "\" for dataset " + getDataAsText(j), false);
181 }
182 }
183 }
184 }
185
186
187 /**
188 *
189 * @return the milliseconds per request
190 */
191 private void performanceCheck() {
192 if (log.isLoggable(Level.FINER)) log.finer("performanceCheck");
193 // for each data set one selector
194 consistencyCheck();
195 /*
196 Sql92Selector[] selectors = new Sql92Selector[this.dataSet.length];
197 for (int i=0; i < this.dataSet.length; i++) {
198 selectors[i] = new Sql92Selector(this.glob);
199 }
200 */
201 Sql92Selector selector = new Sql92Selector(this.glob);
202 try {
203 int kmax = 100;
204 long t0 = System.currentTimeMillis();
205 for (int k=0; k < kmax; k++) {
206 for (int i=0; i < this.querySet.length; i++) {
207 String query = this.querySet[i];
208 for (int j=0; j < this.dataSet.length; j++) {
209 boolean response = selector.select(query, this.dataSet[j]);
210 }
211 }
212 }
213 long dt = System.currentTimeMillis() - t0;
214 int nmax = kmax * this.dataSet.length * this.querySet.length;
215 log.info("performance: '" + nmax + "' requests in '" + dt + "' ms");
216 double ret = 1.0 * dt / nmax;
217 log.info("performance: '" + ret + "' ms per request");
218 log.info("performance: '" + ((int)(1000.0 / ret)) + "' request per second (rps)");
219 }
220 catch (XmlBlasterException ex) {
221 ex.printStackTrace();
222 }
223 }
224
225
226
227 public void interactive() {
228 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
229 System.out.println("\n- input new query: ");
230 while (true) {
231 try {
232 String line = br.readLine();
233 if (line == null) break;
234 System.out.print("Result: ");
235 for (int i=0; i < this.dataSet.length; i++) {
236 Sql92Selector selector = new Sql92Selector(this.glob);
237 boolean ret = selector.select(line, this.dataSet[i]);
238 System.out.print(ret + "\t");
239 }
240 }
241 catch (Exception ex) {
242 ex.printStackTrace();
243 }
244 System.out.println("\n- input new query: ");
245 }
246 }
247
248
249 // THE TESTING METHODS COME HERE .....
250
251 /**
252 * Tests the outer logical operators AND, OR, NOT with and without brackets.
253 * For each data there must be a boolean value telling if the result is true or false
254 */
255 public void testLogicalOps() {
256 ArrayList queries = new ArrayList();
257 ArrayList results = new ArrayList();
258 boolean t = true;
259 boolean f = false;
260
261 // query 0
262 queries.add("age=23 AND city='London' AND amount<200.2");
263 /* helper 0.0.0 0.0.1 0.1.0 0.1.1 1.0.0 1.0.1 1.1.0 1.1.1 */
264 results.add(new boolean[] {false, false, false, false, false, false, false, true });
265
266 // query 1
267 queries.add("age=23 OR city='London' OR amount < 200.2");
268 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
269 results.add(new boolean[] {f||f||f, f||f||t, f||t||f, f||t||t, t||f||f, t||f||t, t||t||f, t||t||t });
270
271 // query 2
272 queries.add("age=23 OR city='London' AND amount < 200.2");
273 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
274 results.add(new boolean[] {f||f&&f, f||f&&t, f||t&&f, f||t&&t, t||f&&f, t||f&&t, t||t&&f, t||t&&t });
275
276 // query 3
277 queries.add("age=23 OR city='London' AND amount < 200.2");
278 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
279 results.add(new boolean[] {f||f&&f, f||f&&t, f||t&&f, f||t&&t, t||f&&f, t||f&&t, t||t&&f, t||t&&t });
280
281 // query 4
282 queries.add("(age=23 AND city='London') OR amount < 200.2");
283 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
284 results.add(new boolean[] {f&&f||f, f&&f||t, f&&t||f, f&&t||t, t&&f||f, t&&f||t, t&&t||f, t&&t||t });
285
286 // query 5
287 queries.add("age=23 OR (city='London' AND amount < 200.2)");
288 /* helper 0 0 0 , 0 0 1 , 0 1 0 , 0 1 1 , 1 0 0, 1 0 1 , 1 1 0 , 1 1 1 */
289 results.add(new boolean[] {f||(f&&f), f||(f&&t), f||(t&&f), f||(t&&t), t||(f&&f), t||(f&&t), t||(t&&f), t||(t&&t) });
290
291 // query 6
292 queries.add("NOT age=23 OR (city='London' AND amount < 200.2)");
293 /* helper 0 0 0 , 0 0 1 , 0 1 0 , 0 1 1 , 1 0 0 , 1 0 1 , 1 1 0 , 1 1 1 */
294 results.add(new boolean[] {!f||(f&&f), !f||(f&&t), !f||(t&&f), !f||(t&&t), !t||(f&&f), !t||(f&&t), !t||(t&&f), !t||(t&&t) });
295
296 // query 7
297 queries.add("age=23 OR NOT (city='London' AND amount < 200.2)");
298 /* helper 0 0 0 , 0 0 1 , 0 1 0 , 0 1 1 , 1 0 0, 1 0 1 , 1 1 0 , 1 1 1 */
299 results.add(new boolean[] {f||!(f&&f), f||!(f&&t), f||!(t&&f), f||!(t&&t), t||!(f&&f), t||!(f&&t), t||!(t&&f), t||!(t&&t) });
300
301 // query 8
302 queries.add("(age=23 OR NOT (city='London' AND amount < 200.2))");
303 /* helper 0 0 0 , 0 0 1 , 0 1 0 , 0 1 1 , 1 0 0, 1 0 1 , 1 1 0 , 1 1 1 */
304 results.add(new boolean[] {f||!(f&&f), f||!(f&&t), f||!(t&&f), f||!(t&&t), t||!(f&&f), t||!(f&&t), t||!(t&&f), t||!(t&&t) });
305
306 // query 9
307 queries.add("NOT (age=23 OR NOT (city='London' AND amount < 200.2))");
308 /* helper 0 0 0 , 0 0 1 , 0 1 0 , 0 1 1 , 1 0 0, 1 0 1 , 1 1 0 , 1 1 1 */
309 results.add(new boolean[] {!(f||!(f&&f)), !(f||!(f&&t)), !(f||!(t&&f)), !(f||!(t&&t)), !(t||!(f&&f)), !(t||!(f&&t)), !(t||!(t&&f)), !(t||!(t&&t)) });
310
311 // query 10
312 queries.add("NOT (age=23 OR NOT (city='London' AND (amount < 200.2)))");
313 /* helper 0 0 0 , 0 0 1 , 0 1 0 , 0 1 1 , 1 0 0, 1 0 1 , 1 1 0 , 1 1 1 */
314 results.add(new boolean[] {!(f||!(f&&f)), !(f||!(f&&t)), !(f||!(t&&f)), !(f||!(t&&t)), !(t||!(f&&f)), !(t||!(f&&t)), !(t||!(t&&f)), !(t||!(t&&t)) });
315
316 // query 11
317 queries.add("age=23 OR NOT ((city='London') AND amount < 200.2)");
318 /* helper 0 0 0 , 0 0 1 , 0 1 0 , 0 1 1 , 1 0 0, 1 0 1 , 1 1 0 , 1 1 1 */
319 results.add(new boolean[] {f||!(f&&f), f||!(f&&t), f||!(t&&f), f||!(t&&t), t||!(f&&f), t||!(f&&t), t||!(t&&f), t||!(t&&t) });
320
321 // query 12
322 queries.add("age=23 OR NOT ((city='London') AND (amount < 200.2))");
323 /* helper 0 0 0 , 0 0 1 , 0 1 0 , 0 1 1 , 1 0 0, 1 0 1 , 1 1 0 , 1 1 1 */
324 results.add(new boolean[] {f||!(f&&f), f||!(f&&t), f||!(t&&f), f||!(t&&t), t||!(f&&f), t||!(f&&t), t||!(t&&f), t||!(t&&t) });
325
326 // query 13
327 queries.add("age=23 OR NOT ((city='London') AND NOT(amount < 200.2))");
328 /* helper 0 0 0 , 0 0 1 , 0 1 0 , 0 1 1 , 1 0 0, 1 0 1 , 1 1 0 , 1 1 1 */
329 results.add(new boolean[] {f||!(f&&!f), f||!(f&&!t), f||!(t&&!f), f||!(t&&!t), t||!(f&&!f), t||!(f&&!t), t||!(t&&!f), t||!(t&&!t) });
330
331 this.querySet = (String[])queries.toArray(new String[queries.size()]);
332 this.resultSet = (boolean[][])results.toArray(new boolean[results.size()][]);
333 // here the real testing is performed (the algorithm is the same for all tests)
334 selectorPerformTest();
335 }
336
337
338 /**
339 * Tests the NULL and NOT NULL statements.
340 * For each data there must be a boolean value telling if the result is true or false
341 */
342 public void testNullOps() {
343 ArrayList queries = new ArrayList();
344 ArrayList results = new ArrayList();
345 boolean t = true;
346 boolean f = false;
347
348 // query 0
349 queries.add("age IS NULL OR city IS NULL OR amount IS NULL");
350 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
351 results.add(new boolean[] {t||t||t, t||t||f, t||f||t, t||f||f, f||t||t, f||t||f, f||f||t, f||f||f });
352
353 // query 1
354 queries.add("age IS NULL AND city IS NULL AND amount IS NULL");
355 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
356 results.add(new boolean[] {t&&t&&t, t&&t&&f, t&&f&&t, t&&f&&f, f&&t&&t, f&&t&&f, f&&f&&t, f&&f&&f });
357
358 // query 2
359 queries.add("age IS NOT NULL OR city IS NOT NULL OR amount IS NOT NULL");
360 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
361 results.add(new boolean[] {f||f||f, f||f||t, f||t||f, f||t||t, t||f||f, t||f||t, t||t||f, t||t||t });
362
363 // query 3
364 queries.add("age IS NOT NULL AND city IS NOT NULL AND amount IS NOT NULL");
365 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
366 results.add(new boolean[] {f&&f&&f, f&&f&&t, f&&t&&f, f&&t&&t, t&&f&&f, t&&f&&t, t&&t&&f, t&&t&&t });
367
368 this.querySet = (String[])queries.toArray(new String[queries.size()]);
369 this.resultSet = (boolean[][])results.toArray(new boolean[results.size()][]);
370 // here the real testing is performed (the algorithm is the same for all tests)
371 selectorPerformTest();
372 }
373
374 /**
375 * Tests the outer logical operators AND, OR, NOT with and without brackets.
376 * For each data there must be a boolean value telling if the result is true or false
377 */
378 public void testArithmeticOps() {
379 ArrayList queries = new ArrayList();
380 ArrayList results = new ArrayList();
381 boolean t = true;
382 boolean f = false;
383
384 // query 0
385 queries.add("age = 15+8 AND amount < 300.0+10.0");
386 /* helper 0 0, 0 1, 0 0, 0 1, 1 0 1 1 1 0 1 1 */
387 results.add(new boolean[] {f&&f, f&&t, f&&f, f&&t, t&&f, t&&t, t&&f, t&&t });
388
389 // query 1
390 queries.add("age = 26-3 AND amount < 300.0-10.0");
391 /* helper 0 0, 0 1, 0 0, 0 1, 1 0 1 1 1 0 1 1 */
392 results.add(new boolean[] {f&&f, f&&t, f&&f, f&&t, t&&f, t&&t, t&&f, t&&t });
393
394 // query 2
395 queries.add("age = 23*1 AND amount < 110.0*2");
396 /* helper 0 0, 0 1, 0 0, 0 1, 1 0 1 1 1 0 1 1 */
397 results.add(new boolean[] {f&&f, f&&t, f&&f, f&&t, t&&f, t&&t, t&&f, t&&t });
398
399 // query 3
400 queries.add("age = 46/2 AND amount < 600.0/2.2");
401 /* helper 0 0, 0 1, 0 0, 0 1, 1 0 1 1 1 0 1 1 */
402 results.add(new boolean[] {f&&f, f&&t, f&&f, f&&t, t&&f, t&&t, t&&f, t&&t });
403
404 // query 4
405 queries.add("age = (26-3) AND amount < 300.0-10.0");
406 /* helper 0 0, 0 1, 0 0, 0 1, 1 0 1 1 1 0 1 1 */
407 results.add(new boolean[] {f&&f, f&&t, f&&f, f&&t, t&&f, t&&t, t&&f, t&&t });
408
409 // query 5
410 queries.add("age = (23*1) AND amount < 110.0*2");
411 /* helper 0 0, 0 1, 0 0, 0 1, 1 0 1 1 1 0 1 1 */
412 results.add(new boolean[] {f&&f, f&&t, f&&f, f&&t, t&&f, t&&t, t&&f, t&&t });
413
414 // query 6
415 queries.add("age = 2*(26-3)-23 AND amount < 300.0-10.0");
416 /* helper 0 0, 0 1, 0 0, 0 1, 1 0 1 1 1 0 1 1 */
417 results.add(new boolean[] {f&&f, f&&t, f&&f, f&&t, t&&f, t&&t, t&&f, t&&t });
418
419 // query 7
420 queries.add("age = -(23*1)+46 AND amount < 110.0*2");
421 /* helper 0 0, 0 1, 0 0, 0 1, 1 0 1 1 1 0 1 1 */
422 results.add(new boolean[] {f&&f, f&&t, f&&f, f&&t, t&&f, t&&t, t&&f, t&&t });
423
424 // query 8
425 queries.add("age = -23*1+46 AND amount < 110.0*2");
426 /* helper 0 0, 0 1, 0 0, 0 1, 1 0 1 1 1 0 1 1 */
427 results.add(new boolean[] {f&&f, f&&t, f&&f, f&&t, t&&f, t&&t, t&&f, t&&t });
428
429 // query 9
430 queries.add("age = 47-12*2 AND amount < 110.0*2");
431 /* helper 0 0, 0 1, 0 0, 0 1, 1 0 1 1 1 0 1 1 */
432 results.add(new boolean[] {f&&f, f&&t, f&&f, f&&t, t&&f, t&&t, t&&f, t&&t });
433
434 // query 10
435 queries.add("2*age = 46 AND amount < 110.0*2");
436 /* helper 0 0, 0 1, 0 0, 0 1, 1 0 1 1 1 0 1 1 */
437 results.add(new boolean[] {f&&f, f&&t, f&&f, f&&t, t&&f, t&&t, t&&f, t&&t });
438
439 // query 11
440 queries.add("age+6 = (27+2) AND amount < 110.0*2");
441 /* helper 0 0, 0 1, 0 0, 0 1, 1 0 1 1 1 0 1 1 */
442 results.add(new boolean[] {f&&f, f&&t, f&&f, f&&t, t&&f, t&&t, t&&f, t&&t });
443
444 // query 12
445 queries.add("age+4 = 27 AND amount < (110.0*2)");
446 /* helper 0 0, 0 1, 0 0, 0 1, 1 0 1 1 1 0 1 1 */
447 results.add(new boolean[] {f&&f, f&&t, f&&f, f&&t, t&&f, t&&t, t&&f, t&&t });
448
449 this.querySet = (String[])queries.toArray(new String[queries.size()]);
450 this.resultSet = (boolean[][])results.toArray(new boolean[results.size()][]);
451 // here the real testing is performed (the algorithm is the same for all tests)
452 selectorPerformTest();
453 }
454
455 /**
456 * Tests the IN (...) statements.
457 * For each data there must be a boolean value telling if the result is true or false
458 */
459 public void testInSetOps() {
460 ArrayList queries = new ArrayList();
461 ArrayList results = new ArrayList();
462 boolean t = true;
463 boolean f = false;
464
465 // query 0
466 queries.add("age IN (25,23,30) AND city IN('London', 'Paris', 'Caslano')");
467 /* helper 0 0, 0 0, 0 1, 0 1, 1 0 1 0 1 1 1 1 */
468 results.add(new boolean[] {f&&f, f&&f, f&&t, f&&t, t&&f, t&&f, t&&t, t&&t });
469
470 // query 1
471 queries.add("age IN (23) AND city IN('London')");
472 /* helper 0 0, 0 0, 0 1, 0 1, 1 0 1 0 1 1 1 1 */
473 results.add(new boolean[] {f&&f, f&&f, f&&t, f&&t, t&&f, t&&f, t&&t, t&&t });
474
475 // query 2
476 queries.add("age IN (24,25) AND city IN('London')");
477 // helper : all must be false
478 results.add(new boolean[] {f&&f, f&&t, f&&f, f&&t, f&&f, f&&t, f&&f, f&&t });
479
480 // query 3
481 queries.add("age IN (23) AND city IN('Caslano', 'Paris')");
482 // helper all must be false
483 results.add(new boolean[] {f&&f, f&&f, f&&f, f&&f, t&&f, t&&f, t&&f, t&&f });
484
485 this.querySet = (String[])queries.toArray(new String[queries.size()]);
486 this.resultSet = (boolean[][])results.toArray(new boolean[results.size()][]);
487 // here the real testing is performed (the algorithm is the same for all tests)
488 selectorPerformTest();
489 }
490
491 /**
492 * Tests the IN (...) statements.
493 * For each data there must be a boolean value telling if the result is true or false
494 */
495 public void testBetweenOps() {
496 ArrayList queries = new ArrayList();
497 ArrayList results = new ArrayList();
498 boolean t = true;
499 boolean f = false;
500
501 // query 0
502 queries.add("age BETWEEN 10 AND 40 OR city BETWEEN 'Amsterdam' AND 'Paris' OR amount BETWEEN 10.0 AND 2000.0");
503 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
504 results.add(new boolean[] {f||f||f, f||f||t, f||t||f, f||t||t, t||f||f, t||f||t, t||t||f, t||t||t });
505
506 // query 1
507 queries.add("age BETWEEN 10 AND 40 AND city BETWEEN 'Amsterdam' AND 'Paris' AND amount BETWEEN 10.0 AND 2000.0");
508 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
509 results.add(new boolean[] {f&&f&&f, f&&f&&t, f&&t&&f, f&&t&&t, t&&f&&f, t&&f&&t, t&&t&&f, t&&t&&t });
510
511 // query 2
512 queries.add("(age BETWEEN 10 AND 40) AND (city BETWEEN 'Amsterdam' AND 'Paris') AND (amount BETWEEN 10.0 AND 2000.0)");
513 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
514 results.add(new boolean[] {f&&f&&f, f&&f&&t, f&&t&&f, f&&t&&t, t&&f&&f, t&&f&&t, t&&t&&f, t&&t&&t });
515
516 // query 3
517 queries.add("age BETWEEN 23 AND 23 OR city BETWEEN 'London' AND 'London' OR amount BETWEEN 10.0 AND 2000.0");
518 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
519 results.add(new boolean[] {f||f||f, f||f||t, f||t||f, f||t||t, t||f||f, t||f||t, t||t||f, t||t||t });
520
521 // query 4
522 queries.add("age BETWEEN 30 AND 10 OR city BETWEEN 'London' AND 'London' OR amount BETWEEN 10.0 AND 2000.0");
523 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 0 0 0 0 0 1 0 1 0 1 1 1 */
524 results.add(new boolean[] {f||f||f, f||f||t, f||t||f, f||t||t, f||f||f, f||f||t, f||t||f, f||t||t });
525
526 // query 5
527 queries.add("age BETWEEN 23 AND 23 OR city BETWEEN 'Amsteram' AND 'Caslano' OR amount BETWEEN 10.0 AND 2000.0");
528 /* helper 0 0 0, 0 0 1, 0 0 0, 0 0 1, 1 0 0 1 0 1 1 0 0 1 0 1 */
529 results.add(new boolean[] {f||f||f, f||f||t, f||f||f, f||f||t, t||f||f, t||f||t, t||f||f, t||f||t });
530
531 this.querySet = (String[])queries.toArray(new String[queries.size()]);
532 this.resultSet = (boolean[][])results.toArray(new boolean[results.size()][]);
533 // here the real testing is performed (the algorithm is the same for all tests)
534 selectorPerformTest();
535 }
536
537 /**
538 * Tests the LIKE statements.
539 * For each data there must be a boolean value telling if the result is true or false
540 */
541 public void testLikeOps() {
542 ArrayList queries = new ArrayList();
543 ArrayList results = new ArrayList();
544 boolean t = true;
545 boolean f = false;
546
547 // query 0
548 queries.add("age=23 OR city LIKE 'L%n' OR amount BETWEEN 10.0 AND 2000.0");
549 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
550 results.add(new boolean[] {f||f||f, f||f||t, f||t||f, f||t||t, t||f||f, t||f||t, t||t||f, t||t||t });
551
552 // query 1
553 queries.add("age=23 OR city LIKE 'L%x' OR amount BETWEEN 10.0 AND 2000.0");
554 /* helper 0 0 0, 0 0 1, 0 0 0, 0 0 1, 1 0 0 1 0 1 1 0 0 1 0 1 */
555 results.add(new boolean[] {f||f||f, f||f||t, f||f||f, f||f||t, t||f||f, t||f||t, t||f||f, t||f||t });
556
557 // query 2
558 queries.add("age=23 OR city LIKE 'L%n' ESCAPE '\\' OR amount BETWEEN 10.0 AND 2000.0");
559 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
560 results.add(new boolean[] {f||f||f, f||f||t, f||t||f, f||t||t, t||f||f, t||f||t, t||t||f, t||t||t });
561
562 // query 3
563 queries.add("age=23 OR city NOT LIKE 'L%n' OR amount BETWEEN 10.0 AND 2000.0");
564 /* helper 0 1 0, 0 1 1, 0 0 0, 0 f 1, 1 1 0 1 t 1 1 0 0 1 0 1 */
565 results.add(new boolean[] {f||t||f, f||t||t, f||f||f, f||f||t, t||t||f, t||t||t, t||f||f, t||f||t });
566
567 // query 4
568 queries.add("age=23 OR city NOT LIKE 'L%n' ESCAPE '\\' OR amount BETWEEN 10.0 AND 2000.0");
569 /* helper 0 1 0, 0 1 1, 0 0 0, 0 f 1, 1 1 0 1 t 1 1 0 0 1 0 1 */
570 results.add(new boolean[] {f||t||f, f||t||t, f||f||f, f||f||t, t||t||f, t||t||t, t||f||f, t||f||t });
571
572 // query 5
573 queries.add("age=23 OR city LIKE 'Lo_d_n' OR amount BETWEEN 10.0 AND 2000.0");
574 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
575 results.add(new boolean[] {f||f||f, f||f||t, f||t||f, f||t||t, t||f||f, t||f||t, t||t||f, t||t||t });
576
577 // query 6
578 queries.add("age=23 OR city LIKE 'Lo_d_x' OR amount BETWEEN 10.0 AND 2000.0");
579 /* helper 0 0 0, 0 0 1, 0 0 0, 0 0 1, 1 0 0 1 0 1 1 0 0 1 0 1 */
580 results.add(new boolean[] {f||f||f, f||f||t, f||f||f, f||f||t, t||f||f, t||f||t, t||f||f, t||f||t });
581
582 // query 7
583 queries.add("age=23 OR city LIKE 'Lo_d_n' ESCAPE '\\' OR amount BETWEEN 10.0 AND 2000.0");
584 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
585 results.add(new boolean[] {f||f||f, f||f||t, f||t||f, f||t||t, t||f||f, t||f||t, t||t||f, t||t||t });
586
587 // query 8
588 queries.add("age=23 OR city NOT LIKE 'Lo_d_n' OR amount BETWEEN 10.0 AND 2000.0");
589 /* helper 0 1 0, 0 1 1, 0 0 0, 0 f 1, 1 1 0 1 t 1 1 0 0 1 0 1 */
590 results.add(new boolean[] {f||t||f, f||t||t, f||f||f, f||f||t, t||t||f, t||t||t, t||f||f, t||f||t });
591
592 // query 9
593 queries.add("age=23 OR city NOT LIKE 'Lo_d_n' ESCAPE '\\' OR amount BETWEEN 10.0 AND 2000.0");
594 /* helper 0 1 0, 0 1 1, 0 0 0, 0 f 1, 1 1 0 1 t 1 1 0 0 1 0 1 */
595 results.add(new boolean[] {f||t||f, f||t||t, f||f||f, f||f||t, t||t||f, t||t||t, t||f||f, t||f||t });
596
597 this.querySet = (String[])queries.toArray(new String[queries.size()]);
598 this.resultSet = (boolean[][])results.toArray(new boolean[results.size()][]);
599 // here the real testing is performed (the algorithm is the same for all tests)
600 selectorPerformTest();
601 }
602
603 /**
604 * Tests the REGEX statements.
605 * For each data there must be a boolean value telling if the result is true or false
606 */
607 public void testRegexOps() {
608 ArrayList queries = new ArrayList();
609 ArrayList results = new ArrayList();
610 boolean t = true;
611 boolean f = false;
612
613 // query 0
614 queries.add("age=23 OR city REGEX 'L.*n' OR amount BETWEEN 10.0 AND 2000.0");
615 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
616 results.add(new boolean[] {f||f||f, f||f||t, f||t||f, f||t||t, t||f||f, t||f||t, t||t||f, t||t||t });
617
618 // query 1
619 queries.add("age=23 OR city REGEX 'L\\Dn\\Son' OR amount BETWEEN 10.0 AND 2000.0");
620 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
621 results.add(new boolean[] {f||f||f, f||f||t, f||t||f, f||t||t, t||f||f, t||f||t, t||t||f, t||t||t });
622
623 // query 2
624 queries.add("age=23 OR city REGEX 'L[m-z]ndo[^z]' OR amount BETWEEN 10.0 AND 2000.0");
625 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
626 results.add(new boolean[] {f||f||f, f||f||t, f||t||f, f||t||t, t||f||f, t||f||t, t||t||f, t||t||t });
627
628 // query 3
629 queries.add("age=23 OR city REGEX 'L.*x' OR amount BETWEEN 10.0 AND 2000.0");
630 /* helper 0 0 0, 0 0 1, 0 0 0, 0 0 1, 1 0 0 1 0 1 1 0 0 1 0 1 */
631 results.add(new boolean[] {f||f||f, f||f||t, f||f||f, f||f||t, t||f||f, t||f||t, t||f||f, t||f||t });
632
633 // query 4
634 queries.add("age=23 OR city REGEX 'Lo.d.n' OR amount BETWEEN 10.0 AND 2000.0");
635 /* helper 0 0 0, 0 0 1, 0 1 0, 0 1 1, 1 0 0 1 0 1 1 1 0 1 1 1 */
636 results.add(new boolean[] {f||f||f, f||f||t, f||t||f, f||t||t, t||f||f, t||f||t, t||t||f, t||t||t });
637
638 // query 5
639 queries.add("age=23 OR city REGEX 'Lo.d.x' OR amount BETWEEN 10.0 AND 2000.0");
640 /* helper 0 0 0, 0 0 1, 0 0 0, 0 0 1, 1 0 0 1 0 1 1 0 0 1 0 1 */
641 results.add(new boolean[] {f||f||f, f||f||t, f||f||f, f||f||t, t||f||f, t||f||t, t||f||f, t||f||t });
642
643 this.querySet = (String[])queries.toArray(new String[queries.size()]);
644 this.resultSet = (boolean[][])results.toArray(new boolean[results.size()][]);
645 // here the real testing is performed (the algorithm is the same for all tests)
646 selectorPerformTest();
647 }
648
649
650
651
652
653
654
655
656 /**
657 * These are the examples found in the JMS 1.1. specification
658 */
659 public void testLikeOpWrapper() {
660 try {
661 String pattern = "12%3";
662 LikeOpWrapper wrapper = new LikeOpWrapper(this.glob, pattern);
663
664 assertEquals("wrong result with pattern '" + pattern + "' for '123'", true, wrapper.match("123"));
665 assertEquals("wrong result with pattern '" + pattern + "' for '12993'", true, wrapper.match("12993"));
666 assertEquals("wrong result with pattern '" + pattern + "' for '1234'", false, wrapper.match("1234"));
667
668 pattern = "l_se";
669 wrapper = new LikeOpWrapper(this.glob, pattern);
670 assertEquals("wrong result with pattern '" + pattern + "' for 'lose'", true, wrapper.match("lose"));
671 assertEquals("wrong result with pattern '" + pattern + "' for 'loose'", false, wrapper.match("loose"));
672
673 pattern = "\\_%";
674 wrapper = new LikeOpWrapper(this.glob, pattern, '\\');
675 assertEquals("wrong result with pattern '" + pattern + "' for '_foo'", true, wrapper.match("_foo"));
676 assertEquals("wrong result with pattern '" + pattern + "' for 'bar'", false, wrapper.match("bar"));
677
678 }
679 catch (XmlBlasterException ex) {
680 ex.printStackTrace();
681 assertTrue("an exception should not occur here " + ex.getMessage(), false);
682 }
683 }
684
685 /**
686 * <pre>
687 * java org.xmlBlaster.test.classtest.Sql92SelectorTest
688 * </pre>
689 */
690 public static void main(String args[])
691 {
692 try {
693 Global global = new Global(args);
694 Sql92SelectorTest testSub = new Sql92SelectorTest(global, "Sql92SelectorTest");
695 if (global.getProperty().get("interactive", false)) {
696 testSub.setUp();
697 testSub.interactive();
698 return;
699 }
700 boolean doPerformance = global.getProperty().get("performance", false);
701
702 testSub.setUp();
703 testSub.testLikeOpWrapper();
704 testSub.tearDown();
705
706 testSub.setUp();
707 testSub.testLogicalOps();
708 if (doPerformance) testSub.performanceCheck();
709 testSub.tearDown();
710
711 testSub.setUp();
712 testSub.testNullOps();
713 if (doPerformance) testSub.performanceCheck();
714 testSub.tearDown();
715
716 testSub.setUp();
717 testSub.testArithmeticOps();
718 if (doPerformance) testSub.performanceCheck();
719 testSub.tearDown();
720
721 testSub.setUp();
722 testSub.testInSetOps();
723 if (doPerformance) testSub.performanceCheck();
724 testSub.tearDown();
725
726 testSub.setUp();
727 testSub.testBetweenOps();
728 if (doPerformance) testSub.performanceCheck();
729 testSub.tearDown();
730
731 testSub.setUp();
732 testSub.testLikeOps();
733 if (doPerformance) testSub.performanceCheck();
734 testSub.tearDown();
735
736 testSub.setUp();
737 testSub.testRegexOps();
738 if (doPerformance) testSub.performanceCheck();
739 testSub.tearDown();
740
741 }
742 catch(Throwable e) {
743 e.printStackTrace();
744 //fail(e.toString());
745 }
746 }
747 }
syntax highlighted by Code2HTML, v. 0.9.1