1 package com.mockobjects.sql; 2 3 import com.mockobjects.util.AssertMo; 4 5 import java.sql.SQLException ; 6 7 12 abstract class CommonMockMultiRowResultSet extends MockResultSet { 13 private Object [][] myRows = new Object [0][0]; 14 private String [] myColumnNames; 15 private int myRowOffset = -1; 16 17 private SQLException myGetException = null; 18 19 public CommonMockMultiRowResultSet() { 20 super(); 21 } 22 23 26 public CommonMockMultiRowResultSet(String name) { 27 super(name); 28 } 29 30 public void setupColumnNames(String [] columnNames) { 31 myColumnNames = columnNames; 32 } 33 34 public void setupRows(Object [][] rows) { 35 myRows = rows; 36 } 37 38 public void setupThrowExceptionOnGet(SQLException exception) { 39 myGetException = exception; 40 } 41 42 public Object getObject(int columnIndex) throws SQLException { 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 getObject(String columnName) throws SQLException { 51 throwGetExceptionIfAny(); 52 return getObject(findIndexForColumnName(columnName)); 53 } 54 55 public boolean next() throws SQLException { 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 { 65 return myRowOffset + 1; 66 } 67 68 private void throwGetExceptionIfAny() throws SQLException { 69 if (null != myGetException) { 70 throw myGetException; 71 } 72 } 73 74 private int findIndexForColumnName(String columnName) throws SQLException { 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 ("Column name not found"); 81 } 82 } 83 | Popular Tags |