Skip to content

Commit

Permalink
Cleanup orphans from tests (#2408)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilgreenbird committed May 8, 2024
1 parent f452337 commit f312d1b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/test/java/com/microsoft/sqlserver/jdbc/TestResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,5 +218,6 @@ protected Object[][] getContents() {
"javax.security.auth.login.LoginException (No LoginModules configured for SQLJDBCDriver)"},
{"R_unexpectedThreadCount", "Thread count is higher than expected."},
{"R_expectedClassDoesNotMatchActualClass",
"Expected column class {0} does not match actual column class {1} for column {2}."}};
"Expected column class {0} does not match actual column class {1} for column {2}."},
{"R_loginFailedMSI", "Login failed for user '<token-identified principal>'"}};
}
26 changes: 25 additions & 1 deletion src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,31 @@ public static void dropDatabaseIfExists(String databaseName, String connectionSt
*/
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));
+ "') DROP SCHEMA " + AbstractSQLGenerator.escapeIdentifier(schemaName));
}

/**
* mimic "DROP USER..."
*
* @param userName
* @param stmt
* @throws SQLException
*/
public static void dropUserIfExists(String userName, Statement stmt) throws SQLException {
stmt.execute("IF EXISTS (SELECT * FROM sys.sysusers where name = '" + escapeSingleQuotes(userName)
+ "') DROP USER " + AbstractSQLGenerator.escapeIdentifier(userName));
}

/**
* mimic "DROP LOGIN..."
*
* @param userName
* @param stmt
* @throws SQLException
*/
public static void dropLoginIfExists(String userName, Statement stmt) throws SQLException {
stmt.execute("IF EXISTS (SELECT * FROM sys.sysusers where name = '" + escapeSingleQuotes(userName)
+ "') DROP LOGIN " + AbstractSQLGenerator.escapeIdentifier(userName));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class DatabaseMetaDataTest extends AbstractTest {
private static final String uuid = UUID.randomUUID().toString().replaceAll("-", "");
private static final String tableName = RandomUtil.getIdentifier("DBMetadataTable");
private static final String functionName = RandomUtil.getIdentifier("DBMetadataFunction");
private static final String newUserName = "newUser" + uuid;
private static final String schema = "schema_demo" + uuid;
private static final String escapedSchema = "schema\\_demo" + uuid;
private static final String tableNameWithSchema = schema + ".resource";
Expand Down Expand Up @@ -227,14 +228,13 @@ public void testDBUserLogin() throws SQLException {
@Test
@Tag(Constants.xAzureSQLDW)
public void testImpersonateGetUserName() throws SQLException {
String newUser = "newUser" + UUID.randomUUID();
String escapedNewUser = AbstractSQLGenerator.escapeIdentifier(newUserName);

try (Connection conn = getConnection(); Statement stmt = conn.createStatement()) {
String escapedNewUser = AbstractSQLGenerator.escapeIdentifier(newUser);
String password = "password" + UUID.randomUUID();

stmt.execute("IF EXISTS (select * from sys.sysusers where name = '" + escapedNewUser + "') DROP USER"
+ escapedNewUser);
TestUtils.dropUserIfExists(newUserName, stmt);
TestUtils.dropLoginIfExists(newUserName, stmt);

// create new user and login
try {
Expand All @@ -248,17 +248,17 @@ public void testImpersonateGetUserName() throws SQLException {
}

DatabaseMetaData databaseMetaData = conn.getMetaData();
try (CallableStatement asOtherUser = conn.prepareCall("EXECUTE AS USER = '" + newUser + "'")) {
try (CallableStatement asOtherUser = conn.prepareCall("EXECUTE AS USER = '" + newUserName + "'")) {
asOtherUser.execute();
assertTrue(newUser.equalsIgnoreCase(databaseMetaData.getUserName()),
assertTrue(newUserName.equalsIgnoreCase(databaseMetaData.getUserName()),
TestResource.getResource("R_userNameNotMatch"));
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
} finally {
stmt.execute("IF EXISTS (select * from sys.sysusers where name = '" + escapedNewUser + "') DROP USER"
+ escapedNewUser);
stmt.execute("IF EXISTS (select * from sys.sysusers where name = '" + escapedNewUser + "') DROP LOGIN"
+ escapedNewUser);
}
} finally {
try (Connection conn = getConnection(); Statement stmt = conn.createStatement()) {
TestUtils.dropUserIfExists(newUserName, stmt);
TestUtils.dropLoginIfExists(newUserName, stmt);
}
}
}
Expand Down Expand Up @@ -1049,6 +1049,8 @@ public static void terminate() throws SQLException {
TestUtils.dropTableWithSchemaIfExists(tableNameWithSchema, stmt);
TestUtils.dropProcedureWithSchemaIfExists(sprocWithSchema, stmt);
TestUtils.dropSchemaIfExists(schema, stmt);
TestUtils.dropUserIfExists(newUserName, stmt);
TestUtils.dropLoginIfExists(newUserName, stmt);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void setupTests() throws Exception {

@Test
@Tag(Constants.xAzureSQLDW)
public void testLoginFailedError() {
public void testLoginFailedError() {
SQLServerDataSource ds = new SQLServerDataSource();
ds.setURL(connectionString);
ds.setLoginTimeout(loginTimeOutInSeconds);
Expand Down

0 comments on commit f312d1b

Please sign in to comment.