001 /*
002 * $Id: OgnlTestCase.java 1188000 2011-10-23 23:10:24Z mcucchiara $
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements. See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership. The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License. You may obtain a copy of the License at
010 *
011 * http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied. See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 */
020 package org.apache.commons.ognl.test;
021
022 import junit.framework.Assert;
023 import org.apache.commons.ognl.Ognl;
024 import org.apache.commons.ognl.OgnlContext;
025 import org.apache.commons.ognl.SimpleNode;
026 import org.junit.Before;
027 import org.junit.Test;
028
029 import java.io.PrintWriter;
030 import java.io.StringWriter;
031 import java.lang.reflect.Array;
032
033 public abstract class OgnlTestCase
034 {
035
036 protected OgnlContext _context;
037
038 private String _expressionString;
039
040 private SimpleNode _expression;
041
042 private Object _expectedResult;
043
044 private Object _root;
045
046 protected boolean _compileExpressions = true;
047
048 private boolean hasSetValue;
049
050 private Object setValue;
051
052 private boolean hasExpectedAfterSetResult;
053
054 private Object expectedAfterSetResult;
055
056 /*
057 * =================================================================== Public static methods
058 * ===================================================================
059 */
060
061 /**
062 * Returns true if object1 is equal to object2 in either the sense that they are the same object or, if both are
063 * non-null if they are equal in the <CODE>equals()</CODE> sense.
064 */
065 public static boolean isEqual(Object object1, Object object2) {
066 boolean result = false;
067
068 if (object1 == object2) {
069 result = true;
070 } else {
071 if ((object1 != null) && object1.getClass().isArray()) {
072 if ((object2 != null) && object2.getClass().isArray() && (object2.getClass() == object1.getClass())) {
073 result = (Array.getLength(object1) == Array.getLength(object2));
074 if (result) {
075 for (int i = 0, icount = Array.getLength(object1); result && (i < icount); i++) {
076 result = isEqual(Array.get(object1, i), Array.get(object2, i));
077 }
078 }
079 }
080 } else {
081 result = (object1 != null) && (object2 != null) && object1.equals(object2);
082 }
083 }
084 return result;
085 }
086
087 /*
088 * =================================================================== Constructors
089 * ===================================================================
090 */
091 public OgnlTestCase(String name, Object root, String expressionString, Object expectedResult)
092 {
093 this( name, root, expressionString, expectedResult, null, false, null, false);
094 }
095
096 public OgnlTestCase( String name, Object root, String expressionString, Object expectedResult, Object setValue,
097 Object expectedAfterSetResult )
098 {
099 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 * =================================================================== Public methods
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 * =================================================================== Overridden methods
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 }