Skip to content

Commit

Permalink
Remove hardcoded names in junit tests (#809)
Browse files Browse the repository at this point in the history
generate unique names for tables and procedures instead of using hardcoded names
  • Loading branch information
lilgreenbird authored Oct 5, 2018
1 parent 14890cd commit c50d817
Show file tree
Hide file tree
Showing 50 changed files with 4,063 additions and 2,858 deletions.
267 changes: 160 additions & 107 deletions src/test/java/com/microsoft/sqlserver/jdbc/AlwaysEncrypted/AESetup.java

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

32 changes: 0 additions & 32 deletions src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,38 +380,6 @@ static byte CharToHex(char value) throws SQLException {
return ret;
}

/**
* Utility function for safely closing open resultset/statement/connection
*
* @param ResultSet
* @param Statement
* @param Connection
*/
public static void close(ResultSet rs, Statement stmt, Connection con) {
if (rs != null) {
try {
rs.close();

} catch (SQLException e) {
System.out.println("The result set cannot be closed.");
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
System.out.println("The statement cannot be closed.");
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
System.out.println("The data source connection cannot be closed.");
}
}
}

/**
* Utility method for a callable statement
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void testNoExplicitCM() {
// create dest table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);

// set up bulkCopy without explicit column mapping
BulkCopyTestWrapper bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false);
Expand All @@ -82,7 +82,7 @@ public void testExplicitCM() {
// create dest table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);

// set up bulkCopy with explicit column mapping
BulkCopyTestWrapper bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == ThreadLocalRandom.current().nextInt(2)) ? true : false);
Expand Down Expand Up @@ -120,13 +120,14 @@ public void testExplicitCM() {
@DisplayName("BulkCopy:test unicode column mapping")
public void testUnicodeCM() {
DBTable sourceTableUnicode = null;
DBTable destTableUnicode = null;
try {
// create source unicode table
sourceTableUnicode = new DBTable(true, true);
stmt.createTable(sourceTableUnicode);

// create destination unicode table with same schema as source
DBTable destTableUnicode = sourceTableUnicode.cloneSchema();
destTableUnicode = sourceTableUnicode.cloneSchema();
stmt.createTable(destTableUnicode);

// set up bulkCopy with explicit column mapping
Expand Down Expand Up @@ -159,6 +160,9 @@ public void testUnicodeCM() {
if (null != sourceTableUnicode) {
dropTable(sourceTableUnicode.getEscapedTableName());
}
if (null != destTableUnicode) {
dropTable(destTableUnicode.getEscapedTableName());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.microsoft.sqlserver.jdbc.SQLServerBulkCopy;
import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement;
import com.microsoft.sqlserver.jdbc.TestUtils;
import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;
import com.microsoft.sqlserver.testframework.AbstractTest;


Expand All @@ -43,8 +44,8 @@ public class BulkCopyResultSetCursorTest extends AbstractTest {
static String[] expectedTimestampStrings = {"2015-06-03 13:35:33.4610000", "2442-09-19 01:59:43.9990000",
"2017-04-02 08:58:53.0000000"};

private static String srcTable = null;
private static String desTable = null;
private static String srcTable = RandomUtil.getIdentifier("BulkCopyResultSetCursorTest_SourceTable");
private static String desTable = RandomUtil.getIdentifier("BulkCopyResultSetCursorTest_DestinationTable");

/**
* Test a previous failure when using server cursor and using the same connection to create Bulk Copy and result
Expand All @@ -68,9 +69,10 @@ private void serverCursorsTest(int resultSetType, int resultSetConcurrency) thro
populateSourceTable();

try (Statement stmt2 = conn.createStatement(resultSetType, resultSetConcurrency);
ResultSet rs = stmt2.executeQuery("select * from " + srcTable);
ResultSet rs = stmt2
.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(srcTable));
SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(conn)) {
bulkCopy.setDestinationTableName(desTable);
bulkCopy.setDestinationTableName(AbstractSQLGenerator.escapeIdentifier(desTable));
bulkCopy.writeToServer(rs);

verifyDestinationTableData(expectedBigDecimals.length);
Expand All @@ -94,10 +96,10 @@ public void testSelectMethodSetToCursor() throws SQLException {
createTables(stmt);
populateSourceTable();

try (ResultSet rs = stmt.executeQuery("select * from " + srcTable);
try (ResultSet rs = stmt.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(srcTable));
SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(conn)) {

bulkCopy.setDestinationTableName(desTable);
bulkCopy.setDestinationTableName(AbstractSQLGenerator.escapeIdentifier(desTable));
bulkCopy.writeToServer(rs);

verifyDestinationTableData(expectedBigDecimals.length);
Expand All @@ -119,28 +121,29 @@ public void testMultiplePreparedStatementAndResultSet() throws SQLException {
populateSourceTable();

try (Statement stmt1 = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt1.executeQuery("select * from " + srcTable)) {
ResultSet rs = stmt1
.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(srcTable))) {
try (SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(conn)) {
bulkCopy.setDestinationTableName(desTable);
bulkCopy.setDestinationTableName(AbstractSQLGenerator.escapeIdentifier(desTable));
bulkCopy.writeToServer(rs);
verifyDestinationTableData(expectedBigDecimals.length);
}

rs.beforeFirst();
try (SQLServerBulkCopy bulkCopy1 = new SQLServerBulkCopy(conn)) {
bulkCopy1.setDestinationTableName(desTable);
bulkCopy1.setDestinationTableName(AbstractSQLGenerator.escapeIdentifier(desTable));
bulkCopy1.writeToServer(rs);
verifyDestinationTableData(expectedBigDecimals.length * 2);
}

rs.beforeFirst();
try (SQLServerBulkCopy bulkCopy2 = new SQLServerBulkCopy(conn)) {
bulkCopy2.setDestinationTableName(desTable);
bulkCopy2.setDestinationTableName(AbstractSQLGenerator.escapeIdentifier(desTable));
bulkCopy2.writeToServer(rs);
verifyDestinationTableData(expectedBigDecimals.length * 3);
}

String sql = "insert into " + desTable + " values (?,?,?,?)";
String sql = "insert into " + AbstractSQLGenerator.escapeIdentifier(desTable) + " values (?,?,?,?)";
Calendar calGMT = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
try (SQLServerPreparedStatement pstmt1 = (SQLServerPreparedStatement) conn.prepareStatement(sql)) {
for (int i = 0; i < expectedBigDecimals.length; i++) {
Expand All @@ -153,9 +156,11 @@ public void testMultiplePreparedStatementAndResultSet() throws SQLException {
verifyDestinationTableData(expectedBigDecimals.length * 4);
}
try (Statement stmt2 = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE); ResultSet rs2 = stmt2.executeQuery("select * from " + srcTable);
ResultSet.CONCUR_UPDATABLE);
ResultSet rs2 = stmt2
.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(srcTable));
SQLServerBulkCopy bulkCopy3 = new SQLServerBulkCopy(conn)) {
bulkCopy3.setDestinationTableName(desTable);
bulkCopy3.setDestinationTableName(AbstractSQLGenerator.escapeIdentifier(desTable));
bulkCopy3.writeToServer(rs2);
verifyDestinationTableData(expectedBigDecimals.length * 5);
}
Expand All @@ -164,8 +169,8 @@ public void testMultiplePreparedStatementAndResultSet() throws SQLException {
}

private static void verifyDestinationTableData(int expectedNumberOfRows) throws SQLException {
try (Connection conn = DriverManager.getConnection(connectionString);
ResultSet rs = conn.createStatement().executeQuery("select * from " + desTable)) {
try (Connection conn = DriverManager.getConnection(connectionString); ResultSet rs = conn.createStatement()
.executeQuery("select * from " + AbstractSQLGenerator.escapeIdentifier(desTable))) {

int expectedArrayLength = expectedBigDecimals.length;

Expand All @@ -187,7 +192,7 @@ private static void verifyDestinationTableData(int expectedNumberOfRows) throws
}

private static void populateSourceTable() throws SQLException {
String sql = "insert into " + srcTable + " values (?,?,?,?)";
String sql = "insert into " + AbstractSQLGenerator.escapeIdentifier(srcTable) + " values (?,?,?,?)";
Calendar calGMT = Calendar.getInstance(TimeZone.getTimeZone("GMT"));

try (Connection conn = DriverManager.getConnection(connectionString);
Expand All @@ -205,26 +210,18 @@ private static void populateSourceTable() throws SQLException {

private static void dropTables(Statement stmt) throws SQLException {
if (null != srcTable) {
TestUtils.dropTableIfExists(srcTable, stmt);
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(srcTable), stmt);
}
if (null != desTable) {
TestUtils.dropTableIfExists(desTable, stmt);
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(desTable), stmt);
}
}

private static void createTables(Statement stmt) throws SQLException {

if (null == srcTable) {
srcTable = "[BulkCopyResultSetCursorTest_Source_" + RandomUtil.getIdentifier("table") + "]";
}
if (null == desTable) {
desTable = "[BulkCopyResultSetCursorTest_Destination_" + RandomUtil.getIdentifier("table") + "]";
}

String sql = "create table " + srcTable
String sql = "create table " + AbstractSQLGenerator.escapeIdentifier(srcTable)
+ " (c1 decimal(10,5) null, c2 nchar(50) null, c3 datetime2(7) null, c4 char(7000));";
stmt.execute(sql);
sql = "create table " + desTable
sql = "create table " + AbstractSQLGenerator.escapeIdentifier(desTable)
+ " (c1 decimal(10,5) null, c2 nchar(50) null, c3 datetime2(7) null, c4 char(7000));";
stmt.execute(sql);
}
Expand Down
Loading

0 comments on commit c50d817

Please sign in to comment.