KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > sql > CommonMockMultiRowResultSet


1 package com.mockobjects.sql;
2
3 import com.mockobjects.util.AssertMo;
4
5 import java.sql.SQLException JavaDoc;
6
7 /**
8  * This is a simple implementation of a MockResultSet.
9  * It returns values for multiple rows.
10  * @version $Revision: 1.4 $
11  */

12 abstract class CommonMockMultiRowResultSet extends MockResultSet {
13     private Object JavaDoc[][] myRows = new Object JavaDoc[0][0];
14     private String JavaDoc[] myColumnNames;
15     private int myRowOffset = -1;
16
17     private SQLException JavaDoc myGetException = null;
18
19     public CommonMockMultiRowResultSet() {
20         super();
21     }
22
23     /**
24      * @param name Label used to identify mock when it errors
25      */

26     public CommonMockMultiRowResultSet(String JavaDoc name) {
27         super(name);
28     }
29
30     public void setupColumnNames(String JavaDoc[] columnNames) {
31         myColumnNames = columnNames;
32     }
33
34     public void setupRows(Object JavaDoc[][] rows) {
35         myRows = rows;
36     }
37
38     public void setupThrowExceptionOnGet(SQLException JavaDoc exception) {
39         myGetException = exception;
40     }
41
42     public Object JavaDoc getObject(int columnIndex) throws SQLException JavaDoc {
43         throwGetExceptionIfAny();
44         if (columnIndex > myRows[myRowOffset].length || columnIndex < 1) {
45             AssertMo.fail("Column " + columnIndex + " not found in " + name);
46         }
47         return myRows[myRowOffset][columnIndex - 1];
48     }
49
50     public Object JavaDoc getObject(String JavaDoc columnName) throws SQLException JavaDoc {
51         throwGetExceptionIfAny();
52         return getObject(findIndexForColumnName(columnName));
53     }
54
55     public boolean next() throws SQLException JavaDoc {
56         myNextCalls.inc();
57         if (myRowOffset + 1 >= myRows.length) {
58             return false;
59         }
60         myRowOffset++;
61         return true;
62     }
63
64     public int getRow() throws SQLException JavaDoc {
65         return myRowOffset + 1;
66     }
67
68     private void throwGetExceptionIfAny() throws SQLException JavaDoc {
69         if (null != myGetException) {
70             throw myGetException;
71         }
72     }
73
74     private int findIndexForColumnName(String JavaDoc columnName) throws SQLException JavaDoc {
75         for (int i = 0; i < myColumnNames.length; ++i) {
76             if (myColumnNames[i].equalsIgnoreCase(columnName)) {
77                 return i + 1;
78             }
79         }
80         throw new SQLException JavaDoc("Column name not found");
81     }
82 }
83
Popular Tags