001: /*
002: * $Id: OutParam.java,v 1.9 2006/06/25 23:02:50 spal Exp $
003: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/beans/OutParam.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.beans;
022:
023: import net.sourceforge.sqlunit.SQLUnitException;
024: import net.sourceforge.sqlunit.TypeMapper;
025: import net.sourceforge.sqlunit.types.BigDecimalType;
026: import net.sourceforge.sqlunit.utils.TypeUtils;
027:
028: import org.apache.log4j.Logger;
029: import org.jdom.Element;
030:
031: /**
032: * The OutParam models a SQLUnit outparam element.
033: * @author Sujit Pal (spal@users.sourceforge.net)
034: * @version $Revision: 1.9 $
035: */
036: public class OutParam {
037:
038: private static final Logger LOG = Logger.getLogger(OutParam.class);
039:
040: private String id;
041: private String name;
042: private String type;
043: private Object value;
044:
045: /**
046: * Default constructor.
047: */
048: public OutParam() {
049: // default constructor
050: }
051:
052: /**
053: * Returns the outparam id.
054: * @return the param id.
055: */
056: public final String getId() {
057: return id;
058: }
059:
060: /**
061: * Sets the outparam id.
062: * @param id the param id.
063: */
064: public final void setId(final String id) {
065: this .id = id;
066: }
067:
068: /**
069: * Returns the outparam name.
070: * @return the outparam name.
071: */
072: public final String getName() {
073: return name;
074: }
075:
076: /**
077: * Sets the outparam name.
078: * @param name the outparam name.
079: */
080: public final void setName(final String name) {
081: this .name = name;
082: }
083:
084: /**
085: * Returns the outparam type.
086: * @return the outparam type.
087: */
088: public final String getType() {
089: return type;
090: }
091:
092: /**
093: * Convenience method to return the SQL (numeric) type from the
094: * supplied (String XML) type.
095: * @return the SQL Type for this parameter.
096: * @exception SQLUnitException if one is thrown.
097: */
098: public final int getSQLType() throws SQLUnitException {
099: return TypeUtils.getSqlTypeFromXmlType(type);
100: }
101:
102: /**
103: * Sets the outparam type.
104: * @param type the outparam type.
105: */
106: public final void setType(final String type) {
107: this .type = type;
108: }
109:
110: /**
111: * Returns the outparam value as a String.
112: * @return the outparam value.
113: */
114: public final String getValue() {
115: return value.toString();
116: }
117:
118: /**
119: * Sets the param value from a String.
120: * @param value the param value.
121: */
122: public final void setValue(final String value) {
123: this .value = value;
124: }
125:
126: /**
127: * Sets the outparam value from a ResultSetBean.
128: * @param value a ResultSetBean object.
129: */
130: public final void setValue(final ResultSetBean value) {
131: this .value = value;
132: }
133:
134: /**
135: * Sets the outparam value from a StructBean.
136: * @param value a ResultSetBean object.
137: */
138: public final void setValue(final StructBean value) {
139: this .value = value;
140: }
141:
142: /**
143: * Returns true if the outparams are equal.
144: * @param obj the Object to compare against.
145: * @return true if the two Objects are equal, false otherwise.
146: */
147: public final boolean equals(final Object obj) {
148: if (!(obj instanceof OutParam)) {
149: return false;
150: }
151: OutParam that = (OutParam) obj;
152: try {
153: boolean isTypeEqual = this .getType().equals(that.getType());
154: if (isTypeEqual) {
155: String className = TypeMapper.findClassById(TypeUtils
156: .getSqlTypeFromXmlType(this .getType()));
157: // special treatment for BigDecimal, we need to normalize
158: // the scales
159: if (BigDecimalType.class.getName().equals(className)) {
160: return isTypeEqual
161: && TypeUtils.isValueOfBigDecimalEqual(this
162: .getValue(), that.getValue());
163: } else {
164: return isTypeEqual
165: && this .getValue().equals(that.getValue());
166: }
167: } else {
168: return false;
169: }
170: } catch (SQLUnitException e) {
171: return false;
172: }
173: }
174:
175: /**
176: * Returns a hashcode for the OutParam object. This is the sum of the
177: * hashcode for the type and the hashcode for the value.
178: * @return the hash code for the OutParam object.
179: */
180: public final int hashCode() {
181: return this .getValue().hashCode() + this .getType().hashCode();
182: }
183:
184: /**
185: * Returns the string representation of this object.
186: * @return the String representation of this object.
187: */
188: public final String toString() {
189: StringBuffer buf = new StringBuffer();
190: buf.append(getValue()).append("(").append(getType())
191: .append(")");
192: return buf.toString();
193: }
194:
195: /**
196: * Returns a JDOM Element representing this bean.
197: * @return a JDOM Element representing this bean.
198: */
199: public final Element toElement() {
200: Element elOutParam = new Element("outparam");
201: elOutParam.setAttribute("id", getId());
202: if (getName() != null) {
203: elOutParam.setAttribute("name", getName());
204: }
205: elOutParam.setAttribute("type", getType());
206: if (value instanceof ResultSetBean) {
207: ResultSetBean rsb = (ResultSetBean) value;
208: Element elResultSet = rsb.toElement();
209: elOutParam.addContent(elResultSet);
210: } else if (value instanceof StructBean) {
211: StructBean sb = (StructBean) value;
212: Element elStruct = sb.toElement();
213: elOutParam.addContent(elStruct);
214: } else {
215: elOutParam.setText(value.toString());
216: }
217: return elOutParam;
218: }
219: }
|