forked from microsoft/mssql-jdbc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBulkCopyColumnMappingTest.java
402 lines (360 loc) · 20.7 KB
/
BulkCopyColumnMappingTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* Microsoft JDBC Driver for SQL Server Copyright(c) Microsoft Corporation All rights reserved. This program is made
* available under the terms of the MIT License. See the LICENSE file in the project root for more information.
*/
package com.microsoft.sqlserver.jdbc.bulkCopy;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.MessageFormat;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
import org.junit.runner.RunWith;
import com.microsoft.sqlserver.jdbc.ComparisonUtil;
import com.microsoft.sqlserver.jdbc.TestResource;
import com.microsoft.sqlserver.jdbc.TestUtils;
import com.microsoft.sqlserver.testframework.DBConnection;
import com.microsoft.sqlserver.testframework.DBResultSet;
import com.microsoft.sqlserver.testframework.DBStatement;
import com.microsoft.sqlserver.testframework.DBTable;
import com.microsoft.sqlserver.testframework.sqlType.SqlType;
/**
* Test BulkCopy Column Mapping
*/
@RunWith(JUnitPlatform.class)
@DisplayName("BulkCopy Column Mapping Test")
public class BulkCopyColumnMappingTest extends BulkCopyTestSetUp {
@Test
@DisplayName("BulkCopy:test no explicit column mapping")
public void testNoExplicitCM() throws SQLException {
try (DBConnection con = new DBConnection(connectionString); DBStatement stmt = con.createStatement()) {
DBTable destTable = null;
try {
// create destination table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy without explicit column mapping
BulkCopyTestWrapper bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == random.nextInt(2)) ? true : false, ds);
bulkWrapper.setUsingXAConnection((0 == random.nextInt(2)) ? true : false, dsXA);
bulkWrapper.setUsingPooledConnection((0 == random.nextInt(2)) ? true : false, dsPool);
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable);
} finally {
TestUtils.dropTableIfExists(destTable.getEscapedTableName(), (Statement) stmt.product());
}
}
}
@Test
@DisplayName("BulkCopy:test explicit column mapping")
public void testExplicitCM() throws SQLException {
try (DBConnection con = new DBConnection(connectionString); DBStatement stmt = con.createStatement()) {
DBTable destTable = null;
try {
// create destination table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy with explicit column mapping
BulkCopyTestWrapper bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == random.nextInt(2)) ? true : false, ds);
bulkWrapper.setUsingXAConnection((0 == random.nextInt(2)) ? true : false, dsXA);
bulkWrapper.setUsingPooledConnection((0 == random.nextInt(2)) ? true : false, dsPool);
for (int i = 1; i <= destTable.totalColumns(); i++) {
int select = i % 4;
switch (select) {
case 0:
bulkWrapper.setColumnMapping(i, i);
break;
case 1:
bulkWrapper.setColumnMapping(i, destTable.getColumnName(i - 1));
break;
case 2:
bulkWrapper.setColumnMapping(sourceTable.getColumnName(i - 1),
destTable.getColumnName(i - 1));
break;
case 3:
bulkWrapper.setColumnMapping(sourceTable.getColumnName(i - 1), i);
break;
}
}
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable);
} finally {
TestUtils.dropTableIfExists(destTable.getEscapedTableName(), (Statement) stmt.product());
}
}
}
@Test
@DisplayName("BulkCopy:test unicode column mapping")
public void testUnicodeCM() throws SQLException {
try (DBConnection con = new DBConnection(connectionString); DBStatement stmt = con.createStatement()) {
DBTable sourceTableUnicode = null;
DBTable destTableUnicode = null;
try {
// create source unicode table
sourceTableUnicode = new DBTable(true, true);
stmt.createTable(sourceTableUnicode);
// create destination unicode table with same schema as source
destTableUnicode = sourceTableUnicode.cloneSchema();
stmt.createTable(destTableUnicode);
// set up bulkCopy with explicit column mapping
BulkCopyTestWrapper bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == random.nextInt(2)) ? true : false, ds);
bulkWrapper.setUsingXAConnection((0 == random.nextInt(2)) ? true : false, dsXA);
bulkWrapper.setUsingPooledConnection((0 == random.nextInt(2)) ? true : false, dsPool);
for (int i = 1; i <= destTableUnicode.totalColumns(); i++) {
int select = i % 4;
switch (select) {
case 0:
bulkWrapper.setColumnMapping(i, i);
break;
case 1:
bulkWrapper.setColumnMapping(i, destTableUnicode.getColumnName(i - 1));
break;
case 2:
bulkWrapper.setColumnMapping(sourceTableUnicode.getColumnName(i - 1),
destTableUnicode.getColumnName(i - 1));
break;
case 3:
bulkWrapper.setColumnMapping(sourceTableUnicode.getColumnName(i - 1), i);
break;
}
}
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTableUnicode, destTableUnicode);
} finally {
TestUtils.dropTableIfExists(sourceTableUnicode.getEscapedTableName(), (Statement) stmt.product());
TestUtils.dropTableIfExists(destTableUnicode.getEscapedTableName(), (Statement) stmt.product());
}
}
}
@Test
@DisplayName("BulkCopy:test repetitive column mapping")
public void testRepetitiveCM() throws SQLException {
try (DBConnection con = new DBConnection(connectionString); DBStatement stmt = con.createStatement()) {
DBTable sourceTable1 = null;
DBTable destTable = null;
try {
// create source table
sourceTable1 = new DBTable(true);
stmt.createTable(sourceTable1);
stmt.populateTable(sourceTable1);
// create destination table with same schema as source
destTable = sourceTable1.cloneSchema();
// add 1 column to destination which will be duplicate of first source column
SqlType sqlType = sourceTable1.getSqlType(0);
destTable.addColumn(sqlType);
stmt.createTable(destTable);
// set up bulkCopy with explicit column mapping
BulkCopyTestWrapper bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == random.nextInt(2)) ? true : false, ds);
bulkWrapper.setUsingXAConnection((0 == random.nextInt(2)) ? true : false, dsXA);
bulkWrapper.setUsingPooledConnection((0 == random.nextInt(2)) ? true : false, dsPool);
for (int i = 1; i <= sourceTable1.totalColumns(); i++) {
int select = i % 4;
switch (select) {
case 0:
bulkWrapper.setColumnMapping(i, i);
break;
case 1:
bulkWrapper.setColumnMapping(i, destTable.getColumnName(i - 1));
break;
case 2:
bulkWrapper.setColumnMapping(sourceTable1.getColumnName(i - 1),
destTable.getColumnName(i - 1));
break;
case 3:
bulkWrapper.setColumnMapping(sourceTable1.getColumnName(i - 1), i);
break;
}
}
// add column mapping for duplicate column in destination
bulkWrapper.setColumnMapping(1, 25);
// perform bulkCopy without validating results or dropping destination table
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable1, destTable, false, false, false);
try {
validateValuesRepetitiveCM(con, sourceTable1, destTable);
} catch (SQLException e) {
MessageFormat form = new MessageFormat(TestResource.getResource("R_failedValidate"));
Object[] msgArgs = {sourceTable1.getTableName() + " and" + destTable.getTableName()};
fail(form.format(msgArgs) + "\n" + destTable.getTableName() + "\n" + e.getMessage());
}
} finally {
TestUtils.dropTableIfExists(sourceTable1.getEscapedTableName(), (Statement) stmt.product());
TestUtils.dropTableIfExists(destTable.getEscapedTableName(), (Statement) stmt.product());
}
}
}
@Test
@DisplayName("BulkCopy:test implicit mismatched column mapping")
public void testImplicitMismatchCM() throws SQLException {
try (DBConnection con = new DBConnection(connectionString); DBStatement stmt = con.createStatement()) {
DBTable destTable = null;
try {
// create non unicode destination table with different schema from source table
destTable = new DBTable(true, false, true);
stmt.createTable(destTable);
// set up bulkCopy with explicit column mapping
BulkCopyTestWrapper bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == random.nextInt(2)) ? true : false, ds);
bulkWrapper.setUsingXAConnection((0 == random.nextInt(2)) ? true : false, dsXA);
bulkWrapper.setUsingPooledConnection((0 == random.nextInt(2)) ? true : false, dsPool);
for (int i = 1; i <= destTable.totalColumns(); i++) {
int select = i % 4;
switch (select) {
case 0:
bulkWrapper.setColumnMapping(i, i);
break;
case 1:
bulkWrapper.setColumnMapping(i, destTable.getColumnName(i - 1));
break;
case 2:
bulkWrapper.setColumnMapping(sourceTable.getColumnName(i - 1),
destTable.getColumnName(i - 1));
break;
case 3:
bulkWrapper.setColumnMapping(sourceTable.getColumnName(i - 1), i);
break;
}
}
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
} finally {
TestUtils.dropTableIfExists(destTable.getEscapedTableName(), (Statement) stmt.product());
}
}
}
@Test
@DisplayName("BulkCopy:test invalid column mapping")
public void testInvalidCM() throws SQLException {
try (DBConnection con = new DBConnection(connectionString); DBStatement stmt = con.createStatement()) {
DBTable destTable = null;
try {
// create destination table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy with wrong column names
BulkCopyTestWrapper bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == random.nextInt(2)) ? true : false, ds);
bulkWrapper.setUsingXAConnection((0 == random.nextInt(2)) ? true : false, dsXA);
bulkWrapper.setUsingPooledConnection((0 == random.nextInt(2)) ? true : false, dsPool);
bulkWrapper.setColumnMapping("wrongFirst", "wrongSecond");
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
// create destination table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy with invalid ordinal, column no 65 does not exist
bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == random.nextInt(2)) ? true : false, ds);
bulkWrapper.setUsingXAConnection((0 == random.nextInt(2)) ? true : false, dsXA);
bulkWrapper.setUsingPooledConnection((0 == random.nextInt(2)) ? true : false, dsPool);
bulkWrapper.setColumnMapping(sourceTable.getColumnName(1), 65);
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
// create destination table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy with invalid ordinal, column no 42 does not exist
bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == random.nextInt(2)) ? true : false, ds);
bulkWrapper.setUsingXAConnection((0 == random.nextInt(2)) ? true : false, dsXA);
bulkWrapper.setUsingPooledConnection((0 == random.nextInt(2)) ? true : false, dsPool);
bulkWrapper.setColumnMapping(42, destTable.getColumnName(1));
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
// create destination table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy with invalid ordinal, column no 42 and 65 do not exist
bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == random.nextInt(2)) ? true : false, ds);
bulkWrapper.setUsingXAConnection((0 == random.nextInt(2)) ? true : false, dsXA);
bulkWrapper.setUsingPooledConnection((0 == random.nextInt(2)) ? true : false, dsPool);
bulkWrapper.setColumnMapping(42, 65);
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
// create destination table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy while passing empty string as column mapping
bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == random.nextInt(2)) ? true : false, ds);
bulkWrapper.setUsingXAConnection((0 == random.nextInt(2)) ? true : false, dsXA);
bulkWrapper.setUsingPooledConnection((0 == random.nextInt(2)) ? true : false, dsPool);
bulkWrapper.setColumnMapping(sourceTable.getColumnName(1), " ");
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
// create destination table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy with 0 ordinal column mapping
bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == random.nextInt(2)) ? true : false, ds);
bulkWrapper.setUsingXAConnection((0 == random.nextInt(2)) ? true : false, dsXA);
bulkWrapper.setUsingPooledConnection((0 == random.nextInt(2)) ? true : false, dsPool);
bulkWrapper.setColumnMapping(0, 0);
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
// create destination table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy with negative ordinal column mapping
bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == random.nextInt(2)) ? true : false, ds);
bulkWrapper.setUsingXAConnection((0 == random.nextInt(2)) ? true : false, dsXA);
bulkWrapper.setUsingPooledConnection((0 == random.nextInt(2)) ? true : false, dsPool);
bulkWrapper.setColumnMapping(-3, -6);
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
// create destination table
destTable = sourceTable.cloneSchema();
stmt.createTable(destTable);
// set up bulkCopy with Integer.MIN_VALUE and Integer.MAX_VALUE column mapping
bulkWrapper = new BulkCopyTestWrapper(connectionString);
bulkWrapper.setUsingConnection((0 == random.nextInt(2)) ? true : false, ds);
bulkWrapper.setUsingXAConnection((0 == random.nextInt(2)) ? true : false, dsXA);
bulkWrapper.setUsingPooledConnection((0 == random.nextInt(2)) ? true : false, dsPool);
bulkWrapper.setColumnMapping(Integer.MIN_VALUE, Integer.MAX_VALUE);
BulkCopyTestUtil.performBulkCopy(bulkWrapper, sourceTable, destTable, true, true);
} finally {
TestUtils.dropTableIfExists(destTable.getEscapedTableName(), (Statement) stmt.product());
}
}
}
/**
* validate if same values are in both source and destination table taking into account 1 extra column in
* destination which should be a copy of first column of source.
*
* @param con
* @param sourceTable
* @param destinationTable
* @throws SQLException
*/
private void validateValuesRepetitiveCM(DBConnection con, DBTable sourceTable,
DBTable destinationTable) throws SQLException {
try (DBStatement srcStmt = con.createStatement(); DBStatement dstStmt = con.createStatement();
DBResultSet srcResultSet = srcStmt
.executeQuery("SELECT * FROM " + sourceTable.getEscapedTableName() + ";");
DBResultSet dstResultSet = dstStmt
.executeQuery("SELECT * FROM " + destinationTable.getEscapedTableName() + ";")) {
ResultSetMetaData sourceMeta = ((ResultSet) srcResultSet.product()).getMetaData();
int totalColumns = sourceMeta.getColumnCount();
// verify data from sourceType and resultSet
int numRows = 0;
while (srcResultSet.next() && dstResultSet.next()) {
numRows++;
for (int i = 1; i <= totalColumns; i++) {
Object srcValue, dstValue;
srcValue = srcResultSet.getObject(i);
dstValue = dstResultSet.getObject(i);
ComparisonUtil.compareExpectedAndActual(sourceMeta.getColumnType(i), srcValue, dstValue);
// compare value of first column of source with extra column in destination
if (1 == i) {
Object srcValueFirstCol = srcResultSet.getObject(i);
Object dstValLastCol = dstResultSet.getObject(totalColumns + 1);
ComparisonUtil.compareExpectedAndActual(sourceMeta.getColumnType(i), srcValueFirstCol,
dstValLastCol);
}
}
}
// verify number of rows and columns
assertTrue(((ResultSet) dstResultSet.product()).getMetaData().getColumnCount() == totalColumns + 1);
assertTrue(sourceTable.getTotalRows() == numRows);
assertTrue(destinationTable.getTotalRows() == numRows);
}
}
}