01: /*
02: * $Id: TextType.java,v 1.11 2004/09/30 17:27:55 spal Exp $
03: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/types/TextType.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.InputStream;
29:
30: /**
31: * Models long character text (TEXT). The difference between this type
32: * and the standard StringType is that this is not sortable, and cannot
33: * be used for input. It can be output from stored procedures, and the
34: * results are digested into an MD5, which is compared to another specified
35: * MD5 String, or a file, whose contents are digested using MD5 and the
36: * resulting MD5 digests compared for equality. Internally, it is created
37: * from an ASCII InputStream returned in the ResultSet. Results for this
38: * type can be provided as an MD5 Digest (prefixed by md5:) or a file name
39: * (prefixed by file:).
40: * @author Ralph Brendler (rbrendler@users.sourceforge.net)
41: * @author Sujit Pal (spal@users.sourceforge.net)
42: * @version $Revision: 1.11 $
43: * @sqlunit.type name="TextType" input="No" output="Yes" sortable="No"
44: * wraps="java.io.InputStream"
45: * @sqlunit.typename name="NTEXT" server="Microsoft SQL Server"
46: */
47: public class TextType extends UnsupportedType {
48:
49: /**
50: * Defines behavior that is activated when the passed in Object is
51: * a String.
52: * @param obj the String to convert.
53: * @return a converted String.
54: * @exception SQLUnitException if there was a problem.
55: */
56: protected String formatString(final String obj)
57: throws SQLUnitException {
58: return DigestUtils.getMD5CheckSum(obj);
59: }
60:
61: /**
62: * Format the Text to an MD5 Digest String.
63: * @param obj an Object to be converted to the IType interface.
64: * @return the String representation of the object.
65: * @exception SQLUnitException if the formatting failed.
66: */
67: protected String format(final Object obj) throws SQLUnitException {
68: if (!(obj instanceof InputStream)) {
69: throw new SQLUnitException(
70: IErrorCodes.UNSUPPORTED_DATATYPE_FORMAT,
71: new String[] {
72: SymbolTable.getCurrentResultKey(),
73: (obj == null ? "NULL" : obj.getClass()
74: .getName()), getName(),
75: (new Integer(getId())).toString() });
76: }
77: String digest = DigestUtils.getMD5CheckSum((InputStream) obj);
78: return digest;
79: }
80: }
|