Skip to content

Commit c164ef9

Browse files
author
Divang Sharma
committed
Added JSON datatype test cases in ResultSet, TVP and Regression scenarios.
1 parent d994fec commit c164ef9

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

src/test/java/com/microsoft/sqlserver/jdbc/resultset/ResultSetTest.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public void testJdbc41ResultSetMethods() throws SQLException {
100100
+ "col2 varchar(512), " + "col3 float, " + "col4 decimal(10,5), " + "col5 uniqueidentifier, "
101101
+ "col6 xml, " + "col7 varbinary(max), " + "col8 text, " + "col9 ntext, " + "col10 varbinary(max), "
102102
+ "col11 date, " + "col12 time, " + "col13 datetime2, " + "col14 datetimeoffset, "
103-
+ "col15 decimal(10,9), " + "col16 decimal(38,38), "
103+
+ "col15 decimal(10,9), " + "col16 decimal(38,38), " + "col17 json, "
104104
+ "order_column int identity(1,1) primary key)");
105105
try {
106106

@@ -120,12 +120,13 @@ public void testJdbc41ResultSetMethods() throws SQLException {
120120
+ "'2017-05-19T10:47:15.1234567'," // col13
121121
+ "'2017-05-19T10:47:15.1234567+02:00'," // col14
122122
+ "0.123456789, " // col15
123-
+ "0.1234567890123456789012345678901234567" // col16
123+
+ "0.1234567890123456789012345678901234567, " // col16
124+
+ "'{\"test\":\"123\"}'" // col17
124125
+ ")");
125126

126127
stmt.executeUpdate("Insert into " + AbstractSQLGenerator.escapeIdentifier(tableName) + " values("
127128
+ "null, " + "null, " + "null, " + "null, " + "null, " + "null, " + "null, " + "null, "
128-
+ "null, " + "null, " + "null, " + "null, " + "null, " + "null, " + "null, " + "null)");
129+
+ "null, " + "null, " + "null, " + "null, " + "null, " + "null, " + "null, " + "null, " + "null)");
129130

130131
try (ResultSet rs = stmt.executeQuery("select * from "
131132
+ AbstractSQLGenerator.escapeIdentifier(tableName) + " order by order_column")) {
@@ -223,7 +224,11 @@ public void testJdbc41ResultSetMethods() throws SQLException {
223224
.compareTo(new BigDecimal("0.12345678901234567890123456789012345670")));
224225
assertEquals(0, rs.getObject("col16", BigDecimal.class)
225226
.compareTo(new BigDecimal("0.12345678901234567890123456789012345670")));
226-
227+
228+
String expectedJsonValue = "{\"test\":\"123\"}";
229+
assertEquals(expectedJsonValue, rs.getObject(17).toString());
230+
assertEquals(expectedJsonValue, rs.getObject("col17").toString());
231+
227232
// test null values, mostly to verify primitive wrappers do not return default values
228233
assertTrue(rs.next());
229234
assertNull(rs.getObject("col1", Boolean.class));
@@ -284,6 +289,9 @@ public void testJdbc41ResultSetMethods() throws SQLException {
284289
assertNull(rs.getObject(16, BigDecimal.class));
285290
assertNull(rs.getObject("col16", BigDecimal.class));
286291

292+
assertNull(rs.getObject(17));
293+
assertNull(rs.getObject("col17"));
294+
287295
assertFalse(rs.next());
288296
}
289297
} finally {

src/test/java/com/microsoft/sqlserver/jdbc/tvp/TVPTypesTest.java

+32
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,38 @@ public void testTVPXMLStoredProcedure() throws SQLException {
379379
}
380380
}
381381

382+
/**
383+
* JSON with StoredProcedure
384+
*
385+
* @throws SQLException
386+
*/
387+
@Test
388+
public void testTVPJSONStoredProcedure() throws SQLException {
389+
createTables("json");
390+
createTVPS("json");
391+
createProcedure();
392+
393+
value = "{\"severity\":\"TRACE\",\"duration\":200,\"date\":\"2024-12-17T15:45:56\"}";
394+
395+
tvp = new SQLServerDataTable();
396+
tvp.addColumnMetadata("c1", microsoft.sql.Types.JSON);
397+
tvp.addRow(value);
398+
399+
final String sql = "{call " + AbstractSQLGenerator.escapeIdentifier(procedureName) + "(?)}";
400+
401+
try (SQLServerCallableStatement callableStmt = (SQLServerCallableStatement) connection.prepareCall(sql)) {
402+
callableStmt.setStructured(1, tvpName, tvp);
403+
callableStmt.execute();
404+
405+
try (Connection con = getConnection(); Statement stmt = con.createStatement();
406+
ResultSet rs = stmt.executeQuery(
407+
"select c1 from " + AbstractSQLGenerator.escapeIdentifier(tableName) + " ORDER BY rowId")) {
408+
while (rs.next())
409+
assertEquals(rs.getString(1), value);
410+
}
411+
}
412+
}
413+
382414
/**
383415
* Text with StoredProcedure
384416
*

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

+24
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,30 @@ public void testXmlQuery() throws SQLException {
238238
}
239239
}
240240

241+
/**
242+
* Tests Json query
243+
*
244+
* @throws SQLException
245+
*/
246+
@Test
247+
public void testJsonQuery() throws SQLException {
248+
try (Connection connection = getConnection(); Statement stmt = connection.createStatement()) {
249+
tableName = RandomUtil.getIdentifier("try_SQLJSON_Table");
250+
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt);
251+
stmt.execute("CREATE TABLE " + AbstractSQLGenerator.escapeIdentifier(tableName)
252+
+ " ([c1] int, [c2] json, [c3] json)");
253+
254+
String sql = "UPDATE " + AbstractSQLGenerator.escapeIdentifier(tableName) + " SET [c2] = ?, [c3] = ?";
255+
try (SQLServerPreparedStatement pstmt = (SQLServerPreparedStatement) connection.prepareStatement(sql)) {
256+
pstmt.setObject(1, null);
257+
pstmt.setObject(2, null);
258+
pstmt.executeUpdate();
259+
} finally {
260+
TestUtils.dropTableIfExists(AbstractSQLGenerator.escapeIdentifier(tableName), stmt);
261+
}
262+
}
263+
}
264+
241265
private void createTable(Statement stmt) throws SQLException {
242266

243267
String sql = "CREATE TABLE " + AbstractSQLGenerator.escapeIdentifier(tableName)

0 commit comments

Comments
 (0)