Skip to content

Commit

Permalink
feat(Input File): Removing the Byte Order Mark if it is present (#268)
Browse files Browse the repository at this point in the history
This solves the issue of having to re-save a CSV file that's already
been saved and happens to have a Byte Order Mark - a hidden first
character in the file.
  • Loading branch information
ndickerson authored Sep 26, 2018
1 parent a4b0d14 commit be900f3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import com.bullhorn.dataloader.util.PropertyFileUtil;
import com.csvreader.CsvReader;
import com.google.common.collect.Sets;
import org.apache.commons.io.input.BOMInputStream;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
Expand All @@ -31,7 +33,8 @@ public class CsvFileReader extends CsvReader {
* @param filePath the path to the CSV file
*/
public CsvFileReader(String filePath, PropertyFileUtil propertyFileUtil, PrintUtil printUtil) throws IOException {
super(filePath, ',', propertyFileUtil.getSingleByteEncoding() ? Charset.forName("ISO-8859-1") : Charset.forName("UTF-8"));
super(new BOMInputStream(new FileInputStream(filePath)), ',',
propertyFileUtil.getSingleByteEncoding() ? Charset.forName("ISO-8859-1") : Charset.forName("UTF-8"));
this.propertyFileUtil = propertyFileUtil;
this.printUtil = printUtil;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public void setup() throws IOException {
propertyValidationUtil, printUtilMock);
}

@Test
public void testByteOrderMarkRemoval() throws IOException {
CsvFileReader csvFileReader = new CsvFileReader(TestUtils.getResourceFilePath("CandidateByteOrderMark.csv"), propertyFileUtil, printUtilMock);
Assert.assertArrayEquals(new String[]{"externalID", "name", "firstName", "lastName", "email"}, csvFileReader.getHeaders());
}

@Test
public void testMappedColumns() throws IOException {
CsvFileReader csvFileReader = new CsvFileReader(TestUtils.getResourceFilePath("Candidate_MappedColumns.csv"), propertyFileUtil, printUtilMock);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ public void testIntegration() throws IOException {
// Test that column header name mapping is working properly
insertUpdateDeleteFromDirectory(TestUtils.getResourceFilePath("columnMapping"), false);

// Test that the byte order mark is ignored when it's present in the input file as the first (hidden) character
insertUpdateDeleteFromDirectory(TestUtils.getResourceFilePath("byteOrderMark"), false);

// Run a test for processing empty association fields (with the setting turned on)
System.setProperty("processEmptyAssociations", "true");
insertUpdateDeleteFromDirectory(TestUtils.getResourceFilePath("processEmptyFields"), false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
externalID ,name ,firstName,lastName,email
candidateBOM-ext-1,Stéphan Soufflé,Stéphan ,Soufflé ,[email protected]
2 changes: 2 additions & 0 deletions src/test/resources/unitTest/CandidateByteOrderMark.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
externalID ,name ,firstName,lastName,email
candidateBOM-ext-1,Stéphan Soufflé,Stéphan ,Soufflé ,[email protected]

0 comments on commit be900f3

Please sign in to comment.