001: package org.drools.base;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.lang.reflect.Method;
020: import java.math.BigDecimal;
021: import java.math.BigInteger;
022: import java.text.SimpleDateFormat;
023: import java.util.ArrayList;
024: import java.util.Arrays;
025: import java.util.Collection;
026: import java.util.Date;
027: import java.util.List;
028: import java.util.Locale;
029:
030: import junit.framework.TestCase;
031:
032: import org.drools.base.evaluators.Operator;
033: import org.drools.common.InternalWorkingMemory;
034: import org.drools.rule.Declaration;
035: import org.drools.rule.VariableRestriction.BooleanVariableContextEntry;
036: import org.drools.rule.VariableRestriction.CharVariableContextEntry;
037: import org.drools.rule.VariableRestriction.DoubleVariableContextEntry;
038: import org.drools.rule.VariableRestriction.LongVariableContextEntry;
039: import org.drools.rule.VariableRestriction.ObjectVariableContextEntry;
040: import org.drools.rule.VariableRestriction.VariableContextEntry;
041: import org.drools.spi.Evaluator;
042: import org.drools.spi.Extractor;
043: import org.drools.spi.FieldExtractor;
044: import org.drools.spi.FieldValue;
045:
046: /**
047: * Some test coverage goodness for the evaluators.
048: * Evaluator concrete instances are inside the factory at this time.
049: * @author Michael Neale
050: */
051: public class EvaluatorFactoryTest extends TestCase {
052:
053: public void testObject() {
054:
055: final List list = new ArrayList();
056: list.add("foo");
057: list.add(null);
058:
059: Collection col = Arrays.asList(new String[] { "foo", "bar",
060: null });
061:
062: final Object[][] data = {
063: { "foo", "==", "bar", Boolean.FALSE },
064: { "foo", "==", "foo", Boolean.TRUE },
065: { "foo", "!=", "bar", Boolean.TRUE },
066: { list, "contains", "foo", Boolean.TRUE },
067: { list, "contains", "bar", Boolean.FALSE },
068: { list, "contains", null, Boolean.TRUE },
069: { null, "contains", "bar", Boolean.FALSE },
070: { null, "contains", null, Boolean.FALSE },
071: { list, "==", null, Boolean.FALSE },
072: { list, "!=", null, Boolean.TRUE },
073: { null, "==", null, Boolean.TRUE },
074: { null, "==", list, Boolean.FALSE },
075: { null, "!=", list, Boolean.TRUE },
076: { null, "<", new Integer(43), Boolean.FALSE },
077: { null, ">=", new Integer(-10), Boolean.FALSE },
078: { null, ">", new Integer(-10), Boolean.FALSE },
079: { null, "<=", new Integer(42), Boolean.FALSE },
080: { new BigDecimal("42.42"), "<", new BigDecimal("43"),
081: Boolean.TRUE },
082: { new BigDecimal("42.42"), ">", new BigDecimal("43"),
083: Boolean.FALSE },
084: { new BigDecimal("42.42"), "<=",
085: new BigDecimal("42.42"), Boolean.TRUE },
086: { new BigInteger("42"), ">=", new BigInteger("43"),
087: Boolean.FALSE },
088: { new BigInteger("42"), ">=", new BigInteger("43"),
089: Boolean.FALSE },
090: { list, "excludes", "baz", Boolean.TRUE },
091: { list, "excludes", "foo", Boolean.FALSE },
092: { "foo", "memberOf", col, Boolean.TRUE },
093: { "xyz", "memberOf", col, Boolean.FALSE },
094: { null, "memberOf", col, Boolean.TRUE },
095: { "foo", "memberOf", null, Boolean.FALSE },
096: { "foo", "not memberOf", col, Boolean.FALSE },
097: { "xyz", "not memberOf", col, Boolean.TRUE },
098: { null, "not memberOf", col, Boolean.FALSE },
099: { "foo", "not memberOf", null, Boolean.FALSE }
100:
101: };
102:
103: runEvaluatorTest(data, ValueType.OBJECT_TYPE);
104:
105: }
106:
107: public void testArrayType() {
108:
109: final Object[] field = new Object[] { "foo", "bar" };
110:
111: final Object[][] data = {
112: { field, "==", new Object[] { "foo" }, Boolean.FALSE },
113: { field, "==", field, Boolean.TRUE },
114: { field, "!=", new Object[] { "foo" }, Boolean.TRUE },
115: { field, "contains", "foo", Boolean.TRUE },
116: { field, "contains", "xyz", Boolean.FALSE },
117: { field, "contains", null, Boolean.FALSE },
118: { null, "contains", "foo", Boolean.FALSE },
119: { field, "excludes", "foo", Boolean.FALSE },
120: { field, "excludes", "xyz", Boolean.TRUE },
121: { field, "excludes", null, Boolean.TRUE },
122: { null, "excludes", "foo", Boolean.FALSE },
123: { field, "!=", null, Boolean.TRUE },
124: { field, "==", null, Boolean.FALSE },
125: { null, "==", field, Boolean.FALSE },
126: { null, "==", null, Boolean.TRUE },
127: { null, "!=", field, Boolean.TRUE },
128: { null, "!=", null, Boolean.FALSE },
129: { "foo", "memberOf", field, Boolean.TRUE },
130: { "xyz", "memberOf", field, Boolean.FALSE },
131: { null, "memberOf", field, Boolean.FALSE },
132: { "foo", "memberOf", null, Boolean.FALSE },
133: { "foo", "not memberOf", field, Boolean.FALSE },
134: { "xyz", "not memberOf", field, Boolean.TRUE },
135: { null, "not memberOf", field, Boolean.TRUE },
136: { "foo", "not memberOf", null, Boolean.FALSE } };
137:
138: runEvaluatorTest(data, ValueType.ARRAY_TYPE);
139:
140: }
141:
142: public void testString() {
143:
144: Collection col = Arrays.asList(new String[] { "foo", "bar",
145: null });
146:
147: final Object[][] data = {
148: { "foo", "==", "bar", Boolean.FALSE },
149: { "foo", "==", "foo", Boolean.TRUE },
150: { "foo", "!=", "bar", Boolean.TRUE },
151: { "something foo", "matches", ".*foo", Boolean.TRUE },
152: { "foo", "matches", ".*foo", Boolean.TRUE },
153: { "foo", "matches", "bar", Boolean.FALSE },
154: { null, "matches", ".*foo", Boolean.FALSE },
155: { "foo", "==", null, Boolean.FALSE },
156: { "foo", "!=", null, Boolean.TRUE },
157: { null, "==", null, Boolean.TRUE },
158: { "foo", "!=", null, Boolean.TRUE },
159: { null, "!=", "foo", Boolean.TRUE },
160: { null, "!=", null, Boolean.FALSE },
161: { "foo", "memberOf", col, Boolean.TRUE },
162: { "xyz", "memberOf", col, Boolean.FALSE },
163: { null, "memberOf", col, Boolean.TRUE },
164: { "foo", "memberOf", null, Boolean.FALSE },
165: { "foo", "not memberOf", col, Boolean.FALSE },
166: { "xyz", "not memberOf", col, Boolean.TRUE },
167: { null, "not memberOf", col, Boolean.FALSE },
168: { "foo", "not memberOf", null, Boolean.FALSE } };
169:
170: runEvaluatorTest(data, ValueType.STRING_TYPE);
171:
172: }
173:
174: public void testInteger() {
175:
176: Collection col = new ArrayList();
177: col.add(new Integer(42));
178: col.add(new Integer(45));
179:
180: final Object[][] data = {
181: { new Integer(42), "==", new Integer(42), Boolean.TRUE },
182: { new Integer(42), "<", new Integer(43), Boolean.TRUE },
183: { new Integer(42), ">=", new Integer(41), Boolean.TRUE },
184: { new Integer(42), "!=", new Integer(41), Boolean.TRUE },
185: { new Integer(42), ">", new Integer(41), Boolean.TRUE },
186: { new Integer(42), "<=", new Integer(42), Boolean.TRUE },
187: { new Integer(42), ">", new Integer(100), Boolean.FALSE },
188: { new Integer(42), "!=", null, Boolean.TRUE },
189: { new Integer(42), "==", null, Boolean.FALSE },
190: { null, "==", null, Boolean.TRUE },
191: { null, "!=", null, Boolean.FALSE },
192: { null, "!=", new Integer(42), Boolean.TRUE },
193: { null, "==", new Integer(42), Boolean.FALSE },
194: { null, "<", new Integer(43), Boolean.FALSE },
195: { null, ">=", new Integer(-10), Boolean.FALSE },
196: { null, ">", new Integer(-10), Boolean.FALSE },
197: { null, "<=", new Integer(42), Boolean.FALSE },
198: { new Integer(42), "memberOf", col, Boolean.TRUE },
199: { new Integer(43), "memberOf", col, Boolean.FALSE },
200: { null, "memberOf", col, Boolean.FALSE },
201: { new Integer(42), "memberOf", null, Boolean.FALSE },
202: { new Integer(42), "not memberOf", col, Boolean.FALSE },
203: { new Integer(43), "not memberOf", col, Boolean.TRUE },
204: { null, "not memberOf", col, Boolean.TRUE },
205: { new Integer(42), "not memberOf", null, Boolean.FALSE } };
206:
207: runEvaluatorTest(data, ValueType.PINTEGER_TYPE);
208:
209: }
210:
211: public void testBigDecimal() {
212:
213: final ValueType type = ValueType
214: .determineValueType(BigDecimal.class);
215: assertSame(type, ValueType.BIG_DECIMAL_TYPE);
216:
217: Collection col = new ArrayList();
218: col.add(new BigDecimal(42));
219: col.add(new BigDecimal(45));
220:
221: final Object[][] data = {
222: { new BigDecimal(42), "==", new BigDecimal(42),
223: Boolean.TRUE },
224: { new BigDecimal(42), "<", new BigDecimal(43),
225: Boolean.TRUE },
226: { new BigDecimal(42), ">=", new BigDecimal(41),
227: Boolean.TRUE },
228: { new BigDecimal(42), "!=", new BigDecimal(41),
229: Boolean.TRUE },
230: { new BigDecimal(42), ">", new BigDecimal(41),
231: Boolean.TRUE },
232: { new BigDecimal(42), "<=", new BigDecimal(42),
233: Boolean.TRUE },
234: { new BigDecimal(42), ">", new BigDecimal(100),
235: Boolean.FALSE },
236: { new BigDecimal(42), "==", null, Boolean.FALSE },
237: { new BigDecimal(42), "!=", null, Boolean.TRUE },
238: { null, "==", new BigDecimal(42), Boolean.FALSE },
239: { null, "!=", new BigDecimal(42), Boolean.TRUE },
240: { null, "<", new BigDecimal(43), Boolean.FALSE },
241: { null, ">=", new BigDecimal(-10), Boolean.FALSE },
242: { null, ">", new BigDecimal(-10), Boolean.FALSE },
243: { null, "<=", new BigDecimal(42), Boolean.FALSE },
244: { new BigDecimal(42), "memberOf", col, Boolean.TRUE },
245: { new BigDecimal(43), "memberOf", col, Boolean.FALSE },
246: { null, "memberOf", col, Boolean.FALSE },
247: { new BigDecimal(42), "memberOf", null, Boolean.FALSE },
248: { new BigDecimal(42), "not memberOf", col,
249: Boolean.FALSE },
250: { new BigDecimal(43), "not memberOf", col, Boolean.TRUE },
251: { null, "not memberOf", col, Boolean.TRUE },
252: { new BigDecimal(42), "not memberOf", null,
253: Boolean.FALSE } };
254:
255: runEvaluatorTest(data, ValueType.BIG_DECIMAL_TYPE);
256:
257: }
258:
259: public void testBigInteger() {
260:
261: final ValueType type = ValueType
262: .determineValueType(BigInteger.class);
263: assertSame(type, ValueType.BIG_INTEGER_TYPE);
264:
265: Collection col = new ArrayList();
266: col.add(new BigInteger("42"));
267: col.add(new BigInteger("45"));
268:
269: final Object[][] data = {
270: { new BigInteger("42"), "==", new BigInteger("42"),
271: Boolean.TRUE },
272: { new BigInteger("42"), "<", new BigInteger("43"),
273: Boolean.TRUE },
274: { new BigInteger("42"), ">=", new BigInteger("41"),
275: Boolean.TRUE },
276: { new BigInteger("42"), "!=", new BigInteger("41"),
277: Boolean.TRUE },
278: { new BigInteger("42"), ">", new BigInteger("41"),
279: Boolean.TRUE },
280: { new BigInteger("42"), "<=", new BigInteger("42"),
281: Boolean.TRUE },
282: { new BigInteger("42"), ">", new BigInteger("100"),
283: Boolean.FALSE },
284: { new BigInteger("42"), "==", null, Boolean.FALSE },
285: { new BigInteger("42"), "!=", null, Boolean.TRUE },
286: { null, "==", new BigInteger("42"), Boolean.FALSE },
287: { null, "!=", new BigInteger("42"), Boolean.TRUE },
288: { null, "<", new BigInteger("43"), Boolean.FALSE },
289: { null, ">=", new BigInteger("-10"), Boolean.FALSE },
290: { null, ">", new BigInteger("-10"), Boolean.FALSE },
291: { null, "<=", new BigInteger("42"), Boolean.FALSE },
292: { new BigInteger("42"), "memberOf", col, Boolean.TRUE },
293: { new BigInteger("43"), "memberOf", col, Boolean.FALSE },
294: { null, "memberOf", col, Boolean.FALSE },
295: { new BigInteger("42"), "memberOf", null, Boolean.FALSE },
296: { new BigInteger("42"), "not memberOf", col,
297: Boolean.FALSE },
298: { new BigInteger("43"), "not memberOf", col,
299: Boolean.TRUE },
300: { null, "not memberOf", col, Boolean.TRUE },
301: { new BigInteger("42"), "not memberOf", null,
302: Boolean.FALSE } };
303:
304: runEvaluatorTest(data, ValueType.BIG_INTEGER_TYPE);
305:
306: }
307:
308: public void testShort() {
309:
310: Collection col = new ArrayList();
311: col.add(new Short((short) 42));
312: col.add(new Short((short) 45));
313:
314: //Test data: Obj1, Operand, Obj2
315: final Object[][] data = {
316: { new Short((short) 42), "==", new Short((short) 42),
317: Boolean.TRUE },
318: { new Short((short) 42), "<", new Short((short) 43),
319: Boolean.TRUE },
320: { new Short((short) 42), ">=", new Short((short) 41),
321: Boolean.TRUE },
322: { new Short((short) 42), "!=", new Short((short) 41),
323: Boolean.TRUE },
324: { new Short((short) 42), "==", null, Boolean.FALSE },
325: { null, "==", null, Boolean.TRUE },
326: { null, "!=", null, Boolean.FALSE },
327: { null, "!=", new Short((short) 42), Boolean.TRUE },
328: { null, "==", new Short((short) 42), Boolean.FALSE },
329: { null, "<", new Short((short) 43), Boolean.FALSE },
330: { null, ">=", new Short((short) -10), Boolean.FALSE },
331: { null, ">", new Short((short) -10), Boolean.FALSE },
332: { null, "<=", new Short((short) 42), Boolean.FALSE },
333: { new Short((short) 42), "memberOf", col, Boolean.TRUE },
334: { new Short((short) 43), "memberOf", col, Boolean.FALSE },
335: { null, "memberOf", col, Boolean.FALSE },
336: { new Short((short) 42), "memberOf", null,
337: Boolean.FALSE },
338: { new Short((short) 42), "not memberOf", col,
339: Boolean.FALSE },
340: { new Short((short) 43), "not memberOf", col,
341: Boolean.TRUE },
342: { null, "not memberOf", col, Boolean.TRUE },
343: { new Short((short) 42), "not memberOf", null,
344: Boolean.FALSE } };
345:
346: runEvaluatorTest(data, ValueType.PSHORT_TYPE);
347: }
348:
349: public void testBoolean() {
350:
351: Collection col = new ArrayList();
352: col.add(new Boolean(true));
353: col.add(new Boolean(true));
354:
355: //Test data: Obj1, Operand, Obj2
356: final Object[][] data = {
357: { new Boolean(true), "==", new Boolean(true),
358: Boolean.TRUE },
359: { new Boolean(false), "!=", new Boolean(true),
360: Boolean.TRUE },
361: { new Boolean(true), "==", new Boolean(false),
362: Boolean.FALSE },
363: { new Boolean(true), "!=", new Boolean(false),
364: Boolean.TRUE },
365: { new Boolean(true), "==", null, Boolean.FALSE },
366: { null, "==", null, Boolean.TRUE },
367: { null, "!=", null, Boolean.FALSE },
368: { null, "!=", new Boolean(true), Boolean.TRUE },
369: { null, "==", new Boolean(true), Boolean.FALSE },
370: { new Boolean(true), "memberOf", col, Boolean.TRUE },
371: { new Boolean(false), "memberOf", col, Boolean.FALSE },
372: { null, "memberOf", col, Boolean.FALSE },
373: { new Boolean(true), "memberOf", null, Boolean.FALSE },
374: { new Boolean(true), "not memberOf", col, Boolean.FALSE },
375: { new Boolean(false), "not memberOf", col, Boolean.TRUE },
376: { null, "not memberOf", col, Boolean.TRUE },
377: { new Boolean(true), "not memberOf", null,
378: Boolean.FALSE } };
379:
380: runEvaluatorTest(data, ValueType.PBOOLEAN_TYPE);
381: }
382:
383: public void testDouble() {
384: Collection col = new ArrayList();
385: col.add(new Double(42));
386: col.add(new Double(45));
387:
388: final Object[][] data = {
389: { new Double(42), "==", new Double(42), Boolean.TRUE },
390: { new Double(42), "<", new Double(43), Boolean.TRUE },
391: { new Double(42), ">=", new Double(41), Boolean.TRUE },
392: { new Double(42), "!=", new Double(41), Boolean.TRUE },
393: { new Double(42), ">", new Double(41), Boolean.TRUE },
394: { new Double(42), ">=", new Double(41), Boolean.TRUE },
395: { new Double(42), ">=", new Double(42), Boolean.TRUE },
396: { new Double(42), ">=", new Double(100), Boolean.FALSE },
397: { new Double(42), "<", new Double(1), Boolean.FALSE },
398: { new Double(42), "==", null, Boolean.FALSE },
399: { null, "==", null, Boolean.TRUE },
400: { null, "!=", null, Boolean.FALSE },
401: { null, "!=", new Double(42), Boolean.TRUE },
402: { null, "==", new Double(42), Boolean.FALSE },
403: { null, "<", new Double(43), Boolean.FALSE },
404: { null, ">=", new Double(-10), Boolean.FALSE },
405: { null, ">", new Double(-10), Boolean.FALSE },
406: { null, "<=", new Double(42), Boolean.FALSE },
407: { new Double(42), "memberOf", col, Boolean.TRUE },
408: { new Double(43), "memberOf", col, Boolean.FALSE },
409: { null, "memberOf", col, Boolean.FALSE },
410: { new Double(42), "memberOf", null, Boolean.FALSE },
411: { new Double(42), "not memberOf", col, Boolean.FALSE },
412: { new Double(43), "not memberOf", col, Boolean.TRUE },
413: { null, "not memberOf", col, Boolean.TRUE },
414: { new Double(42), "not memberOf", null, Boolean.FALSE } };
415:
416: runEvaluatorTest(data, ValueType.PDOUBLE_TYPE);
417: }
418:
419: public void testFloat() {
420: Collection col = new ArrayList();
421: col.add(new Float(42));
422: col.add(new Float(45));
423:
424: final Object[][] data = {
425: { new Float(42), "==", new Float(42), Boolean.TRUE },
426: { new Float(42), "<", new Float(43), Boolean.TRUE },
427: { new Float(42), ">=", new Float(41), Boolean.TRUE },
428: { new Float(42), "!=", new Float(41), Boolean.TRUE },
429: { new Float(42), ">", new Float(41), Boolean.TRUE },
430: { new Float(42), ">=", new Float(41), Boolean.TRUE },
431: { new Float(42), ">=", new Float(42), Boolean.TRUE },
432: { new Float(42), ">=", new Float(100), Boolean.FALSE },
433: { new Float(42), "<", new Float(1), Boolean.FALSE },
434: { new Float(42), "==", null, Boolean.FALSE },
435: { null, "==", null, Boolean.TRUE },
436: { null, "!=", null, Boolean.FALSE },
437: { null, "!=", new Float(42), Boolean.TRUE },
438: { null, "==", new Float(42), Boolean.FALSE },
439: { null, "<", new Float(43), Boolean.FALSE },
440: { null, ">=", new Float(-10), Boolean.FALSE },
441: { null, ">", new Float(-10), Boolean.FALSE },
442: { null, "<=", new Float(42), Boolean.FALSE },
443: { new Float(42), "memberOf", col, Boolean.TRUE },
444: { new Float(43), "memberOf", col, Boolean.FALSE },
445: { null, "memberOf", col, Boolean.FALSE },
446: { new Float(42), "memberOf", null, Boolean.FALSE },
447: { new Float(42), "not memberOf", col, Boolean.FALSE },
448: { new Float(43), "not memberOf", col, Boolean.TRUE },
449: { null, "not memberOf", col, Boolean.TRUE },
450: { new Float(42), "not memberOf", null, Boolean.FALSE } };
451:
452: runEvaluatorTest(data, ValueType.PFLOAT_TYPE);
453: }
454:
455: public void testLong() {
456: Collection col = new ArrayList();
457: col.add(new Long(42));
458: col.add(new Long(45));
459:
460: final Object[][] data = {
461: { new Long(42), "==", new Long(42), Boolean.TRUE },
462: { new Long(42), "<", new Long(43), Boolean.TRUE },
463: { new Long(42), ">=", new Long(41), Boolean.TRUE },
464: { new Long(42), "!=", new Long(41), Boolean.TRUE },
465: { new Long(42), ">", new Long(41), Boolean.TRUE },
466: { new Long(42), ">=", new Long(41), Boolean.TRUE },
467: { new Long(42), ">=", new Long(42), Boolean.TRUE },
468: { new Long(42), ">=", new Long(100), Boolean.FALSE },
469: { new Long(42), "<", new Long(1), Boolean.FALSE },
470: { new Long(42), "==", null, Boolean.FALSE },
471: { null, "==", null, Boolean.TRUE },
472: { null, "!=", null, Boolean.FALSE },
473: { null, "!=", new Long(42), Boolean.TRUE },
474: { null, "==", new Long(42), Boolean.FALSE },
475: { null, "<", new Long(43), Boolean.FALSE },
476: { null, ">=", new Long(-10), Boolean.FALSE },
477: { null, ">", new Long(-10), Boolean.FALSE },
478: { null, "<=", new Long(42), Boolean.FALSE },
479: { new Long(42), "memberOf", col, Boolean.TRUE },
480: { new Long(43), "memberOf", col, Boolean.FALSE },
481: { null, "memberOf", col, Boolean.FALSE },
482: { new Long(42), "memberOf", null, Boolean.FALSE },
483: { new Long(42), "not memberOf", col, Boolean.FALSE },
484: { new Long(43), "not memberOf", col, Boolean.TRUE },
485: { null, "not memberOf", col, Boolean.TRUE },
486: { new Long(42), "not memberOf", null, Boolean.FALSE } };
487:
488: runEvaluatorTest(data, ValueType.PLONG_TYPE);
489: }
490:
491: public void testCharacter() {
492: Collection col = new ArrayList();
493: col.add(new Character('a'));
494: col.add(new Character('b'));
495:
496: final Object[][] data = {
497: { new Character('a'), "==", new Character('a'),
498: Boolean.TRUE },
499: { new Character('a'), "<", new Character('b'),
500: Boolean.TRUE },
501: { new Character('a'), ">=", new Character('a'),
502: Boolean.TRUE },
503: { new Character('a'), "!=", new Character('Z'),
504: Boolean.TRUE },
505: { new Character('z'), ">", new Character('a'),
506: Boolean.TRUE },
507: { new Character('z'), ">=", new Character('z'),
508: Boolean.TRUE },
509: { new Character('z'), ">=", new Character('a'),
510: Boolean.TRUE },
511: { new Character('a'), ">=", new Character('z'),
512: Boolean.FALSE },
513: { new Character('z'), "<", new Character('a'),
514: Boolean.FALSE },
515: { new Character('z'), "==", null, Boolean.FALSE },
516: { null, "==", null, Boolean.TRUE },
517: { null, "!=", null, Boolean.FALSE },
518: { null, "!=", new Character('z'), Boolean.TRUE },
519: { null, "==", new Character('z'), Boolean.FALSE },
520: { null, "<", new Character('a'), Boolean.FALSE },
521: { null, ">=", new Character('\0'), Boolean.FALSE },
522: { null, ">", new Character('\0'), Boolean.FALSE },
523: { null, "<=", new Character('a'), Boolean.FALSE },
524: { new Character('a'), "memberOf", col, Boolean.TRUE },
525: { new Character('z'), "memberOf", col, Boolean.FALSE },
526: { null, "memberOf", col, Boolean.FALSE },
527: { new Character('a'), "memberOf", null, Boolean.FALSE },
528: { new Character('a'), "not memberOf", col,
529: Boolean.FALSE },
530: { new Character('z'), "not memberOf", col, Boolean.TRUE },
531: { null, "not memberOf", col, Boolean.TRUE },
532: { new Character('a'), "not memberOf", null,
533: Boolean.FALSE } };
534: runEvaluatorTest(data, ValueType.PCHAR_TYPE);
535: }
536:
537: public void testDate() throws Exception {
538:
539: final SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy",
540: Locale.ENGLISH);
541: Collection col = new ArrayList();
542: col.add(df.parse("10-Jul-1974"));
543: col.add(df.parse("11-Jul-1974"));
544:
545: //note that strings are also allowed on the right
546: final Object[][] data = {
547: { df.parse("10-Jul-1974"), "==",
548: df.parse("10-Jul-1974"), Boolean.TRUE },
549: { df.parse("10-Jul-1974"), "<",
550: df.parse("11-Jul-1974"), Boolean.TRUE },
551: { df.parse("10-Jul-1974"), ">=",
552: df.parse("10-Jul-1974"), Boolean.TRUE },
553: { df.parse("10-Jul-1974"), "!=",
554: df.parse("11-Jul-1974"), Boolean.TRUE },
555: { df.parse("10-Jul-2000"), ">",
556: df.parse("10-Jul-1974"), Boolean.TRUE },
557: { df.parse("10-Jul-1974"), ">=",
558: df.parse("10-Jul-1974"), Boolean.TRUE },
559: { df.parse("11-Jul-1974"), ">=",
560: df.parse("10-Jul-1974"), Boolean.TRUE },
561: { df.parse("10-Jul-1974"), ">=",
562: df.parse("11-Jul-1974"), Boolean.FALSE },
563: { df.parse("10-Jul-2000"), "<",
564: df.parse("10-Jul-1974"), Boolean.FALSE },
565: { df.parse("10-Jul-1974"), "<",
566: df.parse("11-Jul-1974"), Boolean.TRUE },
567: { df.parse("10-Jul-1974"), "==", null, Boolean.FALSE },
568: { df.parse("10-Jul-1974"), "!=", null, Boolean.TRUE },
569: { null, "==", null, Boolean.TRUE },
570: { null, "==", df.parse("10-Jul-1974"), Boolean.FALSE },
571: { null, "!=", null, Boolean.FALSE },
572: { null, "!=", df.parse("10-Jul-1974"), Boolean.TRUE },
573: { null, "<", df.parse("10-Jul-1974"), Boolean.FALSE },
574: { null, ">=", new Date(0), Boolean.FALSE },
575: { null, ">", new Date(0), Boolean.FALSE },
576: { null, "<=", df.parse("10-Jul-1974"), Boolean.FALSE },
577: { df.parse("10-Jul-1974"), "memberOf", col,
578: Boolean.TRUE },
579: { df.parse("15-Jul-1974"), "memberOf", col,
580: Boolean.FALSE },
581: { null, "memberOf", col, Boolean.FALSE },
582: { df.parse("10-Jul-1974"), "memberOf", null,
583: Boolean.FALSE },
584: { df.parse("10-Jul-1974"), "not memberOf", col,
585: Boolean.FALSE },
586: { df.parse("15-Jul-1974"), "not memberOf", col,
587: Boolean.TRUE },
588: { null, "not memberOf", col, Boolean.TRUE },
589: { df.parse("10-Jul-1974"), "not memberOf", null,
590: Boolean.FALSE } };
591: runEvaluatorTest(data, ValueType.DATE_TYPE);
592: }
593:
594: public void testByte() {
595: Collection col = new ArrayList();
596: col.add(new Byte("1"));
597: col.add(new Byte("2"));
598:
599: final Object[][] data = {
600: { new Byte("1"), "==", new Byte("1"), Boolean.TRUE },
601: { new Byte("1"), "==", new Byte("2"), Boolean.FALSE },
602: { new Byte("1"), "!=", new Byte("2"), Boolean.TRUE },
603: { new Byte("1"), "!=", new Byte("1"), Boolean.FALSE },
604: { new Byte("1"), "<=", new Byte("1"), Boolean.TRUE },
605: { new Byte("1"), "==", null, Boolean.FALSE },
606: { new Byte("1"), "<", new Byte("2"), Boolean.TRUE },
607: { new Byte("2"), ">=", new Byte("1"), Boolean.TRUE },
608: { new Byte("2"), ">", new Byte("1"), Boolean.TRUE },
609: { new Byte("1"), "<=", new Byte("2"), Boolean.TRUE },
610: { null, "==", null, Boolean.TRUE },
611: { null, "!=", null, Boolean.FALSE },
612: { null, "!=", new Byte("1"), Boolean.TRUE },
613: { null, "==", new Byte("1"), Boolean.FALSE },
614: { null, "<", new Byte(Byte.MAX_VALUE), Boolean.FALSE },
615: { null, ">=", new Byte(Byte.MIN_VALUE), Boolean.FALSE },
616: { null, ">", new Byte(Byte.MIN_VALUE), Boolean.FALSE },
617: { null, "<=", new Byte(Byte.MAX_VALUE), Boolean.FALSE },
618: { new Byte("1"), "memberOf", col, Boolean.TRUE },
619: { new Byte("3"), "memberOf", col, Boolean.FALSE },
620: { null, "memberOf", col, Boolean.FALSE },
621: { new Byte("1"), "memberOf", null, Boolean.FALSE },
622: { new Byte("1"), "not memberOf", col, Boolean.FALSE },
623: { new Byte("3"), "not memberOf", col, Boolean.TRUE },
624: { null, "not memberOf", col, Boolean.TRUE },
625: { new Byte("1"), "not memberOf", null, Boolean.FALSE } };
626: runEvaluatorTest(data, ValueType.PBYTE_TYPE);
627:
628: }
629:
630: /**
631: * Test utility to play the data through the evaluators.
632: * @param data The data to try out : Array of {arg1, operator, arg2}
633: * @param valueType The Evaluator.**_TYPE to test
634: */
635: private void runEvaluatorTest(final Object[][] data,
636: final ValueType valueType) {
637: final Extractor extractor = new MockExtractor();
638: for (int i = 0; i < data.length; i++) {
639: final Object[] row = data[i];
640: final Evaluator evaluator = valueType.getEvaluator(Operator
641: .determineOperator((String) row[1]));
642: checkEvaluatorMethodWithFieldValue(valueType, extractor,
643: row, evaluator);
644: checkEvaluatorMethodCachedRight(valueType, extractor, row,
645: evaluator);
646: checkEvaluatorMethodCachedLeft(valueType, extractor, row,
647: evaluator);
648: checkEvaluatorMethodWith2Extractors(valueType, extractor,
649: row, evaluator);
650:
651: assertEquals(valueType, evaluator.getValueType());
652:
653: }
654: }
655:
656: /**
657: * @param valueType
658: * @param extractor
659: * @param row
660: * @param evaluator
661: */
662: private void checkEvaluatorMethodWithFieldValue(
663: final ValueType valueType, final Extractor extractor,
664: final Object[] row, final Evaluator evaluator) {
665: final FieldValue value = FieldFactory.getFieldValue(row[2]);
666: final boolean result = evaluator.evaluate(null, extractor,
667: row[0], value);
668: final String message = "The evaluator type: [" + valueType
669: + "] with FieldValue incorrectly returned " + result
670: + " for [" + row[0] + " " + row[1] + " " + row[2]
671: + "]. It was asserted to return " + row[3];
672:
673: if (row[3] == Boolean.TRUE) {
674: assertTrue(message, result);
675: } else {
676: assertFalse(message, result);
677: }
678: }
679:
680: /**
681: * @param valueType
682: * @param extractor
683: * @param row
684: * @param evaluator
685: */
686: private void checkEvaluatorMethodCachedRight(
687: final ValueType valueType, final Extractor extractor,
688: final Object[] row, final Evaluator evaluator) {
689: final VariableContextEntry context = this .getContextEntry(
690: evaluator, (FieldExtractor) extractor, valueType, row);
691: final boolean result = evaluator.evaluateCachedRight(null,
692: context, row[2]);
693: final String message = "The evaluator type: [" + valueType
694: + "] with CachedRight incorrectly returned " + result
695: + " for [" + row[0] + " " + row[1] + " " + row[2]
696: + "]. It was asserted to return " + row[3];
697:
698: if (row[3] == Boolean.TRUE) {
699: assertTrue(message, result);
700: } else {
701: assertFalse(message, result);
702: }
703: }
704:
705: /**
706: * @param valueType
707: * @param extractor
708: * @param row
709: * @param evaluator
710: */
711: private void checkEvaluatorMethodCachedLeft(
712: final ValueType valueType, final Extractor extractor,
713: final Object[] row, final Evaluator evaluator) {
714: final VariableContextEntry context = this .getContextEntry(
715: evaluator, (FieldExtractor) extractor, valueType, row);
716: final boolean result = evaluator.evaluateCachedLeft(null,
717: context, row[0]);
718: final String message = "The evaluator type: [" + valueType
719: + "] with CachedLeft incorrectly returned " + result
720: + " for [" + row[0] + " " + row[1] + " " + row[2]
721: + "]. It was asserted to return " + row[3];
722:
723: if (row[3] == Boolean.TRUE) {
724: assertTrue(message, result);
725: } else {
726: assertFalse(message, result);
727: }
728: }
729:
730: /**
731: * @param valueType
732: * @param extractor
733: * @param row
734: * @param evaluator
735: */
736: private void checkEvaluatorMethodWith2Extractors(
737: final ValueType valueType, final Extractor extractor,
738: final Object[] row, final Evaluator evaluator) {
739: final boolean result = evaluator.evaluate(null, extractor,
740: row[0], extractor, row[2]);
741: final String message = "The evaluator type: [" + valueType
742: + "] with 2 extractors incorrectly returned " + result
743: + " for [" + row[0] + " " + row[1] + " " + row[2]
744: + "]. It was asserted to return " + row[3];
745:
746: if (row[3] == Boolean.TRUE) {
747: assertTrue(message, result);
748: } else {
749: assertFalse(message, result);
750: }
751: }
752:
753: private VariableContextEntry getContextEntry(
754: final Evaluator evaluator, final FieldExtractor extractor,
755: final ValueType valueType, final Object[] row) {
756: final Declaration declaration = new Declaration("test",
757: extractor, null);
758:
759: if (Operator.MEMBEROF.equals(evaluator.getOperator())
760: || Operator.NOTMEMBEROF.equals(evaluator.getOperator())) {
761:
762: final ObjectVariableContextEntry context = new ObjectVariableContextEntry(
763: extractor, declaration);
764: if (row[2] == null) {
765: context.leftNull = true;
766: } else {
767: context.left = row[2];
768: }
769:
770: if (row[0] == null) {
771: context.rightNull = true;
772: } else {
773: context.right = row[0];
774: }
775: return context;
776:
777: } else {
778: if (valueType.isIntegerNumber()) {
779: final LongVariableContextEntry context = new LongVariableContextEntry(
780: extractor, declaration);
781:
782: if (row[2] == null) {
783: context.leftNull = true;
784: } else {
785: context.left = ((Number) row[2]).longValue();
786: }
787:
788: if (row[0] == null) {
789: context.rightNull = true;
790: } else {
791: context.right = ((Number) row[0]).longValue();
792: }
793: return context;
794: } else if (valueType.isChar()) {
795: final CharVariableContextEntry context = new CharVariableContextEntry(
796: extractor, declaration);
797:
798: if (row[2] == null) {
799: context.leftNull = true;
800: } else {
801: context.left = ((Character) row[2]).charValue();
802: }
803:
804: if (row[0] == null) {
805: context.rightNull = true;
806: } else {
807: context.right = ((Character) row[0]).charValue();
808: }
809: return context;
810: } else if (valueType.isBoolean()) {
811: final BooleanVariableContextEntry context = new BooleanVariableContextEntry(
812: extractor, declaration);
813:
814: if (row[2] == null) {
815: context.leftNull = true;
816: } else {
817: context.left = ((Boolean) row[2]).booleanValue();
818: }
819:
820: if (row[0] == null) {
821: context.rightNull = true;
822: } else {
823: context.right = ((Boolean) row[0]).booleanValue();
824: }
825: return context;
826: } else if (valueType.isFloatNumber()) {
827: final DoubleVariableContextEntry context = new DoubleVariableContextEntry(
828: extractor, declaration);
829: if (row[2] == null) {
830: context.leftNull = true;
831: } else {
832: context.left = ((Number) row[2]).doubleValue();
833: }
834:
835: if (row[0] == null) {
836: context.rightNull = true;
837: } else {
838: context.right = ((Number) row[0]).doubleValue();
839: }
840: return context;
841: } else {
842: final ObjectVariableContextEntry context = new ObjectVariableContextEntry(
843: extractor, declaration);
844: if (row[2] == null) {
845: context.leftNull = true;
846: } else {
847: context.left = row[2];
848: }
849:
850: if (row[0] == null) {
851: context.rightNull = true;
852: } else {
853: context.right = row[0];
854: }
855: return context;
856: }
857: }
858: }
859:
860: private static class MockExtractor implements FieldExtractor {
861:
862: private static final long serialVersionUID = 400L;
863:
864: public boolean getBooleanValue(
865: InternalWorkingMemory workingMemory, final Object object) {
866: return object != null ? ((Boolean) object).booleanValue()
867: : false;
868: }
869:
870: public byte getByteValue(InternalWorkingMemory workingMemory,
871: final Object object) {
872: return object != null ? ((Number) object).byteValue()
873: : (byte) 0;
874: }
875:
876: public char getCharValue(InternalWorkingMemory workingMemory,
877: final Object object) {
878: return object != null ? ((Character) object).charValue()
879: : '\0';
880: }
881:
882: public double getDoubleValue(
883: InternalWorkingMemory workingMemory, final Object object) {
884: return object != null ? ((Number) object).doubleValue()
885: : 0.0;
886: }
887:
888: public Class getExtractToClass() {
889: return null;
890: }
891:
892: public String getExtractToClassName() {
893: return null;
894: }
895:
896: public float getFloatValue(InternalWorkingMemory workingMemory,
897: final Object object) {
898: return object != null ? ((Number) object).floatValue()
899: : (float) 0.0;
900: }
901:
902: public int getHashCode(InternalWorkingMemory workingMemory,
903: final Object object) {
904: return 0;
905: }
906:
907: public int getIntValue(InternalWorkingMemory workingMemory,
908: final Object object) {
909: return object != null ? ((Number) object).intValue() : 0;
910: }
911:
912: public long getLongValue(InternalWorkingMemory workingMemory,
913: final Object object) {
914: return object != null ? ((Number) object).longValue() : 0;
915: }
916:
917: public Method getNativeReadMethod() {
918: return null;
919: }
920:
921: public short getShortValue(InternalWorkingMemory workingMemory,
922: final Object object) {
923: return object != null ? ((Number) object).shortValue()
924: : (short) 0;
925: }
926:
927: public Object getValue(InternalWorkingMemory workingMemory,
928: final Object object) {
929: return object;
930: }
931:
932: public boolean isNullValue(InternalWorkingMemory workingMemory,
933: final Object object) {
934: return object == null;
935: }
936:
937: public ValueType getValueType() {
938: // TODO Auto-generated method stub
939: return null;
940: }
941:
942: public int getIndex() {
943: return 0;
944: }
945:
946: public boolean isGlobal() {
947: return false;
948: }
949:
950: }
951:
952: // public void testRegexFoo() {
953: // Pattern p = Pattern.compile( ".*foo" );
954: // boolean b;
955: // long start = System.currentTimeMillis();
956: // for (int i = 0; i < 1000000; i++) {
957: // b = ("something foo".matches( ".*foo" ));
958: // }
959: // System.out.println("time: " + (System.currentTimeMillis() - start));
960: //
961: // start = System.currentTimeMillis();
962: // for (int i = 0; i < 1000000; i++) {
963: // Matcher m = p.matcher( "something foo" );
964: // b = m.matches();
965: // }
966: // System.out.println("time: " + (System.currentTimeMillis() - start));
967: // }
968:
969: }
|