diff --git a/src/main/java/scm/address/logic/commands/ImportCommand.java b/src/main/java/scm/address/logic/commands/ImportCommand.java index 565c2c1bf2c..6673fcb269b 100644 --- a/src/main/java/scm/address/logic/commands/ImportCommand.java +++ b/src/main/java/scm/address/logic/commands/ImportCommand.java @@ -190,6 +190,11 @@ private List getPersonsFromCsv(String filePath) throws IOExce String headers = br.readLine(); while ((line = br.readLine()) != null) { String[] info = line.split(splitBy); + + if (info.length < 4) { + throw new IOException("Incorrect Data Format!"); + } + String name = info[0]; String phone = info[1]; String email = info[2]; diff --git a/src/test/data/ImportCommandTest/datasample.csv b/src/test/data/ImportCommandTest/datasample.csv new file mode 100644 index 00000000000..cd708c0e68e --- /dev/null +++ b/src/test/data/ImportCommandTest/datasample.csv @@ -0,0 +1,3 @@ +Name,Email,Address +Alan Turing,something@some.com,"21st street" +Alan Burning,somethin2g@some.com,"22nd street" diff --git a/src/test/java/scm/address/logic/commands/ImportCommandTest.java b/src/test/java/scm/address/logic/commands/ImportCommandTest.java index ca46d97ffe0..a5070c2ae83 100644 --- a/src/test/java/scm/address/logic/commands/ImportCommandTest.java +++ b/src/test/java/scm/address/logic/commands/ImportCommandTest.java @@ -32,6 +32,7 @@ public class ImportCommandTest { private static final String UNKNOWN_FILE_NAME = "./src/test/data/ImportCommandTest/abcdefgh_abcdefgh.json"; private static final String ADDRESS_BOOK_PATH = "./src/test/data/ImportCommandTest/addressbook.json"; private static final String TEST_CSV_FILE_PATH = "./src/test/data/ImportCommandTest/contacts.csv"; + private static final String INVALID_DATA_CSV_FILE_PATH = "./src/test/data/ImportCommandTest/datasample.csv"; private static final String UNKNOWN_FILE_EXTENSION = "./src/test/data/ImportCommandTest/contacts.xyz"; private static final String NO_FILE_EXTENSION = "./src/test/data/ImportCommandTest/contacts"; private static final String UNKNOWN_CSV_FILE = "./src/test/data/ImportCommandTest/abcdefg.csv"; @@ -135,6 +136,16 @@ public void retrievePersonsFromFile_invalidCsvFile_failure() { assertThrows(CommandException.class, () -> importCommand.execute(testModel)); } + @Test + public void retrievePersonsFromFile_invalidDataCsvFile_failure() { + HashSet curHashSet = new HashSet<>(); + curHashSet.add(new File(INVALID_DATA_CSV_FILE_PATH)); + + Model testModel = new ModelManager(getTypicalAddressBook(), new UserPrefs(), getTypicalScheduleList()); + ImportCommand importCommand = new ImportCommand(curHashSet); + assertThrows(CommandException.class, () -> importCommand.execute(testModel)); + } + @Test public void execute_importingFromUnknownFileFormat_failure() { HashSet curHashSet = new HashSet<>();