001: /*
002: * $Id: TimestampType.java,v 1.10 2005/04/13 06:26:46 spal Exp $
003: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/types/TimestampType.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: import java.sql.Timestamp;
029: import java.text.ParseException;
030: import java.text.SimpleDateFormat;
031: import java.util.Date;
032:
033: /**
034: * Models a TIMESTAMP type.
035: * @author Ralph Brendler (rbrendler@users.sourceforge.net)
036: * @author Sujit Pal (spal@users.sourceforge.net)
037: * @version $Revision: 1.10 $
038: * @sqlunit.type name="TimestampType" input="Yes" output="Yes" sortable="Yes"
039: * wraps="java.sql.Timestamp"
040: * @sqlunit.typename name="TIMESTAMP" server="Any"
041: */
042: public class TimestampType extends UnsupportedType {
043:
044: /**
045: * Specifies the pattern for the DATE object. This may be overriden in
046: * subclasses to work with alternate forms of the TIMESTAMP type in
047: * different databases.
048: */
049: protected static String PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";
050:
051: private SimpleDateFormat formatter = new SimpleDateFormat(PATTERN);
052:
053: /**
054: * Formats a TIMESTAMP into its String representation.
055: * @param obj an Object to be converted to the IType interface.
056: * @return the String representation of the object.
057: * @exception SQLUnitException if the formatting failed.
058: */
059: protected String format(final Object obj) throws SQLUnitException {
060: if (!(obj instanceof Timestamp)) {
061: throw new SQLUnitException(
062: IErrorCodes.UNSUPPORTED_DATATYPE_FORMAT,
063: new String[] {
064: SymbolTable.getCurrentResultKey(),
065: (obj == null ? "NULL" : obj.getClass()
066: .getName()), getName(),
067: (new Integer(getId())).toString() });
068: }
069: Timestamp timestamp = (Timestamp) obj;
070: long millisSinceEpoch = timestamp.getTime();
071: Date d = new Date(millisSinceEpoch);
072: return formatter.format(d);
073: }
074:
075: /**
076: * Parses a String representing a TIMESTAMP to a Timestamp.
077: * @param str the String representation of the TIMESTAMP.
078: * @return a java.sql.Time object.
079: * @exception SQLUnitException if the parsing failed.
080: */
081: protected Object parse(final String str) throws SQLUnitException {
082: try {
083: Date d = formatter.parse(str);
084: Timestamp timestamp = new Timestamp(d.getTime());
085: return timestamp;
086: } catch (ParseException e) {
087: throw new SQLUnitException(
088: IErrorCodes.UNSUPPORTED_DATATYPE_PARSE,
089: new String[] { SymbolTable.getCurrentResultKey(),
090: str, "java.sql.Timestamp", getName(),
091: (new Integer(getId())).toString() });
092: }
093: }
094:
095: /**
096: * Returns true if this TimestampType is equal to the one passed in.
097: * @param obj the Object to compare against.
098: * @return true or false.
099: */
100: public final boolean equals(final Object obj) {
101: if (!(obj instanceof TimestampType)) {
102: return false;
103: } else {
104: return (compareTo(obj) == 0);
105: }
106: }
107:
108: /**
109: * Returns the hashcode for the underlying wrapped datatype.
110: * @return the hashcode for the underlying datatype.
111: */
112: public final int hashCode() {
113: return super .hashCode();
114: }
115:
116: /**
117: * Returns a negative, zero or positive number according to whether this
118: * object is smaller, equal or larger than the passed in object.
119: * @param obj an Object of type TimestampType.
120: * @return a negative, zero or positive number.
121: */
122: public final int compareTo(final Object obj) {
123: if (!(obj instanceof TimestampType)) {
124: return 0;
125: }
126: TimestampType that = (TimestampType) obj;
127: boolean isEitherNull = TypeUtils.checkIfNull(this , that);
128: if (isEitherNull) {
129: return TypeUtils.compareNulls(this , that);
130: } else {
131: try {
132: // strip off nanoseconds
133: Timestamp this Ts = (Timestamp) this .getValue();
134: String this TsFormatted = formatter.format(new Date(
135: this Ts.getTime()));
136: Timestamp this TsStripped = (Timestamp) this
137: .toObject(this TsFormatted);
138: Timestamp thatTs = (Timestamp) that.getValue();
139: String thatTsFormatted = formatter.format(new Date(
140: thatTs.getTime()));
141: Timestamp thatTsStripped = (Timestamp) that
142: .toObject(thatTsFormatted);
143: return (int) (this TsStripped.getTime() - thatTsStripped
144: .getTime());
145: } catch (SQLUnitException e) {
146: return 0; // :NOTE: will never happen
147: }
148: }
149: }
150: }
|