|
@@ -0,0 +1,98 @@
|
|
1
|
+/*
|
|
2
|
+ * Copyright (c) 2006-2015 DMDirc Developers
|
|
3
|
+ *
|
|
4
|
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+ * of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+ * in the Software without restriction, including without limitation the rights
|
|
7
|
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+ * copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+ * furnished to do so, subject to the following conditions:
|
|
10
|
+ *
|
|
11
|
+ * The above copyright notice and this permission notice shall be included in
|
|
12
|
+ * all copies or substantial portions of the Software.
|
|
13
|
+ *
|
|
14
|
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
20
|
+ * SOFTWARE.
|
|
21
|
+ */
|
|
22
|
+
|
|
23
|
+package com.dmdirc.commandparser.aliases;
|
|
24
|
+
|
|
25
|
+import com.google.common.collect.Sets;
|
|
26
|
+
|
|
27
|
+import java.util.Set;
|
|
28
|
+
|
|
29
|
+import org.junit.Before;
|
|
30
|
+import org.junit.Test;
|
|
31
|
+import org.junit.runner.RunWith;
|
|
32
|
+import org.mockito.Mock;
|
|
33
|
+import org.mockito.runners.MockitoJUnitRunner;
|
|
34
|
+
|
|
35
|
+import static org.mockito.Matchers.anySetOf;
|
|
36
|
+import static org.mockito.Mockito.never;
|
|
37
|
+import static org.mockito.Mockito.verify;
|
|
38
|
+import static org.mockito.Mockito.when;
|
|
39
|
+
|
|
40
|
+@RunWith(MockitoJUnitRunner.class)
|
|
41
|
+public class AliasLoaderTest {
|
|
42
|
+
|
|
43
|
+ @Mock private AliasManager aliasManager;
|
|
44
|
+ @Mock private AliasStore aliasStore;
|
|
45
|
+ @Mock private Alias alias1;
|
|
46
|
+ @Mock private Alias alias2;
|
|
47
|
+ @Mock private Alias alias3;
|
|
48
|
+
|
|
49
|
+ private Set<Alias> aliases;
|
|
50
|
+ private AliasLoader loader;
|
|
51
|
+
|
|
52
|
+ @Before
|
|
53
|
+ public void setup() {
|
|
54
|
+ loader = new AliasLoader(aliasManager, aliasStore);
|
|
55
|
+ aliases = Sets.newHashSet(alias1, alias2, alias3);
|
|
56
|
+ }
|
|
57
|
+
|
|
58
|
+ @Test
|
|
59
|
+ public void testLoadPreservesDirtyState() {
|
|
60
|
+ when(aliasManager.isDirty()).thenReturn(true);
|
|
61
|
+ when(aliasStore.readAliases()).thenReturn(aliases);
|
|
62
|
+ loader.load();
|
|
63
|
+ verify(aliasManager).setDirty(true);
|
|
64
|
+ }
|
|
65
|
+
|
|
66
|
+ @Test
|
|
67
|
+ public void testLoadAddsEachAlias() {
|
|
68
|
+ when(aliasStore.readAliases()).thenReturn(aliases);
|
|
69
|
+ loader.load();
|
|
70
|
+ verify(aliasManager).addAlias(alias1);
|
|
71
|
+ verify(aliasManager).addAlias(alias2);
|
|
72
|
+ verify(aliasManager).addAlias(alias3);
|
|
73
|
+ }
|
|
74
|
+
|
|
75
|
+ @Test
|
|
76
|
+ public void testSaveDoesNotWriteIfManagerNotDirty() {
|
|
77
|
+ when(aliasManager.isDirty()).thenReturn(false);
|
|
78
|
+ loader.save();
|
|
79
|
+ verify(aliasStore, never()).writeAliases(anySetOf(Alias.class));
|
|
80
|
+ }
|
|
81
|
+
|
|
82
|
+ @Test
|
|
83
|
+ public void testSaveSetsDirtyToFalse() {
|
|
84
|
+ when(aliasManager.isDirty()).thenReturn(true);
|
|
85
|
+ when(aliasManager.getAliases()).thenReturn(aliases);
|
|
86
|
+ loader.save();
|
|
87
|
+ verify(aliasManager).setDirty(false);
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ @Test
|
|
91
|
+ public void testSaveSavesAliases() {
|
|
92
|
+ when(aliasManager.isDirty()).thenReturn(true);
|
|
93
|
+ when(aliasManager.getAliases()).thenReturn(aliases);
|
|
94
|
+ loader.save();
|
|
95
|
+ verify(aliasStore).writeAliases(aliases);
|
|
96
|
+ }
|
|
97
|
+
|
|
98
|
+}
|