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;
017:
018: import javax.el.BeanELResolver;
019: import javax.el.MethodInfo;
020:
021: import de.odysseus.el.tree.TreeStore;
022: import de.odysseus.el.tree.impl.Builder;
023: import de.odysseus.el.util.SimpleContext;
024: import de.odysseus.el.util.SimpleResolver;
025:
026: public class TreeMethodExpressionTest extends TestCase {
027:
028: public static int foo() {
029: return 0;
030: }
031:
032: public static int bar() {
033: return 0;
034: }
035:
036: SimpleContext context;
037: TreeStore store = new TreeStore(new Builder(), null);
038:
039: @Override
040: protected void setUp() throws Exception {
041: context = new SimpleContext(new SimpleResolver(
042: new BeanELResolver()));
043: context.getELResolver().setValue(context, null, "base", this );
044: }
045:
046: public void testEqualsAndHashCode() {
047: TreeMethodExpression e1, e2;
048: e1 = new TreeMethodExpression(store, null, null, "${base.foo}",
049: null, new Class[0]);
050: e2 = new TreeMethodExpression(store, null, null, "${base.foo}",
051: null, new Class[0]);
052: assertEquals(e1, e2);
053:
054: e1 = new TreeMethodExpression(store, null, null, "${base.foo}",
055: null, new Class[0]);
056: e2 = new TreeMethodExpression(store, null, null, "${base.bar}",
057: null, new Class[0]);
058: assertFalse(e1.equals(e2));
059: }
060:
061: public void testGetExpressionString() {
062: assertEquals("${base.foo}", new TreeMethodExpression(store,
063: null, null, "${base.foo}", null, new Class[0])
064: .getExpressionString());
065: }
066:
067: public void testIsLiteralText() {
068: assertFalse(new TreeMethodExpression(store, null, null,
069: "${base.foo}", null, new Class[0]).isLiteralText());
070: assertTrue(new TreeMethodExpression(store, null, null,
071: "base.foo", null, new Class[0]).isLiteralText());
072: }
073:
074: public void testIsDeferred() {
075: assertFalse(new TreeMethodExpression(store, null, null, "foo",
076: null, new Class[0]).isDeferred());
077: assertFalse(new TreeMethodExpression(store, null, null,
078: "${foo}", null, new Class[0]).isDeferred());
079: assertTrue(new TreeMethodExpression(store, null, null,
080: "#{foo}", null, new Class[0]).isDeferred());
081: }
082:
083: public void testGetMethodInfo() {
084: TreeMethodExpression e = new TreeMethodExpression(store, null,
085: null, "${base.foo}", null, new Class[0]);
086: MethodInfo info = e.getMethodInfo(context);
087: assertEquals("foo", info.getName());
088: assertEquals(0, info.getParamTypes().length);
089: assertEquals(int.class, info.getReturnType());
090: }
091:
092: public void testInvoke() {
093: assertEquals(0, new TreeMethodExpression(store, null, null,
094: "${base.foo}", null, new Class[0])
095: .invoke(context, null));
096: }
097:
098: public void testSerialize() throws Exception {
099: TreeMethodExpression expression = new TreeMethodExpression(
100: store, null, null, "$base.foo}", null, new Class[0]);
101: assertEquals(expression, deserialize(serialize(expression)));
102: }
103: }
|