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