001: //$Id: AssertObject.java 253 2006-09-11 21:15:25Z 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 java.util.Set;
040:
041: import junitx.ddtunit.util.ClassAnalyser;
042:
043: /**
044: * @author jg
045: */
046: public abstract class AssertObject extends TypedObject {
047:
048: protected String action;
049:
050: protected Object actualObject;
051:
052: protected String actualType;
053:
054: protected boolean actualObjectSet = false;
055:
056: protected boolean markAsProcessed = false;
057:
058: /**
059: * @param id
060: * @param type
061: */
062: public AssertObject(String id, String type, String action) {
063: super (id, type);
064: if (action != null && !"".equals(action)) {
065: this .action = action;
066: } else {
067: throw new IllegalArgumentException(
068: "An action must be specified");
069: }
070: }
071:
072: /**
073: * Process assertion specified in this object. Should throw exception on
074: * assert failure.
075: */
076: abstract public void validate(boolean mark);
077:
078: public String getAction() {
079: return action;
080: }
081:
082: public Object getActualObject() {
083: return actualObject;
084: }
085:
086: /**
087: * Associate actual and expected object. <br/>If actual and expected object
088: * are not of same class type an exception is thrown
089: *
090: * @param actualObject
091: */
092: public void setActualObject(Object actualObject) {
093: // check for identical types of expected and
094: if (actualObject != null) {
095: this .actualType = actualObject.getClass().getName();
096: // if (!validateSubType(actualObject.getClass())) {
097: // throw new DDTException("Class types of actual ("
098: // + this.actualType + ") is no subtype of expected ("
099: // + getType() + ") objects in assert '" + this.getId()
100: // + "'.");
101: // }
102: }
103: this .actualObject = actualObject;
104: this .actualObjectSet = true;
105: }
106:
107: private boolean validateSubType(Class actualClazz) {
108: boolean check = false;
109: try {
110: Class expectedClazz = Class.forName(getType());
111: Set super Set = ClassAnalyser.getSuperElements(actualClazz);
112: if (this .actualType.equals(getType())
113: || super Set.contains(expectedClazz)) {
114: check = true;
115: }
116: } catch (Exception e) {
117: // ignore exception: validation is false
118: }
119: return check;
120: }
121:
122: /**
123: * @return
124: */
125: protected String getActualType() {
126: return this .actualType;
127: }
128:
129: public String toString() {
130: StringBuffer sb = new StringBuffer("Assert(").append(
131: this .getId()).append("):");
132: return sb.toString();
133: }
134: }
|