Skip to content

Commit

Permalink
Merge 99f3ce9 into 5986140
Browse files Browse the repository at this point in the history
  • Loading branch information
irvinlim committed Nov 6, 2016
2 parents 5986140 + 99f3ce9 commit 9c13d95
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 64 deletions.
118 changes: 55 additions & 63 deletions src/main/java/seedu/todo/controllers/ConfigController.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package seedu.todo.controllers;

import java.io.IOException;
import java.util.List;

import seedu.todo.commons.core.Config;
import seedu.todo.commons.core.ConfigCenter;
Expand All @@ -10,12 +9,11 @@
import seedu.todo.models.TodoListDB;
import seedu.todo.ui.UiManager;

// @@author A0139812A
/**
* Controller to configure app settings.
* Has side effects, since it has to perform
* updates on the UI or file sources on update.
*
* @@author A0139812A
*/
public class ConfigController extends Controller {

Expand All @@ -26,12 +24,14 @@ public class ConfigController extends Controller {

private static final String MESSAGE_SHOWING = "Showing all settings.";
private static final String MESSAGE_SUCCESS = "Successfully updated %s.";
private static final String MESSAGE_FAILURE = "Could not update settings: %s";
public static final String MESSAGE_FAILURE = "Could not update settings: %s";
private static final String MESSAGE_INVALID_INPUT = "Invalid config setting provided!";
private static final String MESSAGE_WRONG_EXTENSION = "Could not change storage path: File must end with %s";
private static final String SPACE = " ";
public static final String MESSAGE_WRONG_EXTENSION = "Could not change storage path: File must end with %s";
public static final String TEMPLATE_SET_CONFIG = "config <setting> <value>";

private static final String STRING_SPACE = " ";
private static final int ARGS_LENGTH = 2;
private static final String DB_FILE_EXTENSION = ".json";
public static final String DB_FILE_EXTENSION = ".json";

private static CommandDefinition commandDefinition =
new CommandDefinition(NAME, DESCRIPTION, COMMAND_SYNTAX, COMMAND_KEYWORD);
Expand All @@ -45,58 +45,42 @@ public CommandDefinition getCommandDefinition() {
public void process(String input) {
String params = input.replaceFirst("config", "").trim();

// Check for basic command.
if (params.length() <= 0) {

// Showing all configs
Renderer.renderConfig(MESSAGE_SHOWING);
return;
}

// Check args length
String[] args = params.split(STRING_SPACE, ARGS_LENGTH);
if (args.length != ARGS_LENGTH) {
Renderer.renderDisambiguation(TEMPLATE_SET_CONFIG, MESSAGE_INVALID_INPUT);
return;
}

String configName = args[0];
String configValue = args[1];
Config config = ConfigCenter.getInstance().getConfig();

// Check if configName is a valid name.
if (!config.getDefinitionsNames().contains(configName)) {
Renderer.renderDisambiguation(TEMPLATE_SET_CONFIG, MESSAGE_INVALID_INPUT);
return;
}

} else {

String[] args = params.split(SPACE, ARGS_LENGTH);

// Check args length
if (args.length != ARGS_LENGTH) {
Renderer.renderConfig(MESSAGE_INVALID_INPUT);
return;
}

assert args.length == ARGS_LENGTH;

// Split by args
String configName = args[0];
String configValue = args[1];

// Get current config
Config config = ConfigCenter.getInstance().getConfig();

// Check name
List<String> validConfigDefinitions = config.getDefinitionsNames();
if (!validConfigDefinitions.contains(configName)) {
Renderer.renderConfig(MESSAGE_INVALID_INPUT);
return;
}

assert validConfigDefinitions.contains(configName);

try {
// Update config value
try {
config = updateConfigByName(config, configName, configValue);
} catch (CannotConfigureException e) {
Renderer.renderConfig(e.getMessage());
return;
}

config = updateConfigByName(config, configName, configValue);

// Save config to file
try {
ConfigCenter.getInstance().saveConfig(config);
} catch (IOException e) {
Renderer.renderConfig(String.format(MESSAGE_FAILURE, e.getMessage()));
return;
}

// Update console for success
Renderer.renderConfig(String.format(MESSAGE_SUCCESS, configName));
ConfigCenter.getInstance().saveConfig(config);
} catch (CannotConfigureException | IOException e) {
Renderer.renderConfig(String.format(MESSAGE_FAILURE, e.getMessage()));
return;
}

// Update console for success
Renderer.renderConfig(String.format(MESSAGE_SUCCESS, configName));
}

/**
Expand All @@ -121,17 +105,8 @@ private Config updateConfigByName(Config config, String configName, String confi
break;

case "databaseFilePath" :
// Make sure the new path has a .json extension
if (!configValue.endsWith(DB_FILE_EXTENSION)) {
throw new CannotConfigureException(String.format(MESSAGE_WRONG_EXTENSION, DB_FILE_EXTENSION));
}

// Move the DB file to the new location
try {
TodoListDB.getInstance().move(configValue);
} catch (IOException e) {
throw new CannotConfigureException(e.getMessage());
}
moveDatabaseFile(configValue);

// Update config
config.setDatabaseFilePath(configValue);
Expand All @@ -144,5 +119,22 @@ private Config updateConfigByName(Config config, String configName, String confi

return config;
}

/**
* Moves the database file to the new location.
* Throws an exception if the new path does not exist, or if it has the wrong extension.
*/
private void moveDatabaseFile(String newPath) throws CannotConfigureException {
// Make sure the new path has a .json extension
if (!newPath.endsWith(DB_FILE_EXTENSION)) {
throw new CannotConfigureException(String.format(MESSAGE_WRONG_EXTENSION, DB_FILE_EXTENSION));
}

try {
TodoListDB.getInstance().move(newPath);
} catch (IOException e) {
throw new CannotConfigureException(e.getMessage());
}
}

}
2 changes: 1 addition & 1 deletion src/main/resources/ui/components/ConfigItem.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<?import javafx.scene.text.Text?>

<!-- @@author A0139812A -->
<VBox styleClass="tasklist-dateitem" maxHeight="Infinity" maxWidth="Infinity" minHeight="-Infinity" minWidth="-Infinity"
<VBox fx:id="configItem" styleClass="tasklist-dateitem" maxHeight="Infinity" maxWidth="Infinity" minHeight="-Infinity" minWidth="-Infinity"
xmlns="http://javafx.com/javafx/8.0.65" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="seedu.todo.ui.components.ConfigItem">
<children>
Expand Down
64 changes: 64 additions & 0 deletions src/test/java/seedu/todo/guitests/ConfigCommandTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package seedu.todo.guitests;

import static org.junit.Assert.*;

import java.io.File;

import org.junit.Test;

import seedu.todo.TestApp;
import seedu.todo.commons.core.ConfigCenter;
import seedu.todo.commons.core.ConfigDefinition;
import seedu.todo.controllers.ConfigController;

public class ConfigCommandTest extends GuiTest {

@Test
public void config_showAll_success() {
console.runCommand("config");
for (ConfigDefinition configDefinition : ConfigCenter.getInstance().getConfig().getDefinitions()) {
assertNotNull(configView.getConfigItem(configDefinition));
}
}

@Test
public void config_invalidConfigName_disambig() {
console.runCommand("config invalidConfigName someValue");
assertEquals(ConfigController.TEMPLATE_SET_CONFIG, console.getConsoleInputText());
}

@Test
public void config_tooLittleArgs_disambig() {
console.runCommand("config appTitle");
assertEquals(ConfigController.TEMPLATE_SET_CONFIG, console.getConsoleInputText());
}

@Test
public void config_setAppTitle_success() {
console.runCommand("config appTitle Pokemon Center");
assertEquals("Pokemon Center", header.getAppTitle());
assertEquals("Pokemon Center", ConfigCenter.getInstance().getConfig().getAppTitle());
}

@Test
public void config_setDatabaseFilePath_success() {
console.runCommand("config databaseFilePath databaseMoved.json");
assertEquals("databaseMoved.json", ConfigCenter.getInstance().getConfig().getDatabaseFilePath());

boolean isFileExists = new File("databaseMoved.json").exists();
if (isFileExists) {
new File("databaseMoved.json").delete();
}

assertTrue(isFileExists);
}

@Test
public void configDatabaseFilePath_noJsonExtension_error() {
console.runCommand("config databaseFilePath databaseMoved.txt");
assertEquals(TestApp.SAVE_LOCATION_FOR_TESTING, ConfigCenter.getInstance().getConfig().getDatabaseFilePath());
assertEquals(String.format(ConfigController.MESSAGE_FAILURE, String.format(ConfigController.MESSAGE_WRONG_EXTENSION, ".json")),
console.getConsoleTextArea());
}

}
6 changes: 6 additions & 0 deletions src/test/java/seedu/todo/guitests/GuiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
import seedu.todo.commons.events.BaseEvent;
import seedu.todo.commons.util.DateUtil;
import seedu.todo.guitests.guihandles.AliasViewHandle;
import seedu.todo.guitests.guihandles.ConfigViewHandle;
import seedu.todo.guitests.guihandles.ConsoleHandle;
import seedu.todo.guitests.guihandles.HeaderHandle;
import seedu.todo.guitests.guihandles.HelpViewHandle;
import seedu.todo.guitests.guihandles.MainGuiHandle;
import seedu.todo.guitests.guihandles.TagListHandle;
Expand All @@ -45,9 +47,11 @@ public abstract class GuiTest {
// Handles to GUI elements present at the start up are created in advance for easy access from child classes.
protected MainGuiHandle mainGui;
protected ConsoleHandle console;
protected HeaderHandle header;
protected TaskListHandle taskList;
protected TagListHandle tagList;
protected AliasViewHandle aliasView;
protected ConfigViewHandle configView;
protected HelpViewHandle helpView;

private Stage stage;
Expand All @@ -67,9 +71,11 @@ public void setup() throws Exception {
FxToolkit.setupStage((stage) -> {
mainGui = new MainGuiHandle(new GuiRobot(), stage);
console = mainGui.getConsole();
header = mainGui.getHeader();
taskList = mainGui.getTaskList();
tagList = mainGui.getTagList();
aliasView = mainGui.getAliasView();
configView = mainGui.getConfigView();
helpView = mainGui.getHelpView();
// TODO: create handles for other components
this.stage = stage;
Expand Down
39 changes: 39 additions & 0 deletions src/test/java/seedu/todo/guitests/guihandles/ConfigItemHandle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package seedu.todo.guitests.guihandles;

import javafx.scene.Node;
import javafx.stage.Stage;
import seedu.todo.commons.core.ConfigDefinition;
import seedu.todo.guitests.GuiRobot;

// @@author A0139812A
public class ConfigItemHandle extends GuiHandle {

private static final String CONFIG_NAME_TEXT_ID = "#configName";
private static final String CONFIG_DESC_TEXT_ID = "#configDescription";
private static final String CONFIG_VALUE_TEXT_ID = "#configValue";
private Node node;

public ConfigItemHandle(GuiRobot guiRobot, Stage primaryStage, Node node){
super(guiRobot, primaryStage, null);
this.node = node;
}

public String getConfigName() {
return getStringFromText(CONFIG_NAME_TEXT_ID, node);
}

public String getConfigDescription() {
return getStringFromText(CONFIG_DESC_TEXT_ID, node);
}

public String getConfigValue() {
return getStringFromText(CONFIG_VALUE_TEXT_ID, node);
}

public boolean isEqualsTo(ConfigDefinition configDefinition) {
return getConfigName().equals(configDefinition.getConfigName())
&& getConfigDescription().equals(configDefinition.getConfigDescription())
&& getConfigValue().equals(configDefinition.getConfigValue());
}

}
40 changes: 40 additions & 0 deletions src/test/java/seedu/todo/guitests/guihandles/ConfigViewHandle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@

package seedu.todo.guitests.guihandles;

import java.util.Optional;

import javafx.scene.Node;
import javafx.stage.Stage;
import seedu.todo.commons.core.ConfigDefinition;
import seedu.todo.guitests.GuiRobot;

// @@author A0139812A
public class ConfigViewHandle extends GuiHandle {

private static final String CONFIGVIEW_TEXT_ID = "#configInstructionsText";
private static final String CONFIG_ITEM_ID = "#configItem";

public ConfigViewHandle(GuiRobot guiRobot, Stage primaryStage, String stageTitle) {
super(guiRobot, primaryStage, stageTitle);
}

/**
* Checks for the existence of a child element to determine if the view has loaded correctly.
*/
public boolean hasLoaded() {
return guiRobot.lookup(CONFIGVIEW_TEXT_ID).queryAll().size() > 0;
}

public ConfigItemHandle getConfigItem(ConfigDefinition configDefinition) {
Optional<Node> itemNode = guiRobot.lookup(CONFIG_ITEM_ID).queryAll().stream()
.filter(node -> new ConfigItemHandle(guiRobot, primaryStage, node).isEqualsTo(configDefinition))
.findFirst();

if (itemNode.isPresent()) {
return new ConfigItemHandle(guiRobot, primaryStage, itemNode.get());
} else {
return null;
}
}

}
18 changes: 18 additions & 0 deletions src/test/java/seedu/todo/guitests/guihandles/HeaderHandle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package seedu.todo.guitests.guihandles;

import javafx.stage.Stage;
import seedu.todo.guitests.GuiRobot;

public class HeaderHandle extends GuiHandle {

private static final String HEADER_APPTITLE_TEXT_ID = "#headerAppTitle";

public HeaderHandle(GuiRobot guiRobot, Stage primaryStage, String stageTitle) {
super(guiRobot, primaryStage, stageTitle);
}

public String getAppTitle() {
return getStringFromText(HEADER_APPTITLE_TEXT_ID);
}

}
Loading

0 comments on commit 9c13d95

Please sign in to comment.