01: /*
02: * $Id: BooleanType.java,v 1.10 2005/04/13 06:26:46 spal Exp $
03: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/types/BooleanType.java,v $
04: * SQLUnit - a test harness for unit testing database stored procedures.
05: * Copyright (C) 2003 The SQLUnit Team
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21: package net.sourceforge.sqlunit.types;
22:
23: import net.sourceforge.sqlunit.IErrorCodes;
24: import net.sourceforge.sqlunit.SQLUnitException;
25: import net.sourceforge.sqlunit.SymbolTable;
26: import net.sourceforge.sqlunit.utils.TypeUtils;
27:
28: /**
29: * Models a Boolean type.
30: * @author Ralph Brendler (rbrendler@users.sourceforge.net)
31: * @author Sujit Pal (spal@users.sourceforge.net)
32: * @version $Revision: 1.10 $
33: * @sqlunit.type name="BooleanType" input="Yes" output="Yes" sortable="Yes"
34: * wraps="java.lang.Boolean"
35: * @sqlunit.typename name="BIT" server="Any"
36: * @sqlunit.typename name="BOOLEAN" server="Any"
37: */
38: public class BooleanType extends UnsupportedType {
39:
40: /**
41: * Formats a Boolean type.
42: * @param obj an Object to be converted to the IType interface.
43: * @return the String representation of the object.
44: * @exception SQLUnitException if the formatting failed.
45: */
46: protected String format(final Object obj) throws SQLUnitException {
47: if (!(obj instanceof Boolean)) {
48: throw new SQLUnitException(
49: IErrorCodes.UNSUPPORTED_DATATYPE_FORMAT,
50: new String[] {
51: SymbolTable.getCurrentResultKey(),
52: (obj == null ? "NULL" : obj.getClass()
53: .getName()), getName(),
54: (new Integer(getId())).toString() });
55: }
56: Boolean bool = (Boolean) obj;
57: return (bool.booleanValue() ? "true" : "false");
58: }
59:
60: /**
61: * Parses a Boolean type.
62: * @param str the String representation of the Boolean.
63: * @return a java.sql.Date object.
64: * @exception SQLUnitException if the parsing failed.
65: */
66: protected Object parse(final String str) throws SQLUnitException {
67: Boolean bool = Boolean.FALSE;
68: if (("true").equalsIgnoreCase(str)) {
69: bool = Boolean.TRUE;
70: }
71: return bool;
72: }
73:
74: /**
75: * Returns a negative, zero or positive number according to whether this
76: * object is smaller, equal or larger than the passed in object.
77: * @param obj an Object of type DateType.
78: * @return a negative, zero or positive number.
79: */
80: public final int compareTo(final Object obj) {
81: if (!(obj instanceof BooleanType)) {
82: return 0;
83: }
84: BooleanType that = (BooleanType) obj;
85: boolean isEitherNull = TypeUtils.checkIfNull(this , that);
86: if (isEitherNull) {
87: return TypeUtils.compareNulls(this , that);
88: } else {
89: Boolean this Value = (Boolean) this .getValue();
90: Boolean thatValue = (Boolean) that.getValue();
91: return (this Value.booleanValue() ? 1 : 0)
92: - (thatValue.booleanValue() ? 1 : 0);
93: }
94: }
95: }
|