001: //--------------------------------------------------------------------------
002: // Copyright (c) 2004, Drew Davidson and Luke Blanshard
003: // All rights reserved.
004: //
005: // Redistribution and use in source and binary forms, with or without
006: // modification, are permitted provided that the following conditions are
007: // met:
008: //
009: // Redistributions of source code must retain the above copyright notice,
010: // this list of conditions and the following disclaimer.
011: // Redistributions in binary form must reproduce the above copyright
012: // notice, this list of conditions and the following disclaimer in the
013: // documentation and/or other materials provided with the distribution.
014: // Neither the name of the Drew Davidson nor the names of its contributors
015: // may be used to endorse or promote products derived from this software
016: // without specific prior written permission.
017: //
018: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021: // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022: // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
023: // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
024: // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
025: // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
027: // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
028: // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
029: // DAMAGE.
030: //--------------------------------------------------------------------------
031: package org.ognl.test;
032:
033: import java.io.PrintWriter;
034: import java.io.StringWriter;
035: import java.lang.reflect.Array;
036: import junit.framework.TestCase;
037: import ognl.Ognl;
038: import ognl.OgnlContext;
039: import ognl.OgnlException;
040: import ognl.SimpleNode;
041:
042: public class OgnlTestCase extends TestCase {
043: protected OgnlContext context;
044: private String expressionString;
045: private SimpleNode expression;
046: private Object expectedResult;
047: private Object root;
048: private boolean hasSetValue;
049: private Object setValue;
050: private boolean hasExpectedAfterSetResult;
051: private Object expectedAfterSetResult;
052:
053: /*===================================================================
054: Public static methods
055: ===================================================================*/
056: /**
057: Returns true if object1 is equal to object2 in either the
058: sense that they are the same object or, if both are non-null
059: if they are equal in the <CODE>equals()</CODE> sense.
060: */
061: public static boolean isEqual(Object object1, Object object2) {
062: boolean result = false;
063:
064: if (object1 == object2) {
065: result = true;
066: } else {
067: if ((object1 != null) && object1.getClass().isArray()) {
068: if ((object2 != null) && object2.getClass().isArray()
069: && (object2.getClass() == object1.getClass())) {
070: result = (Array.getLength(object1) == Array
071: .getLength(object2));
072: if (result) {
073: for (int i = 0, icount = Array
074: .getLength(object1); result
075: && (i < icount); i++) {
076: result = isEqual(Array.get(object1, i),
077: Array.get(object2, i));
078: }
079: }
080: }
081: } else {
082: result = (object1 != null) && (object2 != null)
083: && object1.equals(object2);
084: }
085: }
086: return result;
087: }
088:
089: /*===================================================================
090: Constructors
091: ===================================================================*/
092: public OgnlTestCase() {
093: super ();
094: }
095:
096: public OgnlTestCase(String name) {
097: super (name);
098: }
099:
100: public OgnlTestCase(String name, Object root,
101: String expressionString, Object expectedResult,
102: Object setValue, Object expectedAfterSetResult) {
103: this (name, root, expressionString, expectedResult, setValue);
104: this .hasExpectedAfterSetResult = true;
105: this .expectedAfterSetResult = expectedAfterSetResult;
106: }
107:
108: public OgnlTestCase(String name, Object root,
109: String expressionString, Object expectedResult,
110: Object setValue) {
111: this (name, root, expressionString, expectedResult);
112: this .hasSetValue = true;
113: this .setValue = setValue;
114: }
115:
116: public OgnlTestCase(String name, Object root,
117: String expressionString, Object expectedResult) {
118: this (name);
119: this .root = root;
120: this .expressionString = expressionString;
121: this .expectedResult = expectedResult;
122: }
123:
124: /*===================================================================
125: Public methods
126: ===================================================================*/
127: public String getExpressionDump(SimpleNode node) {
128: StringWriter writer = new StringWriter();
129:
130: node.dump(new PrintWriter(writer), " ");
131: return writer.toString();
132: }
133:
134: public String getExpressionString() {
135: return expressionString;
136: }
137:
138: public SimpleNode getExpression() throws OgnlException {
139: if (expression == null) {
140: expression = (SimpleNode) Ognl
141: .parseExpression(expressionString);
142: }
143: return expression;
144: }
145:
146: public Object getExpectedResult() {
147: return expectedResult;
148: }
149:
150: /*===================================================================
151: Overridden methods
152: ===================================================================*/
153: protected void runTest() throws Exception {
154: Object testedResult = null;
155:
156: try {
157: SimpleNode expr;
158:
159: testedResult = expectedResult;
160: expr = getExpression();
161: /*
162: PrintWriter writer = new PrintWriter(System.err);
163: System.err.println(expr.toString());
164: expr.dump(writer, "");
165: writer.flush();
166: */
167: assertTrue(isEqual(Ognl.getValue(expr, context, root),
168: expectedResult));
169: if (hasSetValue) {
170: testedResult = hasExpectedAfterSetResult ? expectedAfterSetResult
171: : setValue;
172: Ognl.setValue(expr, context, root, setValue);
173: assertTrue(isEqual(Ognl.getValue(expr, context, root),
174: testedResult));
175: }
176: } catch (Exception ex) {
177: if (testedResult instanceof Class) {
178: assertTrue(((Class) testedResult).isAssignableFrom(ex
179: .getClass()));
180: } else {
181: throw ex;
182: }
183: }
184: }
185:
186: protected void setUp() {
187: context = (OgnlContext) Ognl.createDefaultContext(null);
188: }
189: }
|