From 28e3a2e36dbcafdad955587e9d5a5706f1e0d2d8 Mon Sep 17 00:00:00 2001 From: Ananya Garg Date: Thu, 11 Sep 2025 18:40:45 +0530 Subject: [PATCH] Fix JSON function tests: resolve UDF schema qualification and Java 8 compatibility issues --- .../jdbc/datatypes/JSONFunctionTest.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/JSONFunctionTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/JSONFunctionTest.java index d3193c241..bf613d832 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/JSONFunctionTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/datatypes/JSONFunctionTest.java @@ -31,6 +31,7 @@ import java.sql.Statement; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; @@ -1378,13 +1379,12 @@ public void testJoinQueryWithJsonType() throws SQLException { public void testJsonInputOutputWithUdf() throws SQLException { String personsTable = TestUtils .escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("Persons"))); - String udfName = TestUtils - .escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("GetAgeFromJson"))); + String udfName = "dbo.GetAgeFromJson"; try (Connection conn = getConnection()) { try (Statement stmt = conn.createStatement()) { TestUtils.dropTableIfExists(personsTable, stmt); - TestUtils.dropFunctionIfExists(udfName, stmt); + TestUtils.dropFunctionWithSchemaIfExists(udfName, stmt); String createUdfSQL = "CREATE FUNCTION " + udfName + " (@json JSON) " + "RETURNS INT " + "AS BEGIN " + @@ -1439,7 +1439,7 @@ public void testJsonInputOutputWithUdf() throws SQLException { } finally { try (Connection conn = getConnection(); Statement stmt = conn.createStatement()) { - TestUtils.dropFunctionIfExists(udfName, stmt); + TestUtils.dropFunctionWithSchemaIfExists(udfName, stmt); TestUtils.dropTableIfExists(personsTable, stmt); } } @@ -1456,13 +1456,12 @@ public void testJsonInputOutputWithUdf() throws SQLException { public void testUdfReturningJson() throws SQLException { String personsTable = TestUtils .escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("Persons"))); - String udfName = TestUtils - .escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("GetPersonJson"))); + String udfName = "dbo.GetPersonJson"; try (Connection conn = getConnection()) { try (Statement stmt = conn.createStatement()) { TestUtils.dropTableIfExists(personsTable, stmt); - TestUtils.dropFunctionIfExists(udfName, stmt); + TestUtils.dropFunctionWithSchemaIfExists(udfName, stmt); String createUdfSQL = "CREATE FUNCTION " + udfName + " (@id INT, @name NVARCHAR(100)) " + "RETURNS JSON " + @@ -1501,7 +1500,7 @@ public void testUdfReturningJson() throws SQLException { } finally { try (Connection conn = getConnection(); Statement stmt = conn.createStatement()) { - TestUtils.dropFunctionIfExists(udfName, stmt); + TestUtils.dropFunctionWithSchemaIfExists(udfName, stmt); TestUtils.dropTableIfExists(personsTable, stmt); } } @@ -1512,6 +1511,7 @@ public void testUdfReturningJson() throws SQLException { * And verify there is no data loss. */ @Test + @Disabled("Disabled due to huge JSON data") public void testInsert1GBJson() throws SQLException, IOException { String dstTable = TestUtils .escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("dstTableJSON"))); @@ -1598,6 +1598,7 @@ private boolean filesAreEqual(Path path1, Path path2) throws IOException { * Note: This test took around 4 mins to run */ @Test + @Disabled("Disabled due to huge JSON data") public void testInsertHugeJsonData() throws SQLException, IOException { String dstTable = TestUtils .escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("dstTableJSON"))); @@ -1653,6 +1654,7 @@ public void testInsertHugeJsonData() throws SQLException, IOException { * Expected error -> org.opentest4j.AssertionFailedError: Test failed due to: Attempting to grow LOB beyond maximum allowed size of 216895848447 bytes. */ @Test + @Disabled("Disabled due to huge JSON data") public void testInsert2GBData() throws SQLException, FileNotFoundException, IOException { String dstTable = TestUtils .escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("dstTableJSON"))); @@ -1812,7 +1814,12 @@ private void generateHugeJsonFile(long targetSize) { if (!firstElement) { writer.write(","); } - String jsonChunk = "{\"value\":\"" + "a".repeat(1000) + "\"}"; + // Create a string of 1000 'a' characters - Java 8 compatible + StringBuilder sb = new StringBuilder(1000); + for (int j = 0; j < 1000; j++) { + sb.append('a'); + } + String jsonChunk = "{\"value\":\"" + sb.toString() + "\"}"; writer.write(jsonChunk); currentSize += jsonChunk.length(); firstElement = false;