001: //$Id: ExceptionAsserter.java 209 2005-12-18 17:45:49Z jg_hamburg $
002: /********************************************************************************
003: * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
004: * Copyright (c) 2004, Joerg and Kai Gellien
005: * All rights reserved.
006: *
007: * The Software is provided under the terms of the Common Public License 1.0
008: * as provided with the distribution of DDTUnit in the file cpl-v10.html.
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * + Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * + Redistributions in binary form must reproduce the above
017: * copyright notice, this list of conditions and the following
018: * disclaimer in the documentation and/or other materials provided
019: * with the distribution.
020: *
021: * + Neither the name of the authors or DDTUnit, nor the
022: * names of its contributors may be used to endorse or promote
023: * products derived from this software without specific prior
024: * written permission.
025: *
026: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
027: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
028: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
029: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
030: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
031: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
032: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
033: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
036: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: ********************************************************************************/package junitx.ddtunit.data;
038:
039: import junit.framework.AssertionFailedError;
040: import junitx.ddtunit.DDTException;
041: import junitx.framework.ThrowableAssert;
042:
043: /**
044: * @author jg
045: */
046: public class ExceptionAsserter extends AssertObject {
047:
048: /**
049: * defines equals action analogue to JUnit assertEquals
050: */
051: public static final String EXCEPTION_ACTION_ISEQUAL = "ISEQUAL";
052:
053: /**
054: * defines equals action analogue to JUnit assertEquals
055: */
056: public static final String EXCEPTION_ACTION_ISSIMILAR = "ISSIMILAR";
057:
058: /**
059: * defines equals action analogue to JUnit assertEquals
060: */
061: public static final String EXCEPTION_ACTION_ISINSTANCEOF = "ISINSTANCEOF";
062:
063: /**
064: * @param assertId
065: * @param assertType
066: * @param exceptionAction
067: */
068: public ExceptionAsserter(String exceptionId, String exceptionType,
069: String exceptionAction) {
070: super (exceptionId, exceptionType, exceptionAction);
071: }
072:
073: /**
074: * validate internal expression.
075: *
076: * @param mark as processed if set to true, else multiple validation is
077: * possible.
078: *
079: * @see junitx.ddtunit.data.AssertObject#validate()
080: */
081: public void validate(boolean mark) {
082: if (!this .actualObjectSet) {
083: throw new DDTException(
084: "Actual object for assertion not provided");
085: } else if (EXCEPTION_ACTION_ISEQUAL.equals(this .action
086: .toUpperCase())) {
087: this .markAsProcessed = mark;
088: ThrowableAssert.assertEquals("Unexpected exception",
089: (Throwable) getValue(),
090: (Throwable) getActualObject());
091: } else if (EXCEPTION_ACTION_ISSIMILAR.equals(this .action
092: .toUpperCase())) {
093: this .markAsProcessed = mark;
094: ThrowableAssert.assertSimilar("Unexpected exception",
095: (Throwable) getValue(),
096: (Throwable) getActualObject());
097: } else if (EXCEPTION_ACTION_ISINSTANCEOF.equals(this .action
098: .toUpperCase())) {
099: this .markAsProcessed = mark;
100: try {
101: Class expectedClazz = Class.forName(this .getType());
102: if (!expectedClazz.isInstance(this .getActualObject())) {
103: throw new AssertionFailedError(
104: "Unexpected exception: expected <"
105: + this .getType() + "> got <"
106: + this .getActualType() + ">");
107: }
108: } catch (ClassNotFoundException ex) {
109: throw new DDTException(
110: "Error instanciating expected exception of type "
111: + this .getType(), ex);
112: }
113: } else {
114: throw new DDTException("Unsupported action " + this .action);
115: }
116: }
117:
118: public Object clone() {
119: ExceptionAsserter newObj = new ExceptionAsserter(this.getId(),
120: this.getType(), this.getAction());
121: newObj.setValue(this.getValue());
122: return newObj;
123: }
124:
125: }
|