Search in sources :

Example 16 with SQLServerConnection

use of com.microsoft.sqlserver.jdbc.SQLServerConnection 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 17 with SQLServerConnection

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

the class ExceptionTest method testSocketTimeoutExceptionCause.

/**
 * Test the SQLException has the proper cause when socket timeout occurs.
 *
 * @throws Exception
 */
@Test
public void testSocketTimeoutExceptionCause() throws Exception {
    SQLServerConnection conn = null;
    try {
        conn = (SQLServerConnection) DriverManager.getConnection(connectionString);
        Utils.dropProcedureIfExists(waitForDelaySPName, conn.createStatement());
        createWaitForDelayPreocedure(conn);
        conn = (SQLServerConnection) DriverManager.getConnection(connectionString + ";socketTimeout=" + (waitForDelaySeconds * 1000 / 2) + ";");
        try {
            conn.createStatement().execute("exec " + waitForDelaySPName);
            throw new Exception("Exception for socketTimeout is not thrown.");
        } catch (Exception e) {
            if (!(e instanceof SQLException)) {
                throw e;
            }
            assertTrue(null != e.getCause(), "Cause should not be null.");
            assertTrue(e.getCause() instanceof SocketTimeoutException, "Cause should be instance of SocketTimeoutException.");
        }
    } finally {
        if (null != conn) {
            conn.close();
        }
    }
}
Also used : SQLServerConnection(com.microsoft.sqlserver.jdbc.SQLServerConnection) SocketTimeoutException(java.net.SocketTimeoutException) SQLException(java.sql.SQLException) SQLException(java.sql.SQLException) SocketTimeoutException(java.net.SocketTimeoutException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Example 18 with SQLServerConnection

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

the class ConnectionDriverTest method testClientConnectionId.

@Test
public void testClientConnectionId() throws Exception {
    SQLServerConnection conn = (SQLServerConnection) DriverManager.getConnection(connectionString);
    assertTrue(conn.getClientConnectionId() != null, "ClientConnectionId is null");
    conn.close();
    try {
        // Call getClientConnectionId on a closed connection, should raise exception
        conn.getClientConnectionId();
        throw new Exception("No exception thrown calling getClientConnectionId on a closed connection");
    } catch (SQLException e) {
        assertEquals(e.getMessage(), "The connection is closed.", "Wrong exception message");
    }
    conn = null;
    try {
        // Wrong database, ClientConnectionId should be available in error message
        conn = (SQLServerConnection) DriverManager.getConnection(connectionString + ";databaseName=" + RandomUtil.getIdentifierForDB("DataBase") + ";");
        conn.close();
    } catch (SQLException e) {
        assertTrue(e.getMessage().indexOf("ClientConnectionId") != -1, "Unexpected: ClientConnectionId is not in exception message due to wrong DB");
    }
    try {
        // Nonexist host, ClientConnectionId should not be available in error message
        conn = (SQLServerConnection) DriverManager.getConnection(connectionString + ";instanceName=" + RandomUtil.getIdentifier("Instance") + ";logintimeout=5;");
        conn.close();
    } catch (SQLException e) {
        assertEquals(false, e.getMessage().indexOf("ClientConnectionId") != -1, "Unexpected: ClientConnectionId is in exception message due to wrong host");
    }
}
Also used : SQLServerConnection(com.microsoft.sqlserver.jdbc.SQLServerConnection) ISQLServerConnection(com.microsoft.sqlserver.jdbc.ISQLServerConnection) SQLException(java.sql.SQLException) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLException(java.sql.SQLException) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 19 with SQLServerConnection

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

the class ConnectionDriverTest method testSetSchema.

@Test
public void testSetSchema() throws SQLException {
    SQLServerConnection conn = (SQLServerConnection) DriverManager.getConnection(connectionString);
    conn.setSchema(RandomUtil.getIdentifier("schema"));
}
Also used : SQLServerConnection(com.microsoft.sqlserver.jdbc.SQLServerConnection) ISQLServerConnection(com.microsoft.sqlserver.jdbc.ISQLServerConnection) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest) Test(org.junit.jupiter.api.Test)

Example 20 with SQLServerConnection

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

the class TimeoutTest method testSocketTimeout.

/**
 * When socketTimeout occurs, the connection will be marked as closed.
 * @throws Exception
 */
@Test
public void testSocketTimeout() throws Exception {
    SQLServerConnection conn = (SQLServerConnection) DriverManager.getConnection(connectionString);
    dropWaitForDelayProcedure(conn);
    createWaitForDelayPreocedure(conn);
    conn = (SQLServerConnection) DriverManager.getConnection(connectionString + ";socketTimeout=" + (waitForDelaySeconds * 1000 / 2) + ";");
    try {
        conn.createStatement().execute("exec " + waitForDelaySPName);
        throw new Exception("Exception for socketTimeout is not thrown.");
    } catch (Exception e) {
        if (!(e instanceof SQLException)) {
            throw e;
        }
        assertEquals(e.getMessage(), "Read timed out", "Invalid exception message");
    }
    try {
        conn.createStatement().execute("SELECT @@version");
    } catch (SQLException e) {
        assertEquals(e.getMessage(), "The connection is closed.", "Invalid exception message");
    }
}
Also used : SQLServerConnection(com.microsoft.sqlserver.jdbc.SQLServerConnection) SQLException(java.sql.SQLException) SQLException(java.sql.SQLException) Test(org.junit.jupiter.api.Test) AbstractTest(com.microsoft.sqlserver.testframework.AbstractTest)

Aggregations

SQLServerConnection (com.microsoft.sqlserver.jdbc.SQLServerConnection)25 AbstractTest (com.microsoft.sqlserver.testframework.AbstractTest)19 Test (org.junit.jupiter.api.Test)19 SQLServerPreparedStatement (com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement)8 SQLException (java.sql.SQLException)8 ISQLServerConnection (com.microsoft.sqlserver.jdbc.ISQLServerConnection)7 Statement (java.sql.Statement)5 SQLServerBulkCopy (com.microsoft.sqlserver.jdbc.SQLServerBulkCopy)4 PreparedStatement (java.sql.PreparedStatement)4 ResultSet (java.sql.ResultSet)3 DBConnection (com.microsoft.sqlserver.testframework.DBConnection)2 BatchUpdateException (java.sql.BatchUpdateException)2 Properties (java.util.Properties)2 SQLServerColumnEncryptionJavaKeyStoreProvider (com.microsoft.sqlserver.jdbc.SQLServerColumnEncryptionJavaKeyStoreProvider)1 SQLServerDataSource (com.microsoft.sqlserver.jdbc.SQLServerDataSource)1 SQLServerStatement (com.microsoft.sqlserver.jdbc.SQLServerStatement)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 Connection (java.sql.Connection)1 JDBCType (java.sql.JDBCType)1