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