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 Object array;
020
021 public MockArray(Object array)
022 {
023 this.array = ArrayUtil.convertToArray(array);
024 }
025
026 public int getBaseType() throws SQLException
027 {
028 return 0;
029 }
030
031 public String getBaseTypeName() throws SQLException
032 {
033 return "";
034 }
035
036 public Object getArray() throws SQLException
037 {
038 return array;
039 }
040
041 public Object getArray(Map map) throws SQLException
042 {
043 return getArray();
044 }
045
046 public Object getArray(long index, int count) throws SQLException
047 {
048 return ArrayUtil.truncateArray(getArray(), (int)(index - 1), count);
049 }
050
051 public Object getArray(long index, int count, Map map) throws SQLException
052 {
053 return getArray(index, count);
054 }
055
056 public ResultSet getResultSet() throws SQLException
057 {
058 return getResultSet(1, java.lang.reflect.Array.getLength(array));
059 }
060
061 public ResultSet getResultSet(long index, int count) throws SQLException
062 {
063 Integer[] firstColumn = new Integer[count];
064 for(int ii = 0; ii < count; ii++)
065 {
066 firstColumn[ii] = new Integer(ii + 1);
067 }
068 Object[] secondColumn = ArrayUtil.convertToObjectArray(array);
069 secondColumn = (Object[])ArrayUtil.truncateArray(secondColumn, (int)(index - 1), count);
070 MockResultSet resultSet = new MockResultSet(String.valueOf(hashCode()));
071 resultSet.setResultSetType(ResultSet.TYPE_SCROLL_INSENSITIVE);
072 resultSet.setResultSetConcurrency(ResultSet.CONCUR_READ_ONLY);
073 resultSet.addColumn(firstColumn);
074 resultSet.addColumn(secondColumn);
075 return resultSet;
076 }
077
078 public ResultSet getResultSet(long index, int count, Map map) throws SQLException
079 {
080 return getResultSet(index, count);
081 }
082
083 public ResultSet getResultSet(Map map) throws SQLException
084 {
085 return getResultSet();
086 }
087
088 public String toString()
089 {
090 StringBuffer buffer = new StringBuffer("Array data: ");
091 Object[] arrayData = ArrayUtil.convertToObjectArray(array);
092 for(int ii = 0; ii < arrayData.length; ii++)
093 {
094 buffer.append("[" + arrayData[ii].toString() + "] ");
095 }
096 return buffer.toString();
097 }
098
099 public Object clone()
100 {
101 try
102 {
103 MockArray copy = (MockArray)super.clone();
104 copy.array = ArrayUtil.copyArray(array);
105 return copy;
106 }
107 catch(CloneNotSupportedException exc)
108 {
109 log.error(exc.getMessage(), exc);
110 }
111 return null;
112 }
113 }