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