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);
}
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();
}
}
}
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");
}
}
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"));
}
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");
}
}
Aggregations