@@ -66,6 +66,7 @@ public class BulkCopyCSVTest extends AbstractTest {
6666 static String inputFile = "BulkCopyCSVTestInput.csv" ;
6767 static String inputFileNoColumnName = "BulkCopyCSVTestInputNoColumnName.csv" ;
6868 static String inputFileDelimiterEscape = "BulkCopyCSVTestInputDelimiterEscape.csv" ;
69+ static String inputFileDelimiterEscapeNoNewLineAtEnd = "BulkCopyCSVTestInputDelimiterEscapeNoNewLineAtEnd.csv" ;
6970 static String inputFileMultipleDoubleQuotes = "BulkCopyCSVTestInputMultipleDoubleQuotes.csv" ;
7071 static String encoding = "UTF-8" ;
7172 static String delimiter = "," ;
@@ -197,12 +198,70 @@ public void testEscapeColumnDelimitersCSV() throws Exception {
197198 assertEquals (expectedEscaped [i ][3 ], rs .getString ("c4" ));
198199 i ++;
199200 }
201+ assertEquals (i , 12 , "Expected to load 12 records, but loaded " + i + " records" );
200202 }
201203
202204 TestUtils .dropTableIfExists (tableName , stmt );
203205 }
204206 }
205207
208+ @ Test
209+ @ DisplayName ("Test setEscapeColumnDelimitersCSVNoNewLineAtEnd" )
210+ public void testEscapeColumnDelimitersCSVNoNewLineAtEnd () throws Exception {
211+ String tableName = AbstractSQLGenerator .escapeIdentifier (RandomUtil .getIdentifier ("BulkEscape" ));
212+ String fileName = filePath + inputFileDelimiterEscapeNoNewLineAtEnd ;
213+ /*
214+ * The list below is the copy of inputFileDelimiterEsc ape with quotes removed.
215+ */
216+ String [][] expectedEscaped = new String [12 ][4 ];
217+ expectedEscaped [0 ] = new String [] {"test" , " test\" " , "no@split" , " testNoQuote" , "" };
218+ expectedEscaped [1 ] = new String [] {null , null , null , null , "" };
219+ expectedEscaped [2 ] = new String [] {"\" " , "test\" test" , "test@\" test" , null , "" };
220+ expectedEscaped [3 ] = new String [] {"testNoQuote " , " testSpaceAround " , " testSpaceInside " ,
221+ " testSpaceQuote\" " , "" };
222+ expectedEscaped [4 ] = new String [] {null , null , null , " testSpaceInside " , "" };
223+ expectedEscaped [5 ] = new String [] {"1997" , "Ford" , "E350" , "E63" , "" };
224+ expectedEscaped [6 ] = new String [] {"1997" , "Ford" , "E350" , "E63" , "" };
225+ expectedEscaped [7 ] = new String [] {"1997" , "Ford" , "E350" , "Super@ luxurious truck" , "" };
226+ expectedEscaped [8 ] = new String [] {"1997" , "Ford" , "E350" , "Super@ \" luxurious\" truck" , "" };
227+ expectedEscaped [9 ] = new String [] {"1997" , "Ford" , "E350" , "E63" , "" };
228+ expectedEscaped [10 ] = new String [] {"1997" , "Ford" , "E350" , " Super luxurious truck " , "" };
229+ expectedEscaped [11 ] = new String [] {"1997" , "F\r \n o\r \n r\r \n d" , "E350" , "\" Super\" \" luxurious\" \" truck\" " ,
230+ "" };
231+
232+ try (Connection con = getConnection (); Statement stmt = con .createStatement ();
233+ SQLServerBulkCopy bulkCopy = new SQLServerBulkCopy (con );
234+ SQLServerBulkCSVFileRecord fileRecord = new SQLServerBulkCSVFileRecord (fileName , encoding , "@" ,
235+ false )) {
236+ bulkCopy .setDestinationTableName (tableName );
237+ fileRecord .setEscapeColumnDelimitersCSV (true );
238+ fileRecord .addColumnMetadata (1 , null , java .sql .Types .INTEGER , 0 , 0 );
239+ fileRecord .addColumnMetadata (2 , null , java .sql .Types .VARCHAR , 50 , 0 );
240+ fileRecord .addColumnMetadata (3 , null , java .sql .Types .VARCHAR , 50 , 0 );
241+ fileRecord .addColumnMetadata (4 , null , java .sql .Types .VARCHAR , 50 , 0 );
242+ fileRecord .addColumnMetadata (5 , null , java .sql .Types .VARCHAR , 50 , 0 );
243+ fileRecord .addColumnMetadata (6 , null , java .sql .Types .VARCHAR , 50 , 0 );
244+ stmt .executeUpdate ("CREATE TABLE " + tableName
245+ + " (id INT IDENTITY(1,1), c1 VARCHAR(50), c2 VARCHAR(50), c3 VARCHAR(50), c4 VARCHAR(50), c5 VARCHAR(50))" );
246+ bulkCopy .writeToServer (fileRecord );
247+
248+ int i = 0 ;
249+ try (ResultSet rs = stmt .executeQuery ("SELECT * FROM " + tableName + " ORDER BY id" );
250+ BufferedReader br = new BufferedReader (new FileReader (fileName ));) {
251+ while (rs .next ()) {
252+ assertEquals (expectedEscaped [i ][0 ], rs .getString ("c1" ));
253+ assertEquals (expectedEscaped [i ][1 ], rs .getString ("c2" ));
254+ assertEquals (expectedEscaped [i ][2 ], rs .getString ("c3" ));
255+ assertEquals (expectedEscaped [i ][3 ], rs .getString ("c4" ));
256+ i ++;
257+ }
258+ assertEquals (i , 12 , "Expected to load 12 records, but loaded " + i + " records" );
259+ } finally {
260+ TestUtils .dropTableIfExists (tableName , stmt );
261+ }
262+ }
263+ }
264+
206265 /**
207266 * test simple csv file for bulkcopy, for GitHub issue 1391 Tests to ensure that the set returned by
208267 * getColumnOrdinals doesn't have to be ordered
0 commit comments