001: /*
002: * Copyright 2002,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.jelly.expression;
017:
018: import junit.framework.Test;
019: import junit.framework.TestCase;
020: import junit.framework.TestSuite;
021: import junit.textui.TestRunner;
022:
023: import org.apache.commons.jelly.JellyContext;
024: import org.apache.commons.jelly.expression.jexl.JexlExpressionFactory;
025:
026: /**
027: * Tests the use of Expression parsing
028: *
029: * @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
030: * @version $Revision: 155420 $
031: */
032: public class TestExpressions extends TestCase {
033:
034: protected JellyContext context = new JellyContext();
035: protected ExpressionFactory factory = new JexlExpressionFactory();
036:
037: public static void main(String[] args) {
038: TestRunner.run(suite());
039: }
040:
041: public static Test suite() {
042: return new TestSuite(TestExpressions.class);
043: }
044:
045: public TestExpressions(String testName) {
046: super (testName);
047: }
048:
049: public void testExpresssions() throws Exception {
050: context.setVariable("topping", "cheese");
051: context.setVariable("type", "deepPan");
052:
053: assertExpression("foo", "foo");
054: assertExpression("${topping}", "cheese");
055: assertExpression("some${topping}", "somecheese");
056: assertExpression(" some ${topping} ", " some cheese ");
057: assertExpression("${topping}y", "cheesey");
058: assertExpression("A ${topping} ${type} pizza",
059: "A cheese deepPan pizza");
060: assertExpression("${topping}-${type}", "cheese-deepPan");
061: }
062:
063: public void testAntExpresssions() throws Exception {
064: context.setVariable("maven.home.foo", "cheese");
065:
066: assertExpression("${maven.home.foo}", "cheese");
067: assertExpression("${maven.some.madeup.name}", null);
068: assertExpression("cheese ${maven.some.madeup.name}pizza",
069: "cheese pizza");
070: assertExpression("ham and ${maven.home.foo} pizza",
071: "ham and cheese pizza");
072: assertExpression("${maven.home.foo.length()}", new Integer(6));
073: }
074:
075: public void testNotConditions() throws Exception {
076: context.setVariable("a", Boolean.TRUE);
077: context.setVariable("b", Boolean.FALSE);
078: context.setVariable("c", "true");
079: context.setVariable("d", "false");
080:
081: assertExpression("${a}", Boolean.TRUE);
082: assertExpression("${!a}", Boolean.FALSE);
083: assertExpression("${b}", Boolean.FALSE);
084: assertExpression("${!b}", Boolean.TRUE);
085:
086: assertExpression("${c}", "true");
087: assertExpression("${!c}", Boolean.FALSE);
088: assertExpression("${d}", "false");
089: assertExpression("${!d}", Boolean.TRUE);
090: }
091:
092: public void testNotConditionsWithDot() throws Exception {
093: context.setVariable("x.a", Boolean.TRUE);
094: context.setVariable("x.b", Boolean.FALSE);
095: context.setVariable("x.c", "true");
096: context.setVariable("x.d", "false");
097:
098: assertExpression("${x.a}", Boolean.TRUE);
099: assertExpression("${!x.a}", Boolean.FALSE);
100: assertExpression("${x.b}", Boolean.FALSE);
101: assertExpression("${!x.b}", Boolean.TRUE);
102:
103: assertExpression("${x.c}", "true");
104: assertExpression("${!x.c}", Boolean.FALSE);
105: assertExpression("${x.d}", "false");
106: assertExpression("${!x.d}", Boolean.TRUE);
107: }
108:
109: public void testNull() throws Exception {
110: context.setVariable("something.blank", "");
111: context.setVariable("something.ok", "cheese");
112:
113: assertExpression("${something.blank.length() == 0}",
114: Boolean.TRUE);
115: assertExpression("${something.blank == ''}", Boolean.TRUE);
116: assertExpression("${something.ok != null}", Boolean.TRUE);
117: assertExpression("${something.ok != ''}", Boolean.TRUE);
118: // null is a reserved word
119: //assertExpression("${something.null != ''}", Boolean.FALSE);
120: assertExpression("${unknown == null}", Boolean.TRUE);
121: }
122:
123: protected void assertExpression(String expressionText,
124: Object expectedValue) throws Exception {
125: Expression expression = CompositeExpression.parse(
126: expressionText, factory);
127: assertTrue("Created a valid expression for: " + expressionText,
128: expression != null);
129: Object value = expression.evaluate(context);
130: assertEquals("Wrong result for expression: " + expressionText,
131: expectedValue, value);
132:
133: String text = expression.getExpressionText();
134: assertEquals(
135: "Wrong textual representation for expression text: ",
136: expressionText, text);
137: }
138: }
|