01: /*
02: * $Id: ArrayType.java,v 1.7 2004/09/30 17:27:55 spal Exp $
03: * $Source: /cvsroot/sqlunit/sqlunit/src/net/sourceforge/sqlunit/types/ArrayType.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.IType;
25: import net.sourceforge.sqlunit.SQLUnitException;
26: import net.sourceforge.sqlunit.SymbolTable;
27: import net.sourceforge.sqlunit.TypeFactory;
28: import net.sourceforge.sqlunit.TypeMapper;
29:
30: import java.sql.Array;
31: import java.sql.SQLException;
32:
33: /**
34: * Models an Array type.
35: * @author Ralph Brendler (rbrendler@users.sourceforge.net)
36: * @author Sujit Pal (spal@users.sourceforge.net)
37: * @version $Revision: 1.7 $
38: * @sqlunit.type name="ArrayType" input="no" output="yes" sortable="no"
39: * wraps="java.sql.Array"
40: * @sqlunit.typename name="ARRAY" server="Any"
41: */
42: public class ArrayType extends UnsupportedType {
43:
44: /**
45: * Formats an Array object into a String representation of the Array.
46: * @param obj an Object to be converted to the IType interface.
47: * @return the String representation of the object.
48: * @exception SQLUnitException if the formatting failed.
49: */
50: protected String format(final Object obj) throws SQLUnitException {
51: if (!(obj instanceof Array)) {
52: throw new SQLUnitException(
53: IErrorCodes.UNSUPPORTED_DATATYPE_FORMAT,
54: new String[] {
55: SymbolTable.getCurrentResultKey(),
56: (obj == null ? "NULL" : obj.getClass()
57: .getName()), getName(),
58: (new Integer(getId())).toString() });
59: }
60: Array array = (Array) obj;
61: StringBuffer buf = new StringBuffer("[");
62: try {
63: int baseTypeId = array.getBaseType();
64: Object[] elements = (Object[]) array.getArray();
65: for (int i = 0; i < elements.length; i++) {
66: if (i > 0) {
67: buf.append(",");
68: }
69: IType type = TypeFactory.getInstance(baseTypeId);
70: type.setId(baseTypeId);
71: type.setName(TypeMapper.findNameById(baseTypeId));
72: type.setValue(elements[i]);
73: buf.append(type.getValue().toString());
74: }
75: buf.append("]");
76: } catch (SQLException e) {
77: throw new SQLUnitException(IErrorCodes.GENERIC_ERROR,
78: new String[] { "SQL", e.getClass().getName(),
79: e.getMessage() }, e);
80: }
81: return buf.toString();
82: }
83: }
|