All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mockrunner.jdbc.ArrayResultSetFactory Maven / Gradle / Ivy

Go to download

Mockrunner is a lightweight framework for unit testing applications in the J2EE environment. It supports servlets, filters, tag classes and Struts actions. It includes a JDBC a JMS and a JCA test framework and can be used to test EJB based applications.

The newest version!
package com.mockrunner.jdbc;

import com.mockrunner.mock.jdbc.MockResultSet;

/**
 * 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 */ public class ArrayResultSetFactory implements ResultSetFactory { private String[] columnNames = new String[0]; private String[][] stringMatrix = new String[0][0]; /** * Creates a new ArrayResultSetFactory that will produce * result sets based on information in the given * StringValuesTable. * * @param stringValuesTable the StringValuesTable to use. This argument * cannot be null. */ public ArrayResultSetFactory(StringValuesTable stringValuesTable) { if (stringValuesTable != null) { this.stringMatrix = stringValuesTable.getStringMatrix(); this.columnNames = stringValuesTable.getColumnNames(); return; } throw new IllegalArgumentException("the string table cannot be null"); } /** * Creates a new ArrayResultSetFactory with the given matrix * for data representation. * * @param 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). */ public ArrayResultSetFactory(String[][] stringMatrix) { this.stringMatrix = StringValuesTable.verifyStringMatrix(stringMatrix); } /** * Creates a new ArrayResultSetFactory with the given set of * column names and the given matrix for data representation. * * @param 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). * @param 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). */ public ArrayResultSetFactory(String[] columnNames, String[][] stringMatrix) { this.stringMatrix = StringValuesTable.verifyStringMatrix(stringMatrix); if (columnNames != null) { this.columnNames = StringValuesTable.verifyColumnNames(columnNames, stringMatrix); } } /** * Returns a MockResultSet with the given ID, containing * values based on the elements given at construction. * * @param id the ID for the result set. This argument cannot be * null. */ public MockResultSet create(String id) { if (id != null) { MockResultSet resultSet = new MockResultSet(id); if (columnNames != null) { for (int ii = 0; ii < columnNames.length; ii++) { resultSet.addColumn(columnNames[ii]); } } for (int jj = 0; jj < stringMatrix.length; jj++) { resultSet.addRow(stringMatrix[jj]); } return resultSet; } throw new IllegalArgumentException("the result set ID cannot be null"); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy