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: import java.util.HashMap;
020:
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.tree.impl.Builder;
028: import de.odysseus.el.util.SimpleContext;
029: import de.odysseus.el.util.SimpleResolver;
030:
031: public class AstBracketTest extends TestCase {
032: AstBracket parseNode(String expression) {
033: return (AstBracket) parse(expression).getRoot().getChild(0);
034: }
035:
036: SimpleContext context;
037: Bindings bindings;
038:
039: long foo = 1l;
040:
041: public long getFoo() {
042: return foo;
043: }
044:
045: public void setFoo(long value) {
046: foo = value;
047: }
048:
049: public long bar() {
050: return 1l;
051: }
052:
053: public long bar(long value) {
054: return value;
055: }
056:
057: @Override
058: protected void setUp() throws Exception {
059: context = new SimpleContext(new SimpleResolver());
060: context.getELResolver().setValue(context, null, "base", this );
061:
062: HashMap<Object, String> nullmap = new HashMap<Object, String>();
063: nullmap.put(null, "foo");
064: context.getELResolver().setValue(context, null, "nullmap",
065: nullmap);
066:
067: bindings = new Bindings(null, new ValueExpression[2]);
068: }
069:
070: public void testEval() {
071: try {
072: parseNode("${base[bad]}").eval(bindings, context);
073: fail();
074: } catch (ELException e) {
075: }
076: assertEquals(1l, parseNode("${base['foo']}").eval(bindings,
077: context));
078: }
079:
080: public void testAppendStructure() {
081: StringBuilder s = new StringBuilder();
082: parseNode("${foo[bar]}").appendStructure(s, null);
083: assertEquals("foo[bar]", s.toString());
084: }
085:
086: public void testIsLiteralText() {
087: assertFalse(parseNode("${foo[bar]}").isLiteralText());
088: }
089:
090: public void testIsLeftValue() {
091: assertFalse(parseNode("${'foo'[bar]}").isLeftValue());
092: assertTrue(parseNode("${foo[bar]}").isLeftValue());
093: }
094:
095: public void testGetType() {
096: try {
097: parseNode("${base[bad]}").getType(bindings, context);
098: fail();
099: } catch (ELException e) {
100: }
101: assertEquals(long.class, parseNode("${base['foo']}").getType(
102: bindings, context));
103: assertNull(parseNode("${'base'['foo']}").getType(bindings,
104: context));
105: if (BUILDER.isEnabled(Builder.Feature.NULL_PROPERTIES)) {
106: assertEquals(Object.class, parseNode("${nullmap[null]}")
107: .getType(bindings, context));
108: } else {
109: try {
110: parseNode("${nullmap[null]}")
111: .getType(bindings, context);
112: fail();
113: } catch (ELException e) {
114: }
115: }
116: }
117:
118: public void testIsReadOnly() {
119: assertFalse(parseNode("${base['foo']}").isReadOnly(bindings,
120: context));
121: assertTrue(parseNode("${'base'['foo']}").isReadOnly(bindings,
122: context));
123: if (BUILDER.isEnabled(Builder.Feature.NULL_PROPERTIES)) {
124: assertFalse(parseNode("${nullmap[null]}").isReadOnly(
125: bindings, context));
126: } else {
127: try {
128: parseNode("${nullmap[null]}").isReadOnly(bindings,
129: context);
130: fail();
131: } catch (ELException e) {
132: }
133: }
134: }
135:
136: public void testSetValue() {
137: try {
138: parseNode("${base[bad]}").setValue(bindings, context,
139: "good");
140: fail();
141: } catch (ELException e) {
142: }
143: parseNode("${base['foo']}").setValue(bindings, context, 2l);
144: assertEquals(2l, getFoo());
145: if (BUILDER.isEnabled(Builder.Feature.NULL_PROPERTIES)) {
146: parseNode("${nullmap[null]}").setValue(bindings, context,
147: "bar");
148: assertEquals("bar", parseNode("${nullmap[null]}").eval(
149: bindings, context));
150: parseNode("${nullmap[null]}").setValue(bindings, context,
151: "foo");
152: } else {
153: try {
154: parseNode("${nullmap[null]}").setValue(bindings,
155: context, "bar");
156: fail();
157: } catch (ELException e) {
158: }
159: }
160: }
161:
162: public void testGetValue() {
163: assertEquals(1l, parseNode("${base['foo']}").getValue(bindings,
164: context, null));
165: assertEquals("1", parseNode("${base['foo']}").getValue(
166: bindings, context, String.class));
167: if (BUILDER.isEnabled(Builder.Feature.NULL_PROPERTIES)) {
168: assertEquals("foo", parseNode("${nullmap[null]}").getValue(
169: bindings, context, null));
170: } else {
171: assertNull(parseNode("${nullmap[null]}").getValue(bindings,
172: context, null));
173: }
174: }
175:
176: public void testInvoke() {
177: assertEquals(1l, parseNode("${base['bar']}").invoke(bindings,
178: context, long.class, new Class[0], null));
179: assertEquals(2l, parseNode("${base['bar']}").invoke(bindings,
180: context, null, new Class[] { long.class },
181: new Object[] { 2l }));
182: }
183:
184: public void testGetMethodInfo() {
185: MethodInfo info = null;
186:
187: // long bar()
188: info = parseNode("${base['bar']}").getMethodInfo(bindings,
189: context, long.class, new Class[0]);
190: assertEquals("bar", info.getName());
191: assertTrue(Arrays.equals(new Class[0], info.getParamTypes()));
192: assertEquals(long.class, info.getReturnType());
193:
194: // long bar(long)
195: info = parseNode("${base['bar']}").getMethodInfo(bindings,
196: context, null, new Class[] { long.class });
197: assertEquals("bar", info.getName());
198: assertTrue(Arrays.equals(new Class[] { long.class }, info
199: .getParamTypes()));
200: assertEquals(long.class, info.getReturnType());
201:
202: // bad arg type
203: try {
204: info = parseNode("${base['bar']}").getMethodInfo(bindings,
205: context, null, new Class[] { String.class });
206: fail();
207: } catch (ELException e) {
208: }
209: // bad return type
210: try {
211: info = parseNode("${base['bar']}").getMethodInfo(bindings,
212: context, String.class, new Class[0]);
213: fail();
214: } catch (ELException e) {
215: }
216: }
217: }
|