1 16 package org.apache.commons.jelly.expression; 17 18 import junit.framework.Test; 19 import junit.framework.TestCase; 20 import junit.framework.TestSuite; 21 import junit.textui.TestRunner; 22 23 import org.apache.commons.jelly.JellyContext; 24 import org.apache.commons.jelly.expression.jexl.JexlExpressionFactory; 25 26 32 public class TestExpressions extends TestCase { 33 34 protected JellyContext context = new JellyContext(); 35 protected ExpressionFactory factory = new JexlExpressionFactory(); 36 37 public static void main(String [] args) { 38 TestRunner.run(suite()); 39 } 40 41 public static Test suite() { 42 return new TestSuite(TestExpressions.class); 43 } 44 45 public TestExpressions(String testName) { 46 super(testName); 47 } 48 49 public void testExpresssions() throws Exception { 50 context.setVariable("topping", "cheese"); 51 context.setVariable("type", "deepPan"); 52 53 54 assertExpression("foo", "foo"); 55 assertExpression("${topping}", "cheese"); 56 assertExpression("some${topping}", "somecheese"); 57 assertExpression(" some ${topping} ", " some cheese "); 58 assertExpression("${topping}y", "cheesey"); 59 assertExpression("A ${topping} ${type} pizza", "A cheese deepPan pizza"); 60 assertExpression("${topping}-${type}", "cheese-deepPan"); 61 } 62 63 public void testAntExpresssions() throws Exception { 64 context.setVariable("maven.home.foo", "cheese"); 65 66 assertExpression("${maven.home.foo}", "cheese"); 67 assertExpression("${maven.some.madeup.name}", null); 68 assertExpression("cheese ${maven.some.madeup.name}pizza", "cheese pizza"); 69 assertExpression("ham and ${maven.home.foo} pizza", "ham and cheese pizza"); 70 assertExpression("${maven.home.foo.length()}", new Integer (6)); 71 } 72 73 public void testNotConditions() throws Exception { 74 context.setVariable("a", Boolean.TRUE); 75 context.setVariable("b", Boolean.FALSE); 76 context.setVariable("c", "true"); 77 context.setVariable("d", "false"); 78 79 assertExpression("${a}", Boolean.TRUE); 80 assertExpression("${!a}", Boolean.FALSE); 81 assertExpression("${b}", Boolean.FALSE); 82 assertExpression("${!b}", Boolean.TRUE); 83 84 assertExpression("${c}", "true"); 85 assertExpression("${!c}", Boolean.FALSE); 86 assertExpression("${d}", "false"); 87 assertExpression("${!d}", Boolean.TRUE); 88 } 89 90 public void testNotConditionsWithDot() throws Exception { 91 context.setVariable("x.a", Boolean.TRUE); 92 context.setVariable("x.b", Boolean.FALSE); 93 context.setVariable("x.c", "true"); 94 context.setVariable("x.d", "false"); 95 96 assertExpression("${x.a}", Boolean.TRUE); 97 assertExpression("${!x.a}", Boolean.FALSE); 98 assertExpression("${x.b}", Boolean.FALSE); 99 assertExpression("${!x.b}", Boolean.TRUE); 100 101 assertExpression("${x.c}", "true"); 102 assertExpression("${!x.c}", Boolean.FALSE); 103 assertExpression("${x.d}", "false"); 104 assertExpression("${!x.d}", Boolean.TRUE); 105 } 106 107 public void testNull() throws Exception { 108 context.setVariable("something.blank", ""); 109 context.setVariable("something.ok", "cheese"); 110 111 assertExpression("${something.blank.length() == 0}", Boolean.TRUE); 112 assertExpression("${something.blank == ''}", Boolean.TRUE); 113 assertExpression("${something.ok != null}", Boolean.TRUE); 114 assertExpression("${something.ok != ''}", Boolean.TRUE); 115 assertExpression("${unknown == null}", Boolean.TRUE); 118 } 119 120 protected void assertExpression(String expressionText, Object expectedValue) throws Exception { 121 Expression expression = CompositeExpression.parse(expressionText, factory); 122 assertTrue( "Created a valid expression for: " + expressionText, expression != null ); 123 Object value = expression.evaluate(context); 124 assertEquals( "Wrong result for expression: " + expressionText, expectedValue, value ); 125 126 String text = expression.getExpressionText(); 127 assertEquals( "Wrong textual representation for expression text: ", expressionText, text); 128 } 129 } 130 | Popular Tags |