Browse Source

Add test for YamlAutoCommandStore

Change-Id: I62a9d05c94a3e2d31466a04c775f3afbddb09cdc
Reviewed-on: http://gerrit.dmdirc.com/3995
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Chris Smith <chris@dmdirc.com>
pull/1/head
Greg Holmes 9 years ago
parent
commit
3fac8a376f

+ 1
- 0
build.gradle View File

@@ -56,6 +56,7 @@ allprojects {
56 56
     dependencies {
57 57
         testCompile group: 'junit', name: 'junit', version: '4.+'
58 58
         testCompile group: 'org.mockito', name: 'mockito-all', version: '1.+'
59
+        testCompile group: 'com.google.jimfs', name: 'jimfs', version: '1.0'
59 60
     }
60 61
 
61 62
     pmd {

+ 10
- 0
test-res/com/dmdirc/commandparser/auto/readtest.yml View File

@@ -0,0 +1,10 @@
1
+-  command: command1
2
+   network: network1
3
+   server: server1
4
+   profile: profile1
5
+-  command: command2
6
+   network: network2
7
+   server: server2
8
+-  command: command3
9
+   network: network3
10
+-  command: command4

+ 99
- 0
test/com/dmdirc/commandparser/auto/YamlAutoCommandStoreTest.java View File

@@ -0,0 +1,99 @@
1
+/*
2
+ * Copyright (c) 2006-2014 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.auto;
24
+
25
+import com.google.common.base.Optional;
26
+import com.google.common.collect.Sets;
27
+import com.google.common.jimfs.Configuration;
28
+import com.google.common.jimfs.Jimfs;
29
+
30
+import java.io.IOException;
31
+import java.nio.file.FileSystem;
32
+import java.nio.file.Files;
33
+import java.util.Set;
34
+
35
+import org.junit.Before;
36
+import org.junit.Test;
37
+
38
+import static junit.framework.TestCase.assertFalse;
39
+import static org.junit.Assert.assertEquals;
40
+import static org.junit.Assert.assertTrue;
41
+
42
+public class YamlAutoCommandStoreTest {
43
+
44
+    private FileSystem fs;
45
+    private YamlAutoCommandStore ycs;
46
+    private AutoCommand command;
47
+    private AutoCommand command1;
48
+    private AutoCommand command2;
49
+    private AutoCommand command3;
50
+    private AutoCommand command4;
51
+
52
+    @Before
53
+    public void setup() throws IOException {
54
+        fs = Jimfs.newFileSystem(Configuration.unix());
55
+        Files.copy(getClass().getResource("readtest.yml").openStream(), fs.getPath("readtest.yml"));
56
+        command = new AutoCommand(Optional.fromNullable("server"),
57
+                Optional.fromNullable("network"),
58
+                Optional.fromNullable("profile"),
59
+                "command");
60
+        command1 = new AutoCommand(Optional.fromNullable("server1"),
61
+                Optional.fromNullable("network1"),
62
+                Optional.fromNullable("profile1"),
63
+                "command1");
64
+        command2 = new AutoCommand(Optional.fromNullable("server2"),
65
+                Optional.fromNullable("network2"),
66
+                Optional.<String>absent(),
67
+                "command2");
68
+        command3 = new AutoCommand(Optional.<String>absent(),
69
+                Optional.fromNullable("network3"),
70
+                Optional.<String>absent(),
71
+                "command3");
72
+        command4 = new AutoCommand(Optional.<String>absent(),
73
+                Optional.<String>absent(),
74
+                Optional.<String>absent(),
75
+                "command4");
76
+    }
77
+
78
+    @Test
79
+    public void testReadAutoCommands() {
80
+        ycs = new YamlAutoCommandStore(fs.getPath("readtest.yml"));
81
+        final Set<AutoCommand> commands = ycs.readAutoCommands();
82
+        assertTrue("Command 1 not present", commands.contains(command1));
83
+        assertTrue("Command 2 not present", commands.contains(command2));
84
+        assertTrue("Command 3 not present", commands.contains(command3));
85
+        assertTrue("Command 4 not present", commands.contains(command4));
86
+    }
87
+
88
+    @Test
89
+    public void testWriteAutoCommands() throws IOException {
90
+        ycs = new YamlAutoCommandStore(fs.getPath("store.yml"));
91
+        assertEquals(0, ycs.readAutoCommands().size());
92
+        assertFalse(Files.exists(fs.getPath("store.yml")));
93
+        ycs.writeAutoCommands(Sets.newHashSet(command));
94
+        final Set<AutoCommand> commands = ycs.readAutoCommands();
95
+        assertTrue("Command not present", commands.contains(command));
96
+        assertTrue(Files.exists(fs.getPath("store.yml")));
97
+    }
98
+
99
+}

Loading…
Cancel
Save