001: /*
002: * $Id: UnsupportedType.java,v 1.8 2004/12/02 21:22:30 spal Exp $
003: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/types/UnsupportedType.java,v $
004: * SQLUnit - a test harness for unit testing database stored procedures.
005: * Copyright (C) 2003 The SQLUnit Team
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021: package net.sourceforge.sqlunit.types;
022:
023: import net.sourceforge.sqlunit.IErrorCodes;
024: import net.sourceforge.sqlunit.IType;
025: import net.sourceforge.sqlunit.SQLUnitException;
026: import net.sourceforge.sqlunit.SymbolTable;
027:
028: import org.apache.log4j.Logger;
029:
030: /**
031: * Defines behavior for a type which is not supported by SQLUnit. This also
032: * serves as the superclass for all the other types.
033: * @author Ralph Brendler (rbrendler@users.sourceforge.net)
034: * @author Sujit Pal (spal@users.sourceforge.net)
035: * @version $Revision: 1.8 $
036: * @sqlunit.type name="UnsupportedType" input="No" output="No" sortable="No"
037: * wraps="n/a"
038: * @sqlunit.typename name="DATALINK" server="Any"
039: * @sqlunit.typename name="DISTINCT" server="Any"
040: * @sqlunit.typename name="NULL" server="Any"
041: * @sqlunit.typename name="REF" server="Any"
042: * @sqlunit.typename name="STRUCT" server="Any"
043: */
044: public class UnsupportedType implements IType {
045:
046: private static final Logger LOG = Logger
047: .getLogger(UnsupportedType.class);
048:
049: private String name;
050: private int id;
051: private Object value;
052:
053: /**
054: * Default constructor.
055: */
056: public UnsupportedType() {
057: // empty default constructor
058: }
059:
060: /**
061: * Returns the XML name for this type.
062: * @return the XML name for this type.
063: */
064: public final String getName() {
065: return name;
066: }
067:
068: /**
069: * Sets the XML name for this type.
070: * @param name the XML name for this type.
071: */
072: public final void setName(final String name) {
073: this .name = name;
074: }
075:
076: /**
077: * Returns the SQL Type Code for this type.
078: * @return the SQL type code for this type.
079: */
080: public final int getId() {
081: return id;
082: }
083:
084: /**
085: * Sets the SQL Type code for this type.
086: * @param id the SQL Type code for this type.
087: */
088: public final void setId(final int id) {
089: this .id = id;
090: }
091:
092: /**
093: * Returns the stored value for this type as an Object.
094: * @return the stored value of this type.
095: */
096: public final Object getValue() {
097: return value;
098: }
099:
100: /**
101: * Sets the stored value for this type.
102: * @param obj the value to store in this type.
103: */
104: public final void setValue(final Object obj) {
105: this .value = obj;
106: }
107:
108: /**
109: * Converts the Object to its String representation. This is called
110: * from the result tag when trying to build the DatabaseResult object
111: * from the specified result, as well as from the ResultSetBean when
112: * trying to build the DatabaseResult from the results of an SQL
113: * or stored procedure call. This method abstracts out some common
114: * operations that need to be done during this time, and provides
115: * hooks for subclasses to provide their own behavior. Each of these
116: * hooks also have default behavior, which is provided by this
117: * class if not overriden by the subclass.
118: * @param obj an Object to be converted to String.
119: * @return the String representation of the object.
120: * @exception SQLUnitException if the conversion to String failed.
121: */
122: public final String toString(final Object obj)
123: throws SQLUnitException {
124: if (obj == null) {
125: return "NULL";
126: }
127: if (obj instanceof String) {
128: return formatString((String) obj);
129: }
130: setValue(obj);
131: return format(obj);
132: }
133:
134: /**
135: * Converts a String representation of the Object to the Object itself.
136: * Where provided, this typically uses the parsing methods of the
137: * underlying Java type. This is called from the param element when
138: * we need to set values of variables for a SQL or stored procedure
139: * to execute. This method abstracts out some common operations that
140: * need to be done in all subclasses, and provides hooks for subclasses
141: * to provide their own behavior. Each of these hooks have default
142: * behavior, which is provided by this class if not overriden by the
143: * subclass.
144: * @param str the String to be converted into an Object.
145: * @return the Object
146: * @exception SQLUnitException if the conversion to Object failed.
147: */
148: public final Object toObject(final String str)
149: throws SQLUnitException {
150: if (("NULL").equals(str)) {
151: return null;
152: }
153: Object obj = parse(str);
154: setValue(obj);
155: return obj;
156: }
157:
158: /**
159: * Specifies the comparison order for this datatype. In this case there
160: * is no comparison possible, so it always returns zero. This will be
161: * overriden by subclasses.
162: * @param obj the Object to compare against.
163: * @return an int indicating whether this object is less than, equal to
164: * or greater than the object passed in.
165: */
166: public int compareTo(final Object obj) {
167: return 0;
168: }
169:
170: /**
171: * Returns the hashcode for the underlying value. This is likely to be
172: * overriden by subclasses.
173: * @return the hashcode for the underlying value.
174: */
175: public int hashCode() {
176: return (value == null ? -1 : value.hashCode());
177: }
178:
179: /**
180: * Returns true if the value of the two types are equal. This is likely
181: * to be overriden by subclasses.
182: * @param obj the Object to compare against.
183: * @return true if the values of the two objects are equal.
184: */
185: public boolean equals(final Object obj) {
186: LOG.debug("Checking for equality");
187: if (!(obj instanceof UnsupportedType)) {
188: return false;
189: }
190: UnsupportedType that = (UnsupportedType) obj;
191: if (this == null && that == null) {
192: return true;
193: }
194: if (this == null ^ that == null) {
195: return false;
196: }
197: Object this Value = this .getValue();
198: Object thatValue = that.getValue();
199: if (this Value == null && thatValue == null) {
200: return true;
201: }
202: if (this Value != null && thatValue != null) {
203: if (this Value.getClass().getName().equals(
204: thatValue.getClass().getName())) {
205: return (this Value.toString().equals(thatValue
206: .toString()));
207: } else {
208: return false;
209: }
210: } else {
211: return false;
212: }
213: }
214:
215: /**
216: * The formatting hook with default behavior which is meant to be
217: * overriden by the subclass. This method is called from within toString()
218: * after verifying that the Object passed in is not a String and is not
219: * null. A setValue() is called before passing control off to this method.
220: * @param obj the Object to convert to a String.
221: * @return the String representation of the object.
222: * @exception SQLUnitException if the conversion to String failed.
223: */
224: protected String format(final Object obj) throws SQLUnitException {
225: throw new SQLUnitException(IErrorCodes.UNSUPPORTED_DATATYPE,
226: new String[] { SymbolTable.getCurrentResultKey(),
227: getName(), new Integer(getId()).toString() });
228: }
229:
230: /**
231: * The formatting hook which gets activated if the passed in Object
232: * is a String. Subclasses will typically just do the default behavior,
233: * ie, pass it back, but some of them convert the String to MD5 Digest.
234: * @param obj the String object.
235: * @return another String object, the contents of which depend on the
236: * particular implementation in the subclass. Default behavior is to
237: * return the passed in String.
238: * @exception SQLUnitException if there was some problem with formatting.
239: */
240: protected String formatString(final String obj)
241: throws SQLUnitException {
242: return (String) obj;
243: }
244:
245: /**
246: * The parsing hook with default behavior which is meant to be overriden
247: * by the subclass. This method is called from within toObject() after
248: * verifying that the String is not a "NULL". A setValue() is called
249: * in the toObject() after parse() is called.
250: * @param str the String to parse into an Object.
251: * @return the Object.
252: * @exception SQLUnitException if the conversion to Object failed.
253: */
254: protected Object parse(final String str) throws SQLUnitException {
255: throw new SQLUnitException(IErrorCodes.UNSUPPORTED_DATATYPE,
256: new String[] { SymbolTable.getCurrentResultKey(),
257: getName(), new Integer(getId()).toString() });
258: }
259: }
|