001: /*
002: * Copyright 2006, 2007 Odysseus Software GmbH
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 de.odysseus.el.tree.impl.ast;
017:
018: import javax.el.ELException;
019:
020: import de.odysseus.el.TestCase;
021: import de.odysseus.el.tree.Tree;
022: import de.odysseus.el.util.SimpleContext;
023:
024: public class AstFunctionTest extends TestCase {
025: public static int foo() {
026: return 0;
027: }
028:
029: public static int bar(int op) {
030: return op;
031: }
032:
033: public static int foobar(int op1, int op2) {
034: return op1 + op2;
035: }
036:
037: AstFunction parseNode(String expression) {
038: return getNode(parse(expression));
039: }
040:
041: AstFunction getNode(Tree tree) {
042: return (AstFunction) tree.getRoot().getChild(0);
043: }
044:
045: SimpleContext context;
046:
047: @Override
048: protected void setUp() throws Exception {
049: context = new SimpleContext();
050:
051: // functions ns:f0(), ns:f1(int), ns:f2(int)
052: context.setFunction("ns", "f0", getClass().getMethod("foo"));
053: context.setFunction("ns", "f1", getClass().getMethod("bar",
054: new Class[] { int.class }));
055: context.setFunction("ns", "f2", getClass().getMethod("foobar",
056: new Class[] { int.class, int.class }));
057:
058: // functions g0(), g1(int), g2(int,int)
059: context.setFunction("", "g0", getClass().getMethod("foo"));
060: context.setFunction("", "g1", getClass().getMethod("bar",
061: new Class[] { int.class }));
062: context.setFunction("", "g2", getClass().getMethod("foobar",
063: new Class[] { int.class, int.class }));
064: }
065:
066: public void testEval() {
067: Tree tree = null;
068:
069: tree = parse("${ns:f0()}");
070: assertEquals(foo(), getNode(tree).eval(
071: tree.bind(context.getFunctionMapper(), null), null));
072:
073: tree = parse("${ns:f1(42)}");
074: assertEquals(bar(42), getNode(tree).eval(
075: tree.bind(context.getFunctionMapper(), null), null));
076:
077: tree = parse("${ns:f2(21,21)}");
078: assertEquals(foobar(21, 21), getNode(tree).eval(
079: tree.bind(context.getFunctionMapper(), null), null));
080:
081: tree = parse("${g0()}");
082: assertEquals(foo(), getNode(tree).eval(
083: tree.bind(context.getFunctionMapper(), null), null));
084:
085: tree = parse("${g1(42)}");
086: assertEquals(bar(42), getNode(tree).eval(
087: tree.bind(context.getFunctionMapper(), null), null));
088:
089: tree = parse("${g2(21,21)}");
090: assertEquals(foobar(21, 21), getNode(tree).eval(
091: tree.bind(context.getFunctionMapper(), null), null));
092: }
093:
094: public void testAppendStructure() {
095: StringBuilder s = null;
096:
097: s = new StringBuilder();
098: parseNode("${f()}").appendStructure(s, null);
099: parseNode("${f(x)}").appendStructure(s, null);
100: parseNode("${f(x,y)}").appendStructure(s, null);
101: assertEquals("f()f(x)f(x, y)", s.toString());
102:
103: s = new StringBuilder();
104: parseNode("${p:f()}").appendStructure(s, null);
105: parseNode("${p:f(x)}").appendStructure(s, null);
106: parseNode("${p:f(x,y)}").appendStructure(s, null);
107: assertEquals("p:f()p:f(x)p:f(x, y)", s.toString());
108: }
109:
110: public void testIsLiteralText() {
111: assertFalse(parseNode("${f()}").isLiteralText());
112: }
113:
114: public void testIsLeftValue() {
115: assertFalse(parseNode("${f()}").isLeftValue());
116: }
117:
118: public void testGetType() {
119: assertNull(parseNode("${f()}").getType(null, null));
120: }
121:
122: public void testIsReadOnly() {
123: assertTrue(parseNode("${f()}").isReadOnly(null, null));
124: }
125:
126: public void testSetValue() {
127: try {
128: parseNode("${f()}").setValue(null, null, null);
129: fail();
130: } catch (ELException e) {
131: }
132: }
133:
134: public void testGetValue() {
135: Tree tree = null;
136:
137: tree = parse("${ns:f0()}");
138:
139: assertEquals(foo(), getNode(tree).getValue(
140: tree.bind(context.getFunctionMapper(), null), null,
141: null));
142: assertEquals("" + foo(), getNode(tree).getValue(
143: tree.bind(context.getFunctionMapper(), null), null,
144: String.class));
145: }
146: }
|