001: /********************************************************************************
002: * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
003: * Copyright (c) 2004, Joerg and Kai Gellien
004: * All rights reserved.
005: *
006: * The Software is provided under the terms of the Common Public License 1.0
007: * as provided with the distribution of DDTUnit in the file cpl-v10.html.
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of the authors or DDTUnit, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package junitx.ddtunit.data;
037:
038: import java.util.Collection;
039: import java.util.Iterator;
040:
041: import junit.framework.AssertionFailedError;
042: import junitx.ddtunit.DDTException;
043: import junitx.framework.ArrayAssert;
044: import junitx.framework.Assert;
045:
046: /**
047: * Class contains all information to execute an assert action between actual and
048: * expected object.
049: *
050: * @author jg
051: */
052: public class ObjectAsserter extends AssertObject {
053:
054: /**
055: * @param id of assert
056: * @param type of expected object
057: * @param action to process
058: */
059: public ObjectAsserter(String id, String type, String action) {
060: super (id, type, action);
061: }
062:
063: /**
064: * defines equals action analogue to JUnit assertEquals
065: */
066: public static final String ASSERT_ACTION_ISEQUAL = "ISEQUAL";
067:
068: /**
069: * defines equals action analogue to JUnit assertEquals
070: */
071: public static final String ASSERT_ACTION_ISNOTEQUAL = "ISNOTEQUAL";
072:
073: /**
074: * defines equals action analogue to JUnit assertSame
075: */
076: public static final String ASSERT_ACTION_ISSAME = "ISSAME";
077:
078: /**
079: * defines equals action analogue to JUnit assertEquals
080: */
081: public static final String ASSERT_ACTION_ISNOTSAME = "ISNOTSAME";
082:
083: /**
084: * defines equals action analogue to JUnit assertEquals
085: */
086: public static final String ASSERT_ACTION_ISNULL = "ISNULL";
087:
088: /**
089: * defines equals action analogue to JUnit assertEquals
090: */
091: public static final String ASSERT_ACTION_ISNOTNULL = "ISNOTNULL";
092:
093: private boolean validated = false;
094:
095: public static final String ASSERT_ACTION_ISTRUE = "ISTRUE";
096:
097: public static final String ASSERT_ACTION_ISFALSE = "ISFALSE";
098:
099: public static final String ASSERT_ACTION_ISGT = "ISGT";
100:
101: public static final String ASSERT_ACTION_ISNOTGT = "ISNOTGT";
102:
103: public static final String ASSERT_ACTION_ISLT = "ISLT";
104:
105: public static final String ASSERT_ACTION_ISNOTLT = "ISNOTLT";
106:
107: public static final String ASSERT_ACTION_ISCONTAINEDIN = "ISCONTAINEDIN";
108:
109: public static final String ASSERT_ACTION_ISNOTCONTAINEDIN = "ISNOTCONTAINEDIN";
110:
111: public static final String ASSERT_ACTION_ISINRANGE = "ISINRANGE";
112:
113: public static final String ASSERT_ACTION_ISNOTINRANGE = "ISNOTINRANGE";
114:
115: /**
116: * Validate expected object against actual object using assert action.
117: *
118: * @param mark validation if set to true, else reprocessing is possible.
119: */
120: public void validate(boolean mark) {
121: if (!this .actualObjectSet) {
122: throw new DDTException(
123: "Actual object for assertion not provided");
124: } else if (ASSERT_ACTION_ISEQUAL.equals(this .action
125: .toUpperCase())) {
126: this .markAsProcessed = mark;
127: isSameType();
128: if (this .getActualType() != null
129: && this .getActualType().startsWith("[L")
130: && this .getType().startsWith("[L")) {
131: ArrayAssert.assertEquals("Wrong isEqual assert ("
132: + this .getId() + ") on arrays", (Object[]) this
133: .getValue(), (Object[]) this .getActualObject());
134: } else {
135: if ("java.lang.String".equals(this .getActualType())
136: && "java.lang.String".equals(this .getType())) {
137: String actualText = (String) getActualObject();
138: String expectedText = (String) getValue();
139: String[] actual = null;
140: String[] expected = null;
141: if (actualText != null
142: && actualText.indexOf("\r\n") > -1) {
143: actual = actualText.split("\r\n");
144: } else {
145: actual = actualText.split("\n");
146: }
147: if (expectedText != null
148: && expectedText.indexOf("\r\n") > -1) {
149: expected = expectedText.split("\r\n");
150: } else {
151: expected = expectedText.split("\n");
152: }
153: ArrayAssert
154: .assertEquals(
155: "Wrong isEqual assert on (multiline) string",
156: expected, actual);
157: } else {
158: Assert.assertEquals("Wrong isEqual assert ("
159: + this .getId() + ")", this .getValue(),
160: getActualObject());
161: }
162: }
163: } else if (ASSERT_ACTION_ISNOTEQUAL.equals(this .action
164: .toUpperCase())) {
165: this .markAsProcessed = mark;
166: Assert.assertNotEquals("Wrong isNotEqual assert ("
167: + this .getId() + ")", this .getValue(),
168: getActualObject());
169: } else if (ASSERT_ACTION_ISSAME.equals(this .action
170: .toUpperCase())) {
171: this .markAsProcessed = mark;
172: isSameType();
173: Assert.assertSame("Wrong isSame assert (" + this .getId()
174: + ")", this .getValue(), getActualObject());
175: } else if (ASSERT_ACTION_ISNOTSAME.equals(this .action
176: .toUpperCase())) {
177: this .markAsProcessed = mark;
178: Assert.assertNotSame("Wrong isNotSame assert ("
179: + this .getId() + ")", this .getValue(),
180: getActualObject());
181: } else if (ASSERT_ACTION_ISNULL.equals(this .action
182: .toUpperCase())) {
183: this .markAsProcessed = mark;
184: isSameType();
185: Assert.assertNull("Object should be null on assert ("
186: + this .getId() + ")", getActualObject());
187: } else if (ASSERT_ACTION_ISNOTNULL.equals(this .action
188: .toUpperCase())) {
189: this .markAsProcessed = mark;
190: isSameType();
191: Assert.assertNotNull(
192: "Object should not be null on assert ("
193: + this .getId() + ")", getActualObject());
194: } else if (ASSERT_ACTION_ISTRUE.equals(this .action
195: .toUpperCase())) {
196: this .markAsProcessed = mark;
197: if (Boolean.class.isInstance(getActualObject())) {
198: isSameType();
199: Assert.assertTrue("Object should be true on assert ("
200: + this .getId() + ")",
201: ((Boolean) getActualObject()).booleanValue());
202: } else {
203: throw new UnsupportedOperationException(
204: "Wrong type used under assert action 'ISTRUE':"
205: + getActualObject());
206: }
207: } else if (ASSERT_ACTION_ISFALSE.equals(this .action
208: .toUpperCase())) {
209: this .markAsProcessed = mark;
210: if (Boolean.class.isInstance(getActualObject())) {
211: isSameType();
212: Assert.assertFalse("Object should be false on assert ("
213: + this .getId() + ")",
214: ((Boolean) getActualObject()).booleanValue());
215: } else {
216: throw new UnsupportedOperationException(
217: "Wrong type used under assert action 'ISFALSE':"
218: + getActualObject());
219: }
220: } else if (ASSERT_ACTION_ISGT.equals(this .action.toUpperCase())
221: || ASSERT_ACTION_ISNOTGT.equals(this .action
222: .toUpperCase())
223: || ASSERT_ACTION_ISLT.equals(this .action.toUpperCase())
224: || ASSERT_ACTION_ISNOTLT.equals(this .action
225: .toUpperCase())
226: || ASSERT_ACTION_ISINRANGE.equals(this .action
227: .toUpperCase())
228: || ASSERT_ACTION_ISNOTINRANGE.equals(this .action
229: .toUpperCase())) {
230: this .markAsProcessed = mark;
231: // if doing range checks do not check expected and actual types
232: if (!(this .getValue() instanceof IRange)) {
233: isSameType();
234: }
235: if (Comparable.class.isInstance(this .actualObject)) {
236: if (ASSERT_ACTION_ISGT
237: .equals(this .action.toUpperCase())) {
238: if (((Comparable) this .getActualObject())
239: .compareTo(this .getValue()) <= 0) {
240: Assert.fail("Expected action: "
241: + this .getAction() + ". Got: "
242: + this .getActualObject() + " <= "
243: + this .getValue() + " on assert ("
244: + this .getId() + ")");
245: }
246: } else if (ASSERT_ACTION_ISNOTGT.equals(this .action
247: .toUpperCase())) {
248: if (((Comparable) this .getActualObject())
249: .compareTo(this .getValue()) > 0) {
250: Assert.fail("Expected action: "
251: + this .getAction() + ". Got: "
252: + this .getActualObject() + " > "
253: + this .getValue() + "on assert ("
254: + this .getId() + ")");
255: }
256: } else if (ASSERT_ACTION_ISLT.equals(this .action
257: .toUpperCase())) {
258: if (((Comparable) this .getActualObject())
259: .compareTo(this .getValue()) >= 0) {
260: Assert.fail("Expected action: "
261: + this .getAction() + ". Got: "
262: + this .getActualObject() + " >= "
263: + this .getValue() + " on assert ("
264: + this .getId() + ")");
265: }
266: } else if (ASSERT_ACTION_ISNOTLT.equals(this .action
267: .toUpperCase())) {
268: if (((Comparable) this .getActualObject())
269: .compareTo(this .getValue()) >= 0) {
270: Assert.fail("Expected action: "
271: + this .getAction() + ". Got: "
272: + this .getActualObject() + " > "
273: + this .getValue() + " on assert ("
274: + this .getId() + ")");
275: }
276: } else if (ASSERT_ACTION_ISINRANGE.equals(this .action
277: .toUpperCase())) {
278: if (!((IRange) this .getValue())
279: .isInRange((Comparable) this
280: .getActualObject())) {
281: Assert.fail("Expected action: "
282: + this .getAction() + ". Got: "
283: + this .getActualObject() + " not in "
284: + this .getValue() + " on assert ("
285: + this .getId() + ")");
286: }
287: } else if (ASSERT_ACTION_ISNOTINRANGE
288: .equals(this .action.toUpperCase())) {
289: if (((IRange) this .getValue())
290: .isInRange((Comparable) this
291: .getActualObject())) {
292: Assert.fail("Expected action: "
293: + this .getAction() + ". Got: "
294: + this .getActualObject() + " in "
295: + this .getValue() + " on assert ("
296: + this .getId() + ")");
297: }
298: }
299: } else {
300: throw new DDTException(
301: "Asserted type does not implement Comparable interface: "
302: + this .getType());
303: }
304: } else if (ASSERT_ACTION_ISCONTAINEDIN.equals(this .action
305: .toUpperCase())
306: || ASSERT_ACTION_ISNOTCONTAINEDIN.equals(this .action
307: .toUpperCase())) {
308: this .markAsProcessed = mark;
309: boolean match = false;
310: RuntimeException assertEx = null;
311: if (ASSERT_ACTION_ISCONTAINEDIN.equals(this .action
312: .toUpperCase())) {
313: // actual type == array type do separate contain check
314: if (this .getActualType().startsWith("[L")) {
315: match = isArrayContainedinList(assertEx);
316: if (!match) {
317: throw assertEx;
318: }
319: } else {
320: Assert.assertTrue(
321: "Object should be member in List on assert ("
322: + this .getId() + ")",
323: ((Collection) this .getValue())
324: .contains(getActualObject()));
325: }
326: } else {
327: // actual type == array type do separate contain check
328: if (this .getActualType().startsWith("[L")) {
329: match = isArrayContainedinList(assertEx);
330: if (match) {
331: throw new AssertionFailedError(
332: "Array not found in expected list on assert ("
333: + this .getId() + ")");
334: }
335: } else {
336: Assert.assertFalse(
337: "Object should not be contained in List on assert ("
338: + this .getId() + ")",
339: ((Collection) this .getValue())
340: .contains(getActualObject()));
341: }
342: }
343: } else {
344: throw new DDTException("Unsupported assert action \""
345: + this .action + "\"");
346: }
347: }
348:
349: /**
350: * Check if actual object array is contained in expected list of arrays.
351: *
352: * @param assertEx is set if no match is found during check
353: * @return true, if match is found, false otherwise
354: */
355: private boolean isArrayContainedinList(RuntimeException assertEx) {
356: boolean match = false;
357: for (Iterator iter = ((Collection) this .getValue()).iterator(); iter
358: .hasNext();) {
359: Object array = iter.next();
360: try {
361: ArrayAssert.assertEquals((Object[]) array,
362: (Object[]) this .getActualObject());
363: match = true;
364: break;
365: } catch (RuntimeException ex) {
366: // ignore error, there might be another match
367: assertEx = ex;
368: }
369: }
370: return match;
371: }
372:
373: /**
374: *
375: */
376: private void isSameType() {
377: Assert.assertEquals(
378: "Class type differs between assert objects, ",
379: getType(), (this .actualObject != null ? getActualType()
380: : getType()));
381: }
382:
383: /**
384: * @return true if this assertion is allready processed
385: */
386: public boolean isValidated() {
387: return validated;
388: }
389:
390: public Object clone() {
391: ObjectAsserter newObj = new ObjectAsserter(this.getId(), this
392: .getType(), this.getAction());
393: newObj.setValue(this.getValue());
394: return newObj;
395: }
396:
397: }
|