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 java.lang.reflect.Method;
019: import java.util.Arrays;
020:
021: import javax.el.ELException;
022: import javax.el.MethodInfo;
023:
024: import de.odysseus.el.ObjectValueExpression;
025: import de.odysseus.el.TestCase;
026: import de.odysseus.el.TreeValueExpression;
027: import de.odysseus.el.tree.Bindings;
028: import de.odysseus.el.tree.Tree;
029: import de.odysseus.el.tree.TreeStore;
030: import de.odysseus.el.util.SimpleContext;
031: import de.odysseus.el.util.SimpleResolver;
032:
033: public class AstIdentifierTest extends TestCase {
034: public static long method_1() {
035: return 1l;
036: }
037:
038: AstIdentifier parseNode(String expression) {
039: return getNode(parse(expression));
040: }
041:
042: AstIdentifier getNode(Tree tree) {
043: return (AstIdentifier) tree.getRoot().getChild(0);
044: }
045:
046: SimpleContext context;
047:
048: @Override
049: protected void setUp() throws Exception {
050: context = new SimpleContext(new SimpleResolver());
051:
052: // variables var_long_1, indentifier_string
053: context.setVariable("var_long_1", new ObjectValueExpression(1l,
054: long.class));
055: context.setVariable("indentifier_string",
056: new ObjectValueExpression("foo", String.class));
057: context.setVariable("var_method_1", new ObjectValueExpression(
058: getClass().getMethod("method_1"), Method.class));
059:
060: // properties property_long_1, indentifier_string
061: context.getELResolver().setValue(context, null,
062: "property_long_1", 1l);
063: context.getELResolver().setValue(context, null,
064: "indentifier_string", "bar"); // shadowed by variable indentifier_string
065: context.getELResolver().setValue(context, null,
066: "property_method_1", getClass().getMethod("method_1"));
067:
068: // var_var_long_1 --> var_long_1, var_property_long_1 --> property_long_1
069: context.setVariable("var_var_long_1", new TreeValueExpression(
070: new TreeStore(BUILDER, null), null, context
071: .getVariableMapper(), "${var_long_1}",
072: long.class));
073: context.setVariable("var_property_long_1",
074: new TreeValueExpression(new TreeStore(BUILDER, null),
075: null, context.getVariableMapper(),
076: "${property_long_1}", long.class));
077: }
078:
079: public void testEval() {
080: Tree tree = null;
081: Bindings bindings = null;
082:
083: tree = parse("${bad}");
084: bindings = tree.bind(null, context.getVariableMapper());
085: try {
086: getNode(tree).eval(bindings, context);
087: fail();
088: } catch (ELException e) {
089: }
090:
091: tree = parse("${var_long_1}");
092: bindings = tree.bind(null, context.getVariableMapper());
093: assertEquals(1l, getNode(tree).eval(bindings, context));
094:
095: tree = parse("${property_long_1}");
096: bindings = tree.bind(null, context.getVariableMapper());
097: assertEquals(1l, getNode(tree).eval(bindings, context));
098:
099: tree = parse("${indentifier_string}");
100: bindings = tree.bind(null, context.getVariableMapper());
101: assertEquals("foo", getNode(tree).eval(bindings, context));
102:
103: tree = parse("${var_var_long_1}");
104: bindings = tree.bind(null, context.getVariableMapper());
105: assertEquals(1l, getNode(tree).eval(bindings, context));
106:
107: tree = parse("${var_property_long_1}");
108: bindings = tree.bind(null, context.getVariableMapper());
109: assertEquals(1l, getNode(tree).eval(bindings, context));
110: }
111:
112: public void testAppendStructure() {
113: StringBuilder s = new StringBuilder();
114: parseNode("${foo}").appendStructure(s, null);
115: assertEquals("foo", s.toString());
116: }
117:
118: public void testIsLiteralText() {
119: assertFalse(parseNode("${foo}").isLiteralText());
120: }
121:
122: public void testIsLeftValue() {
123: assertTrue(parseNode("${foo}").isLeftValue());
124: }
125:
126: public void testGetType() {
127: Tree tree = null;
128: Bindings bindings = null;
129:
130: tree = parse("${var_long_1}");
131: bindings = tree.bind(null, context.getVariableMapper());
132: assertEquals(null, getNode(tree).getType(bindings, context));
133:
134: tree = parse("${property_long_1}");
135: bindings = tree.bind(null, context.getVariableMapper());
136: assertEquals(Object.class, getNode(tree).getType(bindings,
137: context));
138:
139: tree = parse("${var_var_long_1}");
140: bindings = tree.bind(null, context.getVariableMapper());
141: assertEquals(null, getNode(tree).getType(bindings, context));
142:
143: tree = parse("${var_property_long_1}");
144: bindings = tree.bind(null, context.getVariableMapper());
145: assertEquals(Object.class, getNode(tree).getType(bindings,
146: context));
147:
148: tree = parse("${indentifier_string}");
149: bindings = tree.bind(null, context.getVariableMapper());
150: assertEquals(null, getNode(tree).getType(bindings, context));
151: }
152:
153: public void testIsReadOnly() {
154: Tree tree = null;
155: Bindings bindings = null;
156:
157: tree = parse("${var_long_1}");
158: bindings = tree.bind(null, context.getVariableMapper());
159: assertTrue(getNode(tree).isReadOnly(bindings, context));
160:
161: tree = parse("${property_long_1}");
162: bindings = tree.bind(null, context.getVariableMapper());
163: assertFalse(getNode(tree).isReadOnly(bindings, context));
164:
165: tree = parse("${var_var_long_1}");
166: bindings = tree.bind(null, context.getVariableMapper());
167: assertTrue(getNode(tree).isReadOnly(bindings, context));
168:
169: tree = parse("${var_property_long_1}");
170: bindings = tree.bind(null, context.getVariableMapper());
171: assertFalse(getNode(tree).isReadOnly(bindings, context));
172:
173: tree = parse("${indentifier_string}");
174: bindings = tree.bind(null, context.getVariableMapper());
175: assertTrue(getNode(tree).isReadOnly(bindings, context));
176: }
177:
178: public void testSetValue() {
179: Tree tree = null;
180: Bindings bindings = null;
181:
182: tree = parse("${bad}");
183: bindings = tree.bind(null, context.getVariableMapper());
184: getNode(tree).setValue(bindings, context, "good");
185: assertEquals("good", getNode(tree).getValue(bindings, context,
186: null));
187:
188: tree = parse("${var_long_1}");
189: bindings = tree.bind(null, context.getVariableMapper());
190: try {
191: getNode(tree).setValue(bindings, context, 2l);
192: fail();
193: } catch (ELException e) {
194: }
195:
196: tree = parse("${property_long_1}");
197: bindings = tree.bind(null, context.getVariableMapper());
198: assertEquals(1l, getNode(tree)
199: .getValue(bindings, context, null));
200: getNode(tree).setValue(bindings, context, 2l);
201: assertEquals(2l, getNode(tree)
202: .getValue(bindings, context, null));
203:
204: tree = parse("${var_var_long_1}");
205: bindings = tree.bind(null, context.getVariableMapper());
206: try {
207: getNode(tree).setValue(bindings, context, 2l);
208: fail();
209: } catch (ELException e) {
210: }
211:
212: tree = parse("${var_property_long_1}");
213: bindings = tree.bind(null, context.getVariableMapper());
214: assertEquals(2l, getNode(tree)
215: .getValue(bindings, context, null));
216: getNode(tree).setValue(bindings, context, 1l);
217: assertEquals(1l, getNode(tree)
218: .getValue(bindings, context, null));
219:
220: tree = parse("${indentifier_string}");
221: bindings = tree.bind(null, context.getVariableMapper());
222: try {
223: getNode(tree).setValue(bindings, context, "bar");
224: fail();
225: } catch (ELException e) {
226: }
227: }
228:
229: public void testGetValue() {
230: Tree tree = null;
231: Bindings bindings = null;
232:
233: tree = parse("${bad}");
234: bindings = tree.bind(null, context.getVariableMapper());
235: try {
236: getNode(tree).getValue(bindings, context, null);
237: fail();
238: } catch (ELException e) {
239: }
240:
241: tree = parse("${var_long_1}");
242: bindings = tree.bind(null, context.getVariableMapper());
243: assertEquals(1l, getNode(tree)
244: .getValue(bindings, context, null));
245: assertEquals("1", getNode(tree).getValue(bindings, context,
246: String.class));
247: }
248:
249: public void testInvoke() {
250: Tree tree = null;
251: Bindings bindings = null;
252:
253: tree = parse("${bad}");
254: bindings = tree.bind(null, context.getVariableMapper());
255: try {
256: getNode(tree).invoke(bindings, context, long.class,
257: new Class[0], null);
258: fail();
259: } catch (ELException e) {
260: }
261:
262: tree = parse("${var_method_1}");
263: bindings = tree.bind(null, context.getVariableMapper());
264: assertEquals(1l, getNode(tree).invoke(bindings, context,
265: long.class, new Class[0], null));
266:
267: tree = parse("${property_method_1}");
268: bindings = tree.bind(null, context.getVariableMapper());
269: assertEquals(1l, getNode(tree).invoke(bindings, context, null,
270: new Class[0], null));
271:
272: // no return type - ok
273: assertEquals(1l, getNode(tree).invoke(bindings, context,
274: long.class, new Class[0], null));
275: // bad return type
276: try {
277: getNode(tree).invoke(bindings, context, int.class,
278: new Class[0], null);
279: fail();
280: } catch (ELException e) {
281: }
282: // bad arg types
283: try {
284: getNode(tree).invoke(bindings, context, long.class,
285: new Class[] { String.class }, null);
286: fail();
287: } catch (ELException e) {
288: }
289: // bad args
290: try {
291: getNode(tree).invoke(bindings, context, long.class,
292: new Class[0], new Object[] { "" });
293: fail();
294: } catch (ELException e) {
295: }
296: }
297:
298: public void testGetMethodInfo() {
299: Tree tree = null;
300: Bindings bindings = null;
301: MethodInfo info = null;
302:
303: tree = parse("${bad}");
304: bindings = tree.bind(null, context.getVariableMapper());
305: try {
306: getNode(tree).getMethodInfo(bindings, context, long.class,
307: new Class[0]);
308: fail();
309: } catch (ELException e) {
310: }
311:
312: tree = parse("${var_method_1}");
313: bindings = tree.bind(null, context.getVariableMapper());
314: info = getNode(tree).getMethodInfo(bindings, context,
315: long.class, new Class[0]);
316: assertEquals("method_1", info.getName());
317: assertTrue(Arrays.equals(new Class[0], info.getParamTypes()));
318: assertEquals(long.class, info.getReturnType());
319:
320: tree = parse("${property_method_1}");
321: bindings = tree.bind(null, context.getVariableMapper());
322: info = getNode(tree).getMethodInfo(bindings, context,
323: long.class, new Class[0]);
324: assertEquals("method_1", info.getName());
325: assertTrue(Arrays.equals(new Class[0], info.getParamTypes()));
326: assertEquals(long.class, info.getReturnType());
327:
328: // no return type - ok
329: info = getNode(tree).getMethodInfo(bindings, context, null,
330: new Class[0]);
331: assertEquals("method_1", info.getName());
332: assertTrue(Arrays.equals(new Class[0], info.getParamTypes()));
333: assertEquals(long.class, info.getReturnType());
334: // bad return type
335: try {
336: getNode(tree).getMethodInfo(bindings, context, int.class,
337: new Class[0]);
338: fail();
339: } catch (ELException e) {
340: }
341: // bad arg types
342: try {
343: getNode(tree).getMethodInfo(bindings, context, long.class,
344: new Class[] { String.class });
345: fail();
346: } catch (ELException e) {
347: }
348: }
349: }
|