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;
017:
018: import de.odysseus.el.ObjectValueExpression;
019: import de.odysseus.el.TestCase;
020: import de.odysseus.el.tree.Bindings;
021: import de.odysseus.el.util.SimpleContext;
022:
023: public class TreeTest extends TestCase {
024:
025: public static int foo() {
026: return 0;
027: }
028:
029: public static int bar(int op) {
030: return op;
031: }
032:
033: private SimpleContext context;
034:
035: @Override
036: protected void setUp() throws Exception {
037: context = new SimpleContext();
038:
039: // functions ns:f0(), ns:f1(int)
040: context
041: .setFunction("ns", "f0", TreeTest.class
042: .getMethod("foo"));
043: context.setFunction("ns", "f1", TreeTest.class.getMethod("bar",
044: new Class[] { int.class }));
045:
046: // functions g0(), g1(int)
047: context.setFunction("", "g0", TreeTest.class.getMethod("foo"));
048: context.setFunction("", "g1", TreeTest.class.getMethod("bar",
049: new Class[] { int.class }));
050:
051: // variables v0, v1
052: context.setVariable("v0", new ObjectValueExpression(0,
053: long.class));
054: context.setVariable("v1", new ObjectValueExpression(1,
055: long.class));
056: }
057:
058: public void testBindFunctions() throws Exception {
059: Bindings bindings = null;
060:
061: bindings = parse("${ns:f0()}").bind(
062: context.getFunctionMapper(), null);
063: assertSame(context.getFunctionMapper().resolveFunction("ns",
064: "f0"), bindings.getFunction(0));
065: try {
066: bindings.getFunction(1);
067: fail();
068: } catch (Exception e) {
069: }
070:
071: bindings = parse("${ns:f1(1)}").bind(
072: context.getFunctionMapper(), null);
073: assertSame(context.getFunctionMapper().resolveFunction("ns",
074: "f1"), bindings.getFunction(0));
075: try {
076: bindings.getFunction(1);
077: fail();
078: } catch (Exception e) {
079: }
080:
081: bindings = parse("${ns:f0()+ns:f1(1)}").bind(
082: context.getFunctionMapper(), null);
083: assertSame(context.getFunctionMapper().resolveFunction("ns",
084: "f0"), bindings.getFunction(0));
085: assertSame(context.getFunctionMapper().resolveFunction("ns",
086: "f1"), bindings.getFunction(1));
087: try {
088: bindings.getFunction(2);
089: fail();
090: } catch (Exception e) {
091: }
092:
093: // the same for default namespace functions g0(), g1()
094: bindings = parse("${g0()}").bind(context.getFunctionMapper(),
095: null);
096: assertSame(context.getFunctionMapper()
097: .resolveFunction("", "g0"), bindings.getFunction(0));
098: try {
099: bindings.getFunction(1);
100: fail();
101: } catch (Exception e) {
102: }
103:
104: bindings = parse("${g1(1)}").bind(context.getFunctionMapper(),
105: null);
106: assertSame(context.getFunctionMapper()
107: .resolveFunction("", "g1"), bindings.getFunction(0));
108: try {
109: bindings.getFunction(1);
110: fail();
111: } catch (Exception e) {
112: }
113:
114: bindings = parse("${g0()+g1(1)}").bind(
115: context.getFunctionMapper(), null);
116: assertSame(context.getFunctionMapper()
117: .resolveFunction("", "g0"), bindings.getFunction(0));
118: assertSame(context.getFunctionMapper()
119: .resolveFunction("", "g1"), bindings.getFunction(1));
120: try {
121: bindings.getFunction(2);
122: fail();
123: } catch (Exception e) {
124: }
125:
126: try {
127: parse("${foo()}").bind(context.getFunctionMapper(), null);
128: fail();
129: } catch (Exception e) {
130: }
131: try {
132: parse("${g1()}").bind(context.getFunctionMapper(), null);
133: fail();
134: } catch (Exception e) {
135: }
136: try {
137: parse("${g1(1,2)}").bind(context.getFunctionMapper(), null);
138: fail();
139: } catch (Exception e) {
140: }
141: }
142:
143: public void testBindVariables() throws Exception {
144: Bindings bindings = null;
145:
146: bindings = parse("${v0}").bind(null,
147: context.getVariableMapper());
148: assertSame(context.getVariableMapper().resolveVariable("v0"),
149: bindings.getVariable(0));
150: try {
151: bindings.getVariable(1);
152: fail();
153: } catch (Exception e) {
154: }
155:
156: bindings = parse("${v1}").bind(null,
157: context.getVariableMapper());
158: assertSame(context.getVariableMapper().resolveVariable("v1"),
159: bindings.getVariable(0));
160: try {
161: bindings.getVariable(1);
162: fail();
163: } catch (Exception e) {
164: }
165:
166: bindings = parse("${v0+v1}").bind(null,
167: context.getVariableMapper());
168: assertSame(context.getVariableMapper().resolveVariable("v0"),
169: bindings.getVariable(0));
170: assertSame(context.getVariableMapper().resolveVariable("v1"),
171: bindings.getVariable(1));
172: try {
173: bindings.getVariable(2);
174: fail();
175: } catch (Exception e) {
176: }
177:
178: bindings = parse("${foo}").bind(null,
179: context.getVariableMapper());
180: assertNull(bindings.getVariable(0));
181: try {
182: bindings.getVariable(1);
183: fail();
184: } catch (Exception e) {
185: }
186: }
187:
188: public void testBindFunctionsAndVariables() throws Exception {
189: Bindings bindings = parse("${ns:f0()+v0+g1(1)+v1+foo}").bind(
190: context.getFunctionMapper(),
191: context.getVariableMapper());
192: assertSame(context.getFunctionMapper().resolveFunction("ns",
193: "f0"), bindings.getFunction(0));
194: assertSame(context.getFunctionMapper()
195: .resolveFunction("", "g1"), bindings.getFunction(1));
196: try {
197: bindings.getFunction(2);
198: fail();
199: } catch (Exception e) {
200: }
201: assertSame(context.getVariableMapper().resolveVariable("v0"),
202: bindings.getVariable(0));
203: assertSame(context.getVariableMapper().resolveVariable("v1"),
204: bindings.getVariable(1));
205: assertNull(bindings.getVariable(2));
206: try {
207: bindings.getVariable(3);
208: fail();
209: } catch (Exception e) {
210: }
211: }
212: }
|