01: /*
02: * $Id: ClobType.java,v 1.9 2004/09/30 17:27:55 spal Exp $
03: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/types/ClobType.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.DigestUtils;
27:
28: import java.io.ByteArrayInputStream;
29: import java.sql.Clob;
30: import java.sql.SQLException;
31:
32: /**
33: * Models a CLOB type.
34: * @author Ralph Brendler (rbrendler@users.sourceforge.net)
35: * @author Sujit Pal (spal@users.sourceforge.net)
36: * @version $Revision: 1.9 $
37: * @sqlunit.type name="ClobType" input="No" output="Yes" sortable="No"
38: * wraps="java.sql.Clob"
39: * @sqlunit.typename name="CLOB" server="Any"
40: * @sqlunit.typename name="LONGVARCHAR" server="MySQL"
41: */
42: public class ClobType extends UnsupportedType {
43:
44: /**
45: * Defines behavior that is activated when the passed in Object is
46: * a String.
47: * @param obj the String to convert.
48: * @return a converted String.
49: * @exception SQLUnitException if there was a problem.
50: */
51: protected String formatString(final String obj)
52: throws SQLUnitException {
53: return DigestUtils.getMD5CheckSum(obj);
54: }
55:
56: /**
57: * Formats a CLOB into a String representation of the BLOB.
58: * @param obj an Object to be converted to the IType interface.
59: * @return the String representation of the object.
60: * @exception SQLUnitException if the formatting failed.
61: */
62: protected String format(final Object obj) throws SQLUnitException {
63: if (!(obj instanceof Clob)) {
64: throw new SQLUnitException(
65: IErrorCodes.UNSUPPORTED_DATATYPE_FORMAT,
66: new String[] {
67: SymbolTable.getCurrentResultKey(),
68: (obj == null ? "NULL" : obj.getClass()
69: .getName()), getName(),
70: (new Integer(getId())).toString() });
71: }
72: Clob clob = (Clob) obj;
73: try {
74: byte[] bb = clob.getSubString(1, (int) clob.length())
75: .getBytes();
76: return DigestUtils.getMD5CheckSum(new ByteArrayInputStream(
77: bb));
78: } catch (SQLException e) {
79: throw new SQLUnitException(IErrorCodes.GENERIC_ERROR,
80: new String[] { "SQL", e.getClass().getName(),
81: e.getMessage() }, e);
82: }
83: }
84: }
|