Skip to content

Commit

Permalink
Merge pull request #45 from jitwei98/jitwei98/add-exportall-class
Browse files Browse the repository at this point in the history
Add a Filetype class and relevant tests
  • Loading branch information
linnnruoo committed Oct 9, 2018
2 parents f30be4a + 18538c7 commit bacd7f8
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 27 deletions.
12 changes: 4 additions & 8 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand All @@ -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.
Expand Down Expand Up @@ -127,7 +124,6 @@ public static Set<Tag> parseTags(Collection<String> 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.
Expand All @@ -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;
}
Expand Down
93 changes: 93 additions & 0 deletions src/main/java/seedu/address/model/Filetype.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
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\" or \"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].*";

/**
* Filetypes that can be used to export contacts.
*/
public enum Extension {
csv, vcf
}

private 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

/**
* Returns true if an {@code enumClass} contains a specific {@code value}.
* @param enumClass
* @param value
*/
private static <E extends Enum<E>> boolean contains(Class<E> 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +19,9 @@
*/
public class ExportAllCommandTest {

private static final String FILETYPE_CSV = "csv";
private static final String FILETYPE_VCF = "vcf";

private Model model = new ModelManager(getTypicalAddressBook(), new UserPrefs());

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,16 +15,16 @@ public class ExportAllCommandParserTest {

@Test
public void parseFiletypeSuccess() {
String userInput = FILETYPE_CSV;
final String userInput = "csv";
ExportAllCommand expectedCommand = new ExportAllCommand(userInput);
assertParseSuccess(parser, userInput, expectedCommand);
}

@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);
}

Expand Down
22 changes: 22 additions & 0 deletions src/test/java/seedu/address/logic/parser/ParserUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 "));
}
}
37 changes: 37 additions & 0 deletions src/test/java/seedu/address/model/FiletypeTest.java
Original file line number Diff line number Diff line change
@@ -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"));
}
}
11 changes: 0 additions & 11 deletions src/test/java/seedu/address/testutil/TypicalFiletypes.java

This file was deleted.

0 comments on commit bacd7f8

Please sign in to comment.