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