01: /*
02: * Copyright 2006, 2007 Odysseus Software GmbH
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package de.odysseus.el.tree;
17:
18: import java.lang.reflect.Method;
19:
20: import javax.el.ValueExpression;
21:
22: import de.odysseus.el.ObjectValueExpression;
23: import de.odysseus.el.TestCase;
24: import de.odysseus.el.tree.Bindings;
25: import de.odysseus.el.tree.Tree;
26: import de.odysseus.el.util.SimpleContext;
27:
28: public class BindingsTest extends TestCase {
29:
30: public static int foo() {
31: return 0;
32: }
33:
34: public static int bar(int i) {
35: return i;
36: }
37:
38: private SimpleContext context;
39:
40: @Override
41: protected void setUp() throws Exception {
42: context = new SimpleContext();
43:
44: // function ns:f()
45: context.setFunction("ns", "f", BindingsTest.class
46: .getMethod("foo"));
47:
48: // function g()
49: context.setFunction("", "g", BindingsTest.class.getMethod(
50: "bar", new Class[] { int.class }));
51:
52: // variable v
53: context.setVariable("v", new ObjectValueExpression(new Long(0),
54: long.class));
55: }
56:
57: public void testSerialize() throws Exception {
58: Bindings bindings = null;
59:
60: bindings = new Bindings(null, null);
61: assertEquals(bindings, deserialize(serialize(bindings)));
62:
63: bindings = parse("${ns:f()+v+g(1)+x}").bind(
64: context.getFunctionMapper(),
65: context.getVariableMapper());
66: assertEquals(bindings, deserialize(serialize(bindings)));
67: }
68:
69: public void testEqualsAndHashcode() throws Exception {
70: Bindings bindings1 = null;
71: Bindings bindings2 = null;
72:
73: bindings1 = new Bindings(null, null);
74: bindings2 = new Bindings(null, null);
75: assertEquals(bindings1, bindings2);
76: assertEquals(bindings1.hashCode(), bindings2.hashCode());
77:
78: bindings1 = new Bindings(new Method[0], new ValueExpression[0]);
79: bindings2 = new Bindings(null, null);
80: assertEquals(bindings1, bindings2);
81: assertEquals(bindings1.hashCode(), bindings2.hashCode());
82:
83: Tree tree = parse("${ns:f()+v+g(1)}+x");
84: bindings1 = tree.bind(context.getFunctionMapper(), context
85: .getVariableMapper());
86: bindings2 = tree.bind(context.getFunctionMapper(), context
87: .getVariableMapper());
88: assertEquals(bindings1, bindings2);
89: assertEquals(bindings1.hashCode(), bindings2.hashCode());
90: }
91: }
|