package seedu.address.storage;


import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import seedu.address.commons.events.model.AddressBookChangedEvent;
import seedu.address.commons.events.storage.DataSavingExceptionEvent;
import seedu.address.model.AddressBook;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.UserPrefs;
import seedu.address.testutil.EventsCollector;
import seedu.address.testutil.TypicalTestPersons;

public class StorageManagerTest {

    private StorageManager storageManager;

    @Rule
    public TemporaryFolder testFolder = new TemporaryFolder();


    @Before
    public void setUp() {
        storageManager = new StorageManager(getTempFilePath("ab"), getTempFilePath("prefs"));
    }


    private String getTempFilePath(String fileName) {
        return testFolder.getRoot().getPath() + fileName;
    }


    @Test
    public void prefsReadSave() throws Exception {
        /*
         * Note: This is an integration test that verifies the StorageManager is properly wired to the
         * {@link JsonUserPrefsStorage} class.
         * More extensive testing of UserPref saving/reading is done in {@link JsonUserPrefsStorageTest} class.
         */
        UserPrefs original = new UserPrefs();
        original.setGuiSettings(300, 600, 4, 6);
        storageManager.saveUserPrefs(original);
        UserPrefs retrieved = storageManager.readUserPrefs().get();
        assertEquals(original, retrieved);
    }

    @Test
    public void addressBookReadSave() throws Exception {
        /*
         * Note: This is an integration test that verifies the StorageManager is properly wired to the
         * {@link XmlAddressBookStorage} class.
         * More extensive testing of UserPref saving/reading is done in {@link XmlAddressBookStorageTest} class.
         */
        AddressBook original = new TypicalTestPersons().getTypicalAddressBook();
        storageManager.saveAddressBook(original);
        ReadOnlyAddressBook retrieved = storageManager.readAddressBook().get();
        assertEquals(original, new AddressBook(retrieved));
    }

    @Test
    public void getAddressBookFilePath() {
        assertNotNull(storageManager.getAddressBookFilePath());
    }

    @Test
    public void handleAddressBookChangedEvent_exceptionThrown_eventRaised() throws IOException {
        // Create a StorageManager while injecting a stub that  throws an exception when the save method is called
        Storage storage = new StorageManager(new XmlAddressBookStorageExceptionThrowingStub("dummy"),
                                             new JsonUserPrefsStorage("dummy"));
        EventsCollector eventCollector = new EventsCollector();
        storage.handleAddressBookChangedEvent(new AddressBookChangedEvent(new AddressBook()));
        assertTrue(eventCollector.get(0) instanceof DataSavingExceptionEvent);
    }


    /**
     * A Stub class to throw an exception when the save method is called
     */
    class XmlAddressBookStorageExceptionThrowingStub extends XmlAddressBookStorage {

        public XmlAddressBookStorageExceptionThrowingStub(String filePath) {
            super(filePath);
        }

        @Override
        public void saveAddressBook(ReadOnlyAddressBook addressBook, String filePath) throws IOException {
            throw new IOException("dummy exception");
        }
    }


}

@@ -352,27 +352,27 @@
 s.model.
-AddressBook
+TaskManager
 ChangedE
@@ -474,27 +474,35 @@
 s.model.
-AddressBook
+ReadOnlyTaskManager
 ;%0Aimport
@@ -522,35 +522,27 @@
 s.model.
-ReadOnlyAddressBook
+TaskManager
 ;%0Aimport
@@ -661,22 +661,20 @@
 icalTest
-Person
+Task
 s;%0A%0Apubl
@@ -1716,27 +1716,27 @@
 ic void 
-addressBook
+taskManager
 ReadSave
@@ -1888,35 +1888,35 @@
 * %7B@link Xml
-AddressBook
+TaskManager
 Storage%7D cla
@@ -1997,35 +1997,35 @@
 n %7B@link Xml
-AddressBook
+TaskManager
 StorageTest%7D
@@ -2052,27 +2052,27 @@
         
-AddressBook
+TaskManager
  origina
@@ -2094,14 +2094,12 @@
 Test
-Person
+Task
 s().
@@ -2108,27 +2108,27 @@
 tTypical
-AddressBook
+TaskManager
 ();%0A    
@@ -2150,27 +2150,27 @@
 ger.save
-AddressBook
+TaskManager
 (origina
@@ -2189,27 +2189,27 @@
 ReadOnly
-AddressBook
+TaskManager
  retriev
@@ -2232,27 +2232,27 @@
 ger.read
-AddressBook
+TaskManager
 ().get()
@@ -2288,27 +2288,27 @@
 al, new 
-AddressBook
+TaskManager
 (retriev
@@ -2349,27 +2349,27 @@
 void get
-AddressBook
+TaskManager
 FilePath
@@ -2413,27 +2413,27 @@
 ager.get
-AddressBook
+TaskManager
 FilePath
@@ -2472,35 +2472,35 @@
  void handle
-AddressBook
+TaskManager
 ChangedEvent
@@ -2714,35 +2714,35 @@
 ager(new Xml
-AddressBook
+TaskManager
 StorageExcep
@@ -2935,27 +2935,27 @@
 e.handle
-AddressBook
+TaskManager
 ChangedE
@@ -2963,27 +2963,27 @@
 ent(new 
-AddressBook
+TaskManager
 ChangedE
@@ -2991,27 +2991,27 @@
 ent(new 
-AddressBook
+TaskManager
 ()));%0A  
@@ -3193,35 +3193,35 @@
    class Xml
-AddressBook
+TaskManager
 StorageExcep
@@ -3244,35 +3244,35 @@
  extends Xml
-AddressBook
+TaskManager
 Storage %7B%0A%0A 
@@ -3288,27 +3288,27 @@
 blic Xml
-AddressBook
+TaskManager
 StorageE
@@ -3433,51 +3433,51 @@
 save
-AddressBook(ReadOnlyAddressBook addressBook
+TaskManager(ReadOnlyTaskManager taskManager
 , St

Modify StorageManagerTest class