01: /*
02: * $Id: BinaryType.java,v 1.15 2004/12/02 21:22:30 spal Exp $
03: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/types/BinaryType.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.io.InputStream;
30:
31: /**
32: * Models long binary text. This is identical to the ByteArrayType except
33: * it reads from a BinaryStream InputStream object returned in the ResultSet.
34: * It will digest the output of toString() using MD5. Results for this
35: * type can be provided as an MD5 digest string (prefixed by md5:) or as
36: * a file name to compare with (when the file name is prefixed with file:).
37: * @author Ralph Brendler (rbrendler@users.sourceforge.net)
38: * @author Sujit Pal (spal@users.sourceforge.net)
39: * @version $Revision: 1.15 $
40: * @sqlunit.type name="BinaryType" input="No" output="Yes" sortable="No"
41: * wraps="java.io.InputStream"
42: * @sqlunit.typename name="BINARY" server="Any"
43: * @sqlunit.typename name="LONGVARBINARY" server="Any"
44: */
45: public class BinaryType extends UnsupportedType {
46:
47: /**
48: * Defines behavior that is activated when the passed in Object is
49: * a String.
50: * @param obj the String to convert.
51: * @return a converted String.
52: * @exception SQLUnitException if there was a problem.
53: */
54: protected String formatString(final String obj)
55: throws SQLUnitException {
56: return DigestUtils.getMD5CheckSum(obj);
57: }
58:
59: /**
60: * Converts the Binary data to its String representation.
61: * @param obj an Object to be converted to the IType interface.
62: * @return the String representation of the object.
63: * @exception SQLUnitException if the formatting failed.
64: */
65: protected String format(final Object obj) throws SQLUnitException {
66: // it must be an inputstream
67: if (!(obj instanceof InputStream)) {
68: throw new SQLUnitException(
69: IErrorCodes.UNSUPPORTED_DATATYPE_FORMAT,
70: new String[] {
71: SymbolTable.getCurrentResultKey(),
72: (obj == null ? "NULL" : obj.getClass()
73: .getName()), getName(),
74: (new Integer(getId())).toString() });
75: }
76: boolean hasJavaObjectSupport = (("on").equals(SymbolTable
77: .getValue(SymbolTable.JAVA_OBJECT_SUPPORT)));
78: String digest;
79: InputStream istream = (InputStream) obj;
80: if (hasJavaObjectSupport) {
81: byte[] bytesFromStream = DigestUtils
82: .readBytesFromStream(istream);
83: if (DigestUtils.isSerializedJavaObject(bytesFromStream)) {
84: digest = DigestUtils
85: .getStringifiedObject(bytesFromStream);
86: } else {
87: digest = DigestUtils
88: .getMD5CheckSum(new ByteArrayInputStream(
89: bytesFromStream));
90: }
91: } else {
92: digest = DigestUtils.getMD5CheckSum(istream);
93: }
94: return digest;
95: }
96: }
|