001: /*
002: * $Id: IntegerType.java,v 1.10 2005/04/13 06:26:46 spal Exp $
003: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/types/IntegerType.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.SQLUnitException;
025: import net.sourceforge.sqlunit.SymbolTable;
026: import net.sourceforge.sqlunit.utils.TypeUtils;
027:
028: /**
029: * Models a Integer type.
030: * @author Ralph Brendler (rbrendler@users.sourceforge.net)
031: * @author Sujit Pal (spal@users.sourceforge.net)
032: * @version $Revision: 1.10 $
033: * @sqlunit.type name="IntegerType" input="Yes" output="Yes" sortable="Yes"
034: * wraps="java.lang.Integer"
035: * @sqlunit.typename name="INTEGER" server="Any"
036: * @sqlunit.typename name="SMALLINT" server="Sybase ASA"
037: * @sqlunit.typename name="SMALLINT" server="Sybase ASE"
038: */
039: public class IntegerType extends UnsupportedType {
040:
041: /**
042: * Formats a Integer type.
043: * @param obj an Object to be converted to the IType interface.
044: * @return the String representation of the object.
045: * @exception SQLUnitException if the formatting failed.
046: */
047: protected String format(final Object obj) throws SQLUnitException {
048: if (!(obj instanceof Integer)) {
049: throw new SQLUnitException(
050: IErrorCodes.UNSUPPORTED_DATATYPE_FORMAT,
051: new String[] {
052: SymbolTable.getCurrentResultKey(),
053: (obj == null ? "NULL" : obj.getClass()
054: .getName()), getName(),
055: (new Integer(getId())).toString() });
056: }
057: Integer i = (Integer) obj;
058: return i.toString();
059: }
060:
061: /**
062: * Parses a Integer type.
063: * @param str the String representation of the object.
064: * @return an object which needs to be cast to a Integer.
065: * @exception SQLUnitException if the parsing failed.
066: */
067: protected Object parse(final String str) throws SQLUnitException {
068: try {
069: Integer i = new Integer(str);
070: return i;
071: } catch (NumberFormatException e) {
072: throw new SQLUnitException(
073: IErrorCodes.UNSUPPORTED_DATATYPE_PARSE,
074: new String[] { SymbolTable.getCurrentResultKey(),
075: str, "java.lang.Integer", getName(),
076: (new Integer(getId())).toString() });
077: }
078: }
079:
080: /**
081: * Returns a negative, zero or positive number according to whether this
082: * object is smaller, equal or larger than the passed in object.
083: * @param obj an Object of type DateType.
084: * @return a negative, zero or positive number.
085: */
086: public final int compareTo(final Object obj) {
087: if (!(obj instanceof IntegerType)) {
088: return 0;
089: }
090: IntegerType that = (IntegerType) obj;
091: boolean isEitherNull = TypeUtils.checkIfNull(this , that);
092: if (isEitherNull) {
093: return TypeUtils.compareNulls(this , that);
094: } else {
095: Integer this Value = (Integer) this .getValue();
096: Integer thatValue = (Integer) that.getValue();
097: return (thisValue.compareTo(thatValue));
098: }
099: }
100: }
|