Browse Source

Add basic test for PluginFileHandler.

pull/596/head
Chris Smith 9 years ago
parent
commit
3908d97e09

BIN
test-res/com/dmdirc/plugins/plugin.jar View File


+ 86
- 0
test/com/dmdirc/plugins/PluginFileHandlerTest.java View File

@@ -0,0 +1,86 @@
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.plugins;
24
+
25
+import com.dmdirc.tests.JimFsRule;
26
+
27
+import java.io.IOException;
28
+import java.nio.file.Files;
29
+import java.nio.file.Path;
30
+
31
+import org.junit.Before;
32
+import org.junit.Rule;
33
+import org.junit.Test;
34
+import org.junit.runner.RunWith;
35
+import org.mockito.Mock;
36
+import org.mockito.runners.MockitoJUnitRunner;
37
+
38
+import static org.junit.Assert.assertFalse;
39
+import static org.junit.Assert.assertTrue;
40
+import static org.junit.Assume.assumeTrue;
41
+
42
+@RunWith(MockitoJUnitRunner.class)
43
+public class PluginFileHandlerTest {
44
+
45
+    @Rule public final JimFsRule jimFsRule = new JimFsRule();
46
+
47
+    @Mock private PluginManager pluginManager;
48
+
49
+    private PluginFileHandler fileHandler;
50
+    private Path directory;
51
+
52
+    @Before
53
+    public void setUp() throws IOException {
54
+        directory = jimFsRule.getPath("plugins");
55
+
56
+        Files.createDirectory(directory);
57
+        fileHandler = new PluginFileHandler(directory);
58
+    }
59
+
60
+    @Test
61
+    public void testMovesUpdatedJar() throws IOException {
62
+        Files.copy(getClass().getResource("plugin.jar").openStream(),
63
+                directory.resolve("plugin.jar.update"));
64
+
65
+        fileHandler.refresh(pluginManager);
66
+
67
+        assertFalse(Files.exists(directory.resolve("plugin.jar.update")));
68
+        assertTrue(Files.exists(directory.resolve("plugin.jar")));
69
+    }
70
+
71
+    @Test
72
+    public void testReplacesExistingJarWithUpdatedVersion() throws IOException {
73
+        Files.write(directory.resolve("plugin.jar"), new byte[0]);
74
+        Files.copy(getClass().getResource("plugin.jar").openStream(),
75
+                directory.resolve("plugin.jar.update"));
76
+
77
+        assumeTrue(Files.size(directory.resolve("plugin.jar")) == 0);
78
+
79
+        fileHandler.refresh(pluginManager);
80
+
81
+        assertFalse(Files.exists(directory.resolve("plugin.jar.update")));
82
+        assertTrue(Files.exists(directory.resolve("plugin.jar")));
83
+        assertTrue(Files.size(directory.resolve("plugin.jar")) > 0);
84
+    }
85
+
86
+}

Loading…
Cancel
Save