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

Test | Fix DB Metadata tests + fix maven warnings + clean all objects #1060

Merged
merged 7 commits into from May 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1090,6 +1090,10 @@ void checkClosed() throws SQLServerException {

/**
* Returns if Federated Authentication is in use or is about to expire soon
*
* @return true/false
* @throws SQLServerException
* if an error occurs.
*/
protected boolean needsReconnect() throws SQLServerException {
return (null != fedAuthToken && Util.checkIfNeedNewAccessToken(this, fedAuthToken.expiresOn));
Expand Down
Expand Up @@ -46,6 +46,7 @@ protected Object[][] getContents() {
{"R_createDropViewFailed", "Create/drop view with preparedStatement failed!"},
{"R_createDropSchemaFailed", "Create/drop schema with preparedStatement failed!"},
{"R_createDropTableFailed", "Create/drop table failed!"},
{"R_tableNotFound", "Table {0} not found in database."},
{"R_createDropAlterTableFailed", "Create/drop/alter table with preparedStatement failed!"},
{"R_grantFailed", "grant table with preparedStatement failed!"},
{"R_connectionIsClosed", "The connection is closed."},
Expand Down
130 changes: 111 additions & 19 deletions src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java
Expand Up @@ -25,7 +25,7 @@
import java.util.ResourceBundle;

import com.microsoft.sqlserver.testframework.AbstractSQLGenerator;
import com.microsoft.sqlserver.testframework.Constants;
import com.microsoft.sqlserver.testframework.PrepUtil;
import com.microsoft.sqlserver.testframework.sqlType.SqlBigInt;
import com.microsoft.sqlserver.testframework.sqlType.SqlBinary;
import com.microsoft.sqlserver.testframework.sqlType.SqlBit;
Expand Down Expand Up @@ -61,14 +61,15 @@
* @since 6.1.2
*/
public class TestUtils {
// private static SqlType types = null;
private static ArrayList<SqlType> types = null;
private static final char[] HEXCHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
'F'};

final static int ENGINE_EDITION_FOR_SQL_AZURE = 5;
final static int ENGINE_EDITION_FOR_SQL_AZURE_DW = 6;
final static int ENGINE_EDITION_FOR_SQL_AZURE_MI = 8;

cheenamalhotra marked this conversation as resolved.
Show resolved Hide resolved
@SuppressWarnings("unused")
private static Boolean isAzure = null;
private static Boolean isAzureDW = null;
private static Boolean isAzureMI = null;
Expand Down Expand Up @@ -279,36 +280,121 @@ public static String getCurrentClassPath() {
}

/**
* mimic "DROP TABLE IF EXISTS ..." for older versions of SQL Server
* mimic "DROP TABLE ..."
*
* @param tableName
* @param stmt
* @throws SQLException
*/
public static void dropTableIfExists(String tableName, java.sql.Statement stmt) throws SQLException {
dropObjectIfExists(tableName, "IsTable", stmt);
dropObjectIfExists(tableName, "U", stmt);
}

/**
* mimic "DROP PROCEDURE IF EXISTS ..." for older versions of SQL Server
* mimic "DROP PROCEDURE ..."
*
* @param procName
* @param stmt
* @throws SQLException
*/
public static void dropProcedureIfExists(String procName, java.sql.Statement stmt) throws SQLException {
dropObjectIfExists(procName, "IsProcedure", stmt);
dropObjectIfExists(procName, "P", stmt);
}

public static void dropDatabaseIfExists(String databaseName, java.sql.Statement stmt) throws SQLException {
stmt.executeUpdate("USE MASTER; IF EXISTS(SELECT * from sys.databases WHERE name='"
+ escapeSingleQuotes(databaseName) + "') DROP DATABASE [" + databaseName + "]");
/**
* mimic "DROP FUNCTION ..."
*
* @param functionName
* @param stmt
* @throws SQLException
*/
public static void dropFunctionIfExists(String functionName, java.sql.Statement stmt) throws SQLException {
dropObjectIfExists(functionName, "FN", stmt);
}

/**
* mimic "DROP TRIGGER ..."
*
* @param triggerName
* @param stmt
* @throws SQLException
*/
public static void dropTriggerIfExists(String triggerName, java.sql.Statement stmt) throws SQLException {
stmt.execute("IF EXISTS (\r\n" + " SELECT *\r\n" + " FROM sys.objects\r\n"
+ " WHERE [type] = 'TR' AND [name] = '" + TestUtils.escapeSingleQuotes(triggerName) + "'\r\n"
+ " )\r\n" + " DROP TRIGGER " + AbstractSQLGenerator.escapeIdentifier(triggerName)
+ Constants.SEMI_COLON);
dropObjectIfExists(triggerName, "TR", stmt);
}

/**
* mimic "DROP TYPE ..."
*
* @param typeName
* @param stmt
* @throws SQLException
*/
public static void dropTypeIfExists(String typeName, java.sql.Statement stmt) throws SQLException {
dropObjectIfExists(typeName, "TT", stmt);
}

/**
* mimic "DROP DATABASE ..."
*
* @param databaseName
* @param connectionString
* @throws SQLException
*/
public static void dropDatabaseIfExists(String databaseName, String connectionString) throws SQLException {
try (Connection connection = PrepUtil.getConnection(connectionString + ";databaseName=master");
Statement stmt = connection.createStatement()) {
stmt.executeUpdate("IF EXISTS(SELECT * from sys.databases WHERE name='" + escapeSingleQuotes(databaseName)
+ "') DROP DATABASE [" + databaseName + "]");
}
}

/**
* mimic "DROP SCHEMA ..."
*
* @param schemaName
* @param stmt
* @throws SQLException
*/
public static void dropSchemaIfExists(String schemaName, Statement stmt) throws SQLException {
stmt.execute("if EXISTS (SELECT * FROM sys.schemas where name = '" + escapeSingleQuotes(schemaName)
+ "') drop schema " + AbstractSQLGenerator.escapeIdentifier(schemaName));
}

/**
* actually perform the "DROP TABLE / PROCEDURE"
* <pre>
* This method drops objects for below types:
*
* TT - TYPE_TABLE
* TR - TRIGGER
* FN - SQL_SCALAR_FUNCTION
* P -- SQL_STORED_PROCEDURE
* U -- USER_TABLE
*
* </pre>
*/
private static void dropObjectIfExists(String objectName, String objectProperty,
private static void dropObjectIfExists(String objectName, String objectType,
java.sql.Statement stmt) throws SQLException {
String typeName = "";
switch (objectType) {
case "TT":
typeName = "TYPE";
break;
case "TR":
typeName = "TRIGGER";
break;
case "FN":
typeName = "FUNCTION";
break;
case "P":
typeName = "PROCEDURE";
break;
case "U":
typeName = "TABLE";
default:
break;
}

StringBuilder sb = new StringBuilder();
if (!objectName.startsWith("[")) {
sb.append("[");
Expand All @@ -317,11 +403,17 @@ private static void dropObjectIfExists(String objectName, String objectProperty,
if (!objectName.endsWith("]")) {
sb.append("]");
}

String bracketedObjectName = sb.toString();
String sql = String.format("IF EXISTS " + "( " + "SELECT * from sys.objects "
+ "WHERE object_id = OBJECT_ID(N'%s') AND OBJECTPROPERTY(object_id, N'%s') = 1 " + ") " + "DROP %s %s ",
escapeSingleQuotes(bracketedObjectName), objectProperty,
"IsProcedure".equals(objectProperty) ? "PROCEDURE" : "TABLE", bracketedObjectName);
String whereClause = "";
if (objectType != "TT") {
whereClause = "WHERE object_id = OBJECT_ID(N'" + escapeSingleQuotes(bracketedObjectName) + "')";
} else {
whereClause = "WHERE name LIKE '%" + escapeSingleQuotes(objectName) + "%'";
}

String sql = "IF EXISTS ( SELECT * from sys.objects " + whereClause + " AND type='" + objectType + "') DROP "
+ typeName + " " + bracketedObjectName;
try {
stmt.executeUpdate(sql);
} catch (SQLException e) {
Expand Down
Expand Up @@ -74,7 +74,8 @@ public void datatypestest() throws Exception {
}
} finally {
try (Statement stmt = connection.createStatement()) {
TestUtils.dropTableIfExists(escapedTableName, stmt);
TestUtils.dropTableIfExists(tableName, stmt);
TestUtils.dropProcedureIfExists(procName, stmt);
}
}
}
Expand Down
Expand Up @@ -135,13 +135,9 @@ public void testModifiableConnectionProperties() throws SQLException {
holdability2, sendTimeAsDatetime2, statementPoolingCacheSize2, disableStatementPooling2,
serverPreparedStatementDiscardThreshold2, enablePrepareOnFirstPreparedStatementCall2, sCatalog2,
useBulkCopyForBatchInsert2);
// drop the database
con.setCatalog("master");
}
} finally {
try (Connection con = getConnection(); Statement stmt = con.createStatement()) {
TestUtils.dropDatabaseIfExists(sCatalog2, stmt);
}
TestUtils.dropDatabaseIfExists(sCatalog2, connectionString);
}
}

Expand Down