001: /*
002: * $Id: DatabaseResult.java,v 1.8 2005/05/16 17:22:12 spal Exp $
003: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/beans/DatabaseResult.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.utils.XMLUtils;
024:
025: import org.apache.log4j.Logger;
026:
027: import org.jdom.Element;
028:
029: /**
030: * The DatabaseResult models a Database Result.
031: * @author Sujit Pal (spal@users.sourceforge.net)
032: * @version $Revision: 1.8 $
033: */
034: public class DatabaseResult {
035:
036: private static final Logger LOG = Logger
037: .getLogger(DatabaseResult.class);
038:
039: private String errorCode;
040: private String errorMessage;
041: private OutParam[] outParams;
042: private ResultSetBean[] resultSetBeans;
043: private ExceptionBean ex;
044: private int updateCount;
045: private boolean echoOnly;
046:
047: /**
048: * Default constructor. Sets up all underlying map and scalar objects.
049: */
050: public DatabaseResult() {
051: LOG.debug("[DatabaseResult]");
052: // initialize outparam part
053: outParams = new OutParam[0];
054: // initialize resultset part
055: resultSetBeans = new ResultSetBean[0];
056: // initialize exception part
057: ex = null;
058: // initialize updatecount part
059: updateCount = -1;
060: // set echoOnly to false
061: echoOnly = false;
062: }
063:
064: /**
065: * Returns the number of result sets in the DatabaseResult.
066: * @return the number of set elements in the result element.
067: */
068: public final int getNumResults() {
069: return resultSetBeans.length;
070: }
071:
072: /**
073: * Returns the number of rows in resultset identified by resultset index.
074: * @param resultSetIndex the index of the resultset in the DatabaseResult.
075: * The resultset index is 1-based.
076: * @return the number of rows in the resultset.
077: */
078: public final int getNumRows(final int resultSetIndex) {
079: LOG.debug(">> getNumRows(" + resultSetIndex + ")");
080: // if the resultSetIndex is out of bounds, return -1
081: if (resultSetIndex > getNumResults()) {
082: return -1;
083: }
084: ResultSetBean rsb = resultSetBeans[resultSetIndex - 1];
085: Row[] rows = rsb.getRows();
086: return rows.length;
087: }
088:
089: /**
090: * Returns the number of columns in resultset identified by resultset index.
091: * @param resultSetIndex the index of the resultset in the DatabaseResult.
092: * @return the number of columns in the row.
093: */
094: public final int getNumCols(final int resultSetIndex) {
095: LOG.debug(">> getNumCols(" + resultSetIndex + ")");
096: if (resultSetIndex > getNumResults()) {
097: return -1;
098: }
099: ResultSetBean rsb = resultSetBeans[resultSetIndex - 1];
100: Row[] rows = rsb.getRows();
101: if (rows == null || rows.length == 0) {
102: return -1;
103: }
104: Col[] cols = rows[0].getCols();
105: return cols.length;
106: }
107:
108: /**
109: * Returns an array of OutParam objects for this DatabaseResult object.
110: * @return an array of OutParam objects. May be null.
111: */
112: public final OutParam[] getOutParams() {
113: return outParams;
114: }
115:
116: /**
117: * Sets the outparams into the DatabaseResult object.
118: * @param outParams an array of OutParam objects.
119: */
120: public final void setOutParams(final OutParam[] outParams) {
121: this .outParams = outParams;
122: }
123:
124: /**
125: * Returns an array of ResultSetBean objects for this DatabaseResult object.
126: * @return an array of ResultSetBean objects. May be null.
127: */
128: public final ResultSetBean[] getResultSets() {
129: return resultSetBeans;
130: }
131:
132: /**
133: * Sets the resultsets into the DatabaseResult object.
134: * @param rsbs an array of ResultSetBean objects.
135: */
136: public final void setResultSets(final ResultSetBean[] rsbs) {
137: this .resultSetBeans = rsbs;
138: }
139:
140: /**
141: * Returns true if the DatabaseResult represents an Exception.
142: * @return true if this DatabaseResult is an exception, else false.
143: */
144: public final boolean isException() {
145: LOG.debug(">> isException()");
146: return (ex != null);
147: }
148:
149: /**
150: * Returns the exception from the DatabaseResult object.
151: * @return the ExceptionBean object.
152: */
153: public final ExceptionBean getException() {
154: return ex;
155: }
156:
157: /**
158: * Creates the exception version of a DatabaseResult (this).
159: * @param errCode the code to populate.
160: * @param errMessage the message to populate.
161: */
162: public final void resetAsException(final String errCode,
163: final String errMessage) {
164: LOG.debug(">> resetAsException(" + errCode + "," + errMessage
165: + ")");
166: ex = new ExceptionBean();
167: ex.setErrorCode(errCode);
168: ex.setErrorMessage(XMLUtils.stripCRLF(errMessage));
169: }
170:
171: /**
172: * Returns the update count associated with this DatabaseResult. If
173: * the update count is not applicable for this DatabaseResult, then
174: * it will return -1. If applicable, this represents the number of
175: * rows updated by the SQL query or Stored Procedure that generated
176: * this DatabaseResult.
177: * @return the update count for the DatabaseResult.
178: */
179: public final int getUpdateCount() {
180: LOG.debug(">> getUpdateCount()");
181: return updateCount;
182: }
183:
184: /**
185: * Sets the update count for the DatabaseResult.
186: * @param updateCount the updateCount to update.
187: */
188: public final void setUpdateCount(final int updateCount) {
189: LOG.debug(">> setUpdateCount(" + updateCount + ")");
190: this .updateCount = updateCount;
191: }
192:
193: /**
194: * Returns the echoOnly property of the DatabaseResult object.
195: * @return true or false.
196: */
197: public final boolean getEchoOnly() {
198: return echoOnly;
199: }
200:
201: /**
202: * Sets the echoOnly property of the DatabaseResult object. If this
203: * property is set to true in the expected result, SQLUnit will not
204: * attempt to match with the generated result. Instead it will print
205: * the XML String representation of the generated result to the output.
206: * @param echoOnly the echoOnly property. Valid values are true and false.
207: */
208: public final void setEchoOnly(boolean echoOnly) {
209: this .echoOnly = echoOnly;
210: }
211:
212: /**
213: * Returns the DatabaseResult object as a JDOM Element.
214: * @return the DatabaseResult object as a JDOM Element.
215: */
216: public final Element toElement() {
217: LOG.debug(">> toElement()");
218: Element elDatabaseResult = new Element("result");
219: if (isException()) {
220: // (outparam)*, (exception)
221: OutParam[] outparams = getOutParams();
222: for (int i = 0; i < outparams.length; i++) {
223: Element elOutParam = outparams[i].toElement();
224: elDatabaseResult.addContent(elOutParam);
225: }
226: ExceptionBean this Ex = getException();
227: elDatabaseResult.addContent(this Ex.toElement());
228: } else {
229: // (outparam)*, (updatecount)?, (resultset)*
230: OutParam[] outparams = getOutParams();
231: for (int i = 0; i < outparams.length; i++) {
232: Element elOutParam = outparams[i].toElement();
233: elDatabaseResult.addContent(elOutParam);
234: }
235: int this UpdateCount = getUpdateCount();
236: if (this UpdateCount > -1) {
237: Element elUpdateCount = new Element("updatecount");
238: elUpdateCount.setText((new Integer(this UpdateCount))
239: .toString());
240: elDatabaseResult.addContent(elUpdateCount);
241: }
242: ResultSetBean[] resultsetbeans = getResultSets();
243: for (int i = 0; i < resultsetbeans.length; i++) {
244: Element elResultSet = resultsetbeans[i].toElement();
245: elDatabaseResult.addContent(elResultSet);
246: }
247: }
248: return elDatabaseResult;
249: }
250:
251: /**
252: * Returns a String representation of the DatabaseResult object.
253: * @return a String representation of the DatabaseResult object.
254: */
255: public final String toString() {
256: LOG.debug(">> toString()");
257: return XMLUtils.toXMLString(toElement());
258: }
259: }
|