Skip to content

Commit 44daab7

Browse files
committed
Refactored tests: Moved datatype-related stream tests to LobsStreaming test, removed redundant cleanup logging, and handled table cleanup in @AfterEach.
1 parent fae3664 commit 44daab7

File tree

2 files changed

+90
-89
lines changed

2 files changed

+90
-89
lines changed

src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/LobsStreamingTest.java

+90-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package com.microsoft.sqlserver.jdbc.unit.lobs;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
import static org.junit.jupiter.api.Assertions.fail;
47

58
import java.io.IOException;
69
import java.io.InputStream;
@@ -16,9 +19,11 @@
1619
import java.util.Scanner;
1720
import java.util.stream.IntStream;
1821

22+
import org.junit.jupiter.api.AfterEach;
1923
import org.junit.jupiter.api.BeforeAll;
2024
import org.junit.jupiter.api.BeforeEach;
2125
import org.junit.jupiter.api.DisplayName;
26+
import org.junit.jupiter.api.Nested;
2227
import org.junit.jupiter.api.Tag;
2328
import org.junit.jupiter.api.Test;
2429
import org.junit.platform.runner.JUnitPlatform;
@@ -280,4 +285,88 @@ public void testNClobsVarcharCHARA() throws SQLException, IOException {
280285
}
281286
}
282287
}
283-
}
288+
289+
@Nested
290+
public class TestPLP {
291+
private String tableName;
292+
293+
@AfterEach
294+
public void cleanUp() {
295+
try (Connection conn = getConnection();
296+
Statement stmt = conn.createStatement()) {
297+
TestUtils.dropTableIfExists(tableName, stmt);
298+
} catch (SQLException ex) {
299+
fail(ex.getMessage());
300+
}
301+
}
302+
303+
@Test
304+
public void testGetAsciiStreamOnXml() {
305+
tableName = TestUtils.escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("TestXmlTable")));
306+
try (Connection conn = getConnection();
307+
Statement stmt = conn.createStatement()) {
308+
stmt.executeUpdate("CREATE TABLE " + tableName + " (col1 XML NULL)");
309+
stmt.executeUpdate("INSERT INTO " + tableName + " (col1) VALUES ('<root><child>Hello</child></root>')");
310+
stmt.executeUpdate("INSERT INTO " + tableName + " (col1) VALUES (NULL)");
311+
312+
try (ResultSet rs = stmt.executeQuery("SELECT col1 FROM " + tableName)) {
313+
int rowIndex = 0;
314+
while (rs.next()) {
315+
rowIndex++;
316+
try {
317+
InputStream asciiStream = rs.getAsciiStream(1);
318+
if (rowIndex == 1) {
319+
fail("Expected SQLException was not thrown for non-null value"); // Non-null value: Should throw an exception
320+
} else {
321+
assertNull(asciiStream, "Expected null for NULL value, but got a non-null InputStream"); // Null value: Should return null without throwing an exception
322+
}
323+
} catch (SQLException e) {
324+
if (rowIndex == 1) {
325+
assertTrue(e.getMessage().contains("The conversion from xml to AsciiStream is unsupported."),
326+
"Unexpected SQLException message: " + e.getMessage());
327+
} else {
328+
fail("Unexpected SQLException for NULL value: " + e.getMessage());
329+
}
330+
}
331+
}
332+
}
333+
} catch (SQLException e) {
334+
fail("Database setup or execution failed: " + e.getMessage());
335+
}
336+
}
337+
338+
@Test
339+
public void testGetBinaryStreamOnVarchar() {
340+
tableName = TestUtils.escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("TestPLPTable")));
341+
try (Connection conn = getConnection();
342+
Statement stmt = conn.createStatement()) {
343+
stmt.executeUpdate("CREATE TABLE " + tableName + " (col1 VARCHAR(50) NULL)");
344+
stmt.executeUpdate("INSERT INTO " + tableName + " (col1) VALUES ('TestValue')");
345+
stmt.executeUpdate("INSERT INTO " + tableName + " (col1) VALUES (NULL)");
346+
347+
try (ResultSet rs = stmt.executeQuery("SELECT col1 FROM " + tableName)) {
348+
int rowIndex = 0;
349+
while (rs.next()) {
350+
rowIndex++;
351+
try {
352+
InputStream binaryStream = rs.getBinaryStream(1);
353+
if (rowIndex == 1)
354+
fail("Expected SQLException was not thrown for non-null value"); // Non-null value
355+
else
356+
assertNull(binaryStream, "Expected null for NULL value, but got a non-null InputStream"); // Null value
357+
} catch (SQLException e) {
358+
if (rowIndex == 1) {
359+
assertTrue(e.getMessage().contains("The conversion from varchar to BinaryStream is unsupported."),
360+
"Unexpected SQLException message: " + e.getMessage());
361+
} else {
362+
fail("Unexpected SQLException for NULL value: " + e.getMessage());
363+
}
364+
}
365+
}
366+
}
367+
} catch (SQLException e) {
368+
fail("Database setup or execution failed: " + e.getMessage());
369+
}
370+
}
371+
}
372+
}

src/test/java/com/microsoft/sqlserver/jdbc/unit/statement/StatementTest.java

-88
Original file line numberDiff line numberDiff line change
@@ -3080,92 +3080,4 @@ public void terminate() {
30803080
}
30813081
}
30823082

3083-
@Nested
3084-
public class TestPLP {
3085-
3086-
@Test
3087-
public void testGetAsciiStreamOnXml() {
3088-
String tableName = TestUtils.escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("TestXmlTable")));
3089-
try (Connection conn = getConnection();
3090-
Statement stmt = conn.createStatement()) {
3091-
TestUtils.dropTableIfExists(tableName, stmt);
3092-
stmt.executeUpdate("CREATE TABLE " + tableName + " (col1 XML NULL)");
3093-
stmt.executeUpdate("INSERT INTO " + tableName + " (col1) VALUES ('<root><child>Hello</child></root>')");
3094-
stmt.executeUpdate("INSERT INTO " + tableName + " (col1) VALUES (NULL)");
3095-
3096-
try (ResultSet rs = stmt.executeQuery("SELECT col1 FROM " + tableName)) {
3097-
int rowIndex = 0;
3098-
while (rs.next()) {
3099-
rowIndex++;
3100-
try {
3101-
InputStream asciiStream = rs.getAsciiStream(1);
3102-
if (rowIndex == 1) {
3103-
fail("Expected SQLException was not thrown for non-null value"); // Non-null value: Should throw an exception
3104-
} else {
3105-
assertNull(asciiStream, "Expected null for NULL value, but got a non-null InputStream"); // Null value: Should return null without throwing an exception
3106-
}
3107-
} catch (SQLException e) {
3108-
if (rowIndex == 1) {
3109-
assertTrue(e.getMessage().contains("The conversion from xml to AsciiStream is unsupported."),
3110-
"Unexpected SQLException message: " + e.getMessage());
3111-
} else {
3112-
fail("Unexpected SQLException for NULL value: " + e.getMessage());
3113-
}
3114-
}
3115-
}
3116-
}
3117-
} catch (SQLException e) {
3118-
fail("Database setup or execution failed: " + e.getMessage());
3119-
} finally {
3120-
try (Connection conn = getConnection();
3121-
Statement stmt = conn.createStatement()) {
3122-
TestUtils.dropTableIfExists(tableName, stmt);
3123-
} catch (SQLException ex) {
3124-
System.err.println("Failed to clean up test table: " + ex.getMessage());
3125-
}
3126-
}
3127-
}
3128-
3129-
@Test
3130-
public void testGetBinaryStreamOnVarchar() {
3131-
String tableName = TestUtils.escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("TestPLPTable")));
3132-
try (Connection conn = getConnection();
3133-
Statement stmt = conn.createStatement()) {
3134-
TestUtils.dropTableIfExists(tableName, stmt);
3135-
stmt.executeUpdate("CREATE TABLE " + tableName + " (col1 VARCHAR(50) NULL)");
3136-
stmt.executeUpdate("INSERT INTO " + tableName + " (col1) VALUES ('TestValue')");
3137-
stmt.executeUpdate("INSERT INTO " + tableName + " (col1) VALUES (NULL)");
3138-
3139-
try (ResultSet rs = stmt.executeQuery("SELECT col1 FROM " + tableName)) {
3140-
int rowIndex = 0;
3141-
while (rs.next()) {
3142-
rowIndex++;
3143-
try {
3144-
InputStream binaryStream = rs.getBinaryStream(1);
3145-
if (rowIndex == 1)
3146-
fail("Expected SQLException was not thrown for non-null value");// Non-null value
3147-
else
3148-
assertNull(binaryStream, "Expected null for NULL value, but got a non-null InputStream");// Null value
3149-
} catch (SQLException e) {
3150-
if (rowIndex == 1) {
3151-
assertTrue(e.getMessage().contains("The conversion from varchar to BinaryStream is unsupported."),
3152-
"Unexpected SQLException message: " + e.getMessage());
3153-
} else {
3154-
fail("Unexpected SQLException for NULL value: " + e.getMessage());
3155-
}
3156-
}
3157-
}
3158-
}
3159-
} catch (SQLException e) {
3160-
fail("Database setup or execution failed: " + e.getMessage());
3161-
} finally {
3162-
try (Connection conn = getConnection();
3163-
Statement stmt = conn.createStatement()) {
3164-
TestUtils.dropTableIfExists(tableName, stmt);
3165-
} catch (SQLException ex) {
3166-
System.err.println("Failed to clean up test table: " + ex.getMessage());
3167-
}
3168-
}
3169-
}
3170-
}
31713083
}

0 commit comments

Comments
 (0)