com.mockrunner.jdbc
Class ArrayResultSetFactory

java.lang.Object
  extended by com.mockrunner.jdbc.ArrayResultSetFactory
All Implemented Interfaces:
ResultSetFactory

public class ArrayResultSetFactory
extends java.lang.Object
implements ResultSetFactory

A ResultSetFactory implementation which will produce MockResultSet instances based on information given as String arrays.

StringValuesTable and ArrayResultSetFactory can provide easy set up of unit test fixtures and assertion of outcomes with the same data structures, without any need for external sources of test data:

  private static final String _SQL_SELECT_ALL_EMPLOYEES = 
      "SELECT * FROM employee";
  private StringValuesTable _employeeQueryResults;
  ArrayResultSetFactory _arrayResultSetFactory;
  private Employee[] _employees;
  
  protected void setUp() throws Exception {
    super.setUp();
    _employeeQueryResults = new StringValuesTable(
        "employeeQueryResults", 
        new String[] {
            "id", "lastname", "firstname",
        }, 
        new String[][] {
            new String[] {"1", "gibbons", "peter"},
            new String[] {"2", "lumbergh", "bill"},
            new String[] {"3", "waddams", "milton"},
        }
    );
    _employees = new Employee[3] {
        new Employee(
            _employeeQueryResults.getItem(1, "id"),
            _employeeQueryResults.getItem(1, "lastname"),
            _employeeQueryResults.getItem(1, "firstname"),
        ),
        ...
    };
    ...
  }
    
  public void testGetEmployees() throws Exception {
    PreparedStatementResultSetHandler preparedStatementResultSetHandler = 
        getPreparedStatementResultSetHandler();
    _arrayResultSetFactory = 
        new ArrayResultSetFactory(_employeeQueryResults);
    MockResultSet resultSet = 
        preparedStatementResultSetHandler.createResultSet(
            _employeeQueryResults.getName(), 
            arrayResultSetFactory);
    preparedStatementResultSetHandler.prepareResultSet(
        _SQL_SELECT_ALL_EMPLOYEES, resultSet);
        
    // execute query, perhaps calling method on an EmployeeDAO...
    
    assertEquals(
        _employeeQueryResults.getNumberOfRows(), 
        resultsList.size());
    for (int i = 0; i < _employees.length; i++) {
       assertTrue(resultsList.contains(_employees[i]));
    }
    MockResultSet mockResultSet = 
        preparedStatementResultSetHandler.getResultSet(
            SQL_SELECT_ALL_EMPLOYEES);
    int rows = mockResultSet.getRowCount();
    for (int row = 1; row <= rows; row++) {
      verifyResultSetRow(
          _employeeQueryResults.getName(), 
          row, _employeeQueryResults.getRow(row));
    }
    verifySQLStatementExecuted(_SQL_SELECT_ALL_EMPLOYEES);
    verifyAllResultSetsClosed();
    verifyAllStatementsClosed();
    verifyConnectionClosed();     
  }
 

Author:
Erick G. Reid

Constructor Summary
ArrayResultSetFactory(java.lang.String[][] stringMatrix)
          Creates a new ArrayResultSetFactory with the given matrix for data representation.
ArrayResultSetFactory(java.lang.String[] columnNames, java.lang.String[][] stringMatrix)
          Creates a new ArrayResultSetFactory with the given set of column names and the given matrix for data representation.
ArrayResultSetFactory(StringValuesTable stringValuesTable)
          Creates a new ArrayResultSetFactory that will produce result sets based on information in the given StringValuesTable.
 
Method Summary
 MockResultSet create(java.lang.String id)
          Returns a MockResultSet with the given ID, containing values based on the elements given at construction.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ArrayResultSetFactory

public ArrayResultSetFactory(StringValuesTable stringValuesTable)
Creates a new ArrayResultSetFactory that will produce result sets based on information in the given StringValuesTable.

Parameters:
stringValuesTable - the StringValuesTable to use. This argument cannot be null.

ArrayResultSetFactory

public ArrayResultSetFactory(java.lang.String[][] stringMatrix)
Creates a new ArrayResultSetFactory with the given matrix for data representation.

Parameters:
stringMatrix - the data representation for the result sets this factory will produce. This argument cannot be null, must not contain any null values, and each array in the matrix must contain the same number of elements as the first (stringMatrix[0].length == stringMatrix[n].length for any given valid row number, n). Further, this matrix must, at a minimum represent 1 row and 1 column of items (stringMatrix.length >= 1, and stringMatrix[0].length >= 1).

ArrayResultSetFactory

public ArrayResultSetFactory(java.lang.String[] columnNames,
                             java.lang.String[][] stringMatrix)
Creates a new ArrayResultSetFactory with the given set of column names and the given matrix for data representation.

Parameters:
columnNames - the column names for the result sets this factory will produce. This argument may be null if no column names are desired, but if a non-null array reference is given, the array cannot contain any null nor duplicate elements, and must have the same number of elements as there are columns in the given string matrix (stringMatrix[n] for any given valid row number, n).
stringMatrix - the data representation for the result sets this factory will produce. This argument cannot be null, must not contain any null values, and each array in the matrix must contain the same number of elements as the first (stringMatrix[0].length == stringMatrix[n].length for any given valid row number, n). Further, this matrix must, at a minimum represent 1 row and 1 column of items (stringMatrix.length >= 1, and stringMatrix[0].length >= 1).
Method Detail

create

public MockResultSet create(java.lang.String id)
Returns a MockResultSet with the given ID, containing values based on the elements given at construction.

Specified by:
create in interface ResultSetFactory
Parameters:
id - the ID for the result set. This argument cannot be null.