Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v1.4] Fixes and Test cases #182

Merged
merged 20 commits into from
Apr 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/java/seedu/address/commons/util/DrawTeethUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;

Expand All @@ -24,7 +25,7 @@ public static BufferedImage drawTeeth(int[] teeth) throws IOException {
String basepath = System.getProperty("user.dir");
try {
InputStream imageFile = MainApp.class.getClassLoader()
.getResourceAsStream("images/teeth/BaseLayer.png");
.getResourceAsStream("images" + File.separator + "teeth" + File.separator + "BaseLayer.png");
if (imageFile == null) {
throw new IOException();
}
Expand All @@ -37,7 +38,8 @@ public static BufferedImage drawTeeth(int[] teeth) throws IOException {
type = "A_";
}
InputStream layerFile = MainApp.class.getClassLoader()
.getResourceAsStream("images/teeth/" + type + (i + 1) + ".png");
.getResourceAsStream("images" + File.separator + "teeth" + File.separator
+ type + (i + 1) + ".png");
if (layerFile == null) {
throw new IOException();
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/seedu/address/logic/commands/OpenCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,12 @@ private void readFile(Model model) {

model.setAddressBook(openData);
}

/**
* For OpenCommandParserTest.
* @return file
*/
public File getFile() {
return file;
}
}
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/logic/commands/SaveCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.io.File;
import java.io.IOException;

import seedu.address.logic.CommandHistory;
Expand Down Expand Up @@ -65,4 +66,12 @@ private void writeFile(Model model) throws IOException {
throw new IOException(e.getMessage());
}
}

/**
* For SaveCommandParserTest.
* @return file
*/
public File getFile() {
return parsedInput.getFile();
}
}
38 changes: 25 additions & 13 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
public class ParserUtil {

public static final String MESSAGE_INVALID_INDEX = "Index is not a non-zero unsigned integer.";
public static final String MESSAGE_NOT_JSON_OR_PDF = "Input file type is not a .json or .pdf.";

/**
* Parses {@code oneBasedIndex} into an {@code Index} and returns it. Leading and trailing whitespaces will be
Expand Down Expand Up @@ -323,13 +324,18 @@ public static Set<Tag> parseTags(Collection<String> tags) throws ParseException
static ParsedInOut parseOpenSave(String filePath) throws ParseException {
requireNonNull(filePath);
filePath = filePath.trim();
String newPath = "data/";
String newPath = "data" + File.separator;

// Convert example/example.json to example\example.json
char[] pathArr = filePath.toCharArray();
for (int i = 0; i < filePath.length(); i++) {
// Convert example\example.json to example/example.json if the system prefers /
if (pathArr[i] == '\\') {
pathArr[i] = '/';
pathArr[i] = File.separator.toCharArray()[0];
continue;
}
// Convert example/example.json to example\example.json if the system prefers \
if (pathArr[i] == '/') {
pathArr[i] = File.separator.toCharArray()[0];
}
}
filePath = String.valueOf(pathArr);
Expand All @@ -344,7 +350,7 @@ static ParsedInOut parseOpenSave(String filePath) throws ParseException {
if (filePath.matches(pdfRegex)) {
return new ParsedInOut(file, "pdf");
} else {
throw new ParseException("Input file type is not a .json or .pdf.");
throw new ParseException(MESSAGE_NOT_JSON_OR_PDF);
}
}
}
Expand All @@ -356,15 +362,21 @@ static ParsedInOut parseOpenSave(String filePath) throws ParseException {
static ParsedInOut parseImportExport(String input) throws ParseException {
requireNonNull(input);
input = input.trim();
String newPath = "data/";
String filepath = "";
String fileType = "";
String newPath = "data" + File.separator;
String filepath;
String fileType;

// Convert example/example.json to example\example.json
char[] pathArr = input.toCharArray();
for (int i = 0; i < input.length(); i++) {
// Convert example\example.json to example/example.json if the system prefers /
if (pathArr[i] == '\\') {
pathArr[i] = '/';
pathArr[i] = File.separator.toCharArray()[0];
continue;
}
// Convert example/example.json to example\example.json if the system prefers \
if (pathArr[i] == '/') {
pathArr[i] = File.separator.toCharArray()[0];
}
}
input = String.valueOf(pathArr);
Expand All @@ -386,17 +398,17 @@ static ParsedInOut parseImportExport(String input) throws ParseException {
return new ParsedInOut(new File(filepath), fileType);
} else {
// This shouldn't be possible after validationRegex
throw new ParseException("Input file type is not a .json or .pdf.");
throw new ParseException(MESSAGE_NOT_JSON_OR_PDF);
}
} else {
throw new ParseException("Input file type is not a .json or .pdf.");
throw new ParseException(MESSAGE_NOT_JSON_OR_PDF);
}
}

// Parse for index range
final Pattern splitRegex = Pattern.compile("([\\w-/\\s.()]+)+\\.(json|pdf)+\\s?([0-9,-]*)?");
Matcher splitMatcher = splitRegex.matcher(input);
String indexRange = "";
String indexRange;

if (splitMatcher.find()) {
filepath = splitMatcher.group(1).concat(".");
Expand All @@ -406,7 +418,7 @@ static ParsedInOut parseImportExport(String input) throws ParseException {
indexRange = splitMatcher.group(3);
} else {
// This shouldn't be possible after validationRegex
throw new ParseException("Input file type is not a .json or .pdf.");
throw new ParseException(MESSAGE_NOT_JSON_OR_PDF);
}

HashSet<Integer> parsedIndex = new HashSet<>();
Expand All @@ -427,7 +439,7 @@ static ParsedInOut parseImportExport(String input) throws ParseException {
parsedIndex.add(i - 1);
}
} else {
throw new ParseException("Invalid index range!");
throw new ParseException("Invalid index range! Please input a non-zero unsigned index range.");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static java.util.Objects.requireNonNull;

import java.awt.image.BufferedImage;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
Expand Down Expand Up @@ -50,7 +52,7 @@ public class InOutAddressBookStorage implements AddressBookStorage {
private static final PDFont FONT = PDType1Font.HELVETICA;
private static final int FONT_SIZE = 12;
private static final int LINE_SPACING = 3;
private static final String TEETH_IMAGE_PATH = "images/tooth.png";
private static final String TEETH_IMAGE_PATH = "images" + File.separator + "tooth.png";

private static final Logger logger = LogsCenter.getLogger(InOutAddressBookStorage.class);

Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package seedu.address.logic.parser;

import static org.junit.Assert.assertEquals;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;

import java.io.File;
import java.io.IOException;

import org.junit.Test;

import seedu.address.logic.commands.OpenCommand;
import seedu.address.logic.parser.exceptions.ParseException;

/**
* As we are only doing white-box testing, our test cases do not cover path variations
* outside of the OpenCommand code. For example, inputs "records.json" and "records.txt" take the
* same path through the OpenCommand, and therefore we test only one of them.
* The path variation for those two cases occur inside the ParserUtil, and
* therefore should be covered by the ParserUtilTest.
*/
public class OpenCommandParserTest {

private OpenCommandParser parser = new OpenCommandParser();

@Test
public void parse_validArgs_returnsOpenCommand() throws IOException {
File test = new File("data" + File.separator + "records.json");
if (!test.exists()) {
try {
test.createNewFile();
} catch (IOException e) {
throw new IOException("Failed to create test file!");
}
}
try {
assertEquals(parser.parse(" records.json").getFile(), new OpenCommand(test).getFile());
} catch (ParseException pe) {
throw new IllegalArgumentException("Invalid userInput.", pe);
}
test.delete();
}

@Test
public void parse_invalidArgs_throwsParseException() {
assertParseFailure(parser, " records.txt",
ParserUtil.MESSAGE_NOT_JSON_OR_PDF + "\n" + OpenCommand.MESSAGE_USAGE);
}
}
Loading