Skip to content

Commit

Permalink
Clean up tables in tests (#2059)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilgreenbird authored Jan 30, 2023
1 parent d842883 commit 2556f3b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,26 @@
@RunWith(JUnitPlatform.class)
@Tag(Constants.xAzureSQLDW)
public class CallableStatementTest extends AbstractTest {
private static String tableNameGUID = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("uniqueidentifier_Table"));
private static String outputProcedureNameGUID = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("uniqueidentifier_SP"));
private static String setNullProcedureName = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("CallableStatementTest_setNull_SP"));
private static String inputParamsProcedureName = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("CallableStatementTest_inputParams_SP"));
private static String getObjectLocalDateTimeProcedureName = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("CallableStatementTest_getObjectLocalDateTime_SP"));
private static String getObjectOffsetDateTimeProcedureName = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("CallableStatementTest_getObjectOffsetDateTime_SP"));
private static String procName = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("procedureTestCallableStatementSpPrepare"));
private static String manyParamsTable = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("manyParam_Table"));
private static String manyParamProc = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("manyParam_Procedure"));
private static String manyParamUserDefinedType = AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("manyParam_definedType"));
private static String tableNameGUID = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("uniqueidentifier_Table"));
private static String outputProcedureNameGUID = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("uniqueidentifier_SP"));
private static String setNullProcedureName = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("CallableStatementTest_setNull_SP"));
private static String inputParamsProcedureName = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("CallableStatementTest_inputParams_SP"));
private static String getObjectLocalDateTimeProcedureName = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("CallableStatementTest_getObjectLocalDateTime_SP"));
private static String getObjectOffsetDateTimeProcedureName = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("CallableStatementTest_getObjectOffsetDateTime_SP"));
private static String procName = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("procedureTestCallableStatementSpPrepare"));
private static String manyParamsTable = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("manyParam_Table"));
private static String manyParamProc = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("manyParam_Procedure"));
private static String manyParamUserDefinedType = AbstractSQLGenerator
.escapeIdentifier(RandomUtil.getIdentifier("manyParam_definedType"));

/**
* Setup before test
Expand Down Expand Up @@ -96,8 +106,8 @@ public void testCallableStatementManyParameters() throws SQLException {
String tempPass = UUID.randomUUID().toString();
String dropLogin = "IF EXISTS (select * from sys.sql_logins where name = 'NewLogin') DROP LOGIN NewLogin";
String dropUser = "IF EXISTS (select * from sys.sysusers where name = 'NewUser') DROP USER NewUser";
String createLogin = "USE MASTER;CREATE LOGIN NewLogin WITH PASSWORD=N'" + tempPass + "', " +
"DEFAULT_DATABASE = MASTER, DEFAULT_LANGUAGE = US_ENGLISH;ALTER LOGIN NewLogin ENABLE;";
String createLogin = "USE MASTER;CREATE LOGIN NewLogin WITH PASSWORD=N'" + tempPass + "', "
+ "DEFAULT_DATABASE = MASTER, DEFAULT_LANGUAGE = US_ENGLISH;ALTER LOGIN NewLogin ENABLE;";
String createUser = "USE MASTER;CREATE USER NewUser FOR LOGIN NewLogin WITH DEFAULT_SCHEMA = [DBO];";
String grantExecute = "GRANT EXECUTE ON " + manyParamProc + " TO NewUser;";

Expand All @@ -119,8 +129,8 @@ public void testCallableStatementManyParameters() throws SQLException {

// Should not throw an "Index is out of range error"
// Should not throw R_parameterNotDefinedForProcedure
try (CallableStatement callableStatement = conn.prepareCall(
"{call " + manyParamProc + "(?,?,?,?,?,?,?,?,?,?)}")) {
try (CallableStatement callableStatement = conn
.prepareCall("{call " + manyParamProc + "(?,?,?,?,?,?,?,?,?,?)}")) {
callableStatement.setObject("@p1", money, microsoft.sql.Types.MONEY);
callableStatement.setObject("@p2", money, microsoft.sql.Types.MONEY);
callableStatement.setObject("@p3", money, microsoft.sql.Types.MONEY);
Expand All @@ -143,8 +153,7 @@ public void testCallableStatementSpPrepare() throws SQLException {
try (Statement statement = connection.createStatement();) {
statement.executeUpdate("create procedure " + procName + " as select 1 --");

try (CallableStatement callableStatement = connection.prepareCall(
"{call " + procName + "}")) {
try (CallableStatement callableStatement = connection.prepareCall("{call " + procName + "}")) {
try (ResultSet rs = callableStatement.executeQuery()) { // Takes sp_executesql path
rs.next();
assertEquals(1, rs.getInt(1), TestResource.getResource("R_setDataNotEqual"));
Expand Down Expand Up @@ -220,7 +229,6 @@ public void getSetNullWithTypeVarchar() throws SQLException {
}
}


/**
* Tests getObject(n, java.time.LocalDateTime.class).
*
Expand All @@ -229,7 +237,8 @@ public void getSetNullWithTypeVarchar() throws SQLException {
@Test
public void testGetObjectAsLocalDateTime() throws SQLException {
String sql = "{CALL " + getObjectLocalDateTimeProcedureName + " (?)}";
try (Connection con = DriverManager.getConnection(connectionString); CallableStatement cs = con.prepareCall(sql)) {
try (Connection con = DriverManager.getConnection(connectionString);
CallableStatement cs = con.prepareCall(sql)) {
cs.registerOutParameter(1, Types.TIMESTAMP);
TimeZone prevTimeZone = TimeZone.getDefault();
TimeZone.setDefault(TimeZone.getTimeZone("America/Edmonton"));
Expand Down Expand Up @@ -268,7 +277,8 @@ public void testGetObjectAsLocalDateTime() throws SQLException {
@Tag(Constants.xAzureSQLDW)
public void testGetObjectAsOffsetDateTime() throws SQLException {
String sql = "{CALL " + getObjectOffsetDateTimeProcedureName + " (?, ?)}";
try (Connection con = DriverManager.getConnection(connectionString); CallableStatement cs = con.prepareCall(sql)) {
try (Connection con = DriverManager.getConnection(connectionString);
CallableStatement cs = con.prepareCall(sql)) {
cs.registerOutParameter(1, Types.TIMESTAMP_WITH_TIMEZONE);
cs.registerOutParameter(2, Types.TIMESTAMP_WITH_TIMEZONE);

Expand Down Expand Up @@ -341,6 +351,7 @@ public void inputParamsTest() throws SQLException {
public static void cleanup() throws SQLException {
try (Statement stmt = connection.createStatement()) {
TestUtils.dropTableIfExists(tableNameGUID, stmt);
TestUtils.dropTableIfExists(manyParamsTable, stmt);
TestUtils.dropProcedureIfExists(outputProcedureNameGUID, stmt);
TestUtils.dropProcedureIfExists(setNullProcedureName, stmt);
TestUtils.dropProcedureIfExists(inputParamsProcedureName, stmt);
Expand All @@ -351,14 +362,12 @@ public static void cleanup() throws SQLException {

private static void createGUIDStoredProcedure(Statement stmt) throws SQLException {
String sql = "CREATE PROCEDURE " + outputProcedureNameGUID
+ "(@p1 uniqueidentifier OUTPUT) AS SELECT @p1 = c1 FROM "
+ tableNameGUID + Constants.SEMI_COLON;
+ "(@p1 uniqueidentifier OUTPUT) AS SELECT @p1 = c1 FROM " + tableNameGUID + Constants.SEMI_COLON;
stmt.execute(sql);
}

private static void createGUIDTable(Statement stmt) throws SQLException {
String sql = "CREATE TABLE " + tableNameGUID
+ " (c1 uniqueidentifier null)";
String sql = "CREATE TABLE " + tableNameGUID + " (c1 uniqueidentifier null)";
stmt.execute(sql);
}

Expand All @@ -368,16 +377,15 @@ private static void createSetNullProcedure(Statement stmt) throws SQLException {
}

private static void createInputParamsProcedure(Statement stmt) throws SQLException {
String sql = "CREATE PROCEDURE " + inputParamsProcedureName
+ " @p1 nvarchar(max) = N'parameter1', " + " @p2 nvarchar(max) = N'parameter2' " + "AS "
+ "BEGIN " + " SET NOCOUNT ON; " + " SELECT @p1 + @p2 AS result; " + "END ";
String sql = "CREATE PROCEDURE " + inputParamsProcedureName + " @p1 nvarchar(max) = N'parameter1', "
+ " @p2 nvarchar(max) = N'parameter2' " + "AS " + "BEGIN " + " SET NOCOUNT ON; "
+ " SELECT @p1 + @p2 AS result; " + "END ";

stmt.execute(sql);
}

private static void createGetObjectLocalDateTimeProcedure(Statement stmt) throws SQLException {
String sql = "CREATE PROCEDURE " + getObjectLocalDateTimeProcedureName
+ "(@p1 datetime2(7) OUTPUT) AS "
String sql = "CREATE PROCEDURE " + getObjectLocalDateTimeProcedureName + "(@p1 datetime2(7) OUTPUT) AS "
+ "SELECT @p1 = '2018-03-11T02:00:00.1234567'";
stmt.execute(sql);
}
Expand All @@ -391,37 +399,21 @@ private static void createGetObjectOffsetDateTimeProcedure(Statement stmt) throw

private static void createProcedureManyParams() throws SQLException {
String type = manyParamUserDefinedType;
String sql = "CREATE PROCEDURE " + manyParamProc
+ " @p1 " + type
+ ", @p2 " + type
+ ", @p3 " + type
+ ", @p4 " + type
+ ", @p5 " + type
+ ", @p6 " + type
+ ", @p7 " + type
+ ", @p8 " + type
+ ", @p9 " + type
+ ", @p10 " + type
+ " AS INSERT INTO "
+ manyParamsTable + " VALUES(@p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10)";
String sql = "CREATE PROCEDURE " + manyParamProc + " @p1 " + type + ", @p2 " + type + ", @p3 " + type + ", @p4 "
+ type + ", @p5 " + type + ", @p6 " + type + ", @p7 " + type + ", @p8 " + type + ", @p9 " + type
+ ", @p10 " + type + " AS INSERT INTO " + manyParamsTable
+ " VALUES(@p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10)";
try (Statement stmt = connection.createStatement()) {
stmt.execute(sql);
}
}

private static void createTableManyParams() throws SQLException {
String type = manyParamUserDefinedType;
String sql = "CREATE TABLE" + manyParamsTable +
" (c1 " + type + " null, " +
"c2 " + type + " null, " +
"c3 " + type + " null, " +
"c4 " + type + " null, " +
"c5 " + type + " null, " +
"c6 " + type + " null, " +
"c7 " + type + " null, " +
"c8 " + type + " null, " +
"c9 " + type + " null, " +
"c10 " + type + " null);";
String sql = "CREATE TABLE" + manyParamsTable + " (c1 " + type + " null, " + "c2 " + type + " null, " + "c3 "
+ type + " null, " + "c4 " + type + " null, " + "c5 " + type + " null, " + "c6 " + type + " null, "
+ "c7 " + type + " null, " + "c8 " + type + " null, " + "c9 " + type + " null, " + "c10 " + type
+ " null);";
try (Statement stmt = connection.createStatement()) {
stmt.execute(sql);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ public static void setupTests() throws Exception {
@Tag(Constants.xSQLv12)
@Tag(Constants.xSQLv14)
public void testInformationTypeSensitivityLabelIndexMax() throws Exception {
String createTable = "create table " + tableName2 + " (col1 varchar(200), col2 varchar(200), col3 varchar(200))";
String createTable = "create table " + tableName2
+ " (col1 varchar(200), col2 varchar(200), col3 varchar(200))";
String sensitivityClassification = "ADD SENSITIVITY CLASSIFICATION TO %s.%s WITH (LABEL='PII')";
String query = "select * from " + tableName2;

Expand Down Expand Up @@ -114,7 +115,7 @@ public void testDataClassificationSelectMethodCursor() throws Exception {
try (PreparedStatement pstmt = conn.prepareStatement(query)) {
pstmt.setString(1, "1231231230000");
ResultSet rs = pstmt.executeQuery();
if(rs.next()){
if (rs.next()) {
rs.getInt("summation");
} else {
fail("Expected to have a row returned from table " + tableName1);
Expand Down Expand Up @@ -286,6 +287,8 @@ private void verifyLabel(Label label) {
public static void dropTable() throws Exception {
try (Statement stmt = connection.createStatement();) {
TestUtils.dropTableIfExists(tableName, stmt);
TestUtils.dropTableIfExists(tableName1, stmt);
TestUtils.dropTableIfExists(tableName2, stmt);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@
import com.microsoft.sqlserver.jdbc.SQLServerDataSource;
import com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement;
import com.microsoft.sqlserver.jdbc.TestResource;
import com.microsoft.sqlserver.jdbc.TestUtils;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
Expand All @@ -43,15 +47,25 @@
@RunWith(JUnitPlatform.class)
public class PreparedStatementTest extends AbstractTest {

final String tableName = RandomUtil.getIdentifier("tableTestStatementPoolingInternal1");
final String tableName2 = RandomUtil.getIdentifier("tableTestStatementPoolingInternal2");
final String tableName3 = RandomUtil.getIdentifier("tableTestPreparedStatementWithSpPrepare");
final static String tableName = RandomUtil.getIdentifier("tableTestStatementPoolingInternal1");
final static String tableName2 = RandomUtil.getIdentifier("tableTestStatementPoolingInternal2");
final static String tableName3 = RandomUtil.getIdentifier("tableTestPreparedStatementWithSpPrepare");

@BeforeAll
public static void setupTests() throws Exception {
setConnection();
}

@BeforeEach
public void testInit() throws Exception {
dropTables();
}

@AfterEach
public void terminateVariation() throws Exception {
dropTables();
}

private void executeSQL(SQLServerConnection conn, String sql) throws SQLException {
try (Statement stmt = conn.createStatement()) {
stmt.execute(sql);
Expand Down Expand Up @@ -118,8 +132,7 @@ public void testPreparedStatementPoolEvictionWithSpPrepare() throws SQLException
String query = "select 1 --";

for (int i = 0; i < cacheSize; i++) {
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) con
.prepareStatement(query + i)) {
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) con.prepareStatement(query + i)) {
pstmt.execute(); // sp_executesql
pstmt.execute(); // sp_prepare and sp_execute, handle created and cached
}
Expand Down Expand Up @@ -768,4 +781,13 @@ private void modifyConnectionForBulkCopyAPI(SQLServerConnection con) throws Exce

con.setUseBulkCopyForBatchInsert(true);
}

private static void dropTables() throws Exception {
try (Statement stmt = connection.createStatement()) {
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt);
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName2), stmt);
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName3), stmt);
}
}

}

0 comments on commit 2556f3b

Please sign in to comment.