Skip to content

Commit

Permalink
Add test for null values when getting DTV values (#2617)
Browse files Browse the repository at this point in the history
* Fix: Ensure exception is thrown before returning null in getValue()

* Added test cases to validate perf fix for NULL values and removed obsolete FX tests.

* Refactored tests: Moved datatype-related stream tests to LobsStreaming test, removed redundant cleanup logging, and handled table cleanup in @AfterEach.

* Removed unused import.
  • Loading branch information
Ananya2 authored Mar 4, 2025
1 parent b8be643 commit 6c4891d
Showing 1 changed file with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.microsoft.sqlserver.jdbc.unit.lobs;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -16,9 +19,11 @@
import java.util.Scanner;
import java.util.stream.IntStream;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
Expand Down Expand Up @@ -280,4 +285,88 @@ public void testNClobsVarcharCHARA() throws SQLException, IOException {
}
}
}
}

@Nested
public class TestPLP {
private String tableName;

@AfterEach
public void cleanUp() {
try (Connection conn = getConnection();
Statement stmt = conn.createStatement()) {
TestUtils.dropTableIfExists(tableName, stmt);
} catch (SQLException ex) {
fail(ex.getMessage());
}
}

@Test
public void testGetAsciiStreamOnXml() {
tableName = TestUtils.escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("TestXmlTable")));
try (Connection conn = getConnection();
Statement stmt = conn.createStatement()) {
stmt.executeUpdate("CREATE TABLE " + tableName + " (col1 XML NULL)");
stmt.executeUpdate("INSERT INTO " + tableName + " (col1) VALUES ('<root><child>Hello</child></root>')");
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
}
} catch (SQLException e) {
if (rowIndex == 1) {
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());
}
}
}
}
} catch (SQLException e) {
fail("Database setup or execution failed: " + e.getMessage());
}
}

@Test
public void testGetBinaryStreamOnVarchar() {
tableName = TestUtils.escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("TestPLPTable")));
try (Connection conn = getConnection();
Statement stmt = conn.createStatement()) {
stmt.executeUpdate("CREATE TABLE " + tableName + " (col1 VARCHAR(50) NULL)");
stmt.executeUpdate("INSERT INTO " + tableName + " (col1) VALUES ('TestValue')");
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
} catch (SQLException e) {
if (rowIndex == 1) {
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());
}
}
}
}
} catch (SQLException e) {
fail("Database setup or execution failed: " + e.getMessage());
}
}
}
}

0 comments on commit 6c4891d

Please sign in to comment.