001: package org.mvel.tests.main;
002:
003: import junit.framework.TestCase;
004: import org.mvel.MVELTemplateRegistry;
005: import org.mvel.TemplateInterpreter;
006: import org.mvel.TemplateRegistry;
007: import org.mvel.tests.main.res.Bar;
008: import org.mvel.tests.main.res.Base;
009: import org.mvel.tests.main.res.Foo;
010:
011: import java.io.StringReader;
012: import java.util.ArrayList;
013: import java.util.HashMap;
014: import java.util.List;
015: import java.util.Map;
016:
017: public class TemplateConfidenceTests extends TestCase {
018: Foo foo = new Foo();
019: Map<String, Object> map = new HashMap<String, Object>();
020: Base base = new Base();
021:
022: public TemplateConfidenceTests() {
023: foo.setBar(new Bar());
024: map.put("foo", foo);
025: map.put("a", null);
026: map.put("b", null);
027: map.put("c", "cat");
028: map.put("BWAH", "");
029:
030: // map.put("misc", new MiscTestClass());
031:
032: map.put("pi", "3.14");
033: map.put("hour", "60");
034: map.put("zero", 0);
035:
036: //noinspection UnnecessaryBoxing
037: map.put("doubleTen", new Double(10));
038:
039: map.put("variable_with_underscore", "HELLO");
040:
041: map.put("testImpl", new TestInterface() {
042:
043: public String getName() {
044: return "FOOBAR!";
045: }
046:
047: public boolean isFoo() {
048: return true;
049: }
050: });
051: }
052:
053: public void testPassThru2() {
054: assertEquals("foo@bar.com", TemplateInterpreter.eval(
055: "foo@bar.com", map));
056: }
057:
058: public void testMethodOnValue() {
059: assertEquals("DOG", parse("@{foo.bar.name.toUpperCase()}"));
060: }
061:
062: public void testSimpleProperty() {
063: assertEquals("dog", parse("@{foo.bar.name}"));
064: }
065:
066: public void testBooleanOperator() {
067: assertEquals(true, parse("@{foo.bar.woof == true}"));
068: }
069:
070: public void testBooleanOperator2() {
071: assertEquals(false, parse("@{foo.bar.woof == false}"));
072: }
073:
074: public void testTextComparison() {
075: assertEquals(true, parse("@{foo.bar.name == 'dog'}"));
076: }
077:
078: public void testNETextComparison() {
079: assertEquals(true, parse("@{foo.bar.name != 'foo'}"));
080: }
081:
082: public void testChor() {
083: assertEquals("cat", parse("@{a or b or c}"));
084: }
085:
086: public void testChorWithLiteral() {
087: assertEquals("fubar", parse("@{a or 'fubar'}"));
088: }
089:
090: public void testNullCompare() {
091: assertEquals(true, parse("@{c != null}"));
092: }
093:
094: public void testAnd() {
095: assertEquals(
096: true,
097: parse("@{c != null && foo.bar.name == 'dog' && foo.bar.woof}"));
098: }
099:
100: public void testMath() {
101: assertEquals(188.4f, parse("@{pi * hour}"));
102: }
103:
104: public void testTemplating() {
105: assertEquals("dogDOGGIE133.5",
106: parse("@{foo.bar.name}DOGGIE@{hour*2.225+1-1}"));
107: }
108:
109: public void testComplexAnd() {
110: assertEquals(
111: true,
112: parse("@{(pi * hour) > 0 && foo.happy() == 'happyBar'}"));
113: }
114:
115: public void testModulus() {
116: assertEquals(38392 % 2, parse("@{38392 % 2}"));
117: }
118:
119: public void testLessThan() {
120: assertEquals(true, parse("@{pi < 3.15}"));
121: assertEquals(true, parse("@{pi <= 3.14}"));
122: assertEquals(false, parse("@{pi > 3.14}"));
123: assertEquals(true, parse("@{pi >= 3.14}"));
124: }
125:
126: public void testMethodAccess() {
127: assertEquals("happyBar", parse("@{foo.happy()}"));
128: }
129:
130: public void testMethodAccess2() {
131: assertEquals("FUBAR", parse("@{foo.toUC('fubar')}"));
132: }
133:
134: public void testMethodAccess3() {
135: assertEquals(true, parse("@{equalityCheck(c, 'cat')}"));
136: }
137:
138: public void testMethodAccess4() {
139: assertEquals(null, parse("@{readBack(null)}"));
140: }
141:
142: public void testMethodAccess5() {
143: assertEquals("nulltest",
144: parse("@{appendTwoStrings(null, 'test')}"));
145: }
146:
147: public void testMethodAccess6() {
148: assertEquals(false, parse("@{!foo.bar.isWoof()}"));
149: }
150:
151: public void testNegation() {
152: assertEquals(true, parse("@{!fun && !fun}"));
153: }
154:
155: public void testNegation2() {
156: assertEquals(false, parse("@{fun && !fun}"));
157: }
158:
159: public void testNegation3() {
160: assertEquals(true, parse("@{!(fun && fun)}"));
161: }
162:
163: public void testNegation4() {
164: assertEquals(false, parse("@{(fun && fun)}"));
165: }
166:
167: public void testMultiStatement() {
168: assertEquals(true, parse("@{populate(); barfoo == 'sarah'}"));
169: }
170:
171: public void testAssignment2() {
172: assertEquals("sarah", parse("@{populate(); blahfoo = barfoo}"));
173: }
174:
175: public void testOr() {
176: assertEquals(true, parse("@{fun || true}"));
177: }
178:
179: public void testLiteralPassThrough() {
180: assertEquals(true, parse("@{true}"));
181: }
182:
183: public void testLiteralPassThrough2() {
184: assertEquals(false, parse("@{false}"));
185: }
186:
187: public void testLiteralPassThrough3() {
188: assertEquals(null, parse("@{null}"));
189: }
190:
191: public void testControlLoopList() {
192: assertEquals("HappyHappy!JoyJoy!",
193: parse("@foreach{list as fun}" + "@{fun}" + "@end{}"));
194: }
195:
196: public void testControlLoopArray() {
197: assertEquals("Happy0Happy!1Joy2Joy!3",
198: parse("@foreach{array as fun}" + "@{fun}@{i0}"
199: + "@end{}"));
200: }
201:
202: public void testMultiCollectionControlLoop() {
203: assertEquals("HappyHappy0Happy!Happy!1JoyJoy2Joy!Joy!3",
204: parse("@foreach{list as listItem, array as arrayItem}"
205: + "@{listItem}@{arrayItem}@{i0}" + "@end{}"));
206: }
207:
208: public void testMultiCollectionWithSingleCharSeperatorControlLoop() {
209: assertEquals("Happy0Happy,Happy!1Happy!,Joy2Joy,Joy!3Joy!",
210: parse("@foreach{list as listItem, array as arrayItem}"
211: + "@{listItem}@{i0}@{arrayItem}"
212: + "@end{\",\" }"));
213: }
214:
215: public void testMultiCollectionWithMultipleCharSeperatorControlLoop() {
216: assertEquals("HappyHappy,|Happy!Happy!,|JoyJoy,|Joy!Joy!",
217: parse("@foreach{list as listItem, array as arrayItem}"
218: + "@{listItem}@{arrayItem}" + "@end{\",|\" }"));
219: }
220:
221: public void testControlLoopListMultiple() {
222: for (int i = 0; i < 100; i++) {
223: testControlLoopList();
224: }
225: }
226:
227: public void testControlLoopArrayMultiple() {
228: for (int i = 0; i < 100; i++) {
229: testControlLoopArray();
230: }
231: }
232:
233: public void testMultiCollectionControlLoopMultiple() {
234: for (int i = 0; i < 100; i++) {
235: testMultiCollectionControlLoop();
236: }
237: }
238:
239: public void testMultiCollectionWithSingleCharSeperatorControlLoopMultiple() {
240: for (int i = 0; i < 100; i++) {
241: testMultiCollectionWithSingleCharSeperatorControlLoop();
242: }
243: }
244:
245: public void testMultiCollectionWithMultipleCharSeperatorControlLoopMultiple() {
246: for (int i = 0; i < 100; i++) {
247: testMultiCollectionWithMultipleCharSeperatorControlLoop();
248: }
249: }
250:
251: public static interface TestInterface {
252: public String getName();
253:
254: public boolean isFoo();
255: }
256:
257: public void testControlLoop2() {
258: assertEquals("HappyHappy!JoyJoy!", parse("@foreach{list}"
259: + "@{item}" + "@end{}"));
260: }
261:
262: public void testControlLoop3() {
263: assertEquals("HappyHappy!JoyJoy!", parse("@foreach{ list }"
264: + "@{item}" + "@end{}"));
265: }
266:
267: public void testIfStatement() {
268: assertEquals("sarah", parse("@if{'fun' == 'fun'}sarah@end{}"));
269: }
270:
271: public void testIfStatement2() {
272: assertEquals("poo",
273: parse("@if{'fun' == 'bar'}sarah@else{}poo@end{}"));
274: }
275:
276: public void testRegEx() {
277: assertEquals(true, parse("@{foo.bar.name ~= '[a-z].+'}"));
278: }
279:
280: public void testRegExNegate() {
281: assertEquals(false, parse("@{!(foo.bar.name ~= '[a-z].+')}"));
282: }
283:
284: public void testRegEx2() {
285: assertEquals(
286: true,
287: parse("@{foo.bar.name ~= '[a-z].+' && foo.bar.name != null}"));
288: }
289:
290: public void testBlank() {
291: assertEquals(true, parse("@{'' == empty}"));
292: }
293:
294: public void testBlank2() {
295: assertEquals(true, parse("@{BWAH == empty}"));
296: }
297:
298: public void testTernary() {
299: assertEquals("foobie", parse("@{zero==0?'foobie':zero}"));
300: }
301:
302: public void testTernary2() {
303: assertEquals("blimpie", parse("@{zero==1?'foobie':'blimpie'}"));
304: }
305:
306: public void testTernary3() {
307: assertEquals("foobiebarbie",
308: parse("@{zero==1?'foobie':'foobie'+'barbie'}"));
309: }
310:
311: public void testTernary4() {
312: assertEquals("no", parse("@{ackbar ? 'yes' : 'no'}"));
313: }
314:
315: public void testStrAppend() {
316: assertEquals("foobarcar", parse("@{'foo' + 'bar' + 'car'}"));
317: }
318:
319: public void testStrAppend2() {
320: assertEquals("foobarcar1", parse("@{'foobar' + 'car' + 1}"));
321: }
322:
323: public void testInstanceCheck1() {
324: assertEquals(true, parse("@{c is 'java.lang.String'}"));
325: }
326:
327: public void testInstanceCheck2() {
328: assertEquals(false, parse("@{pi is 'java.lang.Integer'}"));
329: }
330:
331: public void testBitwiseOr1() {
332: assertEquals(6, parse("@{2 | 4}"));
333: }
334:
335: public void testBitwiseOr2() {
336: assertEquals(true, parse("@{(2 | 1) > 0}"));
337: }
338:
339: public void testBitwiseOr3() {
340: assertEquals(true, parse("@{(2 | 1) == 3}"));
341: }
342:
343: public void testBitwiseAnd1() {
344: assertEquals(2, parse("@{2 & 3}"));
345: }
346:
347: public void testShiftLeft() {
348: assertEquals(4, parse("@{2 << 1}"));
349: }
350:
351: public void testUnsignedShiftLeft() {
352: assertEquals(2, parse("@{-2 <<< 0}"));
353: }
354:
355: public void testShiftRight() {
356: assertEquals(128, parse("@{256 >> 1}"));
357: }
358:
359: public void testXOR() {
360: assertEquals(3, parse("@{1 ^ 2}"));
361: }
362:
363: public void testContains1() {
364: assertEquals(true, parse("@{list contains 'Happy!'}"));
365: }
366:
367: public void testContains2() {
368: assertEquals(false, parse("@{list contains 'Foobie'}"));
369: }
370:
371: public void testContains3() {
372: assertEquals(true, parse("@{sentence contains 'fox'}"));
373: }
374:
375: public void testContains4() {
376: assertEquals(false, parse("@{sentence contains 'mike'}"));
377: }
378:
379: public void testContains5() {
380: assertEquals(true, parse("@{!(sentence contains 'mike')}"));
381: }
382:
383: public void testTokenMethodAccess() {
384: assertEquals(String.class, parse("@{a = 'foo'; a.getClass()}"));
385: }
386:
387: public void testArrayCreationWithLength() {
388: assertEquals(2, parse("@{Array.getLength({'foo', 'bar'})}"));
389: }
390:
391: public void testMapCreation() {
392: assertEquals(
393: "sarah",
394: parse("@{map = ['mike':'sarah','tom':'jacquelin']; map['mike']}"));
395: }
396:
397: public void testProjectionSupport() {
398: assertEquals(true, parse("@{(name in things) contains 'Bob'}"));
399: }
400:
401: public void testProjectionSupport2() {
402: assertEquals(3, parse("@{(name in things).size()}"));
403: }
404:
405: public void testObjectInstantiation() {
406: assertEquals("foobie",
407: parse("@{new java.lang.String('foobie')}"));
408: }
409:
410: public void testObjectInstantiationWithMethodCall() {
411: assertEquals("foobie",
412: parse("@{new String('foobie').toString()}"));
413: }
414:
415: public void testObjectInstantiation2() {
416: parse("@{new String() is String}");
417: }
418:
419: public void testArrayCoercion() {
420: assertEquals("gonk", parse("@{funMethod( {'gonk', 'foo'} )}"));
421: }
422:
423: public void testMapAccess() {
424: assertEquals("dog", parse("@{funMap['foo'].bar.name}"));
425: }
426:
427: public void testMapAccess2() {
428: assertEquals("dog", parse("@{funMap.foo.bar.name}"));
429: }
430:
431: public void testSoundex() {
432: assertTrue((Boolean) parse("@{'foobar' soundslike 'fubar'}"));
433: }
434:
435: public void testSoundex2() {
436: assertFalse((Boolean) parse("@{'flexbar' soundslike 'fubar'}"));
437: }
438:
439: public void testThisReference() {
440: assertEquals(true, parse("@{this}") instanceof Base);
441: }
442:
443: public void testIncludeByRef() {
444: TemplateRegistry registry = new MVELTemplateRegistry();
445: registry.registerTemplate("templateName", "@{var1}@{var2}");
446:
447: assertEquals(
448: "xvalue1catx",
449: parse(
450: "x@includeByRef{templateName(var1 = \"value1\", var2 = c)}x",
451: registry));
452: }
453:
454: public void testIncludeByRefNoParams() {
455: TemplateRegistry registry = new MVELTemplateRegistry();
456: registry.registerTemplate("templateName", "hello");
457:
458: assertEquals("xhellox", parse(
459: "x@includeByRef{templateName()}x", registry));
460: }
461:
462: public void testIncludeByRefNoSpaces() {
463: TemplateRegistry registry = new MVELTemplateRegistry();
464: registry.registerTemplate("templateName", "@{var1}@{var2}");
465:
466: assertEquals(
467: "xvalue1catx",
468: parse(
469: "x@includeByRef{templateName(var1=\"value1\", var2=c)}x",
470: registry));
471: }
472:
473: public void testRegisterTemplateGroup() {
474: StringReader reader = new StringReader(
475: "myTemplate1() ::=<<@{var1}>>=:: myTemplate2() ::=<<@{var2}>>=::");
476: TemplateRegistry registry = new MVELTemplateRegistry();
477: registry.registerTemplate(reader);
478:
479: assertEquals(
480: "xvalue1catx",
481: parse(
482: "x@includeByRef{myTemplate1(var1 = \"value1\")}@includeByRef{myTemplate2(var2 = c)}x",
483: registry));
484: }
485:
486: public void testRecursiveRegisterTemplateGroup() {
487: StringReader reader = new StringReader(
488: "myTemplate1() ::=<<@{var1}@includeByRef{myTemplate2(var2 = var2)}>>=:: myTemplate2() ::=<<@{var2}>>=::");
489: TemplateRegistry registry = new MVELTemplateRegistry();
490: registry.registerTemplate(reader);
491:
492: assertEquals(
493: "xvalue1catx",
494: parse(
495: "x@includeByRef{myTemplate1(var1 = \"value1\", var2 = c)}x",
496: registry));
497: }
498:
499: public void testIfLoopInTemplate() {
500: assertEquals(
501: "ONETWOTHREE",
502: parse("@foreach{things}@if{item.name=='Bob'}ONE@elseif{item.name=='Smith'}TWO@elseif{item.name=='Cow'}THREE@end{}@end{}"));
503: }
504:
505: public void testStringEscaping() {
506: assertEquals("\"Mike Brock\"",
507: parse("@{\"\\\"Mike Brock\\\"\"}"));
508: }
509:
510: public void testStringEscaping2() {
511: assertEquals("MVEL's Parser is Fast",
512: parse("@{'MVEL\\'s Parser is Fast'}"));
513: }
514:
515: public void testIteration1() {
516: List<String> list = new ArrayList<String>();
517: list.add("a1");
518: list.add("a2");
519: list.add("a3");
520:
521: String template = "@foreach{list}a@end{}";
522: Map map = new HashMap();
523: map.put("list", list);
524: String r = TemplateInterpreter.evalToString(template, map);
525: System.out.println("r: " + r);
526: assertEquals("aaa", r);
527: }
528:
529: public void testIteration2() {
530: Folder f1 = new Folder("f1", null);
531:
532: String template = "@{name} @foreach{children}a@end{}";
533: String r = TemplateInterpreter.evalToString(template, f1);
534: System.out.println("r: " + r);
535: }
536:
537: public void testIteration3() {
538: Folder f = new Folder("a1", null);
539: List<Page> list = f.getChildren();
540:
541: String template = "@foreach{list}a@end{}";
542: Map map = new HashMap();
543: map.put("list", list);
544: String r = TemplateInterpreter.evalToString(template, map);
545: System.out.println("r: " + r);
546: assertEquals("aaa", r);
547: }
548:
549: public void testIteration4() {
550: Folder f = new Folder("a1", null);
551:
552: String template = "@foreach{f.children}a@end{}";
553: Map map = new HashMap();
554: map.put("f", f);
555: String r = TemplateInterpreter.evalToString(template, map);
556: System.out.println("r: " + r);
557: assertEquals("aaa", r);
558: }
559:
560: class Page {
561: String name;
562: Folder parent;
563:
564: Page(String name, Folder parent) {
565: this .name = name;
566: this .parent = parent;
567: }
568:
569: public String getName() {
570: return name;
571: }
572:
573: public TemplateConfidenceTests.Folder getParent() {
574: return parent;
575: }
576: }
577:
578: class Folder extends Page {
579: Folder(String name, Folder parent) {
580: super (name, parent);
581: }
582:
583: public List<Page> getChildren() {
584: List<Page> list = new ArrayList<Page>();
585: list.add(new Page("a1", this ));
586: list.add(new Page("a2", this ));
587: list.add(new Page("a3", this ));
588: return list;
589: }
590: }
591:
592: public Object parse(String ex, TemplateRegistry registry) {
593: return TemplateInterpreter.parse(ex, base, map, registry);
594: }
595:
596: public Object parse(String ex) {
597: return TemplateInterpreter.parse(ex, base, map);
598: }
599:
600: }
|