001: package com.mockrunner.mock.jdbc;
002:
003: import java.sql.Array;
004: import java.sql.ResultSet;
005: import java.sql.SQLException;
006: import java.util.Map;
007:
008: import com.mockrunner.base.NestedApplicationException;
009: import com.mockrunner.util.common.ArrayUtil;
010:
011: /**
012: * Mock implementation of <code>Array</code>.
013: */
014: public class MockArray implements Array, Cloneable {
015: private String sqlTypeName = "";
016: private int baseType = 0;
017: private Object array;
018: private boolean wasFreeCalled = false;
019:
020: public MockArray(Object array) {
021: this .array = ArrayUtil.convertToArray(array);
022: }
023:
024: /**
025: * Sets the base type.
026: * @param baseType the base type
027: */
028: public void setBaseType(int baseType) {
029: this .baseType = baseType;
030: }
031:
032: /**
033: * Sets the base type name.
034: * @param sqlTypeName the base type name
035: */
036: public void setBaseTypeName(String sqlTypeName) {
037: this .sqlTypeName = sqlTypeName;
038: }
039:
040: public int getBaseType() throws SQLException {
041: if (wasFreeCalled) {
042: throw new SQLException("free() was called");
043: }
044: return baseType;
045: }
046:
047: public String getBaseTypeName() throws SQLException {
048: if (wasFreeCalled) {
049: throw new SQLException("free() was called");
050: }
051: return sqlTypeName;
052: }
053:
054: public Object getArray() throws SQLException {
055: if (wasFreeCalled) {
056: throw new SQLException("free() was called");
057: }
058: return array;
059: }
060:
061: public Object getArray(Map map) throws SQLException {
062: return getArray();
063: }
064:
065: public Object getArray(long index, int count) throws SQLException {
066: return ArrayUtil.truncateArray(getArray(), (int) (index - 1),
067: count);
068: }
069:
070: public Object getArray(long index, int count, Map map)
071: throws SQLException {
072: return getArray(index, count);
073: }
074:
075: public ResultSet getResultSet() throws SQLException {
076: return getResultSet(1, java.lang.reflect.Array.getLength(array));
077: }
078:
079: public ResultSet getResultSet(long index, int count)
080: throws SQLException {
081: if (wasFreeCalled) {
082: throw new SQLException("free() was called");
083: }
084: Integer[] firstColumn = new Integer[count];
085: for (int ii = 0; ii < count; ii++) {
086: firstColumn[ii] = new Integer(ii + 1);
087: }
088: Object[] secondColumn = ArrayUtil.convertToObjectArray(array);
089: secondColumn = (Object[]) ArrayUtil.truncateArray(secondColumn,
090: (int) (index - 1), count);
091: MockResultSet resultSet = new MockResultSet(String
092: .valueOf(hashCode()));
093: resultSet.setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
094: resultSet.setResultSetConcurrency(ResultSet.CONCUR_READ_ONLY);
095: resultSet.addColumn(firstColumn);
096: resultSet.addColumn(secondColumn);
097: return resultSet;
098: }
099:
100: public ResultSet getResultSet(long index, int count, Map map)
101: throws SQLException {
102: return getResultSet(index, count);
103: }
104:
105: public ResultSet getResultSet(Map map) throws SQLException {
106: return getResultSet();
107: }
108:
109: public void free() throws SQLException {
110: wasFreeCalled = true;
111: }
112:
113: /**
114: * Returns if {@link #free} has been called.
115: * @return <code>true</code> if {@link #free} has been called,
116: * <code>false</code> otherwise
117: */
118: public boolean wasFreeCalled() {
119: return wasFreeCalled;
120: }
121:
122: public boolean equals(Object obj) {
123: if (null == obj)
124: return false;
125: if (!obj.getClass().equals(this .getClass()))
126: return false;
127: MockArray other = (MockArray) obj;
128: if (baseType != other.baseType)
129: return false;
130: if (null == sqlTypeName && null != other.sqlTypeName)
131: return false;
132: if (null != sqlTypeName
133: && !sqlTypeName.equals(other.sqlTypeName))
134: return false;
135: if (wasFreeCalled != other.wasFreeCalled())
136: return false;
137: return ArrayUtil.areArraysEqual(array, other.array);
138: }
139:
140: public int hashCode() {
141: int hashCode = ArrayUtil.computeHashCode(array);
142: hashCode = (31 * hashCode) + baseType;
143: if (null != sqlTypeName)
144: hashCode = (31 * hashCode) + sqlTypeName.hashCode();
145: hashCode = (31 * hashCode) + (wasFreeCalled ? 31 : 62);
146: return hashCode;
147: }
148:
149: public String toString() {
150: StringBuffer buffer = new StringBuffer("Array data: [");
151: Object[] arrayData = ArrayUtil.convertToObjectArray(array);
152: for (int ii = 0; ii < arrayData.length; ii++) {
153: buffer.append(arrayData[ii]);
154: if (ii < arrayData.length - 1) {
155: buffer.append(", ");
156: }
157: }
158: buffer.append("]");
159: return buffer.toString();
160: }
161:
162: public Object clone() {
163: try {
164: MockArray copy = (MockArray) super .clone();
165: copy.array = ArrayUtil.copyArray(array);
166: return copy;
167: } catch (CloneNotSupportedException exc) {
168: throw new NestedApplicationException(exc);
169: }
170: }
171: }
|