forked from se-edu/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 109
/
StorageManagerTest.java
100 lines (78 loc) · 3.51 KB
/
StorageManagerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package seedu.address.storage;
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.TypicalTestPersons;
import seedu.address.testutil.EventsCollector;
import java.io.IOException;
import static junit.framework.TestCase.assertNotNull;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
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");
}
}
}