Search in sources :

Example 16 with DBResultSet

use of com.microsoft.sqlserver.testframework.DBResultSet in project mssql-jdbc by Microsoft.

the class bvtTest method testResultSetAndCloseStmt.

/**
 * Verify resultset closed after statement is closed
 *
 * @throws SQLException
 */
@Test
public void testResultSetAndCloseStmt() throws SQLException {
    String query = "SELECT * FROM " + table1.getEscapedTableName();
    try (DBConnection conn = new DBConnection(connectionString);
        DBStatement stmt = conn.createStatement();
        DBResultSet rs = stmt.executeQuery(query)) {
        // this should close the resultSet
        stmt.close();
        try {
            rs.next();
        } catch (SQLException e) {
            assertEquals(e.toString(), "com.microsoft.sqlserver.jdbc.SQLServerException: The result set is closed.");
        }
        assertTrue(true, "Previous one should have thrown exception!");
    }
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) SQLException(java.sql.SQLException) DBStatement(com.microsoft.sqlserver.testframework.DBStatement) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet) Test(org.junit.jupiter.api.Test)

Example 17 with DBResultSet

use of com.microsoft.sqlserver.testframework.DBResultSet in project mssql-jdbc by Microsoft.

the class bvtTest method testStmtForwardOnlyReadOnly.

/**
 * Create a statement ResultSet.Type_forward_only, ResultSet.CONCUR_READ_ONLY, executeQuery verify cursor by using next and previous and verify
 * data
 *
 * @throws SQLException
 * @throws ClassNotFoundException
 */
@Test
public void testStmtForwardOnlyReadOnly() throws SQLException, ClassNotFoundException {
    String query = "SELECT * FROM " + table1.getEscapedTableName();
    try (DBConnection conn = new DBConnection(connectionString);
        DBStatement stmt = conn.createStatement(DBResultSetTypes.TYPE_FORWARD_ONLY_CONCUR_READ_ONLY);
        DBResultSet rs = stmt.executeQuery(query)) {
        rs.next();
        rs.verifyCurrentRow(table1);
        rs.next();
        rs.verifyCurrentRow(table1);
        try {
            rs.previous();
            assertTrue(false, "Previous should have thrown an exception");
        } catch (SQLException ex) {
        // expected exception
        }
        rs.verify(table1);
    }
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) SQLException(java.sql.SQLException) DBStatement(com.microsoft.sqlserver.testframework.DBStatement) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet) Test(org.junit.jupiter.api.Test)

Example 18 with DBResultSet

use of com.microsoft.sqlserver.testframework.DBResultSet in project mssql-jdbc by Microsoft.

the class ComparisonUtil method compareSrcTableAndDestTableIgnoreRowOrder.

/**
 * test if source table and destination table are the same
 *
 * @param con
 * @param srcTable
 * @param destTable
 * @throws SQLException
 */
public static void compareSrcTableAndDestTableIgnoreRowOrder(DBConnection con, DBTable srcTable, DBTable destTable) throws SQLException {
    DBResultSet srcResultSetCount = con.createStatement().executeQuery("SELECT COUNT(*) FROM " + srcTable.getEscapedTableName() + ";");
    DBResultSet dstResultSetCount = con.createStatement().executeQuery("SELECT COUNT(*) FROM " + destTable.getEscapedTableName() + ";");
    srcResultSetCount.next();
    dstResultSetCount.next();
    int srcRows = srcResultSetCount.getInt(1);
    int destRows = dstResultSetCount.getInt(1);
    if (srcRows != destRows) {
        fail("Souce table and Destination table have different number of rows.");
    }
    if (srcTable.getColumns().size() != destTable.getColumns().size()) {
        fail("Souce table and Destination table have different number of columns.");
    }
    DBResultSet srcResultSet = con.createStatement().executeQuery("SELECT * FROM " + srcTable.getEscapedTableName() + " ORDER BY [" + srcTable.getColumnName(1) + "], [" + srcTable.getColumnName(2) + "],[" + srcTable.getColumnName(3) + "];");
    DBResultSet dstResultSet = con.createStatement().executeQuery("SELECT * FROM " + destTable.getEscapedTableName() + " ORDER BY [" + destTable.getColumnName(1) + "], [" + destTable.getColumnName(2) + "],[" + destTable.getColumnName(3) + "];");
    while (srcResultSet.next() && dstResultSet.next()) {
        for (int i = 0; i < destTable.getColumns().size(); i++) {
            SQLServerResultSetMetaData srcMeta = (SQLServerResultSetMetaData) ((ResultSet) srcResultSet.product()).getMetaData();
            SQLServerResultSetMetaData destMeta = (SQLServerResultSetMetaData) ((ResultSet) dstResultSet.product()).getMetaData();
            int srcJDBCTypeInt = srcMeta.getColumnType(i + 1);
            int destJDBCTypeInt = destMeta.getColumnType(i + 1);
            // verify column types
            if (srcJDBCTypeInt != destJDBCTypeInt) {
                fail("Souce table and Destination table have different number of columns.");
            }
            Object expectedValue = srcResultSet.getObject(i + 1);
            Object actualValue = dstResultSet.getObject(i + 1);
            compareExpectedAndActual(destJDBCTypeInt, expectedValue, actualValue);
        }
    }
}
Also used : SQLServerResultSetMetaData(com.microsoft.sqlserver.jdbc.SQLServerResultSetMetaData) DBResultSet(com.microsoft.sqlserver.testframework.DBResultSet)

Aggregations

DBResultSet (com.microsoft.sqlserver.testframework.DBResultSet)18 DBStatement (com.microsoft.sqlserver.testframework.DBStatement)15 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)13 Test (org.junit.jupiter.api.Test)11 SQLException (java.sql.SQLException)6 ResultSet (java.sql.ResultSet)4 ResultSetMetaData (java.sql.ResultSetMetaData)4 SQLServerBulkCopy (com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)1 SQLServerResultSetMetaData (com.microsoft.sqlserver.jdbc.SQLServerResultSetMetaData)1 ColumnMap (com.microsoft.sqlserver.jdbc.bulkCopy.BulkCopyTestWrapper.ColumnMap)1 DBColumn (com.microsoft.sqlserver.testframework.DBColumn)1 DBPreparedStatement (com.microsoft.sqlserver.testframework.DBPreparedStatement)1 DBResultSetTypes (com.microsoft.sqlserver.testframework.DBResultSetTypes)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Reader (java.io.Reader)1 BigDecimal (java.math.BigDecimal)1