From d456fd40db4c78a4f0e80825dc989e1e474bcc27 Mon Sep 17 00:00:00 2001 From: jitwei98 Date: Tue, 9 Oct 2018 23:04:29 +0800 Subject: [PATCH 01/10] Filetype.java: Create new class --- .../java/seedu/address/model/Filetype.java | 84 +++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/main/java/seedu/address/model/Filetype.java diff --git a/src/main/java/seedu/address/model/Filetype.java b/src/main/java/seedu/address/model/Filetype.java new file mode 100644 index 000000000000..5a4024e7bd6d --- /dev/null +++ b/src/main/java/seedu/address/model/Filetype.java @@ -0,0 +1,84 @@ +package seedu.address.model; + +import static java.util.Objects.requireNonNull; +import static seedu.address.commons.util.AppUtil.checkArgument; + +import java.util.EnumSet; + +/** + * Represents filetype available for import/export. + * Guarantees: immutable; is valid as declared in {@link #isValidFiletype(String)} + */ +public class Filetype { + + public static final String MESSAGE_FILETYPE_CONSTRAINTS = + "Filetype can take either csv/CSV or vcf/VCF, and it should not be blank"; + + /* + * The first character of the filetype must not be a whitespace, + * otherwise " " (a blank string) becomes a valid input. + */ + public static final String FILETYPE_VALIDATION_REGEX = "[^\\s].*"; + + private enum Extension { + csv, CSV, vcf, VCF; + } + + public final String value; + + /** + * Constructs an {@code Filetype}. + * + * @param filetype A valid filetype. + */ + public Filetype(String filetype) { + requireNonNull(filetype); + checkArgument(isValidFiletype(filetype), MESSAGE_FILETYPE_CONSTRAINTS); + value = filetype; + } + + /** + * Returns true if a given string is a valid filetype. + */ + public static boolean isValidFiletype(String test) { + return test.matches(FILETYPE_VALIDATION_REGEX) && isValidExtension(test); + } + + /** + * Returns true if a given string matches any of the valid Extension. + */ + public static boolean isValidExtension(String extension) { + return contains(Extension.class, extension); + } + + // Reused from + // http://www.java2s.com/Tutorials/Java/Data_Type_How_to/String/ + // Check_if_enum_contains_a_given_string.html with minor modifications + private static > boolean contains(Class _enumClass, + String value) { + try { + return EnumSet.allOf(_enumClass) + .contains(Enum.valueOf(_enumClass, value)); + } catch (Exception e) { + return false; + } + } + + @Override + public String toString() { + return value; + } + + @Override + public boolean equals(Object other) { + return other == this // short circuit if same object + || (other instanceof Filetype // instanceof handles nulls + && value.equals(((Filetype) other).value)); // state check + } + + @Override + public int hashCode() { + return value.hashCode(); + } + +} From ec05a15a497494c12f920fb7466e61afef37d61d Mon Sep 17 00:00:00 2001 From: jitwei98 Date: Tue, 9 Oct 2018 23:13:11 +0800 Subject: [PATCH 02/10] ParserUtil.java: Use Filetype.java to check parsed result --- .../java/seedu/address/logic/parser/ParserUtil.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index 561f7b7c2059..0d2a1f76b073 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -1,6 +1,7 @@ package seedu.address.logic.parser; import static java.util.Objects.requireNonNull; +import static seedu.address.model.Filetype.isValidFiletype; import java.util.Collection; import java.util.HashSet; @@ -9,6 +10,7 @@ import seedu.address.commons.core.index.Index; import seedu.address.commons.util.StringUtil; import seedu.address.logic.parser.exceptions.ParseException; +import seedu.address.model.Filetype; import seedu.address.model.person.Address; import seedu.address.model.person.Email; import seedu.address.model.person.Name; @@ -22,11 +24,6 @@ public class ParserUtil { public static final String MESSAGE_INVALID_INDEX = "Index is not a non-zero unsigned integer."; - // TODO: Move this to a Filetype class. - public static final String FILETYPE_CSV = "csv"; - public static final String FILETYPE_VCF = "vcf"; - public static final String MESSAGE_FILETYPE_CONSTRAINTS = "Filetype can only be \"csv\" or \"vcf\"."; - /** * Parses {@code oneBasedIndex} into an {@code Index} and returns it. Leading and trailing whitespaces will be * trimmed. @@ -127,7 +124,6 @@ public static Set parseTags(Collection tags) throws ParseException return tagSet; } - // TODO: Implement Filetype class (refer to Name class). /** * Parses a {@code String filetype} into a {@code Filetype}. * Leading and trailing whitespaces will be trimmed. @@ -138,8 +134,8 @@ public static String parseFiletype(String filetype) throws ParseException { requireNonNull(filetype); String trimmedFiletype = filetype.trim(); - if (!(filetype.equals(ParserUtil.FILETYPE_CSV) || filetype.equals(ParserUtil.FILETYPE_VCF))) { - throw new ParseException(ParserUtil.MESSAGE_FILETYPE_CONSTRAINTS); + if (!isValidFiletype(trimmedFiletype)) { + throw new ParseException(Filetype.MESSAGE_FILETYPE_CONSTRAINTS); } return trimmedFiletype; } From d3e1ea97078a4094fac7f581a539c3a6defae54f Mon Sep 17 00:00:00 2001 From: jitwei98 Date: Tue, 9 Oct 2018 23:13:33 +0800 Subject: [PATCH 03/10] Filetype.java: Remove uppercase extensions --- src/main/java/seedu/address/model/Filetype.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/address/model/Filetype.java b/src/main/java/seedu/address/model/Filetype.java index 5a4024e7bd6d..727b7c19ed75 100644 --- a/src/main/java/seedu/address/model/Filetype.java +++ b/src/main/java/seedu/address/model/Filetype.java @@ -12,7 +12,7 @@ public class Filetype { public static final String MESSAGE_FILETYPE_CONSTRAINTS = - "Filetype can take either csv/CSV or vcf/VCF, and it should not be blank"; + "Filetype can take either \"csv\" or \"vcf\", and it should not be blank"; /* * The first character of the filetype must not be a whitespace, @@ -20,8 +20,8 @@ public class Filetype { */ public static final String FILETYPE_VALIDATION_REGEX = "[^\\s].*"; - private enum Extension { - csv, CSV, vcf, VCF; + public enum Extension { + csv, vcf } public final String value; From fae306376aaa63ca2028fa966472c1eb9fe5f9b6 Mon Sep 17 00:00:00 2001 From: jitwei98 Date: Tue, 9 Oct 2018 23:20:40 +0800 Subject: [PATCH 04/10] Filetype.java: Add javadocs for enum Extension and rename parameter from _enumClass to enumClass --- src/main/java/seedu/address/model/Filetype.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/seedu/address/model/Filetype.java b/src/main/java/seedu/address/model/Filetype.java index 727b7c19ed75..f5d244735539 100644 --- a/src/main/java/seedu/address/model/Filetype.java +++ b/src/main/java/seedu/address/model/Filetype.java @@ -20,6 +20,9 @@ public class Filetype { */ public static final String FILETYPE_VALIDATION_REGEX = "[^\\s].*"; + /** + * Filetypes that can be used to export contacts. + */ public enum Extension { csv, vcf } @@ -54,11 +57,11 @@ public static boolean isValidExtension(String extension) { // Reused from // http://www.java2s.com/Tutorials/Java/Data_Type_How_to/String/ // Check_if_enum_contains_a_given_string.html with minor modifications - private static > boolean contains(Class _enumClass, + private static > boolean contains(Class enumClass, String value) { try { - return EnumSet.allOf(_enumClass) - .contains(Enum.valueOf(_enumClass, value)); + return EnumSet.allOf(enumClass) + .contains(Enum.valueOf(enumClass, value)); } catch (Exception e) { return false; } From 42265319071c474dd8a4bdeba3c910e3843d013d Mon Sep 17 00:00:00 2001 From: jitwei98 Date: Tue, 9 Oct 2018 23:29:18 +0800 Subject: [PATCH 05/10] Filetype.java#contains(): Add javadocs header --- src/main/java/seedu/address/model/Filetype.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main/java/seedu/address/model/Filetype.java b/src/main/java/seedu/address/model/Filetype.java index f5d244735539..aac6cefa8b69 100644 --- a/src/main/java/seedu/address/model/Filetype.java +++ b/src/main/java/seedu/address/model/Filetype.java @@ -57,6 +57,12 @@ public static boolean isValidExtension(String extension) { // Reused from // http://www.java2s.com/Tutorials/Java/Data_Type_How_to/String/ // Check_if_enum_contains_a_given_string.html with minor modifications + + /** + * Returns true if an enum contains a specific string. + * @param enumClass + * @param value + */ private static > boolean contains(Class enumClass, String value) { try { From 106047801c7846cb7ddfccc24341f4f8a6de641e Mon Sep 17 00:00:00 2001 From: jitwei98 Date: Tue, 9 Oct 2018 23:48:16 +0800 Subject: [PATCH 06/10] Filetype.java: Set value as private, modify header of contains() --- src/main/java/seedu/address/model/Filetype.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/seedu/address/model/Filetype.java b/src/main/java/seedu/address/model/Filetype.java index aac6cefa8b69..cfaa9cd2d678 100644 --- a/src/main/java/seedu/address/model/Filetype.java +++ b/src/main/java/seedu/address/model/Filetype.java @@ -27,7 +27,7 @@ public enum Extension { csv, vcf } - public final String value; + private final String value; /** * Constructs an {@code Filetype}. @@ -59,7 +59,7 @@ public static boolean isValidExtension(String extension) { // Check_if_enum_contains_a_given_string.html with minor modifications /** - * Returns true if an enum contains a specific string. + * Returns true if an {@code enumClass} contains a specific {@code value}. * @param enumClass * @param value */ From c28058ee1e668c982826b11ef347c5efa230d3e7 Mon Sep 17 00:00:00 2001 From: jitwei98 Date: Tue, 9 Oct 2018 23:48:56 +0800 Subject: [PATCH 07/10] ParserUtilTest.java: Implement test to test parseFiletype() --- .../address/logic/parser/ParserUtilTest.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/test/java/seedu/address/logic/parser/ParserUtilTest.java b/src/test/java/seedu/address/logic/parser/ParserUtilTest.java index 78fd9c877708..96553c908d3b 100644 --- a/src/test/java/seedu/address/logic/parser/ParserUtilTest.java +++ b/src/test/java/seedu/address/logic/parser/ParserUtilTest.java @@ -37,6 +37,8 @@ public class ParserUtilTest { private static final String VALID_TAG_1 = "friend"; private static final String VALID_TAG_2 = "neighbour"; + private static final String VALID_FILETYPE = "csv"; + private static final String WHITESPACE = " \t\r\n"; @Rule @@ -205,4 +207,24 @@ public void parseTags_collectionWithValidTags_returnsTagSet() throws Exception { assertEquals(expectedTagSet, actualTagSet); } + + @Test + public void parseFiletype_invalidInput_throwsNullPointerException() throws Exception { + thrown.expect(ParseException.class); + ParserUtil.parseFiletype("abc"); + } + + @Test + public void parseFiletype_null_throwsNullPointerException() { + Assert.assertThrows(NullPointerException.class, () -> ParserUtil.parseFiletype(null)); + } + + @Test + public void parseFiletype_validInput_success() throws Exception { + // No whitespaces + assertEquals(VALID_FILETYPE, ParserUtil.parseFiletype("csv")); + + // Leading and trailing whitespaces + assertEquals(VALID_FILETYPE, ParserUtil.parseFiletype(" csv ")); + } } From 458bcb20fb53c81d2bdc028669f247c04d45c6cb Mon Sep 17 00:00:00 2001 From: jitwei98 Date: Tue, 9 Oct 2018 23:49:25 +0800 Subject: [PATCH 08/10] Remove TypicalFiletypes.java from test --- .../address/logic/commands/ExportAllCommandTest.java | 5 +++-- .../address/logic/parser/AddressBookParserTest.java | 3 +-- .../logic/parser/ExportAllCommandParserTest.java | 7 +++---- .../java/seedu/address/testutil/TypicalFiletypes.java | 11 ----------- 4 files changed, 7 insertions(+), 19 deletions(-) delete mode 100644 src/test/java/seedu/address/testutil/TypicalFiletypes.java diff --git a/src/test/java/seedu/address/logic/commands/ExportAllCommandTest.java b/src/test/java/seedu/address/logic/commands/ExportAllCommandTest.java index 05c0e1a156bf..1f38257ad47c 100644 --- a/src/test/java/seedu/address/logic/commands/ExportAllCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/ExportAllCommandTest.java @@ -4,8 +4,6 @@ import static org.junit.Assert.assertTrue; import static seedu.address.logic.commands.CommandTestUtil.assertCommandFailure; import static seedu.address.logic.commands.ExportAllCommand.MESSAGE_ARGUMENTS; -import static seedu.address.testutil.TypicalFiletypes.FILETYPE_CSV; -import static seedu.address.testutil.TypicalFiletypes.FILETYPE_VCF; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; import org.junit.Test; @@ -23,6 +21,9 @@ public class ExportAllCommandTest { private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + private static final String FILETYPE_CSV = "csv"; + private static final String FILETYPE_VCF = "vcf"; + @Test public void execute() { assertCommandFailure(new ExportAllCommand(FILETYPE_CSV), model, new CommandHistory(), diff --git a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java index 6ef0dd5fb596..6994d121743f 100644 --- a/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java +++ b/src/test/java/seedu/address/logic/parser/AddressBookParserTest.java @@ -4,7 +4,6 @@ import static org.junit.Assert.assertTrue; import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; import static seedu.address.commons.core.Messages.MESSAGE_UNKNOWN_COMMAND; -import static seedu.address.testutil.TypicalFiletypes.FILETYPE_CSV; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; import java.util.Arrays; @@ -85,7 +84,7 @@ public void parseCommand_edit() throws Exception { @Test public void parseCommand_exportall() throws Exception { - assertTrue(parser.parseCommand(ExportAllCommand.COMMAND_WORD + " " + FILETYPE_CSV) instanceof ExportAllCommand); + assertTrue(parser.parseCommand(ExportAllCommand.COMMAND_WORD + " " + "csv") instanceof ExportAllCommand); } @Test diff --git a/src/test/java/seedu/address/logic/parser/ExportAllCommandParserTest.java b/src/test/java/seedu/address/logic/parser/ExportAllCommandParserTest.java index 3217f642b609..631fec2df54b 100644 --- a/src/test/java/seedu/address/logic/parser/ExportAllCommandParserTest.java +++ b/src/test/java/seedu/address/logic/parser/ExportAllCommandParserTest.java @@ -3,7 +3,6 @@ import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure; import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess; -import static seedu.address.testutil.TypicalFiletypes.FILETYPE_CSV; import org.junit.Test; @@ -16,7 +15,7 @@ public class ExportAllCommandParserTest { @Test public void parseFiletypeSuccess() { - String userInput = FILETYPE_CSV; + final String userInput = "csv"; ExportAllCommand expectedCommand = new ExportAllCommand(userInput); assertParseSuccess(parser, userInput, expectedCommand); } @@ -24,8 +23,8 @@ public void parseFiletypeSuccess() { @Test public void parseMissingNotNullFieldFailure() { // no parameters - String userInput = ExportAllCommand.COMMAND_WORD + " "; - String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportAllCommand.MESSAGE_USAGE); + final String userInput = ExportAllCommand.COMMAND_WORD + " "; + final String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportAllCommand.MESSAGE_USAGE); assertParseFailure(parser, userInput, expectedMessage); } diff --git a/src/test/java/seedu/address/testutil/TypicalFiletypes.java b/src/test/java/seedu/address/testutil/TypicalFiletypes.java deleted file mode 100644 index 57a0905eb81f..000000000000 --- a/src/test/java/seedu/address/testutil/TypicalFiletypes.java +++ /dev/null @@ -1,11 +0,0 @@ -package seedu.address.testutil; - -// TODO: Implement a new class called Filetypes. -/** - * A utility class containing a list of {@code string Filetypes} objects to be used in tests. - */ -public class TypicalFiletypes { - - public static final String FILETYPE_CSV = "csv"; - public static final String FILETYPE_VCF = "vcf"; -} From 155198058c1b7c42a2f22420952f5a8a39c18cc9 Mon Sep 17 00:00:00 2001 From: jitwei98 Date: Wed, 10 Oct 2018 00:01:14 +0800 Subject: [PATCH 09/10] FiletypeTest.java: Implement test for Filetype class --- .../seedu/address/model/FiletypeTest.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/test/java/seedu/address/model/FiletypeTest.java diff --git a/src/test/java/seedu/address/model/FiletypeTest.java b/src/test/java/seedu/address/model/FiletypeTest.java new file mode 100644 index 000000000000..08fd43bbc796 --- /dev/null +++ b/src/test/java/seedu/address/model/FiletypeTest.java @@ -0,0 +1,37 @@ +package seedu.address.model; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.jupiter.api.Test; + +import seedu.address.testutil.Assert; + +class FiletypeTest { + + @Test + public void constructor_null_throwsNullPointerException() { + Assert.assertThrows(NullPointerException.class, () -> new Filetype(null)); + } + + @Test + public void constructor_invalidFiletype_throwsIllegalArgumentException() { + String invalidFiletype = "ccc"; + Assert.assertThrows(IllegalArgumentException.class, () -> new Filetype(invalidFiletype)); + } + + @Test + public void isValidFiletype() { + // null filetype + Assert.assertThrows(NullPointerException.class, () -> Filetype.isValidFiletype(null)); + + // invalid filetype + assertFalse(Filetype.isValidFiletype("")); // empty string + assertFalse(Filetype.isValidFiletype(" ")); // spaces only + assertFalse(Filetype.isValidFiletype("coconut")); // not "csv" or "vcf" + + // valid filetype + assertTrue(Filetype.isValidFiletype("csv")); + assertTrue(Filetype.isValidFiletype("vcf")); + } +} From 18538c759421d669202d4dbe26776515a59946e7 Mon Sep 17 00:00:00 2001 From: jitwei98 Date: Wed, 10 Oct 2018 00:01:45 +0800 Subject: [PATCH 10/10] ExportAllCommandTest.java: Rearrange declaration orders of variables --- .../seedu/address/logic/commands/ExportAllCommandTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/seedu/address/logic/commands/ExportAllCommandTest.java b/src/test/java/seedu/address/logic/commands/ExportAllCommandTest.java index 1f38257ad47c..5875b23bf0c1 100644 --- a/src/test/java/seedu/address/logic/commands/ExportAllCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/ExportAllCommandTest.java @@ -19,11 +19,11 @@ */ public class ExportAllCommandTest { - private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); - private static final String FILETYPE_CSV = "csv"; private static final String FILETYPE_VCF = "vcf"; + private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs()); + @Test public void execute() { assertCommandFailure(new ExportAllCommand(FILETYPE_CSV), model, new CommandHistory(),