001: /*
002: * $Id: ByteType.java,v 1.10 2005/04/13 06:26:46 spal Exp $
003: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/types/ByteType.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 Byte 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="ByteType" input="Yes" output="Yes" sortable="Yes"
034: * wraps="java.lang.Byte"
035: * @sqlunit.typename name="TINYINT" server="Any"
036: */
037: public class ByteType extends UnsupportedType {
038:
039: /**
040: * Formats a Byte type.
041: * @param obj an Object to be converted to the IType interface.
042: * @return the String representation of the object.
043: * @exception SQLUnitException if the formatting failed.
044: */
045: protected final String format(final Object obj)
046: throws SQLUnitException {
047: if (!(obj instanceof Byte)) {
048: throw new SQLUnitException(
049: IErrorCodes.UNSUPPORTED_DATATYPE_FORMAT,
050: new String[] {
051: SymbolTable.getCurrentResultKey(),
052: (obj == null ? "NULL" : obj.getClass()
053: .getName()), getName(),
054: (new Integer(getId())).toString() });
055: }
056: Byte s = (Byte) obj;
057: return s.toString();
058: }
059:
060: /**
061: * Parses a Byte type.
062: * @param str the String representation of the object.
063: * @return an object which needs to be cast to a Byte.
064: * @exception SQLUnitException if the parsing failed.
065: */
066: protected final Object parse(final String str)
067: throws SQLUnitException {
068: try {
069: Byte s = new Byte(str);
070: return s;
071: } catch (NumberFormatException e) {
072: throw new SQLUnitException(
073: IErrorCodes.UNSUPPORTED_DATATYPE_PARSE,
074: new String[] { SymbolTable.getCurrentResultKey(),
075: str, "java.lang.Byte", 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 ByteType)) {
088: return 0;
089: }
090: ByteType that = (ByteType) obj;
091: boolean isEitherNull = TypeUtils.checkIfNull(this , that);
092: if (isEitherNull) {
093: return TypeUtils.compareNulls(this , that);
094: } else {
095: Byte this Value = (Byte) this .getValue();
096: Byte thatValue = (Byte) that.getValue();
097: return (thisValue.compareTo(thatValue));
098: }
099: }
100: }
|