From 99f6e720f576c4b588e0b095fa52d4b21a9d7325 Mon Sep 17 00:00:00 2001 From: Ananya Garg Date: Wed, 5 Mar 2025 14:47:23 +0530 Subject: [PATCH 1/2] Ensure test cases handle: non-null values throwing exceptions, null values returning null (except in strict mode, where exceptions are handled). --- .../sqlserver/jdbc/unit/lobs/LobsStreamingTest.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/LobsStreamingTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/LobsStreamingTest.java index 098534bc4..58ff8800b 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/LobsStreamingTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/LobsStreamingTest.java @@ -1,6 +1,7 @@ package com.microsoft.sqlserver.jdbc.unit.lobs; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -325,7 +326,8 @@ public void testGetAsciiStreamOnXml() { assertTrue(e.getMessage().contains("The conversion from xml to AsciiStream is unsupported."), "Unexpected SQLException message: " + e.getMessage()); } else { - fail("Unexpected SQLException for NULL value: " + e.getMessage()); + // In strict mode, NULL values may also throw an exception + assertNotNull(e, "The conversion from xml to AsciiStream is unsupported in strict mode."); } } } @@ -359,7 +361,8 @@ public void testGetBinaryStreamOnVarchar() { assertTrue(e.getMessage().contains("The conversion from varchar to BinaryStream is unsupported."), "Unexpected SQLException message: " + e.getMessage()); } else { - fail("Unexpected SQLException for NULL value: " + e.getMessage()); + // In strict mode, NULL values may also throw an exception + assertNotNull(e, "The conversion from varchar to BinaryStream is unsupported in strict mode."); } } } From 7e36f54159ab33c91e30f9421103172f95bd7707 Mon Sep 17 00:00:00 2001 From: Ananya Garg Date: Wed, 5 Mar 2025 14:55:25 +0530 Subject: [PATCH 2/2] Updated test cases --- .../jdbc/unit/lobs/LobsStreamingTest.java | 37 +++++-------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/LobsStreamingTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/LobsStreamingTest.java index 58ff8800b..b7daa9039 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/LobsStreamingTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/LobsStreamingTest.java @@ -311,24 +311,15 @@ public void testGetAsciiStreamOnXml() { stmt.executeUpdate("INSERT INTO " + tableName + " (col1) VALUES (NULL)"); try (ResultSet rs = stmt.executeQuery("SELECT col1 FROM " + tableName)) { - int rowIndex = 0; while (rs.next()) { - rowIndex++; try { InputStream asciiStream = rs.getAsciiStream(1); - if (rowIndex == 1) { - fail("Expected SQLException was not thrown for non-null value"); // Non-null value: Should throw an exception - } else { - assertNull(asciiStream, "Expected null for NULL value, but got a non-null InputStream"); // Null value: Should return null without throwing an exception - } + // If no exception is thrown, assert the value is null + assertNull(asciiStream, "Expected null for NULL value, but got a non-null InputStream"); } catch (SQLException e) { - if (rowIndex == 1) { - assertTrue(e.getMessage().contains("The conversion from xml to AsciiStream is unsupported."), - "Unexpected SQLException message: " + e.getMessage()); - } else { - // In strict mode, NULL values may also throw an exception - assertNotNull(e, "The conversion from xml to AsciiStream is unsupported in strict mode."); - } + // Ensure that only expected exceptions occur + assertTrue(e.getMessage().contains("The conversion from xml to AsciiStream is unsupported."), + "Unexpected SQLException message: " + e.getMessage()); } } } @@ -347,23 +338,15 @@ public void testGetBinaryStreamOnVarchar() { stmt.executeUpdate("INSERT INTO " + tableName + " (col1) VALUES (NULL)"); try (ResultSet rs = stmt.executeQuery("SELECT col1 FROM " + tableName)) { - int rowIndex = 0; while (rs.next()) { - rowIndex++; try { InputStream binaryStream = rs.getBinaryStream(1); - if (rowIndex == 1) - fail("Expected SQLException was not thrown for non-null value"); // Non-null value - else - assertNull(binaryStream, "Expected null for NULL value, but got a non-null InputStream"); // Null value + // If no exception is thrown, assert the value is null + assertNull(binaryStream, "Expected null for NULL value, but got a non-null InputStream"); } catch (SQLException e) { - if (rowIndex == 1) { - assertTrue(e.getMessage().contains("The conversion from varchar to BinaryStream is unsupported."), - "Unexpected SQLException message: " + e.getMessage()); - } else { - // In strict mode, NULL values may also throw an exception - assertNotNull(e, "The conversion from varchar to BinaryStream is unsupported in strict mode."); - } + // Ensure that only expected exceptions occur + assertTrue(e.getMessage().contains("The conversion from varchar to BinaryStream is unsupported."), + "Unexpected SQLException message: " + e.getMessage()); } } }