Просмотр исходного кода

Add some alias tests.

pull/504/head
Chris Smith 9 лет назад
Родитель
Сommit
d2c0dcd323

+ 98
- 0
test/com/dmdirc/commandparser/aliases/AliasLoaderTest.java Просмотреть файл

@@ -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
+}

+ 101
- 0
test/com/dmdirc/commandparser/aliases/DefaultAliasInstallerTest.java Просмотреть файл

@@ -0,0 +1,101 @@
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.dmdirc.DMDircMBassador;
26
+import com.dmdirc.events.AppErrorEvent;
27
+
28
+import com.google.common.jimfs.Configuration;
29
+import com.google.common.jimfs.Jimfs;
30
+
31
+import java.io.IOException;
32
+import java.nio.file.FileSystem;
33
+import java.nio.file.Files;
34
+import java.nio.file.Path;
35
+
36
+import org.junit.After;
37
+import org.junit.Before;
38
+import org.junit.Test;
39
+import org.junit.runner.RunWith;
40
+import org.mockito.Mock;
41
+import org.mockito.runners.MockitoJUnitRunner;
42
+
43
+import static java.nio.file.attribute.PosixFilePermissions.asFileAttribute;
44
+import static java.nio.file.attribute.PosixFilePermissions.fromString;
45
+import static org.junit.Assert.assertFalse;
46
+import static org.junit.Assert.assertTrue;
47
+import static org.mockito.Matchers.any;
48
+import static org.mockito.Matchers.isA;
49
+import static org.mockito.Mockito.never;
50
+import static org.mockito.Mockito.verify;
51
+
52
+@RunWith(MockitoJUnitRunner.class)
53
+public class DefaultAliasInstallerTest {
54
+
55
+    @Mock private DMDircMBassador eventBus;
56
+
57
+    private FileSystem fs;
58
+    private Path path;
59
+
60
+    private DefaultAliasInstaller installer;
61
+
62
+    @Before
63
+    public void setup() {
64
+        fs = Jimfs.newFileSystem(Configuration.unix().toBuilder()
65
+                .setAttributeViews("posix").build());
66
+        path = fs.getPath("aliases.yml");
67
+
68
+        installer = new DefaultAliasInstaller(path, eventBus);
69
+    }
70
+
71
+    @After
72
+    public void tearDown() throws IOException {
73
+        fs.close();
74
+    }
75
+
76
+    @Test
77
+    public void testNeedsMigrationIfFileDoesntExist() {
78
+        assertTrue(installer.needsMigration());
79
+    }
80
+
81
+    @Test
82
+    public void testNeedsMigrationIfFileExist() throws IOException {
83
+        Files.createFile(path);
84
+        assertFalse(installer.needsMigration());
85
+    }
86
+
87
+    @Test
88
+    public void testCopiesDefaultAliases() throws IOException {
89
+        installer.migrate();
90
+        assertTrue(Files.exists(path));
91
+        verify(eventBus, never()).publish(any());
92
+    }
93
+
94
+    @Test
95
+    public void testRaisesErrorIfCannotCopyDefaultAliases() throws IOException {
96
+        Files.createFile(path, asFileAttribute(fromString("r--r--r--")));
97
+        installer.migrate();
98
+        verify(eventBus).publish(isA(AppErrorEvent.class));
99
+    }
100
+
101
+}

Загрузка…
Отмена
Сохранить