Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change scope of Connection/Statement in test to prevent random failures #862

Merged
merged 4 commits into from
Nov 21, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 89 additions & 74 deletions src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPAllTypesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@

@RunWith(JUnitPlatform.class)
public class TVPAllTypesTest extends AbstractTest {
private static Connection conn = null;
static Statement stmt = null;

private static String tvpName;
private static String procedureName;

private static DBTable tableSrc = null;
private static DBTable tableDest = null;

Expand All @@ -59,20 +56,38 @@ public void testTVPResultSet() throws SQLException {

private void testTVPResultSet(boolean setSelectMethod, Integer resultSetType,
Integer resultSetConcurrency) throws SQLException {
setupVariation(setSelectMethod, resultSetType, resultSetConcurrency);

try (ResultSet rs = stmt.executeQuery("select * from " + tableSrc.getEscapedTableName());
SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) conn
.prepareStatement("INSERT INTO " + tableDest.getEscapedTableName() + " select * from ? ;")) {
pstmt.setStructured(1, tvpName, rs);
pstmt.execute();

ComparisonUtil.compareSrcTableAndDestTableIgnoreRowOrder(new DBConnection(connectionString), tableSrc,
tableDest);
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.toString());

String connString;
Statement stmt = null;

if (setSelectMethod) {
connString = connectionString + ";selectMethod=cursor;";
} else {
connString = connectionString;
}

try (Connection conn = DriverManager.getConnection(connString)) {
if (null != resultSetType && null != resultSetConcurrency) {
stmt = conn.createStatement(resultSetType, resultSetConcurrency);
} else {
stmt = conn.createStatement();
}

setupVariation(setSelectMethod, stmt);
try (ResultSet rs = stmt.executeQuery("select * from " + tableSrc.getEscapedTableName());
SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) conn.prepareStatement(
"INSERT INTO " + tableDest.getEscapedTableName() + " select * from ? ;")) {
pstmt.setStructured(1, tvpName, rs);
pstmt.execute();
ComparisonUtil.compareSrcTableAndDestTableIgnoreRowOrder(new DBConnection(connectionString), tableSrc,
tableDest);
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.toString());
} finally {
terminateVariation(stmt);
}
} finally {
terminateVariation();
stmt.close();
}
}

Expand All @@ -93,17 +108,35 @@ public void testTVPStoredProcedureResultSet() throws SQLException {

private void testTVPStoredProcedureResultSet(boolean setSelectMethod, Integer resultSetType,
Integer resultSetConcurrency) throws SQLException {
setupVariation(setSelectMethod, resultSetType, resultSetConcurrency);
try (ResultSet rs = stmt.executeQuery("select * from " + tableSrc.getEscapedTableName());
SQLServerCallableStatement Cstmt = (SQLServerCallableStatement) conn
.prepareCall("{call " + AbstractSQLGenerator.escapeIdentifier(procedureName) + "(?)}")) {
Cstmt.setStructured(1, tvpName, rs);
Cstmt.execute();

ComparisonUtil.compareSrcTableAndDestTableIgnoreRowOrder(new DBConnection(connectionString), tableSrc,
tableDest);
String connString;
Statement stmt = null;

if (setSelectMethod) {
connString = connectionString + ";selectMethod=cursor;";
} else {
connString = connectionString;
}

try (Connection conn = DriverManager.getConnection(connString)) {
if (null != resultSetType && null != resultSetConcurrency) {
stmt = conn.createStatement(resultSetType, resultSetConcurrency);
} else {
stmt = conn.createStatement();
}

setupVariation(setSelectMethod, stmt);
try (ResultSet rs = stmt.executeQuery("select * from " + tableSrc.getEscapedTableName());
SQLServerCallableStatement Cstmt = (SQLServerCallableStatement) conn
.prepareCall("{call " + AbstractSQLGenerator.escapeIdentifier(procedureName) + "(?)}")) {
Cstmt.setStructured(1, tvpName, rs);
Cstmt.execute();
ComparisonUtil.compareSrcTableAndDestTableIgnoreRowOrder(new DBConnection(connectionString), tableSrc,
tableDest);
} finally {
terminateVariation(stmt);
}
} finally {
terminateVariation();
stmt.close();
}
}

Expand All @@ -114,64 +147,53 @@ private void testTVPStoredProcedureResultSet(boolean setSelectMethod, Integer re
*/
@Test
public void testTVPDataTable() throws SQLException {
setupVariation(false, null, null);

SQLServerDataTable dt = new SQLServerDataTable();

int numberOfColumns = tableDest.getColumns().size();
Object[] values = new Object[numberOfColumns];
for (int i = 0; i < numberOfColumns; i++) {
SqlType sqlType = tableDest.getColumns().get(i).getSqlType();
dt.addColumnMetadata(tableDest.getColumnName(i), sqlType.getJdbctype().getVendorTypeNumber());
values[i] = sqlType.createdata();
}

int numberOfRows = 10;
for (int i = 0; i < numberOfRows; i++) {
dt.addRow(values);
}

try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection
.prepareStatement("INSERT INTO " + tableDest.getEscapedTableName() + " select * from ? ;")) {
pstmt.setStructured(1, tvpName, dt);
pstmt.execute();
} finally {
terminateVariation();
try (Connection conn = DriverManager.getConnection(connectionString); Statement stmt = conn.createStatement()) {
setupVariation(false, stmt);
SQLServerDataTable dt = new SQLServerDataTable();
int numberOfColumns = tableDest.getColumns().size();
Object[] values = new Object[numberOfColumns];

for (int i = 0; i < numberOfColumns; i++) {
SqlType sqlType = tableDest.getColumns().get(i).getSqlType();
dt.addColumnMetadata(tableDest.getColumnName(i), sqlType.getJdbctype().getVendorTypeNumber());
values[i] = sqlType.createdata();
}

int numberOfRows = 10;
for (int i = 0; i < numberOfRows; i++) {
dt.addRow(values);
}

try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) conn
.prepareStatement("INSERT INTO " + tableDest.getEscapedTableName() + " select * from ? ;")) {
pstmt.setStructured(1, tvpName, dt);
pstmt.execute();
} finally {
terminateVariation(stmt);
}
}
}

private static void createPreocedure(String procedureName, String destTable) throws SQLException {
private static void createPreocedure(String procedureName, String destTable, Statement stmt) throws SQLException {
String sql = "CREATE PROCEDURE " + AbstractSQLGenerator.escapeIdentifier(procedureName) + " @InputData "
+ AbstractSQLGenerator.escapeIdentifier(tvpName) + " READONLY " + " AS " + " BEGIN " + " INSERT INTO "
+ destTable + " SELECT * FROM @InputData" + " END";

stmt.execute(sql);
}

private static void createTVPS(String tvpName, String TVPDefinition) throws SQLException {
private static void createTVPS(String tvpName, String TVPDefinition, Statement stmt) throws SQLException {
String TVPCreateCmd = "CREATE TYPE " + AbstractSQLGenerator.escapeIdentifier(tvpName) + " as table ("
+ TVPDefinition + ");";
stmt.executeUpdate(TVPCreateCmd);
}

private void setupVariation(boolean setSelectMethod, Integer resultSetType,
Integer resultSetConcurrency) throws SQLException {
private void setupVariation(boolean setSelectMethod, Statement stmt) throws SQLException {

tvpName = RandomUtil.getIdentifier("TVP");
procedureName = RandomUtil.getIdentifier("TVP");

if (setSelectMethod) {
conn = DriverManager.getConnection(connectionString + ";selectMethod=cursor;");
} else {
conn = DriverManager.getConnection(connectionString);
}

if (null != resultSetType || null != resultSetConcurrency) {
stmt = conn.createStatement(resultSetType, resultSetConcurrency);
} else {
stmt = conn.createStatement();
}

TestUtils.dropProcedureIfExists(AbstractSQLGenerator.escapeIdentifier(procedureName), stmt);
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tvpName), stmt);

Expand All @@ -184,24 +206,17 @@ private void setupVariation(boolean setSelectMethod, Integer resultSetType,
dbStmt.createTable(tableSrc);
dbStmt.createTable(tableDest);

createTVPS(tvpName, tableSrc.getDefinitionOfColumns());
createPreocedure(procedureName, tableDest.getEscapedTableName());
createTVPS(tvpName, tableSrc.getDefinitionOfColumns(), stmt);
createPreocedure(procedureName, tableDest.getEscapedTableName(), stmt);

dbStmt.populateTable(tableSrc);
}
}

private void terminateVariation() throws SQLException {
private void terminateVariation(Statement stmt) throws SQLException {
TestUtils.dropProcedureIfExists(AbstractSQLGenerator.escapeIdentifier(procedureName), stmt);
TestUtils.dropTableIfExists(tableSrc.getEscapedTableName(), stmt);
TestUtils.dropTableIfExists(tableDest.getEscapedTableName(), stmt);
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tvpName), stmt);

if (null != stmt) {
stmt.close();
}
if (null != conn) {
conn.close();
}
}
}