Search in sources :

Example 26 with DBConnection

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

the class AESetup method setUpConnection.

/**
 * Create connection, statement and generate path of resource file
 *
 * @throws Exception
 * @throws TestAbortedException
 */
@BeforeAll
static void setUpConnection() throws TestAbortedException, Exception {
    assumeTrue(13 <= new DBConnection(connectionString).getServerVersion(), "Aborting test case as SQL Server version is not compatible with Always encrypted ");
    String AETestConenctionString = connectionString + ";sendTimeAsDateTime=false";
    readFromFile(javaKeyStoreInputFile, "Alias name");
    try (SQLServerConnection con = (SQLServerConnection) DriverManager.getConnection(AETestConenctionString);
        SQLServerStatement stmt = (SQLServerStatement) con.createStatement()) {
        dropCEK(stmt);
        dropCMK(stmt);
    }
    keyPath = Utils.getCurrentClassPath() + jksName;
    storeProvider = new SQLServerColumnEncryptionJavaKeyStoreProvider(keyPath, secretstrJks.toCharArray());
    stmtColEncSetting = SQLServerStatementColumnEncryptionSetting.Enabled;
    Properties info = new Properties();
    info.setProperty("ColumnEncryptionSetting", "Enabled");
    info.setProperty("keyStoreAuthentication", "JavaKeyStorePassword");
    info.setProperty("keyStoreLocation", keyPath);
    info.setProperty("keyStoreSecret", secretstrJks);
    con = (SQLServerConnection) DriverManager.getConnection(AETestConenctionString, info);
    stmt = (SQLServerStatement) con.createStatement();
    createCMK(keyStoreName, javaKeyAliases);
    createCEK(storeProvider);
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) SQLServerConnection(com.microsoft.sqlserver.jdbc.SQLServerConnection) Properties(java.util.Properties) SQLServerStatement(com.microsoft.sqlserver.jdbc.SQLServerStatement) SQLServerColumnEncryptionJavaKeyStoreProvider(com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionJavaKeyStoreProvider) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 27 with DBConnection

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

the class TVPAllTypes method testTVPStoredProcedureResultSet.

private void testTVPStoredProcedureResultSet(boolean setSelectMethod, Integer resultSetType, Integer resultSetConcurrency) throws SQLException {
    setupVariation();
    Connection connnection = null;
    if (setSelectMethod) {
        connnection = DriverManager.getConnection(connectionString + ";selectMethod=cursor;");
    } else {
        connnection = DriverManager.getConnection(connectionString);
    }
    Statement stmtement = null;
    if (null != resultSetType || null != resultSetConcurrency) {
        stmtement = connnection.createStatement(resultSetType, resultSetConcurrency);
    } else {
        stmtement = connnection.createStatement();
    }
    ResultSet rs = stmtement.executeQuery("select * from " + tableSrc.getEscapedTableName());
    String sql = "{call " + procedureName + "(?)}";
    SQLServerCallableStatement Cstmt = (SQLServerCallableStatement) connnection.prepareCall(sql);
    Cstmt.setStructured(1, tvpName, rs);
    Cstmt.execute();
    ComparisonUtil.compareSrcTableAndDestTableIgnoreRowOrder(new DBConnection(connectionString), tableSrc, tableDest);
    terminateVariation();
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) DBStatement(com.microsoft.sqlserver.testframework.DBStatement) SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) Statement(java.sql.Statement) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) Connection(java.sql.Connection) DBConnection(com.microsoft.sqlserver.testframework.DBConnection) ResultSet(java.sql.ResultSet)

Example 28 with DBConnection

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

the class TVPAllTypes method testTVPResultSet.

private void testTVPResultSet(boolean setSelectMethod, Integer resultSetType, Integer resultSetConcurrency) throws SQLException {
    setupVariation();
    Connection connnection = null;
    if (setSelectMethod) {
        connnection = DriverManager.getConnection(connectionString + ";selectMethod=cursor;");
    } else {
        connnection = DriverManager.getConnection(connectionString);
    }
    Statement stmtement = null;
    if (null != resultSetType || null != resultSetConcurrency) {
        stmtement = connnection.createStatement(resultSetType, resultSetConcurrency);
    } else {
        stmtement = connnection.createStatement();
    }
    ResultSet rs = stmtement.executeQuery("select * from " + tableSrc.getEscapedTableName());
    SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connnection.prepareStatement("INSERT INTO " + tableDest.getEscapedTableName() + " select * from ? ;");
    pstmt.setStructured(1, tvpName, rs);
    pstmt.execute();
    ComparisonUtil.compareSrcTableAndDestTableIgnoreRowOrder(new DBConnection(connectionString), tableSrc, tableDest);
    terminateVariation();
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) DBStatement(com.microsoft.sqlserver.testframework.DBStatement) SQLServerCallableStatement(com.microsoft.sqlserver.jdbc.SQLServerCallableStatement) Statement(java.sql.Statement) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement) Connection(java.sql.Connection) DBConnection(com.microsoft.sqlserver.testframework.DBConnection) ResultSet(java.sql.ResultSet) SQLServerPreparedStatement(com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)

Example 29 with DBConnection

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

the class TVPNumericTest method testSetup.

@BeforeEach
private void testSetup() throws SQLException {
    conn = new DBConnection(connectionString);
    stmt = conn.createStatement();
    dropProcedure();
    dropTables();
    dropTVPS();
    createTVPS();
    createTables();
    createPreocedure();
}
Also used : DBConnection(com.microsoft.sqlserver.testframework.DBConnection) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 30 with DBConnection

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

the class BatchExecuteWithErrorsTest method Repro47239large.

/**
 * Tests large methods, supported in 42
 *
 * @throws Exception
 */
@Test
@DisplayName("Regression test for using 'large' methods")
public void Repro47239large() throws Exception {
    assumeTrue("JDBC42".equals(Utils.getConfiguredProperty("JDBC_Version")), "Aborting test case as JDBC version is not compatible. ");
    // the DBConnection for detecting whether the server is SQL Azure or SQL Server.
    con = DriverManager.getConnection(connectionString);
    final String warning;
    final String error;
    final String severe;
    if (DBConnection.isSqlAzure(con)) {
        // SQL Azure will throw exception for "raiserror WITH LOG", so the following RAISERROR statements have not "with log" option
        warning = "RAISERROR ('raiserror level 4',4,1)";
        error = "RAISERROR ('raiserror level 11',11,1)";
        // On SQL Azure, raising FATAL error by RAISERROR() is not supported and there is no way to
        // cut the current connection by a statement inside a SQL batch.
        // Details: Although one can simulate a fatal error (that cuts the connections) by dropping the database,
        // this simulation cannot be written entirely in TSQL (because it needs a new connection),
        // and thus it cannot be put into a TSQL batch and it is useless here.
        // So we have to skip the last scenario of this test case, i.e. "Test Severe (connection-closing) errors"
        // It is worthwhile to still execute the first 5 test scenarios of this test case, in order to have best test coverage.
        // this is a dummy statement that never being executed on SQL Azure
        severe = "--Not executed when testing against SQL Azure";
    } else {
        warning = "RAISERROR ('raiserror level 4',4,1) WITH LOG";
        error = "RAISERROR ('raiserror level 11',11,1) WITH LOG";
        severe = "RAISERROR ('raiserror level 20',20,1) WITH LOG";
    }
    con.close();
    long[] actualUpdateCounts;
    long[] expectedUpdateCounts;
    String actualExceptionText;
    // SQL Server 2005 driver
    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    Connection conn = DriverManager.getConnection(connectionString);
    Statement stmt = conn.createStatement();
    try {
        stmt.executeLargeUpdate("drop table " + tableName);
    } catch (Exception ignored) {
    }
    try {
        stmt.executeLargeUpdate("create table " + tableName + " (c1_int int, c2_varchar varchar(20), c3_date datetime, c4_int int identity(1,1) primary key)");
    } catch (Exception ignored) {
    }
    // Regular Statement batch update
    expectedUpdateCounts = new long[] { 1, -2, 1, -2, 1, -2 };
    Statement batchStmt = conn.createStatement();
    batchStmt.addBatch(insertStmt);
    batchStmt.addBatch(warning);
    batchStmt.addBatch(insertStmt);
    batchStmt.addBatch(warning);
    batchStmt.addBatch(insertStmt);
    batchStmt.addBatch(warning);
    try {
        actualUpdateCounts = batchStmt.executeLargeBatch();
        actualExceptionText = "";
    } catch (BatchUpdateException bue) {
        actualUpdateCounts = bue.getLargeUpdateCounts();
        actualExceptionText = bue.getMessage();
        log.fine("BatchUpdateException occurred. Message:" + actualExceptionText);
    } finally {
        batchStmt.close();
    }
    log.fine("UpdateCounts:");
    for (long updateCount : actualUpdateCounts) {
        log.fine("" + updateCount + ",");
    }
    log.fine("");
    assertTrue(Arrays.equals(actualUpdateCounts, expectedUpdateCounts), "Test interleaved inserts and warnings");
    expectedUpdateCounts = new long[] { -3, 1, 1, 1 };
    stmt.addBatch(error);
    stmt.addBatch(insertStmt);
    stmt.addBatch(insertStmt);
    stmt.addBatch(insertStmt);
    try {
        actualUpdateCounts = stmt.executeLargeBatch();
        actualExceptionText = "";
    } catch (BatchUpdateException bue) {
        actualUpdateCounts = bue.getLargeUpdateCounts();
        actualExceptionText = bue.getMessage();
    }
    log.fine("UpdateCounts:");
    for (long updateCount : actualUpdateCounts) {
        log.fine("" + updateCount + ",");
    }
    log.fine("");
    assertTrue(Arrays.equals(actualUpdateCounts, expectedUpdateCounts), "Test error followed by inserts");
    // 50280
    expectedUpdateCounts = new long[] { 1, -3 };
    stmt.addBatch(insertStmt);
    stmt.addBatch(error16);
    try {
        actualUpdateCounts = stmt.executeLargeBatch();
        actualExceptionText = "";
    } catch (BatchUpdateException bue) {
        actualUpdateCounts = bue.getLargeUpdateCounts();
        actualExceptionText = bue.getMessage();
    }
    for (long updateCount : actualUpdateCounts) {
        log.fine("" + updateCount + ",");
    }
    log.fine("");
    assertTrue(Arrays.equals(actualUpdateCounts, expectedUpdateCounts), "Test insert followed by non-fatal error (50280)");
    // Test "soft" errors
    conn.setAutoCommit(false);
    stmt.addBatch(select);
    stmt.addBatch(insertStmt);
    stmt.addBatch(select);
    stmt.addBatch(insertStmt);
    try {
        stmt.executeLargeBatch();
        assertEquals(false, true, "Soft error test: executeLargeBatch unexpectedly succeeded");
    } catch (BatchUpdateException bue) {
        assertEquals("A result set was generated for update.", bue.getMessage(), "Soft error test: wrong error message in BatchUpdateException");
        assertEquals(Arrays.equals(bue.getLargeUpdateCounts(), new long[] { -3, 1, -3, 1 }), true, "Soft error test: wrong update counts in BatchUpdateException");
    }
    conn.rollback();
    // Defect 128801: Rollback (with conversion error) should throw SQLException
    stmt.addBatch(dateConversionError);
    stmt.addBatch(insertStmt);
    stmt.addBatch(insertStmt);
    stmt.addBatch(insertStmt);
    try {
        stmt.executeLargeBatch();
    } catch (BatchUpdateException bue) {
        assertThat(bue.getMessage(), containsString("Syntax error converting date"));
    } catch (SQLException e) {
        assertThat(e.getMessage(), containsString("Conversion failed when converting date"));
    }
    conn.setAutoCommit(true);
    // It is worthwhile to still execute the first 5 test scenarios of this test case, in order to have best test coverage.
    if (!DBConnection.isSqlAzure(DriverManager.getConnection(connectionString))) {
        // Test Severe (connection-closing) errors
        stmt.addBatch(error);
        stmt.addBatch(insertStmt);
        stmt.addBatch(warning);
        stmt.addBatch(insertStmt);
        stmt.addBatch(severe);
        stmt.addBatch(insertStmt);
        stmt.addBatch(insertStmt);
        try {
            stmt.executeLargeBatch();
            assertEquals(false, true, "Test fatal errors batch execution succeeded (should have failed)");
        } catch (BatchUpdateException bue) {
            assertEquals(false, true, "Test fatal errors returned BatchUpdateException rather than SQLException");
        } catch (SQLException e) {
            actualExceptionText = e.getMessage();
            if (actualExceptionText.endsWith("reset")) {
                assertTrue(actualExceptionText.equalsIgnoreCase("Connection reset"), "Test fatal errors");
            } else {
                assertTrue(actualExceptionText.equalsIgnoreCase("raiserror level 20"), "Test fatal errors");
            }
        }
    }
    try {
        stmt.executeLargeUpdate("drop table " + tableName);
    } catch (Exception ignored) {
    }
    stmt.close();
    conn.close();
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) DBConnection(com.microsoft.sqlserver.testframework.DBConnection) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) BatchUpdateException(java.sql.BatchUpdateException) SQLException(java.sql.SQLException) BatchUpdateException(java.sql.BatchUpdateException) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

DBConnection (com.microsoft.sqlserver.testframework.DBConnection)38 DBStatement (com.microsoft.sqlserver.testframework.DBStatement)25 Test (org.junit.jupiter.api.Test)16 DBResultSet (com.microsoft.sqlserver.testframework.DBResultSet)14 SQLException (java.sql.SQLException)8 DBTable (com.microsoft.sqlserver.testframework.DBTable)6 BeforeAll (org.junit.jupiter.api.BeforeAll)6 Connection (java.sql.Connection)5 Statement (java.sql.Statement)5 ResultSet (java.sql.ResultSet)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 SQLServerBulkCopy (com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)3 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)3 SQLServerCallableStatement (com.microsoft.sqlserver.jdbc.SQLServerCallableStatement)2 SQLServerPreparedStatement (com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)2 SQLServerStatement (com.microsoft.sqlserver.jdbc.SQLServerStatement)2 DBColumn (com.microsoft.sqlserver.testframework.DBColumn)2 DBPreparedStatement (com.microsoft.sqlserver.testframework.DBPreparedStatement)2 DisplayName (org.junit.jupiter.api.DisplayName)2 SQLServerColumnEncryptionJavaKeyStoreProvider (com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionJavaKeyStoreProvider)1