Skip to content

Commit

Permalink
Merge pull request #158 from JonathanLeeWH/update-export-command
Browse files Browse the repository at this point in the history
Update Junit test cases for export command
  • Loading branch information
JonathanLeeWH authored Apr 4, 2019
2 parents a3bd6ef + 1e62114 commit c6c330e
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 14 deletions.
8 changes: 4 additions & 4 deletions docs/team/jonathanleewh.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ a|WARNING: {sp}
* *Major enhancement*: Added *`export` command support*
** What it does: Allows the user to export the current medicine inventory data shown in the Graphical User Interface (GUI) to a spreadsheet file which is supported by commonly used spreadsheet applications such as Microsoft Excel.
** Justification: Allows the user to save the data so that it can be printed, organised or for advanced data analysis using a spreadsheet application such as Microsoft Excel. This is especially useful for the user, superior or supplier's reference.
** Highlights: This enhancement requires an indepth understanding of how each major components work and the methods available as it involves retrieving of data from the GUI and parsing the retrieved data. Despite using the Opencsv library, the implementation was still challenging as it required using methods from different components, parsing and organising the data so as that the exported file contains only the essential information for easier reference.
** Highlights: This enhancement requires an indepth understanding of how each major components work and the methods available as it involves retrieving of data from the GUI and parsing the retrieved data. Despite using the Opencsv library, the implementation was still challenging as it required using methods from different components, parsing and organising the data such that the exported file contains only the essential information for easier reference.
** Credits: link:http://opencsv.sourceforge.net/team.html[Opencsv Team]

* *Minor enhancement*: As there are currently no third party libraries available which validates file name such that it adheres to the file naming conventions of the major operating systems (OS), I have created a `FileName` class which implements this feature to complement the `export` feature and is also used by the 'label' feature. It requires an indepth knowledge on the different file naming conventions of the various OS and also numerous testing as I discovered that some file name conventions were not documented and not allowed to be used as file names.
* *Minor enhancement*: As there are currently no third party libraries available which validates file name such that it adheres to the file naming conventions of the major operating systems (OS), I have created a `FileName` class which implements this feature to complement the `export` feature and is also used by the `label` feature. It requires an indepth knowledge on the different file naming conventions of the various OS and also numerous testing as I discovered that some file name conventions were not documented and not allowed to be used as file names.

* *Code contributed*: https://nus-cs2103-ay1819s2.github.io/cs2103-dashboard/#=undefined&search=jonathanleewh[Project Code Dashboard]

Expand All @@ -48,11 +48,11 @@ a|WARNING: {sp}
*** Set up team project Github repository including setting up issue tracker, permissions, project page, milestones and labels.
*** Managed releases `v1.1` - `v1.4` (6 releases) on GitHub
** Documentation:
*** Added Introduction section to both User Guide and Developer Guide: https://github.com/CS2103-AY1819S2-T12-3/main/pull/115[#115], https://github.com/CS2103-AY1819S2-T12-3/main/pull/140[#140]
*** Added Introduction sections to both User Guide and Developer Guide: https://github.com/CS2103-AY1819S2-T12-3/main/pull/115[#115], https://github.com/CS2103-AY1819S2-T12-3/main/pull/140[#140]
*** Added Frequently Asked Question (FAQ) and File Naming Convention sections to User Guide: https://github.com/CS2103-AY1819S2-T12-3/main/pull/123[#123], https://github.com/CS2103-AY1819S2-T12-3/main/pull/135[#135]
*** Added Validation of File Name section to Developer Guide which includes code snippet for reference on how to take advantage of the `FileName` class provided: https://github.com/CS2103-AY1819S2-T12-3/main/pull/140[#140]
*** Update stylesheet to PDF printing friendly format: https://github.com/CS2103-AY1819S2-T12-3/main/pull/150[#150]
*** Updated User Guide's Quick Start section and Developer Guide's Setting Up section to only allow Java version `9` and JDK `9` including providing references to the reason for the change: https://github.com/CS2103-AY1819S2-T12-3/main/pull/117[#117], https://github.com/CS2103-AY1819S2-T12-3/main/pull/123[#123]
*** Updated User Guide's Quick Start section and Developer Guide's Setting Up section to only allow Java version `9` and JDK `9` including references to the reason for the change: https://github.com/CS2103-AY1819S2-T12-3/main/pull/117[#117], https://github.com/CS2103-AY1819S2-T12-3/main/pull/123[#123]
*** Updated User Guide Quick Start section with an additional step on how users can seek assistance: https://github.com/CS2103-AY1819S2-T12-3/main/pull/115[#115]
*** Updated Storage component class diagram of Developer Guide to match our project: https://github.com/CS2103-AY1819S2-T12-3/main/pull/79[#79]
** Community:
Expand Down
1 change: 1 addition & 0 deletions src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public class Messages {
public static final String MESSAGE_MEDICINES_LISTED_OVERVIEW = "%1$d medicines listed!";
public static final String MESSAGE_SHOW_CURRENT_THRESHOLDS = "Expiry threshold: %1$d days\n"
+ "Low stock threshold: %2$d";
public static final String MESSAGE_INVALID_COMMAND_FORMAT_DETAILED = "Invalid command format! %1$s\n%2$s";

}
5 changes: 5 additions & 0 deletions src/main/java/seedu/address/commons/util/csv/CsvWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ private void writeDataToCsv(List currentGuiList, List lowQuantityMedicineList) t
* @throws CommandException if there are errors creating the export directory.
*/
private void createIfExportDirectoryMissing() throws CommandException {
if (Files.isDirectory(DEFAULT_EXPORT_FOLDER_PATH) == false && Files.exists(DEFAULT_EXPORT_FOLDER_PATH)) {
throw new CommandException(FILE_OPS_ERROR_MESSAGE + "an \"" + DEFAULT_EXPORT_FOLDER_NAME + "\" file without"
+ " any file extension already exists and it is not a directory. "
+ "Please remove it in order for the \"exported\" directory to be created.");
}
if (Files.isDirectory(DEFAULT_EXPORT_FOLDER_PATH) == false) {
try {
Files.createDirectory(DEFAULT_EXPORT_FOLDER_PATH);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT_DETAILED;

import seedu.address.commons.util.FileName;
import seedu.address.logic.commands.ExportCommand;
Expand All @@ -22,7 +22,8 @@ public ExportCommand parse(String args) throws ParseException {
return new ExportCommand(fileName);
} catch (ParseException pe) {
throw new ParseException(
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportCommand.MESSAGE_USAGE), pe);
String.format(MESSAGE_INVALID_COMMAND_FORMAT_DETAILED, pe.getMessage(),
ExportCommand.MESSAGE_USAGE), pe);
}
}
}
1 change: 1 addition & 0 deletions src/main/java/seedu/address/ui/ResultDisplay.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public ResultDisplay() {
public void setFeedbackToUser(String feedbackToUser) {
requireNonNull(feedbackToUser);
resultDisplay.setText(feedbackToUser);
resultDisplay.setWrapText(true);
}

}
36 changes: 36 additions & 0 deletions src/test/java/seedu/address/logic/commands/ExportCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,42 @@ public void execute_csvFileInformationMatchesData_success() throws Exception {
String expectedMessage = String.format(ExportCommand.MESSAGE_SUCCESS, fileNameWithoutFileExtension);

assertCommandSuccess(exportCommand, model, commandHistory, expectedMessage, model);
compareActualAndExpectedData(filePath);
}

/**
* Compares actual and expected data for the exported Csv file.
* Note: This version did not take advantage of JUnit TemporaryFolder method
* and instead uses manual checking using File exists() method and File delete() method
* as I am still finding the best way to implement it as unlike the usual usage of TemporaryFolder method
* in this case, the method I am testing includes creating its own default export folder which users
* will be using and it also creates its own csv file when the method which is being tested is executed
* so I am trying to find the best way to take advantage of the Junit TemporaryFolder method
* without accidentally deleting any of the existing data which the user might have exported
* before running tests which might be crucial data though in practice usually when running tests
* you should not have crucial data but this is an additional safety precaution I decide to implement
* in case the developers do have crucial export data which they might accidentally did not back up before testing
* There are two alternatives I can think of to solve this issue:
* 1) Update CsvWrapper.java to support either a test situation (depending on how it is implemented
* it still can simulate real world scenario without affecting testing the main functionality) basically
* if the test flag is enabled it will allow use of custom export directory and custom csv file
* 2) I could just update CsvWrapper.java to support allowing custom export directory but that is actually
* planned for v2.0 and beyond as that is nice to have and not top priority as of now so in order to minimise
* any potential regression bugs, option 1 would be a better choice if compared to this option as option 1 hides
* the custom export directory option from users and only allow it to use by developers or to be more precise
* only use for testing purposes (abstraction and information hiding concepts)
* 3) Create the JUnit TemporaryFolder within the default exported directory
* and move the temporary created csv file to that folder within the default exported directory
* so that both will be deleted when each test case complete its execution (Might be possible
* and still researching though it might be shift to Milestone v2.0 and beyond depending on availability of time
* as there might be higher priority tasks)
* The advantage of using JUnit TemporaryFolder is it allows cleaner code without having
* to manually delete the temporary file manually using File delete() method as it will be deleted
* after each test method has executed
* @param filePath The actual csv file to be compared with the expected data.
* @throws IOException If the reading of the data from the exported Csv file encounters an exception.
*/
private void compareActualAndExpectedData(File filePath) throws IOException {
try (CSVReader reader = new CSVReader(new FileReader(filePath))) {
String[] expectedData;
String[] actualData;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package seedu.address.logic.parser;

import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT_DETAILED;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseFailure;
import static seedu.address.logic.parser.CommandParserTestUtil.assertParseSuccess;

Expand Down Expand Up @@ -38,24 +38,31 @@ public void parse_optionalFileNameFieldsMissing_success() {
public void parse_invalidValue_failure() {
// invalid file name (hyphen only)
assertParseFailure(parser, "-",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportCommand.MESSAGE_USAGE));
String.format(MESSAGE_INVALID_COMMAND_FORMAT_DETAILED, FileName.MESSAGE_CONSTRAINTS,
ExportCommand.MESSAGE_USAGE));
// invalid file name (underscore only)
assertParseFailure(parser, "_",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportCommand.MESSAGE_USAGE));
String.format(MESSAGE_INVALID_COMMAND_FORMAT_DETAILED, FileName.MESSAGE_CONSTRAINTS,
ExportCommand.MESSAGE_USAGE));
// invalid file name (only not supported non-alphanumeric characters)
assertParseFailure(parser, "*",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportCommand.MESSAGE_USAGE));
String.format(MESSAGE_INVALID_COMMAND_FORMAT_DETAILED, FileName.MESSAGE_CONSTRAINTS,
ExportCommand.MESSAGE_USAGE));
// invalid file name (contains not supported non-alphanumeric characters)
assertParseFailure(parser, "example*",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportCommand.MESSAGE_USAGE));
String.format(MESSAGE_INVALID_COMMAND_FORMAT_DETAILED, FileName.MESSAGE_CONSTRAINTS,
ExportCommand.MESSAGE_USAGE));
// invalid file name (alphabets with a space only)
assertParseFailure(parser, "example record",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportCommand.MESSAGE_USAGE));
String.format(MESSAGE_INVALID_COMMAND_FORMAT_DETAILED, FileName.MESSAGE_CONSTRAINTS,
ExportCommand.MESSAGE_USAGE));
// invalid file name (alphabets with a file format)
assertParseFailure(parser, "exampleRecord.csv",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportCommand.MESSAGE_USAGE));
String.format(MESSAGE_INVALID_COMMAND_FORMAT_DETAILED, FileName.MESSAGE_CONSTRAINTS,
ExportCommand.MESSAGE_USAGE));
// invalid file name (file format)
assertParseFailure(parser, ".csv",
String.format(MESSAGE_INVALID_COMMAND_FORMAT, ExportCommand.MESSAGE_USAGE));
String.format(MESSAGE_INVALID_COMMAND_FORMAT_DETAILED, FileName.MESSAGE_CONSTRAINTS,
ExportCommand.MESSAGE_USAGE));
}
}

0 comments on commit c6c330e

Please sign in to comment.