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