001: package com.mockrunner.jdbc;
002:
003: import com.mockrunner.mock.jdbc.MockResultSet;
004:
005: /**
006: * A <code>ResultSetFactory</code> implementation which will produce
007: * <code>MockResultSet</code> instances based on information given as
008: * <code>String</code> arrays.
009: *
010: * <p>
011: * <code>StringValuesTable</code> and <code>ArrayResultSetFactory</code> can
012: * provide easy set up of unit test fixtures and assertion of outcomes with the
013: * same data structures, without any need for external sources of test data:
014: * </p>
015: *
016: * <p>
017: * <pre>
018: * private static final String _SQL_SELECT_ALL_EMPLOYEES =
019: * "SELECT * FROM employee";
020: * private StringValuesTable <b>_employeeQueryResults</b>;
021: * ArrayResultSetFactory <b>_arrayResultSetFactory</b>;
022: * private Employee[] _employees;
023: *
024: * protected void setUp() throws Exception {
025: * super.setUp();
026: * <b>_employeeQueryResults</b> = new StringValuesTable(
027: * "employeeQueryResults",
028: * new String[] {
029: * "id", "lastname", "firstname",
030: * },
031: * new String[][] {
032: * new String[] {"1", "gibbons", "peter"},
033: * new String[] {"2", "lumbergh", "bill"},
034: * new String[] {"3", "waddams", "milton"},
035: * }
036: * );
037: * _employees = new Employee[3] {
038: * new Employee(
039: * <b>_employeeQueryResults.getItem(1, "id")</b>,
040: * <b>_employeeQueryResults.getItem(1, "lastname")</b>,
041: * <b>_employeeQueryResults.getItem(1, "firstname")</b>,
042: * ),
043: * ...
044: * };
045: * ...
046: * }
047: *
048: * public void testGetEmployees() throws Exception {
049: * PreparedStatementResultSetHandler preparedStatementResultSetHandler =
050: * getPreparedStatementResultSetHandler();
051: * <b>_arrayResultSetFactory</b> =
052: * new ArrayResultSetFactory(<b>_employeeQueryResults</b>);
053: * MockResultSet resultSet =
054: * preparedStatementResultSetHandler.createResultSet(
055: * <b>_employeeQueryResults.getName()</b>,
056: * <b>arrayResultSetFactory</b>);
057: * preparedStatementResultSetHandler.prepareResultSet(
058: * _SQL_SELECT_ALL_EMPLOYEES, resultSet);
059: *
060: * // execute query, perhaps calling method on an EmployeeDAO...
061: *
062: * assertEquals(
063: * <b>_employeeQueryResults.getNumberOfRows()</b>,
064: * resultsList.size());
065: * for (int i = 0; i < _employees.length; i++) {
066: * assertTrue(resultsList.contains(_employees[i]));
067: * }
068: * MockResultSet mockResultSet =
069: * preparedStatementResultSetHandler.getResultSet(
070: * SQL_SELECT_ALL_EMPLOYEES);
071: * int rows = mockResultSet.getRowCount();
072: * for (int row = 1; row <= rows; row++) {
073: * verifyResultSetRow(
074: * <b>_employeeQueryResults.getName()</b>,
075: * row, <b>_employeeQueryResults.getRow(row)</b>);
076: * }
077: * verifySQLStatementExecuted(_SQL_SELECT_ALL_EMPLOYEES);
078: * verifyAllResultSetsClosed();
079: * verifyAllStatementsClosed();
080: * verifyConnectionClosed();
081: * }
082: * </pre>
083: * </p>
084: *
085: * @author Erick G. Reid
086: */
087: public class ArrayResultSetFactory implements ResultSetFactory {
088: private String[] columnNames = new String[0];
089: private String[][] stringMatrix = new String[0][0];
090:
091: /**
092: * Creates a new <code>ArrayResultSetFactory</code> that will produce
093: * result sets based on information in the given
094: * <code>StringValuesTable</code>.
095: *
096: * @param stringValuesTable the <code>StringValuesTable</code> to use. This argument
097: * cannot be <code>null</code>.
098: */
099: public ArrayResultSetFactory(StringValuesTable stringValuesTable) {
100: if (stringValuesTable != null) {
101: this .stringMatrix = stringValuesTable.getStringMatrix();
102: this .columnNames = stringValuesTable.getColumnNames();
103: return;
104: }
105: throw new IllegalArgumentException(
106: "the string table cannot be null");
107: }
108:
109: /**
110: * Creates a new <code>ArrayResultSetFactory</code> with the given matrix
111: * for data representation.
112: *
113: * @param stringMatrix the data representation for the result sets this factory will
114: * produce. This argument cannot be <code>null</code>, must
115: * not contain any null values, and each array in the matrix must
116: * contain the same number of elements as the first (<code>stringMatrix[0].length == stringMatrix[n].length</code>
117: * for any given valid row number, <code>n</code>). Further,
118: * this matrix must, at a minimum represent <code>1</code> row
119: * and <code>1</code> column of items (<code>stringMatrix.length >= 1</code>,
120: * and <code>stringMatrix[0].length >= 1</code>).
121: */
122: public ArrayResultSetFactory(String[][] stringMatrix) {
123: this .stringMatrix = StringValuesTable
124: .verifyStringMatrix(stringMatrix);
125: }
126:
127: /**
128: * Creates a new <code>ArrayResultSetFactory</code> with the given set of
129: * column names and the given matrix for data representation.
130: *
131: * @param columnNames the column names for the result sets this factory will
132: * produce. This argument may be <code>null</code> if no column
133: * names are desired, but if a non-<code>null</code> array
134: * reference is given, the array cannot contain any
135: * <code>null</code> nor duplicate elements, and must have the
136: * same number of elements as there are columns in the given
137: * string matrix (<code>stringMatrix[n]</code> for any given
138: * valid row number, <code>n</code>).
139: * @param stringMatrix the data representation for the result sets this factory will
140: * produce. This argument cannot be <code>null</code>, must
141: * not contain any null values, and each array in the matrix must
142: * contain the same number of elements as the first (<code>stringMatrix[0].length == stringMatrix[n].length</code>
143: * for any given valid row number, <code>n</code>). Further,
144: * this matrix must, at a minimum represent <code>1</code> row
145: * and <code>1</code> column of items (<code>stringMatrix.length >= 1</code>,
146: * and <code>stringMatrix[0].length >= 1</code>).
147: */
148: public ArrayResultSetFactory(String[] columnNames,
149: String[][] stringMatrix) {
150: this .stringMatrix = StringValuesTable
151: .verifyStringMatrix(stringMatrix);
152: if (columnNames != null) {
153: this .columnNames = StringValuesTable.verifyColumnNames(
154: columnNames, stringMatrix);
155: }
156: }
157:
158: /**
159: * Returns a <code>MockResultSet</code> with the given ID, containing
160: * values based on the elements given at construction.
161: *
162: * @param id the ID for the result set. This argument cannot be
163: * <code>null</code>.
164: */
165: public MockResultSet create(String id) {
166: if (id != null) {
167: MockResultSet resultSet = new MockResultSet(id);
168: if (columnNames != null) {
169: for (int ii = 0; ii < columnNames.length; ii++) {
170: resultSet.addColumn(columnNames[ii]);
171: }
172: }
173: for (int jj = 0; jj < stringMatrix.length; jj++) {
174: resultSet.addRow(stringMatrix[jj]);
175: }
176: return resultSet;
177: }
178: throw new IllegalArgumentException(
179: "the result set ID cannot be null");
180: }
181: }
|