001: /*
002: * $Id: OutParamHandler.java,v 1.6 2006/06/25 23:02:50 spal Exp $
003: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/handlers/OutParamHandler.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.handlers;
022:
023: import net.sourceforge.sqlunit.HandlerFactory;
024: import net.sourceforge.sqlunit.IErrorCodes;
025: import net.sourceforge.sqlunit.IHandler;
026: import net.sourceforge.sqlunit.SQLUnitException;
027: import net.sourceforge.sqlunit.beans.OutParam;
028: import net.sourceforge.sqlunit.beans.ResultSetBean;
029: import net.sourceforge.sqlunit.beans.StructBean;
030: import net.sourceforge.sqlunit.utils.XMLUtils;
031:
032: import org.apache.log4j.Logger;
033: import org.jdom.Element;
034:
035: /**
036: * The OutParamHandler takes the JDOM Element representing the outparam tag
037: * and converts it to a OutParam object.
038: * @author Sujit Pal (spal@users.sourceforge.net)
039: * @version $Revision: 1.6 $
040: * @sqlunit.parent name="result" ref="result"
041: * @sqlunit.element name="outparam"
042: * description="The outparam tag is used to represent OUT parameters
043: * returned as a result of a Stored procedure call. The value is
044: * stored in the body text as a String or as a embedded resultset
045: * element in case of CURSOR type OUT parameters."
046: * syntax="(BODY)|(resultset)|(struct)"
047: * @sqlunit.attrib name="id"
048: * description="The param-id of the outparam parameter."
049: * required="Yes"
050: * @sqlunit.attrib name="name"
051: * description="The name of the outparam parameter. This is mainly for
052: * readability to help stored procedure authors/testers to spot errors
053: * quickly."
054: * required="No"
055: * @sqlunit.attrib name="type"
056: * description="Specifies the type name of the parameter."
057: * required="Yes"
058: * @sqlunit.child name="resultset"
059: * description="This is needed only when the type of the OUT parameter
060: * is a CURSOR, and specifies the value of the CURSOR outparam."
061: * required="Not if the type is not CURSOR"
062: * ref="resultset"
063: * @sqlunit.example name="A outparam with a INTEGER value"
064: * description="
065: * <outparam id=\"1\" type=\"INTEGER\">24</outparam>
066: * "
067: * @sqlunit.example name="A outparam tag with an embedded CURSOR"
068: * description="
069: * <outparam id=\"1\" type=\"oracle.CURSOR\">{\n}
070: * {\t}<resultset id=\"1\">{\n}
071: * {\t}{\t}<row id=\"1\">{\n}
072: * {\t}{\t}{\t}<col id=\"1\" type=\"INTEGER\">7</col>{\n}
073: * {\t}{\t}{\t}<col id=\"2\" type=\"VARCHAR\">James Bond</col>{\n}
074: * {\t}{\t}{\t}<col id=\"3\" type=\"VARCHAR\">Martini</col>{\n}
075: * {\t}{\t}</row>{\n}
076: * {\t}</resultset>{\n}
077: * </outparam>
078: * "
079: */
080: public class OutParamHandler implements IHandler {
081:
082: private static final Logger LOG = Logger
083: .getLogger(OutParamHandler.class);
084:
085: /**
086: * Processes the JDOM Element representing the outparam tag returns the
087: * OutParam object.
088: * @param elOutParam the JDOM Element representing the outparam tag.
089: * @return a OutParam object. Client needs to cast to a OutParam.
090: * @exception Exception if something went wrong processing.
091: */
092: public final Object process(final Element elOutParam)
093: throws Exception {
094: LOG.debug(">> process(elOutParam)");
095: if (elOutParam == null) {
096: throw new SQLUnitException(IErrorCodes.ELEMENT_IS_NULL,
097: new String[] { "param" });
098: }
099: OutParam op = new OutParam();
100: op.setId(XMLUtils.getAttributeValue(elOutParam, "id"));
101: op.setName(XMLUtils.getAttributeValue(elOutParam, "name"));
102: op.setType(XMLUtils.getAttributeValue(elOutParam, "type"));
103: Element elResultSet = elOutParam.getChild("resultset");
104: Element elStruct = elOutParam.getChild("struct");
105: if (elResultSet != null) {
106: // contains a resultset bean
107: if (!op.getType().endsWith("CURSOR")) {
108: throw new SQLUnitException(IErrorCodes.IS_A_CURSOR,
109: new String[] { op.getId(), op.getType() });
110: }
111: IHandler resultSetHandler = HandlerFactory
112: .getInstance(elResultSet.getName());
113: ResultSetBean rsb = (ResultSetBean) resultSetHandler
114: .process(elResultSet);
115: op.setValue(rsb);
116: } else if (elStruct != null) {
117: if (!op.getType().endsWith("STRUCT")) {
118: throw new SQLUnitException(IErrorCodes.IS_A_STRUCT,
119: new String[] { op.getId(), op.getType() });
120: }
121: IHandler structHandler = HandlerFactory
122: .getInstance(elStruct.getName());
123: StructBean sb = (StructBean) structHandler
124: .process(elStruct);
125: op.setValue(sb);
126: } else {
127: // contains a String
128: op.setValue(XMLUtils.getText(elOutParam));
129: }
130: return op;
131: }
132: }
|