01: /*
02: * $Id: ExceptionHandler.java,v 1.6 2004/09/28 19:15:45 spal Exp $
03: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/handlers/ExceptionHandler.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.handlers;
22:
23: import net.sourceforge.sqlunit.IErrorCodes;
24: import net.sourceforge.sqlunit.IHandler;
25: import net.sourceforge.sqlunit.SQLUnitException;
26: import net.sourceforge.sqlunit.beans.ExceptionBean;
27: import net.sourceforge.sqlunit.utils.XMLUtils;
28:
29: import org.apache.log4j.Logger;
30: import org.jdom.Element;
31:
32: /**
33: * The Exception Handler parses a JDOM Element representing an exception
34: * tag and returns an ExceptionBean object.
35: * @author Sujit Pal (spal@users.sourceforge.net)
36: * @version $Revision: 1.6 $
37: * @sqlunit.parent name="result" ref="result"
38: * @sqlunit.element name="exception"
39: * description="Specifies an expected exception with expected error code
40: * and message"
41: * syntax="(code)?, (message)?"
42: * @sqlunit.child name="code"
43: * description="The error code for the expected exception"
44: * required="No"
45: * ref="none"
46: * @sqlunit.child name="message"
47: * description="The error message for the expected exception"
48: * required="No"
49: * ref="none"
50: * @sqlunit.example name="An exception declaration"
51: * description="
52: * <exception>{\n}
53: * {\t}<code>0</code>{\n}
54: * {\t}<message>ERROR: Cannot insert a duplicate key into unique index ux2_employee</message>{\n}
55: * </exception>
56: * "
57: */
58: public class ExceptionHandler implements IHandler {
59:
60: private static final Logger LOG = Logger
61: .getLogger(ExceptionHandler.class);
62:
63: /**
64: * Processes the JDOM Element representing the param tag returns the
65: * Exception object.
66: * @param elException the JDOM Element representing the param tag.
67: * @return a Exception object. Client needs to cast to a Exception.
68: * @exception Exception if something went wrong processing the param.
69: */
70: public final Object process(final Element elException)
71: throws Exception {
72: LOG.debug(">> process(elException)");
73: if (elException == null) {
74: throw new SQLUnitException(IErrorCodes.ELEMENT_IS_NULL,
75: new String[] { "exception" });
76: }
77: ExceptionBean eb = new ExceptionBean();
78: Element elCode = elException.getChild("code");
79: String errorCode = XMLUtils.getText(elCode);
80: if (errorCode != null && errorCode.trim().length() > 0) {
81: eb.setErrorCode(errorCode.trim());
82: }
83: Element elMessage = elException.getChild("message");
84: String errorMessage = XMLUtils.getText(elMessage);
85: if (errorMessage != null && errorMessage.trim().length() > 0) {
86: eb.setErrorMessage(errorMessage);
87: }
88: return eb;
89: }
90: }
|