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