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