001: package org.mvel.tests.main;
002:
003: import junit.framework.TestCase;
004: import org.mvel.MVEL;
005: import org.mvel.tests.main.res.Bar;
006: import org.mvel.tests.main.res.Base;
007: import org.mvel.tests.main.res.DerivedClass;
008: import org.mvel.tests.main.res.Foo;
009:
010: import java.io.Serializable;
011: import java.util.*;
012:
013: public class CompiledUnitTest extends TestCase {
014: protected Foo foo = new Foo();
015: protected Map<String, Object> map = new HashMap<String, Object>();
016: protected Base base = new Base();
017: protected DerivedClass derived = new DerivedClass();
018:
019: public CompiledUnitTest() {
020: foo.setBar(new Bar());
021: map.put("foo", foo);
022: map.put("a", null);
023: map.put("b", null);
024: map.put("c", "cat");
025: map.put("BWAH", "");
026:
027: map.put("misc", new MiscTestClass());
028:
029: map.put("pi", "3.14");
030: map.put("hour", "60");
031: map.put("zero", 0);
032:
033: map.put("testImpl", new ParserUnitTest.TestInterface() {
034:
035: public String getName() {
036: return "FOOBAR!";
037: }
038:
039: public boolean isFoo() {
040: return true;
041: }
042: });
043:
044: map.put("derived", derived);
045: }
046:
047: public void testSingleProperty() {
048: assertEquals(false, parseDirect("fun"));
049: }
050:
051: public void testMethodOnValue() {
052: assertEquals("DOG", parseDirect("foo.bar.name.toUpperCase()"));
053: }
054:
055: public void testSimpleProperty() {
056: assertEquals("dog", parseDirect("foo.bar.name"));
057: }
058:
059: public void testPropertyViaDerivedClass() {
060: assertEquals("cat", parseDirect("derived.data"));
061: }
062:
063: public void testThroughInterface() {
064: assertEquals("FOOBAR!", parseDirect("testImpl.name"));
065: }
066:
067: public void testThroughInterface2() {
068: assertEquals(true, parseDirect("testImpl.foo"));
069: }
070:
071: public void testMapAccessWithMethodCall() {
072: assertEquals("happyBar", parseDirect("funMap['foo'].happy()"));
073: }
074:
075: public void testBooleanOperator() {
076: assertEquals(true, parseDirect("foo.bar.woof == true"));
077: }
078:
079: public void testBooleanOperator2() {
080: assertEquals(false, parseDirect("foo.bar.woof == false"));
081: }
082:
083: public void testTextComparison() {
084: assertEquals(true, parseDirect("foo.bar.name == 'dog'"));
085: }
086:
087: public void testNETextComparison() {
088: assertEquals(true, parseDirect("foo.bar.name != 'foo'"));
089: }
090:
091: public void testChor() {
092: assertEquals("cat", parseDirect("a or b or c"));
093: }
094:
095: public void testChorWithLiteral() {
096: assertEquals("fubar", parseDirect("a or 'fubar'"));
097: }
098:
099: public void testNullCompare() {
100: assertEquals(true, parseDirect("c != null"));
101: }
102:
103: public void testUninitializedInt() {
104: assertEquals(0, parseDirect("sarahl"));
105: }
106:
107: public void testAnd() {
108: assertEquals(
109: true,
110: parseDirect("c != null && foo.bar.name == 'dog' && foo.bar.woof"));
111: }
112:
113: public void testMath() {
114: assertEquals(188.4, parseDirect("pi * hour"));
115: }
116:
117: public void testComplexExpression() {
118: assertEquals(
119: "bar",
120: parseDirect("a = 'foo'; b = 'bar'; c = 'jim'; list = {a,b,c}; list[1]"));
121: }
122:
123: public void testComplexAnd() {
124: assertEquals(
125: true,
126: parseDirect("(pi * hour) > 0 && foo.happy() == 'happyBar'"));
127: }
128:
129: public void testShortPathExpression() {
130: assertEquals(null,
131: parseDirect("3 > 4 && foo.toUC('test'); foo.register"));
132: }
133:
134: public void testShortPathExpression2() {
135: assertEquals(true, parseDirect("4 > 3 || foo.toUC('test')"));
136: }
137:
138: public void testShortPathExpression3() {
139: assertEquals(false,
140: parseDirect("defnull != null && defnull.length() > 0"));
141: }
142:
143: public void testModulus() {
144: assertEquals(38392 % 2, parseDirect("38392 % 2"));
145: }
146:
147: public void testLessThan() {
148: assertEquals(true, parseDirect("pi < 3.15"));
149: assertEquals(true, parseDirect("pi <= 3.14"));
150: assertEquals(false, parseDirect("pi > 3.14"));
151: assertEquals(true, parseDirect("pi >= 3.14"));
152: }
153:
154: public void testMethodAccess() {
155: assertEquals("happyBar", parseDirect("foo.happy()"));
156: }
157:
158: public void testMethodAccess2() {
159: assertEquals("FUBAR", parseDirect("foo.toUC('fubar')"));
160: }
161:
162: public void testMethodAccess3() {
163: assertEquals(true, parseDirect("equalityCheck(c, 'cat')"));
164: }
165:
166: public void testMethodAccess4() {
167: assertEquals(null, parseDirect("readBack(null)"));
168: }
169:
170: public void testMethodAccess5() {
171: assertEquals("nulltest",
172: parseDirect("appendTwoStrings(null, 'test')"));
173: }
174:
175: public void testNegation() {
176: assertEquals(true, parseDirect("!fun && !fun"));
177: }
178:
179: public void testNegation2() {
180: assertEquals(false, parseDirect("fun && !fun"));
181: }
182:
183: public void testNegation3() {
184: assertEquals(true, parseDirect("!(fun && fun)"));
185: }
186:
187: public void testNegation4() {
188: assertEquals(false, parseDirect("(fun && fun)"));
189: }
190:
191: public void testMultiStatement() {
192: assertEquals(true, parseDirect("populate(); barfoo == 'sarah'"));
193: }
194:
195: public void testAssignment() {
196: assertEquals(
197: true,
198: parseDirect("populate(); blahfoo = 'sarah'; blahfoo == 'sarah'"));
199: }
200:
201: public void testAssignment2() {
202: assertEquals("sarah",
203: parseDirect("populate(); blahfoo = barfoo"));
204: }
205:
206: public void testAssignment3() {
207: assertEquals(java.lang.Integer.class, parseDirect("blah = 5")
208: .getClass());
209: }
210:
211: public void testOr() {
212: assertEquals(true, parseDirect("fun || true"));
213: }
214:
215: public void testLiteralPassThrough() {
216: assertEquals(true, parseDirect("true"));
217: }
218:
219: public void testLiteralPassThrough2() {
220: assertEquals(false, parseDirect("false"));
221: }
222:
223: public void testLiteralPassThrough3() {
224: assertEquals(null, parseDirect("null"));
225: }
226:
227: public void testRegEx() {
228: assertEquals(true, parseDirect("foo.bar.name ~= '[a-z].+'"));
229: }
230:
231: public void testRegExNegate() {
232: assertEquals(false, parseDirect("!(foo.bar.name ~= '[a-z].+')"));
233: }
234:
235: public void testRegEx2() {
236: assertEquals(
237: true,
238: parseDirect("foo.bar.name ~= '[a-z].+' && foo.bar.name != null"));
239: }
240:
241: public void testBlank() {
242: assertEquals(true, parseDirect("'' == empty"));
243: }
244:
245: public void testBlank2() {
246: assertEquals(true, parseDirect("BWAH == empty"));
247: }
248:
249: public void testBooleanModeOnly2() {
250: assertEquals(false, (Object) MVEL.evalToBoolean("BWAH", base,
251: map));
252: }
253:
254: public void testBooleanModeOnly4() {
255: assertEquals(true, (Object) MVEL.evalToBoolean(
256: "hour == (hour + 0)", base, map));
257: }
258:
259: public void testTernary() {
260: assertEquals("foobie", parseDirect("zero==0?'foobie':zero"));
261: }
262:
263: public void testTernary2() {
264: assertEquals("blimpie",
265: parseDirect("zero==1?'foobie':'blimpie'"));
266: }
267:
268: public void testTernary3() {
269: assertEquals("foobiebarbie",
270: parseDirect("zero==1?'foobie':'foobie'+'barbie'"));
271: }
272:
273: public void testStrAppend() {
274: assertEquals("foobarcar", parseDirect("'foo' + 'bar' + 'car'"));
275: }
276:
277: public void testStrAppend2() {
278: assertEquals("foobarcar1", parseDirect("'foobar' + 'car' + 1"));
279: }
280:
281: public void testInstanceCheck1() {
282: assertEquals(true, parseDirect("c is 'java.lang.String'"));
283: }
284:
285: public void testInstanceCheck2() {
286: assertEquals(false, parseDirect("pi is 'java.lang.Integer'"));
287: }
288:
289: public void testBitwiseOr1() {
290: assertEquals(6, parseDirect("2 | 4"));
291: }
292:
293: public void testBitwiseOr2() {
294: assertEquals(true, parseDirect("(2 | 1) > 0"));
295: }
296:
297: public void testBitwiseOr3() {
298: assertEquals(true, parseDirect("(2 | 1) == 3"));
299: }
300:
301: public void testBitwiseAnd1() {
302: assertEquals(2, parseDirect("2 & 3"));
303: }
304:
305: public void testShiftLeft() {
306: assertEquals(4, parseDirect("2 << 1"));
307: }
308:
309: public void testUnsignedShiftLeft() {
310: assertEquals(2, parseDirect("-2 <<< 0"));
311: }
312:
313: public void testShiftRight() {
314: assertEquals(128, parseDirect("256 >> 1"));
315: }
316:
317: public void testXOR() {
318: assertEquals(3, parseDirect("1 ^ 2"));
319: }
320:
321: public void testContains1() {
322: assertEquals(true, parseDirect("list contains 'Happy!'"));
323: }
324:
325: public void testContains2() {
326: assertEquals(false, parseDirect("list contains 'Foobie'"));
327: }
328:
329: public void testContains3() {
330: assertEquals(true, parseDirect("sentence contains 'fox'"));
331: }
332:
333: public void testContains4() {
334: assertEquals(false, parseDirect("sentence contains 'mike'"));
335: }
336:
337: public void testContains5() {
338: assertEquals(true, parseDirect("!(sentence contains 'mike')"));
339: }
340:
341: public void testInvert() {
342: assertEquals(~10, parseDirect("~10"));
343: }
344:
345: public void testInvert2() {
346: assertEquals(~(10 + 1), parseDirect("~(10 + 1)"));
347: }
348:
349: public void testInvert3() {
350: assertEquals(~10 + (1 + ~50), parseDirect("~10 + (1 + ~50)"));
351: }
352:
353: public void testListCreation2() {
354: assertTrue(parseDirect("[\"test\"]") instanceof List);
355: }
356:
357: public void testListCreation3() {
358: assertTrue(parseDirect("[66]") instanceof List);
359: }
360:
361: public void testListCreation4() {
362: List ar = (List) parseDirect("[ 66 , \"test\" ]");
363: assertEquals(2, ar.size());
364: assertEquals(66, ar.get(0));
365: assertEquals("test", ar.get(1));
366: }
367:
368: public void testListCreationWithCall() {
369: assertEquals(1, parseDirect("[\"apple\"].size()"));
370: }
371:
372: public void testArrayCreationWithLength() {
373: assertEquals(2, parseDirect("Array.getLength({'foo', 'bar'})"));
374: }
375:
376: public void testEmptyList() {
377: assertTrue(parseDirect("[]") instanceof List);
378: }
379:
380: public void testEmptyArray() {
381: assertTrue(((Object[]) parseDirect("{}")).length == 0);
382: }
383:
384: public void testArrayCreation() {
385: assertEquals(
386: 0,
387: parseDirect("arrayTest = {{1, 2, 3}, {2, 1, 0}}; arrayTest[1][2]"));
388: }
389:
390: public void testMapCreation() {
391: assertEquals(
392: "sarah",
393: parseDirect("map = ['mike':'sarah','tom':'jacquelin']; map['mike']"));
394: }
395:
396: public void testMapCreation2() {
397: assertEquals(
398: "sarah",
399: parseDirect("map = ['mike' :'sarah' ,'tom' :'jacquelin' ]; map['mike']"));
400: }
401:
402: public void testProjectionSupport() {
403: assertEquals(true,
404: parseDirect("(name in things) contains 'Bob'"));
405: }
406:
407: public void testProjectionSupport2() {
408: assertEquals(3, parseDirect("(name in things).size()"));
409: }
410:
411: public void testStaticMethodFromLiteral() {
412: assertEquals(
413: String.class.getName(),
414: parseDirect("String.valueOf(Class.forName('java.lang.String').getName())"));
415: }
416:
417: public void testMethodCallsEtc() {
418: parseDirect("title = 1; "
419: + "frame = new javax.swing.JFrame; "
420: + "label = new javax.swing.JLabel; "
421: + "title = title + 1;"
422: + "frame.setTitle(title);"
423: + "label.setText('MVEL UNIT TEST PACKAGE -- IF YOU SEE THIS, THAT IS GOOD');"
424: + "frame.getContentPane().add(label);"
425: + "frame.pack();" + "frame.setVisible(true);");
426: }
427:
428: public void testObjectInstantiation() {
429: parseDirect("new java.lang.String('foobie')");
430: }
431:
432: public void testObjectInstantiationWithMethodCall() {
433: parseDirect("new String('foobie').toString()");
434: }
435:
436: public void testObjectInstantiation2() {
437: parseDirect("new String() is String");
438: }
439:
440: public void testObjectInstantiation3() {
441: parseDirect("new java.text.SimpleDateFormat('yyyy').format(new java.util.Date(System.currentTimeMillis()))");
442: }
443:
444: public void testArrayCoercion() {
445: assertEquals("gonk",
446: parseDirect("funMethod( {'gonk', 'foo'} )"));
447: }
448:
449: public void testArrayCoercion2() {
450: assertEquals(10, parseDirect("sum({2,2,2,2,2})"));
451: }
452:
453: public void testMapAccess() {
454: assertEquals("dog", parseDirect("funMap['foo'].bar.name"));
455: }
456:
457: public void testMapAccess2() {
458: assertEquals("dog", parseDirect("funMap.foo.bar.name"));
459: }
460:
461: public void testSoundex() {
462: assertTrue((Boolean) parseDirect("'foobar' soundslike 'fubar'"));
463: }
464:
465: public void testSoundex2() {
466: assertFalse((Boolean) parseDirect("'flexbar' soundslike 'fubar'"));
467: }
468:
469: public void testThisReference() {
470: assertEquals(true, parseDirect("this") instanceof Base);
471: }
472:
473: public void testThisReference2() {
474: assertEquals(true, parseDirect("this.funMap") instanceof Map);
475: }
476:
477: public void testThisReference3() {
478: assertEquals(true,
479: parseDirect("this is 'org.mvel.tests.main.res.Base'"));
480: }
481:
482: public void testStringEscaping() {
483: assertEquals("\"Mike Brock\"",
484: parseDirect("\"\\\"Mike Brock\\\"\""));
485: }
486:
487: public void testStringEscaping2() {
488: assertEquals("MVEL's Parser is Fast",
489: parseDirect("'MVEL\\'s Parser is Fast'"));
490: }
491:
492: public void testEvalToBoolean() {
493: assertEquals(true, (boolean) MVEL
494: .evalToBoolean("true ", "true"));
495: assertEquals(true, (boolean) MVEL
496: .evalToBoolean("true ", "true"));
497: }
498:
499: public void testCompiledMapStructures() {
500: Serializable compiled = MVEL
501: .compileExpression("['foo':'bar'] contains 'foo'");
502: MVEL.executeExpression(compiled, null, null, Boolean.class);
503: }
504:
505: public void testSubListInMap() {
506: assertEquals(
507: "pear",
508: parseDirect("map = ['test' : 'poo', 'foo' : [c, 'pear']]; map['foo'][1]"));
509: }
510:
511: public void testCompiledMethodCall() {
512: Serializable compiled = MVEL.compileExpression("c.getClass()");
513: assertEquals(String.class, MVEL.executeExpression(compiled,
514: base, map));
515: }
516:
517: public void testStaticNamespaceCall() {
518: assertEquals(java.util.ArrayList.class,
519: parseDirect("java.util.ArrayList"));
520: }
521:
522: public void testStaticNamespaceClassWithMethod() {
523: assertEquals("FooBar",
524: parseDirect("java.lang.String.valueOf('FooBar')"));
525: }
526:
527: public void testThisReferenceInMethodCall() {
528: assertEquals(101, parseDirect("Integer.parseInt(this.number)"));
529: }
530:
531: public void testThisReferenceInConstructor() {
532: assertEquals("101", parseDirect("new String(this.number)"));
533: }
534:
535: public void testStaticNamespaceClassWithField() {
536: assertEquals(Integer.MAX_VALUE,
537: parseDirect("java.lang.Integer.MAX_VALUE"));
538: }
539:
540: public void testStaticNamespaceClassWithField2() {
541: assertEquals(Integer.MAX_VALUE,
542: parseDirect("Integer.MAX_VALUE"));
543: }
544:
545: public void testStaticFieldAsMethodParm() {
546: assertEquals(String.valueOf(Integer.MAX_VALUE),
547: parseDirect("String.valueOf(Integer.MAX_VALUE)"));
548: }
549:
550: public Object parseDirect(String ex) {
551: return compiledExecute(ex);
552: }
553:
554: public Object compiledExecute(String ex) {
555: Serializable compiled = MVEL.compileExpression(ex);
556: Object first = MVEL.executeExpression(compiled, base, map);
557: Object second = MVEL.executeExpression(compiled, base, map);
558:
559: if (first != null && !first.getClass().isArray())
560: assertEquals(first, second);
561:
562: return second;
563: }
564:
565: public Object compiledExecute(String ex, Object base, Map map) {
566: Serializable compiled = MVEL.compileExpression(ex);
567: Object first = MVEL.executeExpression(compiled, base, map);
568: Object second = MVEL.executeExpression(compiled, base, map);
569:
570: if (first != null && !first.getClass().isArray())
571: assertSame(first, second);
572: return second;
573: }
574:
575: //
576: // public void testSimplePropertyAccess() {
577: // final String expr = "c";
578: // Serializable compiled = compileExpression(expr);
579: //
580: // for (int i = 0; i < 100000; i++) {
581: // executeExpression(compiled, map);
582: // }
583: // }
584:
585: // public void testMathPerformance() {
586: // final String expr = "10 + 1 + 3";
587: // Serializable compiled = compileExpression(expr);
588: //
589: // for (int i = 0; i < 10000; i++) {
590: // executeExpression(compiled, map);
591: // }
592: // }
593:
594: public void testDifferentImplSameCompile() {
595: Serializable compiled = MVEL
596: .compileExpression("a.funMap.hello");
597:
598: Map testMap = new HashMap();
599:
600: for (int i = 0; i < 100; i++) {
601: Base b = new Base();
602: b.funMap.put("hello", "dog");
603: testMap.put("a", b);
604:
605: assertEquals("dog", MVEL.executeExpression(compiled,
606: testMap));
607:
608: b = new Base();
609: b.funMap.put("hello", "cat");
610: testMap.put("a", b);
611:
612: assertEquals("cat", MVEL.executeExpression(compiled,
613: testMap));
614: }
615: }
616:
617: public void testToList() {
618: String text = "misc.toList(foo.bar.name, 'hello', 42, ['key1' : 'value1', c : [ foo.bar.age, 'car', 42 ]], [42, [c : 'value1']] )";
619: List list = (List) parseDirect(text);
620: assertSame("dog", list.get(0));
621: assertEquals("hello", list.get(1));
622: assertEquals(new Integer(42), list.get(2));
623: Map map = (Map) list.get(3);
624: assertEquals("value1", map.get("key1"));
625:
626: List nestedList = (List) map.get("cat");
627: assertEquals(14, nestedList.get(0));
628: assertEquals("car", nestedList.get(1));
629: assertEquals(42, nestedList.get(2));
630:
631: nestedList = (List) list.get(4);
632: assertEquals(42, nestedList.get(0));
633: map = (Map) nestedList.get(1);
634: assertEquals("value1", map.get("cat"));
635: }
636:
637: public void testToList2() {
638: for (int i = 0; i < 10; i++) {
639: testToList();
640: }
641: }
642:
643: public static class MiscTestClass {
644: int exec = 0;
645:
646: public List toList(Object object1, String string, int integer,
647: Map map, List list) {
648: exec++;
649: List l = new ArrayList();
650: l.add(object1);
651: l.add(string);
652: l.add(new Integer(integer));
653: l.add(map);
654: l.add(list);
655: return l;
656: }
657:
658: public int getExec() {
659: return exec;
660: }
661: }
662:
663: public void testCalculateAge() {
664: // System.out.println("Calculating the Age");
665: Calendar c1 = Calendar.getInstance();
666: c1.set(1999, 0, 10); // 1999 jan 20
667: Map objectMap = new HashMap(1);
668: Map propertyMap = new HashMap(1);
669: propertyMap.put("GEBDAT", c1.getTime());
670: objectMap.put("EV_VI_ANT1", propertyMap);
671: assertEquals(
672: "N",
673: compiledExecute(
674: "new org.mvel.tests.main.res.PDFFieldUtil().calculateAge(EV_VI_ANT1.GEBDAT) >= 25 ? 'Y' : 'N'",
675: null, objectMap));
676: }
677:
678: }
|