1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.commons.ognl.test;
21
22 import junit.framework.Assert;
23 import org.apache.commons.ognl.Ognl;
24 import org.apache.commons.ognl.OgnlContext;
25 import org.apache.commons.ognl.SimpleNode;
26 import org.junit.Before;
27 import org.junit.Test;
28
29 import java.io.PrintWriter;
30 import java.io.StringWriter;
31 import java.lang.reflect.Array;
32
33 public abstract class OgnlTestCase
34 {
35
36 protected OgnlContext _context;
37
38 private String _expressionString;
39
40 private SimpleNode _expression;
41
42 private Object _expectedResult;
43
44 private Object _root;
45
46 protected boolean _compileExpressions = true;
47
48 private boolean hasSetValue;
49
50 private Object setValue;
51
52 private boolean hasExpectedAfterSetResult;
53
54 private Object expectedAfterSetResult;
55
56
57
58
59
60
61
62
63
64
65 public static boolean isEqual(Object object1, Object object2) {
66 boolean result = false;
67
68 if (object1 == object2) {
69 result = true;
70 } else {
71 if ((object1 != null) && object1.getClass().isArray()) {
72 if ((object2 != null) && object2.getClass().isArray() && (object2.getClass() == object1.getClass())) {
73 result = (Array.getLength(object1) == Array.getLength(object2));
74 if (result) {
75 for (int i = 0, icount = Array.getLength(object1); result && (i < icount); i++) {
76 result = isEqual(Array.get(object1, i), Array.get(object2, i));
77 }
78 }
79 }
80 } else {
81 result = (object1 != null) && (object2 != null) && object1.equals(object2);
82 }
83 }
84 return result;
85 }
86
87
88
89
90
91 public OgnlTestCase(String name, Object root, String expressionString, Object expectedResult)
92 {
93 this( name, root, expressionString, expectedResult, null, false, null, false);
94 }
95
96 public OgnlTestCase( String name, Object root, String expressionString, Object expectedResult, Object setValue,
97 Object expectedAfterSetResult )
98 {
99 this( name, root, expressionString, expectedResult, setValue, setValue != null, expectedAfterSetResult,
100 expectedAfterSetResult != null );
101 }
102
103 public OgnlTestCase( String name, Object root, String expressionString, Object expectedResult, Object setValue,
104 boolean hasSetValue, Object expectedAfterSetResult, boolean hasExpectedAfterSetResult )
105 {
106 this._root = root;
107 this._expressionString = expressionString;
108 this._expectedResult = expectedResult;
109
110 this.hasExpectedAfterSetResult = hasExpectedAfterSetResult;
111 this.expectedAfterSetResult = expectedAfterSetResult;
112 this.hasSetValue = hasSetValue;
113 this.setValue = setValue;
114
115 }
116
117
118
119
120
121 public String getExpressionDump(SimpleNode node) {
122 StringWriter writer = new StringWriter();
123
124 node.dump(new PrintWriter(writer), " ");
125 return writer.toString();
126 }
127
128 public String getExpressionString() {
129 return _expressionString;
130 }
131
132 public SimpleNode getExpression()
133 throws Exception {
134 if (_expression == null) {
135 _expression = (SimpleNode) Ognl.parseExpression(_expressionString);
136 }
137
138 if (_compileExpressions) {
139 _expression = (SimpleNode) Ognl.compileExpression(_context, _root, _expressionString);
140 }
141
142 return _expression;
143 }
144
145 public Object getExpectedResult() {
146 return _expectedResult;
147 }
148
149 public static void assertEquals(Object expected, Object actual) {
150 if (expected != null && expected.getClass().isArray() && actual != null && actual.getClass().isArray()) {
151
152 Assert.assertEquals(Array.getLength(expected), Array.getLength(actual));
153
154 int length = Array.getLength(expected);
155
156 for (int i = 0; i < length; i++) {
157 Object aexpected = Array.get(expected, i);
158 Object aactual = Array.get(actual, i);
159
160 if (aexpected != null && aactual != null && Boolean.class.isAssignableFrom(aexpected.getClass())) {
161 Assert.assertEquals(aexpected.toString(), aactual.toString());
162 } else
163 OgnlTestCase.assertEquals(aexpected, aactual);
164 }
165 } else if (expected != null && actual != null && Character.class.isInstance(expected)
166 && Character.class.isInstance(actual)) {
167
168 Assert.assertEquals(((Character) expected).charValue(), ((Character) actual).charValue());
169 } else {
170
171 Assert.assertEquals(expected, actual);
172 }
173 }
174
175
176
177
178
179 @Test
180 public void runTest()
181 throws Exception {
182 Object testedResult = null;
183
184 try {
185 SimpleNode expr;
186
187 testedResult = _expectedResult;
188 expr = getExpression();
189
190 assertEquals(_expectedResult, Ognl.getValue(expr, _context, _root));
191
192 if (hasSetValue) {
193 testedResult = hasExpectedAfterSetResult ? expectedAfterSetResult : setValue;
194 Ognl.setValue(expr, _context, _root, setValue);
195
196 assertEquals(testedResult, Ognl.getValue(expr, _context, _root));
197 }
198
199 } catch (Exception ex) {
200 if (RuntimeException.class.isInstance(ex) && ex.getCause() != null
201 && Exception.class.isAssignableFrom( ex.getCause().getClass())) {
202 ex = (Exception) ex.getCause( );
203 }
204
205 if (testedResult instanceof Class) {
206 Assert.assertTrue(Exception.class.isAssignableFrom((Class) testedResult));
207 } else {
208 throw ex;
209 }
210 }
211 }
212
213 @Before
214 public void setUp() {
215 _context = (OgnlContext) Ognl.createDefaultContext(null);
216 }
217 }