From 31cc8a9f0bf16574fea9858af324d4ece8712eab Mon Sep 17 00:00:00 2001 From: Justin Tay Date: Fri, 4 Nov 2016 21:01:21 +0800 Subject: [PATCH] Change config filepath for tests (#195) * Change testapp config file path * Add method which creates temp config file in sandbox * Update TestApp and StorageManagerTest to use temp config file * Add new private variable to store config file path * Update MainApp, StorageManager and TestUtil to use the new variable --- src/main/java/seedu/agendum/MainApp.java | 1 + .../seedu/agendum/commons/core/Config.java | 9 +++ .../seedu/agendum/storage/StorageManager.java | 2 +- src/test/java/seedu/agendum/TestApp.java | 4 +- .../agendum/storage/StorageManagerTest.java | 6 +- .../java/seedu/agendum/testutil/TestUtil.java | 59 ++++++++++++++----- 6 files changed, 59 insertions(+), 22 deletions(-) diff --git a/src/main/java/seedu/agendum/MainApp.java b/src/main/java/seedu/agendum/MainApp.java index 2db9bc3e8a4e..c240b1a011d1 100644 --- a/src/main/java/seedu/agendum/MainApp.java +++ b/src/main/java/seedu/agendum/MainApp.java @@ -121,6 +121,7 @@ protected Config initConfig(String configFilePath) { //Update config file in case it was missing to begin with or there are new/unused fields try { + initializedConfig.setConfigFilePath(configFilePathUsed); ConfigUtil.saveConfig(initializedConfig, configFilePathUsed); } catch (IOException e) { logger.warning("Failed to save config file : " + StringUtil.getDetails(e)); diff --git a/src/main/java/seedu/agendum/commons/core/Config.java b/src/main/java/seedu/agendum/commons/core/Config.java index 7ea0ff247f93..22a5b31db408 100644 --- a/src/main/java/seedu/agendum/commons/core/Config.java +++ b/src/main/java/seedu/agendum/commons/core/Config.java @@ -21,6 +21,7 @@ public class Config { private String aliasTableFilePath = DEFAULT_ALIAS_TABLE_FILE; private String userPrefsFilePath = DEFAULT_USER_PREFS_FILE; private String toDoListFilePath = DEFAULT_SAVE_LOCATION; + private String configFilePath = DEFAULT_CONFIG_FILE; private String toDoListName = "MyToDoList"; public String getAppTitle() { @@ -70,6 +71,14 @@ public String getToDoListName() { public void setToDoListName(String toDoListName) { this.toDoListName = toDoListName; } + + public String getConfigFilePath() { + return configFilePath; + } + + public void setConfigFilePath(String configFilePath) { + this.configFilePath = configFilePath; + } @Override diff --git a/src/main/java/seedu/agendum/storage/StorageManager.java b/src/main/java/seedu/agendum/storage/StorageManager.java index 253ffc24dbab..98432ee02e34 100644 --- a/src/main/java/seedu/agendum/storage/StorageManager.java +++ b/src/main/java/seedu/agendum/storage/StorageManager.java @@ -114,7 +114,7 @@ public void setToDoListFilePath(String filePath){ private void saveConfigFile() { try { - ConfigUtil.saveConfig(config, Config.DEFAULT_CONFIG_FILE); + ConfigUtil.saveConfig(config, config.getConfigFilePath()); } catch (IOException e) { logger.warning("Failed to save config file : " + StringUtil.getDetails(e)); } diff --git a/src/test/java/seedu/agendum/TestApp.java b/src/test/java/seedu/agendum/TestApp.java index e842b5b595e3..9e4ddd1dfe42 100644 --- a/src/test/java/seedu/agendum/TestApp.java +++ b/src/test/java/seedu/agendum/TestApp.java @@ -18,7 +18,6 @@ public class TestApp extends MainApp { public static final String SAVE_LOCATION_FOR_TESTING = TestUtil.getFilePathInSandboxFolder("sampleData.xml"); - protected static final String DEFAULT_PREF_FILE_LOCATION_FOR_TESTING = TestUtil.getFilePathInSandboxFolder("pref_testing.json"); public static final String APP_TITLE = "Test App"; protected static final String TO_DO_LIST_NAME = "Test"; protected Supplier initialDataSupplier = () -> null; @@ -42,10 +41,9 @@ public TestApp(Supplier initialDataSupplier, String saveFileLo @Override protected Config initConfig(String configFilePath) { - Config config = super.initConfig(configFilePath); + Config config = TestUtil.createTempConfig(); config.setAppTitle(APP_TITLE); config.setToDoListFilePath(saveFileLocation); - config.setUserPrefsFilePath(DEFAULT_PREF_FILE_LOCATION_FOR_TESTING); config.setToDoListName(TO_DO_LIST_NAME); return config; } diff --git a/src/test/java/seedu/agendum/storage/StorageManagerTest.java b/src/test/java/seedu/agendum/storage/StorageManagerTest.java index 2152fae6c326..025223e1e3a4 100644 --- a/src/test/java/seedu/agendum/storage/StorageManagerTest.java +++ b/src/test/java/seedu/agendum/storage/StorageManagerTest.java @@ -15,7 +15,6 @@ import org.junit.Test; import org.junit.rules.TemporaryFolder; -import seedu.agendum.commons.core.Config; import seedu.agendum.commons.events.model.ChangeSaveLocationEvent; import seedu.agendum.commons.events.model.LoadDataRequestEvent; import seedu.agendum.commons.events.model.ToDoListChangedEvent; @@ -28,6 +27,7 @@ import seedu.agendum.model.ToDoList; import seedu.agendum.model.UserPrefs; import seedu.agendum.testutil.EventsCollector; +import seedu.agendum.testutil.TestUtil; import seedu.agendum.testutil.TypicalTestTasks; public class StorageManagerTest { @@ -40,7 +40,7 @@ public class StorageManagerTest { @Before public void setup() { storageManager = new StorageManager(getTempFilePath("ab"), getTempFilePath("command"), - getTempFilePath("prefs"), new Config()); + getTempFilePath("prefs"), TestUtil.createTempConfig()); } @@ -95,7 +95,7 @@ public void getToDoListFilePath(){ public void handleToDoListChangedEventExceptionThrownEventRaised() throws IOException { //Create a StorageManager while injecting a stub that throws an exception when the save method is called Storage storage = new StorageManager(new XmlToDoListStorageExceptionThrowingStub("dummy"), - new JsonAliasTableStorage("dummy"), new JsonUserPrefsStorage("dummy"), new Config()); + new JsonAliasTableStorage("dummy"), new JsonUserPrefsStorage("dummy"), TestUtil.createTempConfig()); EventsCollector eventCollector = new EventsCollector(); storage.handleToDoListChangedEvent(new ToDoListChangedEvent(new ToDoList())); assertTrue(eventCollector.get(0) instanceof DataSavingExceptionEvent); diff --git a/src/test/java/seedu/agendum/testutil/TestUtil.java b/src/test/java/seedu/agendum/testutil/TestUtil.java index 593f1dd19359..9a2f76ffe9b5 100644 --- a/src/test/java/seedu/agendum/testutil/TestUtil.java +++ b/src/test/java/seedu/agendum/testutil/TestUtil.java @@ -1,6 +1,22 @@ package seedu.agendum.testutil; +import java.io.File; +import java.io.IOException; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.TimeoutException; +import java.util.stream.Collectors; + +import org.loadui.testfx.GuiTest; +import org.testfx.api.FxToolkit; + import com.google.common.io.Files; + import guitests.guihandles.TaskCardHandle; import javafx.geometry.Bounds; import javafx.geometry.Point2D; @@ -10,28 +26,20 @@ import javafx.scene.input.KeyCodeCombination; import javafx.scene.input.KeyCombination; import junit.framework.AssertionFailedError; -import org.loadui.testfx.GuiTest; -import org.testfx.api.FxToolkit; import seedu.agendum.TestApp; +import seedu.agendum.commons.core.Config; import seedu.agendum.commons.exceptions.IllegalValueException; +import seedu.agendum.commons.util.ConfigUtil; import seedu.agendum.commons.util.FileUtil; +import seedu.agendum.commons.util.StringUtil; import seedu.agendum.commons.util.XmlUtil; import seedu.agendum.model.ToDoList; -import seedu.agendum.model.task.*; +import seedu.agendum.model.task.Name; +import seedu.agendum.model.task.ReadOnlyTask; +import seedu.agendum.model.task.Task; +import seedu.agendum.model.task.UniqueTaskList; import seedu.agendum.storage.XmlSerializableToDoList; -import java.io.File; -import java.io.IOException; -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.concurrent.TimeoutException; -import java.util.stream.Collectors; - /** * A utility class for test cases. */ @@ -348,5 +356,26 @@ public static TestTask[] getDoneTasks(TestTask[] tasks) { public static boolean compareCardAndTask(TaskCardHandle card, ReadOnlyTask task) { return card.isSameTask(task); } + + public static Config createTempConfig() { + Config config = new Config(); + final String DEFAULT_CONFIG_FILE_LOCATION_FOR_TESTING = getFilePathInSandboxFolder("config_testing.json"); + final String DEFAULT_PREF_FILE_LOCATION_FOR_TESTING = TestUtil.getFilePathInSandboxFolder("pref_testing.json"); + final String DEFAULT_TODOLIST_FILE_LOCATION_FOR_TESTING = TestUtil.getFilePathInSandboxFolder("todolist_testing.xml"); + final String DEFAULT_ALIAS_TABLE_FILE_LOCATION_FOR_TESTING = TestUtil.getFilePathInSandboxFolder("todolist_testing.xml"); + + config.setAppTitle(TestApp.APP_TITLE); + config.setUserPrefsFilePath(DEFAULT_PREF_FILE_LOCATION_FOR_TESTING); + config.setToDoListFilePath(DEFAULT_TODOLIST_FILE_LOCATION_FOR_TESTING); + config.setAliasTableFilePath(DEFAULT_ALIAS_TABLE_FILE_LOCATION_FOR_TESTING); + config.setConfigFilePath(DEFAULT_CONFIG_FILE_LOCATION_FOR_TESTING); + + try { + ConfigUtil.saveConfig(config, DEFAULT_CONFIG_FILE_LOCATION_FOR_TESTING); + } catch (IOException e) { + System.out.println("Failed to save config file : " + StringUtil.getDetails(e)); + } + return config; + } }