Skip to content

Commit 4cb784a

Browse files
Fix using the shared/global connection in IntegrationTestUtil (#494)
All the integration test use the static `Connection` object in `IntegrationTestUtil` to execute query. There are 2 issues - TestUtil should not have any static mutable member - The connection member will be overwritten by lots of the test and will result corrupted state We should pass the connection object as the argument of the execute API in the util. Fix stub mappings --------- Co-authored-by: Jayant Singh <[email protected]>
1 parent 336e2a1 commit 4cb784a

File tree

617 files changed

+7515
-8733
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

617 files changed

+7515
-8733
lines changed

src/test/java/com/databricks/jdbc/integration/IntegrationTestUtil.java

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ public class IntegrationTestUtil {
2424
private static final boolean isFakeServiceTest =
2525
Boolean.parseBoolean(System.getProperty(IS_FAKE_SERVICE_TEST_PROP));
2626

27-
private static Connection JDBCConnection;
28-
2927
/** Get the host of the embedded web server of fake service to be used in the tests. */
3028
public static String getFakeServiceHost() {
3129
// Target base URL of the fake service type
@@ -166,10 +164,6 @@ public static Connection getBenchfoodJDBCConnection() throws SQLException {
166164
getBenchfoodJDBCUrl(), getDatabricksUser(), getDatabricksBenchfoodToken());
167165
}
168166

169-
public static void resetJDBCConnection() {
170-
JDBCConnection = null;
171-
}
172-
173167
public static String getJDBCUrl() {
174168
String template = "jdbc:databricks://%s/default;ssl=1;AuthMech=3;httpPath=%s";
175169
String host = getDatabricksHost();
@@ -213,89 +207,88 @@ public static String getDogfoodJDBCUrl() {
213207
return String.format(template, host, httpPath);
214208
}
215209

216-
public static boolean executeSQL(String sql) {
210+
public static boolean executeSQL(Connection conn, String sql) {
217211
try {
218-
if (JDBCConnection == null) JDBCConnection = getValidJDBCConnection();
219-
JDBCConnection.createStatement().execute(sql);
212+
conn.createStatement().execute(sql);
220213
return true;
221214
} catch (SQLException e) {
222215
System.out.println("Error executing SQL: " + e.getMessage());
223216
return false;
224217
}
225218
}
226219

227-
public static ResultSet executeQuery(String sql) {
220+
public static ResultSet executeQuery(Connection conn, String sql) {
228221
try {
229-
if (JDBCConnection == null) JDBCConnection = getValidJDBCConnection();
230-
ResultSet rs = JDBCConnection.createStatement().executeQuery(sql);
222+
ResultSet rs = conn.createStatement().executeQuery(sql);
231223
return rs;
232224
} catch (SQLException e) {
233225
System.out.println("Error executing SQL: " + e.getMessage());
234226
return null;
235227
}
236228
}
237229

238-
public static void setupDatabaseTable(String tableName) {
230+
public static void setupDatabaseTable(Connection conn, String tableName) {
239231
String tableDeletionSQL = "DROP TABLE IF EXISTS " + getFullyQualifiedTableName(tableName);
240-
executeSQL(tableDeletionSQL);
232+
executeSQL(conn, tableDeletionSQL);
241233

242234
String tableCreationSQL =
243235
"CREATE TABLE IF NOT EXISTS "
244236
+ getFullyQualifiedTableName(tableName)
245237
+ " (id INT PRIMARY KEY, col1 VARCHAR(255), col2 VARCHAR(255))";
246238

247-
executeSQL(tableCreationSQL);
239+
executeSQL(conn, tableCreationSQL);
248240
}
249241

250-
public static void setupDatabaseTable(String tableName, String tableCreationSQL) {
242+
public static void setupDatabaseTable(
243+
Connection conn, String tableName, String tableCreationSQL) {
251244
String tableDeletionSQL = "DROP TABLE IF EXISTS " + getFullyQualifiedTableName(tableName);
252245

253-
executeSQL(tableDeletionSQL);
254-
executeSQL(tableCreationSQL);
246+
executeSQL(conn, tableDeletionSQL);
247+
executeSQL(conn, tableCreationSQL);
255248
}
256249

257-
public static void deleteTable(String tableName) {
250+
public static void deleteTable(Connection conn, String tableName) {
258251
String SQL = "DROP TABLE IF EXISTS " + getFullyQualifiedTableName(tableName);
259-
executeSQL(SQL);
252+
executeSQL(conn, SQL);
260253
}
261254

262255
public static String getFullyQualifiedTableName(String tableName) {
263256
return getDatabricksCatalog() + "." + getDatabricksSchema() + "." + tableName;
264257
}
265258

266-
public static void insertTestDataForJoins(String table1Name, String table2Name) {
259+
public static void insertTestDataForJoins(Connection conn, String table1Name, String table2Name) {
267260
// Insert data into the first table
268261
String insertTable1SQL1 =
269262
"INSERT INTO "
270263
+ getFullyQualifiedTableName(table1Name)
271264
+ " (id, col1, col2) VALUES (1, 'value1_table1', 'value2_table1')";
272-
executeSQL(insertTable1SQL1);
265+
executeSQL(conn, insertTable1SQL1);
273266

274267
String insertTable1SQL2 =
275268
"INSERT INTO "
276269
+ getFullyQualifiedTableName(table1Name)
277270
+ " (id, col1, col2) VALUES (2, 'value3_table1', 'value4_table1')";
278-
executeSQL(insertTable1SQL2);
271+
executeSQL(conn, insertTable1SQL2);
279272

280273
// Insert related data into the second table
281274
String insertTable2SQL1 =
282275
"INSERT INTO "
283276
+ getFullyQualifiedTableName(table2Name)
284277
+ " (id, col1, col2) VALUES (1, 'related_value1_table2', 'related_value2_table2')";
285-
executeSQL(insertTable2SQL1);
278+
executeSQL(conn, insertTable2SQL1);
286279

287280
String insertTable2SQL2 =
288281
"INSERT INTO "
289282
+ getFullyQualifiedTableName(table2Name)
290283
+ " (id, col1, col2) VALUES (2, 'related_value3_table2', 'related_value4_table2')";
291-
executeSQL(insertTable2SQL2);
284+
executeSQL(conn, insertTable2SQL2);
292285
}
293286

294-
public static void insertTestData(String tableName) {
287+
public static void insertTestData(Connection conn, String tableName) {
295288
String insertSQL =
296289
"INSERT INTO "
297290
+ getFullyQualifiedTableName(tableName)
298291
+ " (id, col1, col2) VALUES (1, 'value1', 'value2')";
299-
executeSQL(insertSQL);
292+
executeSQL(conn, insertSQL);
300293
}
301294
}

src/test/java/com/databricks/jdbc/integration/e2e/LargeQueryExecutionTests.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,34 @@
33
import static com.databricks.jdbc.integration.IntegrationTestUtil.*;
44
import static org.junit.jupiter.api.Assertions.*;
55

6+
import java.sql.Connection;
67
import java.sql.ResultSet;
78
import java.sql.SQLException;
89
import java.util.stream.Collectors;
910
import java.util.stream.IntStream;
11+
import org.junit.jupiter.api.AfterEach;
12+
import org.junit.jupiter.api.BeforeEach;
1013
import org.junit.jupiter.api.Test;
1114

1215
public class LargeQueryExecutionTests {
16+
private Connection connection;
17+
18+
@BeforeEach
19+
void setUp() throws SQLException {
20+
connection = getValidJDBCConnection();
21+
}
22+
23+
@AfterEach
24+
void cleanUp() throws SQLException {
25+
if (connection != null) {
26+
connection.close();
27+
}
28+
}
29+
1330
@Test
1431
void testQueryYieldingLargeNarrowResultSet() throws SQLException {
1532
String largeQuerySQL = "SELECT * FROM range(37500000)"; // ~300MB of data
16-
ResultSet rs = executeQuery(largeQuerySQL);
33+
ResultSet rs = executeQuery(connection, largeQuerySQL);
1734
int rows = 0;
1835
while (rs != null && rs.next()) {
1936
rows++;
@@ -36,7 +53,7 @@ void testQueryYieldingLargeWideResultSet() throws SQLException {
3653

3754
// Create the SQL query
3855
String query = String.format("SELECT id, %s FROM RANGE(%d)", uuids, rows);
39-
ResultSet rs = executeQuery(query);
56+
ResultSet rs = executeQuery(connection, query);
4057
int rowCount = 0;
4158
while (rs != null && rs.next()) {
4259
assertEquals(rs.getInt("id"), rowCount, "Expected id to be equal to row number");

src/test/java/com/databricks/jdbc/integration/e2e/MetadataTests.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,17 +61,17 @@ void testResultSetMetadataRetrieval() throws SQLException {
6161
+ "name VARCHAR(255), "
6262
+ "age INT"
6363
+ ");";
64-
setupDatabaseTable(tableName, createTableSQL);
64+
setupDatabaseTable(connection, tableName, createTableSQL);
6565

6666
String insertSQL =
6767
"INSERT INTO "
6868
+ getFullyQualifiedTableName(tableName)
6969
+ " (id, name, age) VALUES (1, 'Madhav', 24)";
70-
executeSQL(insertSQL);
70+
executeSQL(connection, insertSQL);
7171

7272
String query = "SELECT id, name, age FROM " + getFullyQualifiedTableName(tableName);
7373

74-
ResultSet resultSet = executeQuery(query);
74+
ResultSet resultSet = executeQuery(connection, query);
7575
ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
7676

7777
// Check the number of columns
@@ -106,7 +106,7 @@ void testResultSetMetadataRetrieval() throws SQLException {
106106
"Column " + i + " should be nullable");
107107
}
108108
String SQL = "DROP TABLE IF EXISTS " + getFullyQualifiedTableName(tableName);
109-
executeSQL(SQL);
109+
executeSQL(connection, SQL);
110110
}
111111

112112
@Test
@@ -141,7 +141,7 @@ void testTableInformation() throws SQLException {
141141
String catalog = "main";
142142
String schemaPattern = "jdbc_test_schema";
143143
String tableName = "catalog_and_schema_test_table";
144-
setupDatabaseTable(tableName);
144+
setupDatabaseTable(connection, tableName);
145145
try (ResultSet tables = metaData.getTables(catalog, schemaPattern, "%", null)) {
146146
assertTrue(
147147
tables.next(), "There should be at least one table in the specified catalog and schema");
@@ -150,7 +150,7 @@ void testTableInformation() throws SQLException {
150150
assertNotNull(fetchedTableName, "Table name should not be null");
151151
} while (tables.next());
152152
}
153-
deleteTable(tableName);
153+
deleteTable(connection, tableName);
154154
}
155155

156156
@Test
@@ -159,7 +159,7 @@ void testTableInformationExactMatch() throws SQLException {
159159
String catalog = "main";
160160
String schemaPattern = "jdbc_test_schema";
161161
String tableName = "catalog_and_schema_test_table";
162-
setupDatabaseTable(tableName);
162+
setupDatabaseTable(connection, tableName);
163163
try (ResultSet tables = metaData.getTables(catalog, schemaPattern, tableName, null)) {
164164
assertTrue(
165165
tables.next(), "There should be at least one table in the specified catalog and schema");
@@ -169,6 +169,6 @@ void testTableInformationExactMatch() throws SQLException {
169169
tableName, fetchedTableName, "Table name should match the specified table name");
170170
} while (tables.next());
171171
}
172-
deleteTable(tableName);
172+
deleteTable(connection, tableName);
173173
}
174174
}

src/test/java/com/databricks/jdbc/integration/e2e/UCMetadataTests.java

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,43 @@ static void setUp() throws SQLException {
3030
// Change connection to actual test warehouse once it supports latest runtime
3131
connection = getDogfoodJDBCConnection(List.of(List.of("useLegacyMetadata", "0")));
3232

33-
executeSQL("CREATE CATALOG IF NOT EXISTS " + catA);
34-
executeSQL("USE CATALOG " + catA);
35-
executeSQL("CREATE DATABASE IF NOT EXISTS " + db1);
36-
executeSQL("CREATE DATABASE IF NOT EXISTS " + db2);
33+
executeSQL(connection, "CREATE CATALOG IF NOT EXISTS " + catA);
34+
executeSQL(connection, "USE CATALOG " + catA);
35+
executeSQL(connection, "CREATE DATABASE IF NOT EXISTS " + db1);
36+
executeSQL(connection, "CREATE DATABASE IF NOT EXISTS " + db2);
3737

38-
executeSQL("USE " + db1);
38+
executeSQL(connection, "USE " + db1);
3939
executeSQL(
40+
connection,
4041
"CREATE TABLE IF NOT EXISTS a_1 AS (SELECT 1 AS col_1, '"
4142
+ catA
4243
+ "."
4344
+ db1
4445
+ ".a_1' AS col_2)");
4546
executeSQL(
47+
connection,
4648
"CREATE TABLE IF NOT EXISTS a_2 AS (SELECT 1 AS col_1, '"
4749
+ catA
4850
+ "."
4951
+ db1
5052
+ ".a_2' AS col_2)");
5153

52-
executeSQL("USE " + db2);
54+
executeSQL(connection, "USE " + db2);
5355
executeSQL(
56+
connection,
5457
"CREATE TABLE IF NOT EXISTS a_1 AS (SELECT 1 AS col_1, '"
5558
+ catA
5659
+ "."
5760
+ db2
5861
+ ".a_1' AS col_2)");
5962

60-
executeSQL("USE CATALOG " + mainCatalog);
61-
executeSQL("CREATE DATABASE IF NOT EXISTS " + db1);
62-
executeSQL("CREATE DATABASE IF NOT EXISTS " + db2);
63+
executeSQL(connection, "USE CATALOG " + mainCatalog);
64+
executeSQL(connection, "CREATE DATABASE IF NOT EXISTS " + db1);
65+
executeSQL(connection, "CREATE DATABASE IF NOT EXISTS " + db2);
6366

64-
executeSQL("USE " + db1);
67+
executeSQL(connection, "USE " + db1);
6568
executeSQL(
69+
connection,
6670
"CREATE TABLE IF NOT EXISTS "
6771
+ mainDb1Table1
6872
+ " AS (SELECT 1 AS col_1, 'main."
@@ -71,10 +75,11 @@ static void setUp() throws SQLException {
7175
+ mainDb1Table1
7276
+ "' AS col_2)");
7377

74-
executeSQL("USE CATALOG " + hiveCatalog);
75-
executeSQL("CREATE DATABASE IF NOT EXISTS " + db2);
76-
executeSQL("USE " + db2);
78+
executeSQL(connection, "USE CATALOG " + hiveCatalog);
79+
executeSQL(connection, "CREATE DATABASE IF NOT EXISTS " + db2);
80+
executeSQL(connection, "USE " + db2);
7781
executeSQL(
82+
connection,
7883
"CREATE TABLE IF NOT EXISTS "
7984
+ table1
8085
+ " AS (SELECT 1 AS col_1, '"
@@ -85,6 +90,7 @@ static void setUp() throws SQLException {
8590
+ table1
8691
+ "' AS col_2)");
8792
executeSQL(
93+
connection,
8894
"CREATE TABLE IF NOT EXISTS "
8995
+ table2
9096
+ " AS (SELECT 1 AS col_1, '"
@@ -110,20 +116,20 @@ static void setUp() throws SQLException {
110116
@AfterAll
111117
static void cleanUp() throws SQLException {
112118
// Cleanup
113-
executeSQL("USE CATALOG " + catA);
114-
executeSQL("DROP DATABASE " + db1 + " CASCADE");
115-
executeSQL("DROP DATABASE " + db2 + " CASCADE");
116-
executeSQL("DROP DATABASE IF EXISTS default CASCADE");
117-
executeSQL("DROP CATALOG " + catA);
119+
executeSQL(connection, "USE CATALOG " + catA);
120+
executeSQL(connection, "DROP DATABASE " + db1 + " CASCADE");
121+
executeSQL(connection, "DROP DATABASE " + db2 + " CASCADE");
122+
executeSQL(connection, "DROP DATABASE IF EXISTS default CASCADE");
123+
executeSQL(connection, "DROP CATALOG " + catA);
118124

119125
// Cleanup main catalog
120-
executeSQL("USE CATALOG " + mainCatalog);
121-
executeSQL("DROP DATABASE " + db1 + " CASCADE");
122-
executeSQL("DROP DATABASE " + db2 + " CASCADE");
126+
executeSQL(connection, "USE CATALOG " + mainCatalog);
127+
executeSQL(connection, "DROP DATABASE " + db1 + " CASCADE");
128+
executeSQL(connection, "DROP DATABASE " + db2 + " CASCADE");
123129

124130
// Cleanup legacy catalog
125-
executeSQL("USE CATALOG " + hiveCatalog);
126-
executeSQL("DROP DATABASE " + db2 + " CASCADE");
131+
executeSQL(connection, "USE CATALOG " + hiveCatalog);
132+
executeSQL(connection, "DROP DATABASE " + db2 + " CASCADE");
127133

128134
if (connection != null) {
129135
connection.close();
@@ -138,7 +144,7 @@ void testGetCatalogs() throws SQLException {
138144

139145
@Test
140146
void testGetSchemas() throws SQLException {
141-
executeSQL("USE CATALOG hive_metastore");
147+
executeSQL(connection, "USE CATALOG hive_metastore");
142148
ResultSet r = connection.getMetaData().getSchemas("hive_metastore", "%");
143149
verifyContainsSchemas(
144150
r,

src/test/java/com/databricks/jdbc/integration/fakeservice/FakeServiceExtension.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import static com.github.tomakehurst.wiremock.common.AbstractFileSource.byFileExtension;
66

77
import com.databricks.jdbc.common.DatabricksJdbcConstants.FakeServiceType;
8-
import com.databricks.jdbc.integration.IntegrationTestUtil;
98
import com.github.tomakehurst.wiremock.client.ResponseDefinitionBuilder;
109
import com.github.tomakehurst.wiremock.common.BinaryFile;
1110
import com.github.tomakehurst.wiremock.common.ContentTypes;
@@ -162,7 +161,6 @@ protected void onBeforeAll(WireMockRuntimeInfo wireMockRuntimeInfo, ExtensionCon
162161
wireMockServerHttpPort = wireMockRuntimeInfo.getHttpPort();
163162

164163
setFakeServiceProperties(wireMockServerHttpPort);
165-
IntegrationTestUtil.resetJDBCConnection();
166164
}
167165

168166
/** {@inheritDoc} */
@@ -182,7 +180,6 @@ protected void onBeforeEach(WireMockRuntimeInfo wireMockRuntimeInfo, ExtensionCo
182180
@Override
183181
protected void onAfterEach(WireMockRuntimeInfo wireMockRuntimeInfo, ExtensionContext context)
184182
throws Exception {
185-
IntegrationTestUtil.resetJDBCConnection();
186183

187184
if (fakeServiceMode == FakeServiceMode.RECORD) {
188185
saveStubMappings(wireMockRuntimeInfo, context);

0 commit comments

Comments
 (0)