View Javadoc

1   //$Id: AssertObject.java 253 2006-09-11 21:15:25Z jg_hamburg $
2   /********************************************************************************
3    * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
4    * Copyright (c) 2004, Joerg and Kai Gellien
5    * All rights reserved.
6    *
7    * The Software is provided under the terms of the Common Public License 1.0
8    * as provided with the distribution of DDTUnit in the file cpl-v10.html.
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   *     + Redistributions of source code must retain the above copyright
14   *       notice, this list of conditions and the following disclaimer.
15   *
16   *     + Redistributions in binary form must reproduce the above
17   *       copyright notice, this list of conditions and the following
18   *       disclaimer in the documentation and/or other materials provided
19   *       with the distribution.
20   *
21   *     + Neither the name of the authors or DDTUnit, nor the
22   *       names of its contributors may be used to endorse or promote
23   *       products derived from this software without specific prior
24   *       written permission.
25   *
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
30   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   ********************************************************************************/
38  package junitx.ddtunit.data;
39  
40  import java.util.Set;
41  
42  import junitx.ddtunit.util.ClassAnalyser;
43  
44  /**
45   * @author jg
46   */
47  public abstract class AssertObject extends TypedObject {
48  
49      protected String action;
50  
51      protected Object actualObject;
52  
53      protected String actualType;
54  
55      protected boolean actualObjectSet = false;
56  
57      protected boolean markAsProcessed = false;
58  
59      /**
60       * @param id
61       * @param type
62       */
63      public AssertObject(String id, String type, String action) {
64          super(id, type);
65          if (action != null && !"".equals(action)) {
66              this.action = action;
67          } else {
68              throw new IllegalArgumentException("An action must be specified");
69          }
70      }
71  
72      /**
73       * Process assertion specified in this object. Should throw exception on
74       * assert failure.
75       */
76      abstract public void validate(boolean mark);
77  
78      public String getAction() {
79          return action;
80      }
81  
82      public Object getActualObject() {
83          return actualObject;
84      }
85  
86      /**
87       * Associate actual and expected object. <br/>If actual and expected object
88       * are not of same class type an exception is thrown
89       * 
90       * @param actualObject
91       */
92      public void setActualObject(Object actualObject) {
93          // check for identical types of expected and
94          if (actualObject != null) {
95              this.actualType = actualObject.getClass().getName();
96              // if (!validateSubType(actualObject.getClass())) {
97              // throw new DDTException("Class types of actual ("
98              // + this.actualType + ") is no subtype of expected ("
99              // + 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 superSet = ClassAnalyser.getSuperElements(actualClazz);
112             if (this.actualType.equals(getType())
113                     || superSet.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(this.getId())
131             .append("):");
132         return sb.toString();
133     }
134 }