KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > walend > somnifugi > sql92 > test > SelectorTest


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
9  * See the License for the specific language governing
10  * permissions and limitations under the License.
11  *
12  * When distributing Covered Code, include this CDDL
13  * HEADER in each file and include the License file at
14  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
15  *
16  * If applicable add the following below this CDDL HEADER,
17  * with the fields enclosed by brackets "[]" replaced with
18  * your own identifying information: Portions Copyright
19  * [year] [name of copyright owner]
20  */

21
22 /*
23  * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
24  */

25  package net.walend.somnifugi.sql92.test;
26
27 import java.util.HashMap JavaDoc;
28
29 import junit.framework.TestCase;
30 import junit.framework.TestSuite;
31
32 import net.walend.somnifugi.sql92.RegularExpression;
33 import net.walend.somnifugi.sql92.Selector;
34 import net.walend.somnifugi.sql92.SelectorFormatException;
35
36 /**
37  Tests the Selector.
38  
39 @author <a HREF="http://walend.net">David Walend</a> <a HREF="mailto:david@walend.net">david@walend.net</a>
40  */

41 public class SelectorTest
42         extends TestCase
43 {
44     
45     public SelectorTest(String JavaDoc testName)
46     {
47         super(testName);
48     }
49
50     public void testOriginalRegularExpression()
51     {
52         String JavaDoc[][] tests = {
53             /* RE String Escape Result */
54             {"abcd", "abcd", null, "true"},
55             {"a_cd", "abcd", null, "true"},
56             {"_bcd", "abcd", null, "true"},
57             {"_bc_", "abcd", null, "true"},
58             {"____", "abcd", null, "true"},
59             {"_X__", "abcd", null, "false"},
60             {"a%d", "abcd", null, "true"},
61             {"a%d", "ad", null, "true"},
62             {"%cd", "abcd", null, "true"},
63             {"%cd", "cd", null, "true"},
64             {"ab%", "abcd", null, "true"},
65             {"ab%", "ab", null, "true"},
66             {"ab%%", "ab", null, "true"},
67             {"%%ab", "ab", null, "true"},
68             {"a%c%", "abcd", null, "true"},
69             {"a%%d", "abcd", null, "true"},
70             {"%%%%", "abcd", null, "true"},
71             {"a%cd", "acdcdbcd", null, "true"},
72             {"X%cd", "acdcdbcd", null, "false"},
73             
74             {"a%_cd", "abcd", null, "true"},
75             {"a%_cd", "abbbbcd", null, "true"},
76             
77             {"a%_%d", "abbbbcd", null, "true"},
78             {"a%_%d", "ad", null, "false"},
79             
80             {"a~_c", "abc", "~", "false"},
81             {"a~_c", "a_c", "~", "true"},
82             {"a~%c", "abc", "~", "false"},
83             {"a~%c", "a%c", "~", "true"},
84             {"a%c", "abc", "~", "true"},
85             {"~a%c", "abc", "~", "true"},
86         };
87         
88         int failCnt = 0;
89         
90         for (int n = 0; n < tests.length; n++)
91         {
92             RegularExpression re = new RegularExpression(tests[n][0], tests[n][2]);
93             boolean expected = Boolean.valueOf(tests[n][3]).booleanValue();
94             boolean actual = re.match(tests[n][1]);
95             String JavaDoc result;
96             if (actual == expected)
97             {
98             }
99             else
100             {
101                 result = "***** FAIL";
102                 failCnt++;
103                 System.err.println(result + " " + tests[n][0] + "=" + tests[n][1] +
104                     (tests[n][2] != null ? " (esc=" + tests[n][2] + ")" : "") +
105                     " : expected=" + expected + " actual=" + actual + " ");
106
107             }
108         }
109         
110         System.err.println(tests.length + " tests: " + (tests.length - failCnt) +
111                 " passed " + failCnt + " failed ");
112         
113         if (failCnt > 0)
114         {
115             fail("Failed with "+ failCnt);
116         }
117     }
118     
119     
120     public void testOriginalSelector()
121     {
122         HashMap JavaDoc<String JavaDoc,Object JavaDoc> props = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
123         HashMap JavaDoc<String JavaDoc,Object JavaDoc> fields = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
124         boolean convert = false;
125         int loop = 0;
126         
127         
128         /* Dummy up some message properties and fields */
129         props.put("color", "red");
130         props.put("description", "Galvanized hot dipped wing nuts");
131         props.put("size", new Integer JavaDoc(1024));
132         props.put("msgnum", new Integer JavaDoc(5));
133         props.put("msgnumStr", new String JavaDoc("5"));
134         props.put("price", new Float JavaDoc(1.50));
135         props.put("quantity", new Long JavaDoc(500));
136         props.put("minlong", new Long JavaDoc(Long.MIN_VALUE));
137         props.put("maxlong", new Long JavaDoc(Long.MAX_VALUE));
138         
139         props.put("trueProp", new Boolean JavaDoc(true));
140         props.put("falseProp", new Boolean JavaDoc(false));
141         props.put("byteProp", new Byte JavaDoc((byte)4));
142         props.put("shortProp", new Short JavaDoc((short)4));
143         props.put("intProp", new Integer JavaDoc(4));
144         props.put("negIntProp", new Integer JavaDoc(-4));
145         props.put("floatProp", new Float JavaDoc(4.0));
146         props.put("stringProp", new String JavaDoc("4"));
147         props.put("nullProp", null);
148         
149         /* Throw in some more complex ones */
150         props.put("Event", "*Service Change*Restart*");
151         props.put("Region", "*EA*SO*WE*BC*");
152         props.put("Airspace", "*ASSS*ARCC*BVNF*");
153         props.put("LIDFAC", "*ZDC/ARTCC*EKN/RCAG*");
154         props.put("SVCPDC", "*ECOM/CA*YTR/RCG*");
155         props.put("CLASS", "*1*2*3*4*5*");
156         props.put("USI", "*USISAMPLEUSI000*");
157         
158         props.put("JMSXUserID", "testUser");
159         fields.put("JMSDeliveryMode", "PERSISTENT");
160         fields.put("JMSPriority", new Integer JavaDoc(7));
161         fields.put("JMSTimestamp", new Long JavaDoc(System.currentTimeMillis()));
162         fields.put("JMSCorrelationID", "123456789");
163         fields.put("JMSType", "order");
164         fields.put("JMSMessageID", "messageid_" + System.currentTimeMillis());
165         
166 // System.err.println("\nProperties=" + props + "\n");
167
// System.err.println("\nFields=" + fields + "\n");
168

169         boolean DEBUG = true;
170         boolean VERBOSE_DEBUG = true;
171         boolean shortCircuit = false;
172 //todo test shortCircuit = true;
173

174         // Simple selector tests
175
String JavaDoc[][] tests = {
176             /* Selector Result */
177             {"color = 'red'", "true"},
178             {"color = 'blue'", "false"},
179             {"color <> 'red'", "false"},
180             {"color <> 'blue'", "true"},
181             {"color in ('red', 'white', 'blue')", "true"},
182             {"color in ('orange', 'white', 'blue')", "false"},
183             {"color not in ('orange', 'white', 'blue')", "true"},
184             {"description like '%hot%'", "true"},
185             {"description not like '%hot%'", "false"},
186             {"color like 'r_d'", "true"},
187             {"color like 'r_d' or color like 'bl_e'", "true"},
188             {"color like 'r_d' and (color like 'b%' or color like '%d')", "true"},
189             {"quantity between 400 and 1000.0", "true"},
190             {"price between 1.10 and 2", "true"},
191             {"price not between 5 and 10e2", "true"},
192             {"price not between 5 and 10e2 and price between 1 and 2", "true"},
193             {"price not between 5 and 10e2 or price between 1 and 2", "true"},
194             {"nullProp is null and price is not null", "true"},
195             {"nullProp is null or price is not null", "true"},
196             {"price is not null", "true"},
197             {"price > 0.75", "true"},
198             {"price < 9.75", "true"},
199             {"price >= 1.50", "true"},
200             {"price <= 1.50", "true"},
201             {"price > 9.75", "false"},
202             {"price >= 9.75", "false"},
203             {"msgnum > 1.75", "true"},
204             {"size > msgnum", "true"},
205             {"size > price", "true"},
206             {"size > price + msgnum", "true"},
207             {"size > price * msgnum", "true"},
208             {"quantity * price > 3.00", "true"},
209             {"JMSXUserID = 'testUser'", "true"},
210             {"JMSMessageID like '%~_%' escape '~'", "true"},
211             {"JMSTimestamp > 4", "true"},
212             {"JMSCorrelationID like '1_34__%9'", "true"},
213             {"JMSType <> 'quote'", "true"},
214             {"JMSPriority > 5", "true"},
215             {"JMSPriority < JMSTimestamp", "true"},
216             {"byteProp = 4", "true"},
217             {"byteProp <> 5.0", "true"},
218             {"shortProp <> 5.0", "true"},
219             {"intProp <> 5.0", "true"},
220             {"byteProp = shortProp", "true"},
221             {"byteProp = floatProp", "true"},
222             {"floatProp = 4.0 ", "true"},
223             {"floatProp * 2 > byteProp", "true"},
224             {"stringProp = '4'", "true"},
225             {"stringProp = 4" , "error"},
226             {"stringProp <> '5'", "true"},
227             {"stringProp <> 5", "error"},
228             {"byteProp <> 4", "false"},
229             {"byteProp = 5.0", "false"},
230             {"shortProp = 5.0", "false"},
231             {"intProp = 5.0", "false"},
232             
233             {"1 + 4 * 5 = 21", "true"},
234             {"1+4*5=21", "true"},
235             {"1 + -4 * 5 = -19", "true"},
236             {"(1 + 4) * +5 = 25", "true"},
237             {"(1 + 4) * -5 = -25", "true"},
238             {"(1 + 4) * 5 = (3 + 2) * 5", "true"},
239             {"1 + (4 * 5) = 21", "true"},
240             {"1 + 4 * 5 = 21", "true"},
241             {"(1 + 4) * 5 = 25", "true"},
242             {"(1 + 4) - 5 = 0 ", "true"},
243             {"2.0 * 4E2 + 5 = 805.0", "true"},
244             {"2.0 * 4E2 + 5 = 805", "true"},
245             {"2.0 = 2.0", "true"},
246             {"1.0+2.0*3.0-4.0/4.0 = 6", "true"},
247             
248             {"price > 0.75 OR color <> 'blue'", "true"},
249             {"(price > 0.75 OR color <> 'blue') AND color <> 'green'", "true"},
250             {" 2 * quantity between msgnum AND msgnum * size", "true"},
251             {"NOT (2 * quantity between msgnum AND msgnum * size)", "false"},
252             
253             {"-price < 0 AND +negIntProp < 0", "true"},
254             {"negIntProp+4 = 0 AND intProp-1 = 3", "true"},
255             {"- intProp + intProp = 0", "true"},
256             {"intProp between -1 and 5", "true"},
257             {"intProp between 1 and 5 AND intProp between -1 and 5", "true"},
258             
259             {"minlong=" + Long.MIN_VALUE + " AND maxlong=" + Long.MAX_VALUE,
260                      "true"},
261             
262             // An expression with a NULL property is always NULL
263
{"unknownProp NOT IN ('foo','jms','test')", "false"},
264                      {"nullProp NOT IN ('foo','jms','test')", "false"},
265                      {"unknownProp NOT LIKE '1_3'", "false"},
266                      {"nullProp NOT LIKE '1_3'", "false"},
267                      
268                      {"0x1d = 29", "true"},
269                      {"0x1D = 29", "true"},
270                      {"035 = 29", "true"},
271                      {"29L = 29", "true"},
272                      {"29l = 29", "true"},
273                      {"18. = 1.8e1", "true"},
274                      {"18. = .18e2", "true"},
275                      {"18.0f = .18e2", "true"},
276                      {"18.0F = .18e2", "true"},
277                      {"18.0d = .18e2", "true"},
278                      {"18.0D = .18e2", "true"},
279                      {".7e4 = 7000.0", "true"},
280                      {" is null nullProp", "true"},
281                      {"NOT is null nullProp", "false"},
282                      {" is null unknownProp", "true"},
283                      {" is not null nullProp", "false"},
284                      {"NOT is not null nullProp", "true"},
285                      {" is not null unknownProp", "false"},
286                      
287                      {"TRUE", "true"},
288                      {"NOT TRUE", "false"},
289                      {"(NOT (NOT (NOT (NOT TRUE))))", "true"},
290                      {"FALSE", "false"},
291                      {"NOT FALSE", "true"},
292                      {"trueProp", "true"},
293                      {"NOT trueProp", "false"},
294                      {"trueProp = TRUE", "true"},
295                      {"trueProp = FALSE", "false"},
296                      {"trueProp <> FALSE", "true"},
297                      {"falseProp", "false"},
298                      {"falseProp = TRUE", "false"},
299                      {"falseProp = FALSE", "true"},
300                      {"falseProp <> TRUE", "true"},
301                      {"NOT falseProp", "true"},
302                      
303                      {"description LIKE '%nuts%' AND color in ('black', 'white') OR color = 'blue'", "false"},
304                      {"description LIKE '%nuts%' OR color in ('black', 'white') OR color = 'blue'", "true"},
305                      
306                      {"Event LIKE '%*Service Change*%' OR Event LIKE '%*Restart*%' AND Region LIKE '%*EA*%' AND Airspace LIKE '%*ARCC*%'", "true"},
307                      
308                      // Make sure we don't have any blatant errors in short circuit code
309
{"color = 'red' OR color <> 'blue' AND color <> 'green'", "true"},
310                      {"color = 'white' OR color <> 'blue' AND color <> 'green'", "true"},
311                      {"color = 'white' OR color <> 'red' AND color <> 'green'", "false"},
312                      {"color = 'red' OR color <> 'blue' AND color <> 'red'", "true"},
313                      {"(color = 'red' OR color <> 'blue') AND color <> 'red'", "false"},
314                      {"(color = 'red' OR color <> 'blue') AND NOT color <> 'red'", "true"},
315                      
316                      {"true OR true OR true OR true", "true"},
317                      {"(true OR true) OR (true OR true)", "true"},
318                      {"true OR false OR true OR false", "true"},
319                      {"false OR false OR false OR true", "true"},
320                      {"true OR false OR false OR false", "true"},
321                      {"false OR false OR false OR false", "false"},
322                      {"false OR false OR false OR true", "true"},
323                      
324                      {"true AND true AND true AND true", "true"},
325                      {"(true AND false) AND (true AND false)", "false"},
326                      {"false AND false AND false AND true", "false"},
327                      {"true AND false AND false AND false", "false"},
328                      {"false AND true AND true AND true", "false"},
329                      {"true AND true AND true AND false", "false"},
330                      
331                      {"true AND false OR true AND true", "true"},
332                      {"true OR false AND true OR false", "true"},
333                      {"(true OR false) AND (true OR false)", "true"},
334                      {"NOT ((true OR false) AND (true OR false))", "false"},
335                      
336                      {"color in ('red', 'white', 'blue'(", "error"},
337                      {"size not between 'red' and 'green'", "error"},
338                      {"+ + + +", "error"},
339                      {"1 2 3 4", "error"},
340                      {"= = = =", "error"},
341                      {"((1 + 2) * 4 = 3", "error"},
342                      {"red red red", "error"},
343                      {"4 >> 1", "error"},
344                      {"color = 'red", "error"},
345                      {"color == 'red'", "error"},
346                      
347                      {"intProp BETWEEN 'foo' and 'test'", "error"},
348                      {"intProp > 'foo'", "error"},
349                      {"color > 'foo'", "error"},
350                      {"unknownProp > 'foo'", "error"},
351                      {"unknownProp < 'foo'", "error"},
352                      {"unknownProp =< 'foo'", "error"},
353                      {"unknownProp >= 'foo'", "error"},
354                      {"intProp >= 'foo'", "error"},
355                      {"intProp < 'foo'", "error"},
356                      {"intProp <= 'foo'", "error"},
357                      {"intProp between 'foo' and 'bar'", "error"},
358                      {"color between 1 and 7", "error"},
359                      {"'color' between 1 and 7", "error"},
360                      {"7 in ('red', 'blue')", "error"},
361                      {"intProp in ('red', 'blue')", "error"},
362                      {"7 not in ('red', 'blue')", "error"},
363                      {"intProp not in ('red', 'blue')", "error"},
364                      {"NULL = 0", "error"},
365                      {"=color 'red'", "error"},
366                      {"size like '7'", "error"},
367                      {"size not like '7'", "error"},
368                      {"4 = 'red'", "error"},
369                      {"4 <> 'red'", "error"},
370                      {"'red' <> 4", "error"},
371                      {"'red' = 4", "error"},
372                      {"intProp = 'red'", "error"},
373                      {"intProp <> 'red'", "error"},
374                      {"'red' = intProp", "error"},
375                      {"'red' <> intProp", "error"},
376                      {"msgnumStr = 5", "error"},
377                      {"msgnum = '5'", "error"},
378         };
379         
380         
381         int failCnt = 0;
382         for (int n = 0; n < tests.length; n++)
383         {
384             
385             Selector selector = null;
386             String JavaDoc expected = tests[n][1];
387             String JavaDoc actual = null;
388             String JavaDoc result;
389             HashMap JavaDoc _props = null;
390             HashMap JavaDoc _fields = null;
391             
392             try
393             {
394                 selector = Selector.compile(tests[n][0]);
395                 // Test optimization
396
if (selector.usesProperties())
397                 {
398                     _props = props;
399                 }
400                 else
401                 {
402                     _props = null;
403                 }
404                 if (selector.usesFields())
405                 {
406                     _fields = fields;
407                 }
408                 else
409                 {
410                     _fields = null;
411                 }
412                 if (selector.match(_props, _fields))
413                 {
414                     actual = "true";
415                 }
416                 else
417                 {
418                     actual = "false";
419                 }
420             }
421             catch (SelectorFormatException e)
422             {
423                 actual = "error";
424                 if (!actual.equals(expected))
425                 {
426                     System.err.println(e);
427                 }
428             }
429             
430             if (actual.equals(expected))
431             {
432             }
433             else
434             {
435                 result = "***** FAIL";
436                 failCnt++;
437                 System.err.println(result + " " + tests[n][0] +
438                     " : expected=" + expected + " actual=" + actual);
439             }
440             
441         }
442         
443         
444         if (failCnt > 0)
445         {
446             System.err.println(tests.length + " tests: " + (tests.length - failCnt) + " passed " + failCnt + " failed ");
447             fail(""+failCnt+" tests failed.");
448         }
449     }
450     
451     public static TestSuite smokeTests()
452     {
453         TestSuite suite = new TestSuite();
454         
455         suite.addTest(new SelectorTest("testOriginalSelector"));
456         suite.addTest(new SelectorTest("testOriginalRegularExpression"));
457         
458         return suite;
459     }
460     
461 }
462
Popular Tags