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.util.Arrays;
019:
020: import javax.el.BeanELResolver;
021: import javax.el.ELException;
022: import javax.el.MethodInfo;
023: import javax.el.ValueExpression;
024:
025: import de.odysseus.el.TestCase;
026: import de.odysseus.el.tree.Bindings;
027: import de.odysseus.el.util.SimpleContext;
028: import de.odysseus.el.util.SimpleResolver;
029:
030: public class AstDotTest extends TestCase {
031: AstDot parseNode(String expression) {
032: return (AstDot) parse(expression).getRoot().getChild(0);
033: }
034:
035: SimpleContext context;
036: Bindings bindings;
037:
038: long foo = 1l;
039:
040: public long getFoo() {
041: return foo;
042: }
043:
044: public void setFoo(long value) {
045: foo = value;
046: }
047:
048: public long bar() {
049: return 1l;
050: }
051:
052: public long bar(long value) {
053: return value;
054: }
055:
056: @Override
057: protected void setUp() throws Exception {
058: context = new SimpleContext(new SimpleResolver(
059: new BeanELResolver()));
060: context.getELResolver().setValue(context, null, "base", this );
061:
062: bindings = new Bindings(null, new ValueExpression[1]);
063: }
064:
065: public void testEval() {
066: try {
067: parseNode("${base.bad}").eval(bindings, context);
068: fail();
069: } catch (ELException e) {
070: }
071: assertEquals(1l, parseNode("${base.foo}").eval(bindings,
072: context));
073: }
074:
075: public void testAppendStructure() {
076: StringBuilder s = new StringBuilder();
077: parseNode("${foo.bar}").appendStructure(s, null);
078: assertEquals("foo.bar", s.toString());
079: }
080:
081: public void testIsLiteralText() {
082: assertFalse(parseNode("${foo.bar}").isLiteralText());
083: }
084:
085: public void testIsLeftValue() {
086: assertFalse(parseNode("${'foo'.bar}").isLeftValue());
087: assertTrue(parseNode("${foo.bar}").isLeftValue());
088: }
089:
090: public void testGetType() {
091: try {
092: parseNode("${base.bad}").getType(bindings, context);
093: fail();
094: } catch (ELException e) {
095: }
096: assertEquals(long.class, parseNode("${base.foo}").getType(
097: bindings, context));
098: assertNull(parseNode("${'base'.foo}")
099: .getType(bindings, context));
100: }
101:
102: public void testIsReadOnly() {
103: assertFalse(parseNode("${base.foo}").isReadOnly(bindings,
104: context));
105: assertTrue(parseNode("${'base'.foo}").isReadOnly(bindings,
106: context));
107: }
108:
109: public void testSetValue() {
110: try {
111: parseNode("${base.bad}")
112: .setValue(bindings, context, "good");
113: fail();
114: } catch (ELException e) {
115: }
116: parseNode("${base.foo}").setValue(bindings, context, 2l);
117: assertEquals(2l, getFoo());
118: }
119:
120: public void testGetValue() {
121: assertEquals(1l, parseNode("${base.foo}").getValue(bindings,
122: context, null));
123: assertEquals("1", parseNode("${base.foo}").getValue(bindings,
124: context, String.class));
125: }
126:
127: public void testInvoke() {
128: assertEquals(1l, parseNode("${base.bar}").invoke(bindings,
129: context, long.class, new Class[0], null));
130: assertEquals(2l, parseNode("${base.bar}").invoke(bindings,
131: context, null, new Class[] { long.class },
132: new Object[] { 2l }));
133: }
134:
135: public void testGetMethodInfo() {
136: MethodInfo info = null;
137:
138: // long bar()
139: info = parseNode("${base.bar}").getMethodInfo(bindings,
140: context, long.class, new Class[0]);
141: assertEquals("bar", info.getName());
142: assertTrue(Arrays.equals(new Class[0], info.getParamTypes()));
143: assertEquals(long.class, info.getReturnType());
144:
145: // long bar(long)
146: info = parseNode("${base.bar}").getMethodInfo(bindings,
147: context, null, new Class[] { long.class });
148: assertEquals("bar", info.getName());
149: assertTrue(Arrays.equals(new Class[] { long.class }, info
150: .getParamTypes()));
151: assertEquals(long.class, info.getReturnType());
152:
153: // bad arg type
154: try {
155: info = parseNode("${base.bar}").getMethodInfo(bindings,
156: context, null, new Class[] { String.class });
157: fail();
158: } catch (ELException e) {
159: }
160: // bad return type
161: try {
162: info = parseNode("${base.bar}").getMethodInfo(bindings,
163: context, String.class, new Class[0]);
164: fail();
165: } catch (ELException e) {
166: }
167: }
168: }
|