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:
020: import de.odysseus.el.tree.TreeStore;
021: import de.odysseus.el.tree.impl.Builder;
022: import de.odysseus.el.util.SimpleContext;
023: import de.odysseus.el.util.SimpleResolver;
024:
025: public class TreeValueExpressionTest extends TestCase {
026:
027: public static int foo() {
028: return 0;
029: }
030:
031: public static int bar() {
032: return 0;
033: }
034:
035: SimpleContext context;
036: TreeStore store = new TreeStore(new Builder(), null);
037:
038: @Override
039: protected void setUp() throws Exception {
040: context = new SimpleContext(new SimpleResolver(
041: new BeanELResolver()));
042: context.getELResolver().setValue(context, null, "base", this );
043:
044: // variables var_long_1, var_long_2
045: context.setVariable("var_long_1", new TreeValueExpression(
046: store, null, null, "${1}", long.class));
047: context.setVariable("var_long_2", new TreeValueExpression(
048: store, null, null, "${1}", long.class));
049: // var_var_long_1 --> var_long_1, var_var_long_2 --> var_long_1
050: context.setVariable("var_var_long_1", new TreeValueExpression(
051: store, null, context.getVariableMapper(),
052: "${var_long_1}", long.class));
053: context.setVariable("var_var_long_2", new TreeValueExpression(
054: store, null, context.getVariableMapper(),
055: "${var_long_2}", long.class));
056:
057: // functions ns:f0(), ns:f1()
058: context.setFunction("", "foo", getClass().getMethod("foo"));
059: context.setFunction("ns", "foo_1", getClass().getMethod("foo"));
060: context.setFunction("ns", "foo_2", getClass().getMethod("foo"));
061:
062: context.setVariable("var_foo_1", new TreeValueExpression(store,
063: context.getFunctionMapper(), null, "${ns:foo_1()}",
064: long.class));
065: context.setVariable("var_foo_2", new TreeValueExpression(store,
066: context.getFunctionMapper(), null, "${ns:foo_2()}",
067: long.class));
068:
069: context.getELResolver().setValue(context, null, "property_foo",
070: "foo");
071: }
072:
073: public void testEqualsAndHashCode() throws NoSuchMethodException {
074: TreeValueExpression e1, e2;
075:
076: e1 = new TreeValueExpression(store, null, context
077: .getVariableMapper(), "${1}", Object.class);
078: e2 = new TreeValueExpression(store, null, context
079: .getVariableMapper(), "${1}", Object.class);
080: assertEquals(e1, e2);
081: assertEquals(e1.hashCode(), e2.hashCode());
082:
083: e1 = new TreeValueExpression(store, null, context
084: .getVariableMapper(), "${var_long_1}", Object.class);
085: e2 = new TreeValueExpression(store, null, context
086: .getVariableMapper(), "${var_long_2}", Object.class);
087: assertEquals(e1, e2);
088: assertEquals(e1.hashCode(), e2.hashCode());
089:
090: e1 = new TreeValueExpression(store, null, context
091: .getVariableMapper(), "${var_var_long_1}", Object.class);
092: e2 = new TreeValueExpression(store, null, context
093: .getVariableMapper(), "${var_var_long_2}", Object.class);
094: assertEquals(e1, e2);
095: assertEquals(e1.hashCode(), e2.hashCode());
096:
097: e1 = new TreeValueExpression(store, null, context
098: .getVariableMapper(), "${var_long_1}", Object.class);
099: context.getVariableMapper().setVariable(
100: "var_long_1",
101: new TreeValueExpression(store, null, context
102: .getVariableMapper(), "${-1}", Object.class));
103: e2 = new TreeValueExpression(store, null, context
104: .getVariableMapper(), "${var_long_1}", Object.class);
105: assertFalse(e1.equals(e2));
106:
107: e1 = new TreeValueExpression(store, null, context
108: .getVariableMapper(), "${var_foo_1}", Object.class);
109: e2 = new TreeValueExpression(store, null, context
110: .getVariableMapper(), "${var_foo_2}", Object.class);
111: assertEquals(e1, e2);
112: assertEquals(e1.hashCode(), e2.hashCode());
113:
114: e2 = new TreeValueExpression(store,
115: context.getFunctionMapper(), null, "${ns:foo_1()}",
116: Object.class);
117: e1 = new TreeValueExpression(store,
118: context.getFunctionMapper(), null, "${ns:foo_2()}",
119: Object.class);
120: assertEquals(e1, e2);
121: assertEquals(e1.hashCode(), e2.hashCode());
122:
123: e2 = new TreeValueExpression(store,
124: context.getFunctionMapper(), null, "${foo()}",
125: Object.class);
126: e1 = new TreeValueExpression(store,
127: context.getFunctionMapper(), null, "${ns:foo_1()}",
128: Object.class);
129: assertEquals(e1, e2);
130: assertEquals(e1.hashCode(), e2.hashCode());
131:
132: e2 = new TreeValueExpression(store,
133: context.getFunctionMapper(), null, "${foo()}",
134: Object.class);
135: context.setFunction("", "foo", getClass().getMethod("bar"));
136: e1 = new TreeValueExpression(store,
137: context.getFunctionMapper(), null, "${foo()}",
138: Object.class);
139: assertFalse(e1.equals(e2));
140:
141: e2 = new TreeValueExpression(store,
142: context.getFunctionMapper(), null, "${ns:foo_1()}",
143: Object.class);
144: context.setFunction("ns", "foo_1", getClass().getMethod("bar"));
145: e1 = new TreeValueExpression(store,
146: context.getFunctionMapper(), null, "${ns:foo_1()}",
147: Object.class);
148: assertFalse(e1.equals(e2));
149: }
150:
151: public void testGetExpressionString() {
152: assertEquals("foo", new TreeValueExpression(store, null, null,
153: "foo", Object.class).getExpressionString());
154: }
155:
156: public void testIsLiteralText() {
157: assertTrue(new TreeValueExpression(store, null, null, "foo",
158: Object.class).isLiteralText());
159: assertFalse(new TreeValueExpression(store, null, null,
160: "${foo}", Object.class).isLiteralText());
161: }
162:
163: public void testIsDeferred() {
164: assertFalse(new TreeValueExpression(store, null, null, "foo",
165: Object.class).isDeferred());
166: assertFalse(new TreeValueExpression(store, null, null,
167: "${foo}", Object.class).isDeferred());
168: assertTrue(new TreeValueExpression(store, null, null, "#{foo}",
169: Object.class).isDeferred());
170: }
171:
172: public void testGetExpectedType() {
173: assertEquals(Object.class, new TreeValueExpression(store, null,
174: null, "${foo}", Object.class).getExpectedType());
175: assertEquals(String.class, new TreeValueExpression(store, null,
176: null, "${foo}", String.class).getExpectedType());
177: }
178:
179: public void testGetType() {
180: assertFalse(new TreeValueExpression(store, null, null,
181: "${property_foo}", Object.class).isReadOnly(context));
182: }
183:
184: public void testIsReadOnly() {
185: assertFalse(new TreeValueExpression(store, null, null,
186: "${property_foo}", Object.class).isReadOnly(context));
187: }
188:
189: public void testSetValue() {
190: new TreeValueExpression(store, null, null, "${property_foo}",
191: Object.class).setValue(context, "bar");
192: assertEquals("bar", new TreeValueExpression(store, null, null,
193: "${property_foo}", Object.class).getValue(context));
194: }
195:
196: public void testGetValue() {
197: assertEquals("foo", new TreeValueExpression(store, null, null,
198: "${property_foo}", Object.class).getValue(context));
199: }
200:
201: public void testSerialize() throws Exception {
202: TreeValueExpression expression = new TreeValueExpression(store,
203: context.getFunctionMapper(), context
204: .getVariableMapper(), "${var_long_1 + foo()}",
205: Object.class);
206: assertEquals(expression, deserialize(serialize(expression)));
207: }
208: }
|