Skip to content

Commit b34bc80

Browse files
committed
Added test for Bulk Copy
1 parent cd28b53 commit b34bc80

File tree

2 files changed

+229
-5
lines changed

2 files changed

+229
-5
lines changed

src/test/java/com/microsoft/sqlserver/jdbc/bulkCopy/BulkCopyISQLServerBulkRecordTest.java

+228-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55
package com.microsoft.sqlserver.jdbc.bulkCopy;
66

7+
import static org.junit.jupiter.api.Assertions.assertEquals;
78
import static org.junit.jupiter.api.Assertions.assertTrue;
89
import static org.junit.jupiter.api.Assertions.fail;
910

@@ -43,7 +44,6 @@
4344
import com.microsoft.sqlserver.testframework.DBTable;
4445
import com.microsoft.sqlserver.testframework.sqlType.SqlType;
4546

46-
4747
/**
4848
* Test bulk copy decimal scale and precision
4949
*/
@@ -150,6 +150,9 @@ public void testBulkCopyDateTimePrecision() throws SQLException {
150150
}
151151
}
152152

153+
/**
154+
* Test bulk copy with a single JSON row.
155+
*/
153156
@Test
154157
public void testBulkCopyJSON() throws SQLException {
155158
String dstTable = TestUtils
@@ -164,13 +167,179 @@ public void testBulkCopyJSON() throws SQLException {
164167
bulkCopy.setDestinationTableName(dstTable);
165168
String data = "{\"key\":\"value\"}";
166169
bulkCopy.writeToServer(new BulkRecordJSON(data));
167-
170+
168171
String select = "SELECT * FROM " + dstTable;
169172
ResultSet rs = dstStmt.executeQuery(select);
170173

171174
assertTrue(rs.next());
172175
assertTrue(data.equals(rs.getObject(1)));
173-
176+
177+
} catch (Exception e) {
178+
fail(e.getMessage());
179+
} finally {
180+
try (Statement stmt = conn.createStatement();) {
181+
TestUtils.dropTableIfExists(dstTable, stmt);
182+
}
183+
}
184+
}
185+
}
186+
187+
/**
188+
* Test bulk copy with multiple JSON rows.
189+
*/
190+
@Test
191+
public void testBulkCopyMultipleJsonRows() throws SQLException {
192+
String dstTable = TestUtils
193+
.escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("dstTable")));
194+
195+
try (Connection conn = DriverManager.getConnection(connectionString);) {
196+
try (Statement dstStmt = conn.createStatement(); SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(conn)) {
197+
198+
dstStmt.executeUpdate(
199+
"CREATE TABLE " + dstTable + " (testCol JSON);");
200+
201+
bulkCopy.setDestinationTableName(dstTable);
202+
String data1 = "{\"key1\":\"value1\"}";
203+
String data2 = "{\"key2\":\"value2\"}";
204+
String data3 = "{\"key3\":\"value3\"}";
205+
bulkCopy.writeToServer(new BulkRecordJSON(data1));
206+
bulkCopy.writeToServer(new BulkRecordJSON(data2));
207+
bulkCopy.writeToServer(new BulkRecordJSON(data3));
208+
209+
String select = "SELECT * FROM " + dstTable;
210+
ResultSet rs = dstStmt.executeQuery(select);
211+
212+
assertTrue(rs.next());
213+
assertTrue(data1.equals(rs.getObject(1)));
214+
assertTrue(rs.next());
215+
assertTrue(data2.equals(rs.getObject(1)));
216+
assertTrue(rs.next());
217+
assertTrue(data3.equals(rs.getObject(1)));
218+
219+
} catch (Exception e) {
220+
fail(e.getMessage());
221+
} finally {
222+
try (Statement stmt = conn.createStatement();) {
223+
TestUtils.dropTableIfExists(dstTable, stmt);
224+
}
225+
}
226+
}
227+
}
228+
229+
/**
230+
* Test bulk copy with multiple JSON rows and columns.
231+
*/
232+
@Test
233+
public void testBulkCopyMultipleJsonRowsAndColumns() throws SQLException {
234+
String dstTable = TestUtils
235+
.escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("dstTable")));
236+
237+
try (Connection conn = DriverManager.getConnection(connectionString);) {
238+
try (Statement dstStmt = conn.createStatement(); SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(conn)) {
239+
240+
dstStmt.executeUpdate(
241+
"CREATE TABLE " + dstTable + " (testCol1 JSON, testCol2 JSON);");
242+
243+
bulkCopy.setDestinationTableName(dstTable);
244+
String data1Col1 = "{\"key1\":\"value1\"}";
245+
String data1Col2 = "{\"key2\":\"value2\"}";
246+
String data2Col1 = "{\"key3\":\"value3\"}";
247+
String data2Col2 = "{\"key4\":\"value4\"}";
248+
bulkCopy.writeToServer(new BulkRecordJSONMultipleColumns(data1Col1, data1Col2));
249+
bulkCopy.writeToServer(new BulkRecordJSONMultipleColumns(data2Col1, data2Col2));
250+
251+
String select = "SELECT * FROM " + dstTable;
252+
ResultSet rs = dstStmt.executeQuery(select);
253+
254+
assertTrue(rs.next());
255+
assertTrue(data1Col1.equals(rs.getObject(1)));
256+
assertTrue(data1Col2.equals(rs.getObject(2)));
257+
assertTrue(rs.next());
258+
assertTrue(data2Col1.equals(rs.getObject(1)));
259+
assertTrue(data2Col2.equals(rs.getObject(2)));
260+
261+
} catch (Exception e) {
262+
fail(e.getMessage());
263+
} finally {
264+
try (Statement stmt = conn.createStatement();) {
265+
TestUtils.dropTableIfExists(dstTable, stmt);
266+
}
267+
}
268+
}
269+
}
270+
271+
/**
272+
* Test bulk copy with nested JSON rows.
273+
*/
274+
@Test
275+
public void testBulkCopyNestedJsonRows() throws SQLException {
276+
String dstTable = TestUtils
277+
.escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("dstTable")));
278+
279+
try (Connection conn = DriverManager.getConnection(connectionString);) {
280+
try (Statement dstStmt = conn.createStatement(); SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(conn)) {
281+
282+
dstStmt.executeUpdate(
283+
"CREATE TABLE " + dstTable + " (testCol JSON);");
284+
285+
bulkCopy.setDestinationTableName(dstTable);
286+
String data1 = "{\"key1\":{\"nestedKey1\":\"nestedValue1\"}}";
287+
String data2 = "{\"key2\":{\"nestedKey2\":\"nestedValue2\"}}";
288+
String data3 = "{\"key3\":{\"nestedKey3\":\"nestedValue3\"}}";
289+
bulkCopy.writeToServer(new BulkRecordJSON(data1));
290+
bulkCopy.writeToServer(new BulkRecordJSON(data2));
291+
bulkCopy.writeToServer(new BulkRecordJSON(data3));
292+
293+
String select = "SELECT * FROM " + dstTable;
294+
ResultSet rs = dstStmt.executeQuery(select);
295+
296+
assertTrue(rs.next());
297+
assertTrue(data1.equals(rs.getObject(1)));
298+
assertTrue(rs.next());
299+
assertTrue(data2.equals(rs.getObject(1)));
300+
assertTrue(rs.next());
301+
assertTrue(data3.equals(rs.getObject(1)));
302+
303+
} catch (Exception e) {
304+
fail(e.getMessage());
305+
} finally {
306+
try (Statement stmt = conn.createStatement();) {
307+
TestUtils.dropTableIfExists(dstTable, stmt);
308+
}
309+
}
310+
}
311+
}
312+
313+
/**
314+
* Test bulk copy with various data types in JSON.
315+
*/
316+
@Test
317+
public void testBulkCopyWithVariousDataTypes() throws SQLException {
318+
String dstTable = TestUtils
319+
.escapeSingleQuotes(AbstractSQLGenerator.escapeIdentifier(RandomUtil.getIdentifier("dstTable")));
320+
321+
try (Connection conn = DriverManager.getConnection(connectionString);) {
322+
try (Statement dstStmt = conn.createStatement(); SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy(conn)) {
323+
324+
dstStmt.executeUpdate(
325+
"CREATE TABLE " + dstTable + " (testCol JSON)");
326+
327+
bulkCopy.setDestinationTableName(dstTable);
328+
329+
// JSON data to be inserted
330+
String data = "{\"bitCol\":true,\"tinyIntCol\":2,\"smallIntCol\":-32768,\"intCol\":0,\"bigIntCol\":0,\"floatCol\":-1700.0000000000,\"realCol\":-3400.0000000000,\"decimalCol\":22.335600,\"numericCol\":22.3356,\"moneyCol\":-922337203685477.5808,\"smallMoneyCol\":-214748.3648,\"charCol\":\"a5()b\",\"nCharCol\":\"?????\",\"varcharCol\":\"test to test csv files\",\"nVarcharCol\":\"???\",\"binaryCol\":\"6163686974\",\"varBinaryCol\":\"6163686974\",\"dateCol\":\"1922-11-02\",\"datetimeCol\":\"2004-05-23 14:25:10.487\",\"datetime2Col\":\"2007-05-02 19:58:47.1234567\",\"datetimeOffsetCol\":\"2025-12-10 12:32:10.1234567+01:00\"}";
331+
332+
// Insert data directly
333+
String insertSql = "INSERT INTO " + dstTable + " VALUES ('" + data + "')";
334+
dstStmt.executeUpdate(insertSql);
335+
336+
String select = "SELECT * FROM " + dstTable;
337+
ResultSet rs = dstStmt.executeQuery(select);
338+
339+
assertTrue(rs.next());
340+
String jsonData = rs.getString(1);
341+
assertEquals(data, jsonData);
342+
174343
} catch (Exception e) {
175344
fail(e.getMessage());
176345
} finally {
@@ -403,4 +572,60 @@ public boolean next() {
403572
return true;
404573
}
405574
}
575+
576+
private static class BulkRecordJSONMultipleColumns implements ISQLServerBulkData {
577+
boolean anyMoreData = true;
578+
Object[] data;
579+
580+
BulkRecordJSONMultipleColumns(Object data1, Object data2) {
581+
this.data = new Object[2];
582+
this.data[0] = data1;
583+
this.data[1] = data2;
584+
}
585+
586+
@Override
587+
public Set<Integer> getColumnOrdinals() {
588+
Set<Integer> ords = new HashSet<>();
589+
ords.add(1);
590+
ords.add(2);
591+
return ords;
592+
}
593+
594+
@Override
595+
public String getColumnName(int column) {
596+
if (column == 1) {
597+
return "testCol1";
598+
} else {
599+
return "testCol2";
600+
}
601+
}
602+
603+
@Override
604+
public int getColumnType(int column) {
605+
return microsoft.sql.Types.JSON;
606+
}
607+
608+
@Override
609+
public int getPrecision(int column) {
610+
return 0;
611+
}
612+
613+
@Override
614+
public int getScale(int column) {
615+
return 0;
616+
}
617+
618+
@Override
619+
public Object[] getRowData() {
620+
return data;
621+
}
622+
623+
@Override
624+
public boolean next() {
625+
if (!anyMoreData)
626+
return false;
627+
anyMoreData = false;
628+
return true;
629+
}
630+
}
406631
}

src/test/java/com/microsoft/sqlserver/testframework/AbstractTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ public static void setup() throws Exception {
158158
// no config file used
159159
}
160160

161-
connectionString = getConfiguredPropertyOrEnv(Constants.MSSQL_JDBC_TEST_CONNECTION_PROPERTIES);
162-
161+
connectionString = getConfiguredPropertyOrEnv(Constants.MSSQL_JDBC_TEST_CONNECTION_PROPERTIES);
163162
applicationClientID = getConfiguredProperty("applicationClientID");
164163
applicationKey = getConfiguredProperty("applicationKey");
165164
tenantID = getConfiguredProperty("tenantID");

0 commit comments

Comments
 (0)