001: package com.mockrunner.mock.jdbc;
002:
003: import java.sql.SQLException;
004: import java.sql.Struct;
005: import java.util.ArrayList;
006: import java.util.List;
007: import java.util.Map;
008:
009: import com.mockrunner.base.NestedApplicationException;
010:
011: /**
012: * Mock implementation of <code>Struct</code>.
013: */
014: public class MockStruct implements Struct, Cloneable {
015: private String sqlTypeName;
016: private List attributes;
017:
018: public MockStruct(Object[] attributes) {
019: this (null, attributes);
020: }
021:
022: public MockStruct(String sqlTypeName, Object[] attributes) {
023: this .sqlTypeName = sqlTypeName;
024: this .attributes = new ArrayList();
025: addAttributes(attributes);
026: }
027:
028: public MockStruct(String sqlTypeName) {
029: this (sqlTypeName, new Object[0]);
030: }
031:
032: public String getSQLTypeName() throws SQLException {
033: return sqlTypeName;
034: }
035:
036: public void setSQLTypeName(String sqlTypeName) {
037: this .sqlTypeName = sqlTypeName;
038: }
039:
040: public Object[] getAttributes() throws SQLException {
041: return attributes.toArray();
042: }
043:
044: public Object[] getAttributes(Map map) throws SQLException {
045: return getAttributes();
046: }
047:
048: public void addAttribute(Object attribute) {
049: attributes.add(attribute);
050: }
051:
052: public void addAttributes(Object[] attributes) {
053: for (int ii = 0; ii < attributes.length; ii++) {
054: addAttribute(attributes[ii]);
055: }
056: }
057:
058: public void addAttributes(List attributes) {
059: addAttributes(attributes.toArray());
060: }
061:
062: public void clearAttributes() {
063: attributes.clear();
064: }
065:
066: public boolean equals(Object obj) {
067: if (null == obj)
068: return false;
069: if (!obj.getClass().equals(this .getClass()))
070: return false;
071: MockStruct other = (MockStruct) obj;
072: if (null != sqlTypeName
073: && !sqlTypeName.equals(other.sqlTypeName))
074: return false;
075: if (null != other.sqlTypeName
076: && !other.sqlTypeName.equals(sqlTypeName))
077: return false;
078: if (null == attributes && null == other.attributes)
079: return true;
080: if (null == attributes || null == other.attributes)
081: return false;
082: return attributes.equals(other.attributes);
083: }
084:
085: public int hashCode() {
086: int hashCode = 17;
087: if (null != sqlTypeName)
088: hashCode = (31 * hashCode) + sqlTypeName.hashCode();
089: if (null != attributes)
090: hashCode = (31 * hashCode) + attributes.hashCode();
091: return hashCode;
092: }
093:
094: public String toString() {
095: return "Struct data: " + attributes.toString();
096: }
097:
098: public Object clone() {
099: try {
100: MockStruct copy = (MockStruct) super .clone();
101: copy.attributes = new ArrayList();
102: copy.addAttributes(attributes.toArray());
103: return copy;
104: } catch (CloneNotSupportedException exc) {
105: throw new NestedApplicationException(exc);
106: }
107: }
108: }
|