Browse Source

Merge pull request #594 from csmith/master

Add getPath utility method to JimFsRule.
pull/598/merge
Shane Mc Cormack 9 years ago
parent
commit
aab5a4fd4c

+ 16
- 20
test/com/dmdirc/commandparser/aliases/ActionAliasMigratorTest.java View File

38
 import static org.junit.Assert.assertTrue;
38
 import static org.junit.Assert.assertTrue;
39
 import static org.mockito.Mockito.verify;
39
 import static org.mockito.Mockito.verify;
40
 
40
 
41
-@SuppressWarnings("resource")
42
 @RunWith(MockitoJUnitRunner.class)
41
 @RunWith(MockitoJUnitRunner.class)
43
 public class ActionAliasMigratorTest {
42
 public class ActionAliasMigratorTest {
44
 
43
 
53
 
52
 
54
     @Before
53
     @Before
55
     public void setup() throws IOException {
54
     public void setup() throws IOException {
56
-        Files.createDirectories(jimFsRule.getFileSystem().getPath("test1/aliases"));
57
-        Files.createDirectories(jimFsRule.getFileSystem().getPath("test2/other-stuff/aliases"));
58
-        Files.createDirectories(jimFsRule.getFileSystem().getPath("test3/aliases"));
55
+        Files.createDirectories(jimFsRule.getPath("test1/aliases"));
56
+        Files.createDirectories(jimFsRule.getPath("test2/other-stuff/aliases"));
57
+        Files.createDirectories(jimFsRule.getPath("test3/aliases"));
59
 
58
 
60
         Files.copy(getClass().getResource("op-greater-0").openStream(),
59
         Files.copy(getClass().getResource("op-greater-0").openStream(),
61
-                jimFsRule.getFileSystem().getPath("test1/aliases/op"));
60
+                jimFsRule.getPath("test1/aliases/op"));
62
         Files.copy(getClass().getResource("unset-equals-2").openStream(),
61
         Files.copy(getClass().getResource("unset-equals-2").openStream(),
63
-                jimFsRule.getFileSystem().getPath("test1/aliases/unset"));
62
+                jimFsRule.getPath("test1/aliases/unset"));
64
         Files.copy(getClass().getResource("no-trigger").openStream(),
63
         Files.copy(getClass().getResource("no-trigger").openStream(),
65
-                jimFsRule.getFileSystem().getPath("test3/aliases/bad"));
66
-
67
-        migrator1 = new ActionAliasMigrator(jimFsRule.getFileSystem().getPath("test1"),
68
-                aliasFactory, aliasManager);
69
-        migrator2 = new ActionAliasMigrator(jimFsRule.getFileSystem().getPath("test2"),
70
-                aliasFactory, aliasManager);
71
-        migrator3 = new ActionAliasMigrator(jimFsRule.getFileSystem().getPath("test3"),
72
-                aliasFactory, aliasManager);
64
+                jimFsRule.getPath("test3/aliases/bad"));
65
+
66
+        migrator1 = new ActionAliasMigrator(jimFsRule.getPath("test1"), aliasFactory, aliasManager);
67
+        migrator2 = new ActionAliasMigrator(jimFsRule.getPath("test2"), aliasFactory, aliasManager);
68
+        migrator3 = new ActionAliasMigrator(jimFsRule.getPath("test3"), aliasFactory, aliasManager);
73
     }
69
     }
74
 
70
 
75
     @Test
71
     @Test
81
     @Test
77
     @Test
82
     public void testDeletesFilesAfterMigration() {
78
     public void testDeletesFilesAfterMigration() {
83
         migrator1.migrate();
79
         migrator1.migrate();
84
-        assertFalse(Files.exists(jimFsRule.getFileSystem().getPath("test1/aliases/unset")));
85
-        assertFalse(Files.exists(jimFsRule.getFileSystem().getPath("test1/aliases/op")));
86
-        assertFalse(Files.exists(jimFsRule.getFileSystem().getPath("test1/aliases")));
87
-        assertTrue(Files.exists(jimFsRule.getFileSystem().getPath("test1")));
80
+        assertFalse(Files.exists(jimFsRule.getPath("test1/aliases/unset")));
81
+        assertFalse(Files.exists(jimFsRule.getPath("test1/aliases/op")));
82
+        assertFalse(Files.exists(jimFsRule.getPath("test1/aliases")));
83
+        assertTrue(Files.exists(jimFsRule.getPath("test1")));
88
     }
84
     }
89
 
85
 
90
     @Test
86
     @Test
91
     public void testLeavesOtherFilesDuringMigration() {
87
     public void testLeavesOtherFilesDuringMigration() {
92
         migrator2.migrate();
88
         migrator2.migrate();
93
-        assertTrue(Files.exists(jimFsRule.getFileSystem().getPath("test2/other-stuff/aliases")));
89
+        assertTrue(Files.exists(jimFsRule.getPath("test2/other-stuff/aliases")));
94
     }
90
     }
95
 
91
 
96
     @Test
92
     @Test
103
     @Test
99
     @Test
104
     public void testBadActionsLeftOnDisk() {
100
     public void testBadActionsLeftOnDisk() {
105
         migrator3.migrate();
101
         migrator3.migrate();
106
-        assertTrue(Files.exists(jimFsRule.getFileSystem().getPath("test3/aliases/bad")));
102
+        assertTrue(Files.exists(jimFsRule.getPath("test3/aliases/bad")));
107
     }
103
     }
108
 
104
 
109
 }
105
 }

+ 1
- 2
test/com/dmdirc/commandparser/aliases/DefaultAliasInstallerTest.java View File

37
 import static org.junit.Assert.assertFalse;
37
 import static org.junit.Assert.assertFalse;
38
 import static org.junit.Assert.assertTrue;
38
 import static org.junit.Assert.assertTrue;
39
 
39
 
40
-@SuppressWarnings("resource")
41
 @RunWith(MockitoJUnitRunner.class)
40
 @RunWith(MockitoJUnitRunner.class)
42
 public class DefaultAliasInstallerTest {
41
 public class DefaultAliasInstallerTest {
43
 
42
 
49
 
48
 
50
     @Before
49
     @Before
51
     public void setup() {
50
     public void setup() {
52
-        path = jimFsRule.getFileSystem().getPath("aliases.yml");
51
+        path = jimFsRule.getPath("aliases.yml");
53
         installer = new DefaultAliasInstaller(path);
52
         installer = new DefaultAliasInstaller(path);
54
     }
53
     }
55
 
54
 

+ 5
- 6
test/com/dmdirc/commandparser/auto/YamlAutoCommandStoreTest.java View File

39
 import static org.junit.Assert.assertEquals;
39
 import static org.junit.Assert.assertEquals;
40
 import static org.junit.Assert.assertTrue;
40
 import static org.junit.Assert.assertTrue;
41
 
41
 
42
-@SuppressWarnings("resource")
43
 public class YamlAutoCommandStoreTest {
42
 public class YamlAutoCommandStoreTest {
44
 
43
 
45
     @Rule public final JimFsRule jimFsRule = new JimFsRule();
44
     @Rule public final JimFsRule jimFsRule = new JimFsRule();
54
     @Before
53
     @Before
55
     public void setup() throws IOException {
54
     public void setup() throws IOException {
56
         Files.copy(getClass().getResource("readtest.yml").openStream(),
55
         Files.copy(getClass().getResource("readtest.yml").openStream(),
57
-                jimFsRule.getFileSystem().getPath("readtest.yml"));
56
+                jimFsRule.getPath("readtest.yml"));
58
         command = AutoCommand.create(Optional.ofNullable("server"),
57
         command = AutoCommand.create(Optional.ofNullable("server"),
59
                 Optional.ofNullable("network"),
58
                 Optional.ofNullable("network"),
60
                 Optional.ofNullable("profile"),
59
                 Optional.ofNullable("profile"),
77
 
76
 
78
     @Test
77
     @Test
79
     public void testReadAutoCommands() {
78
     public void testReadAutoCommands() {
80
-        ycs = new YamlAutoCommandStore(jimFsRule.getFileSystem().getPath("readtest.yml"));
79
+        ycs = new YamlAutoCommandStore(jimFsRule.getPath("readtest.yml"));
81
         final Set<AutoCommand> commands = ycs.readAutoCommands();
80
         final Set<AutoCommand> commands = ycs.readAutoCommands();
82
         assertTrue("Command 1 not present", commands.contains(command1));
81
         assertTrue("Command 1 not present", commands.contains(command1));
83
         assertTrue("Command 2 not present", commands.contains(command2));
82
         assertTrue("Command 2 not present", commands.contains(command2));
87
 
86
 
88
     @Test
87
     @Test
89
     public void testWriteAutoCommands() throws IOException {
88
     public void testWriteAutoCommands() throws IOException {
90
-        ycs = new YamlAutoCommandStore(jimFsRule.getFileSystem().getPath("store.yml"));
89
+        ycs = new YamlAutoCommandStore(jimFsRule.getPath("store.yml"));
91
         assertEquals(0, ycs.readAutoCommands().size());
90
         assertEquals(0, ycs.readAutoCommands().size());
92
-        assertFalse(Files.exists(jimFsRule.getFileSystem().getPath("store.yml")));
91
+        assertFalse(Files.exists(jimFsRule.getPath("store.yml")));
93
         ycs.writeAutoCommands(Sets.newHashSet(command));
92
         ycs.writeAutoCommands(Sets.newHashSet(command));
94
         final Set<AutoCommand> commands = ycs.readAutoCommands();
93
         final Set<AutoCommand> commands = ycs.readAutoCommands();
95
         assertTrue("Command not present", commands.contains(command));
94
         assertTrue("Command not present", commands.contains(command));
96
-        assertTrue(Files.exists(jimFsRule.getFileSystem().getPath("store.yml")));
95
+        assertTrue(Files.exists(jimFsRule.getPath("store.yml")));
97
     }
96
     }
98
 
97
 
99
 }
98
 }

+ 6
- 10
test/com/dmdirc/config/ConfigFileBackedConfigProviderTest.java View File

50
 import static org.mockito.Mockito.never;
50
 import static org.mockito.Mockito.never;
51
 import static org.mockito.Mockito.verify;
51
 import static org.mockito.Mockito.verify;
52
 
52
 
53
-@SuppressWarnings("resource")
54
 @RunWith(MockitoJUnitRunner.class)
53
 @RunWith(MockitoJUnitRunner.class)
55
 public class ConfigFileBackedConfigProviderTest {
54
 public class ConfigFileBackedConfigProviderTest {
56
 
55
 
70
     @Before
69
     @Before
71
     public void setUp() throws Exception {
70
     public void setUp() throws Exception {
72
         for (String file: FILES) {
71
         for (String file: FILES) {
73
-            Files.copy(getClass().getResourceAsStream(file),
74
-                    jimFsRule.getFileSystem().getPath(file));
72
+            Files.copy(getClass().getResourceAsStream(file), jimFsRule.getPath(file));
75
         }
73
         }
76
     }
74
     }
77
 
75
 
78
     @Test(expected = InvalidIdentityFileException.class)
76
     @Test(expected = InvalidIdentityFileException.class)
79
     public void testNoName() throws IOException, InvalidIdentityFileException {
77
     public void testNoName() throws IOException, InvalidIdentityFileException {
80
         new ConfigFileBackedConfigProvider(identityManager,
78
         new ConfigFileBackedConfigProvider(identityManager,
81
-                jimFsRule.getFileSystem().getPath("no-name"), false);
79
+                jimFsRule.getPath("no-name"), false);
82
     }
80
     }
83
 
81
 
84
     @Test(expected = InvalidIdentityFileException.class)
82
     @Test(expected = InvalidIdentityFileException.class)
85
     public void testNoTarget() throws IOException, InvalidIdentityFileException {
83
     public void testNoTarget() throws IOException, InvalidIdentityFileException {
86
         new ConfigFileBackedConfigProvider(identityManager,
84
         new ConfigFileBackedConfigProvider(identityManager,
87
-                jimFsRule.getFileSystem().getPath("no-target"), false);
85
+                jimFsRule.getPath("no-target"), false);
88
     }
86
     }
89
 
87
 
90
     @Test(expected = InvalidIdentityFileException.class)
88
     @Test(expected = InvalidIdentityFileException.class)
91
     public void testInvalidConfigFile() throws IOException, InvalidIdentityFileException {
89
     public void testInvalidConfigFile() throws IOException, InvalidIdentityFileException {
92
         new ConfigFileBackedConfigProvider(identityManager,
90
         new ConfigFileBackedConfigProvider(identityManager,
93
-                jimFsRule.getFileSystem().getPath("invalid-config-file"), false);
91
+                jimFsRule.getPath("invalid-config-file"), false);
94
     }
92
     }
95
 
93
 
96
     @Test
94
     @Test
313
 
311
 
314
     private void copyFileAndReload(final ConfigProvider provider)
312
     private void copyFileAndReload(final ConfigProvider provider)
315
             throws IOException, InvalidConfigFileException {
313
             throws IOException, InvalidConfigFileException {
316
-        Files.copy(jimFsRule.getFileSystem().getPath("simple-ircd-extra"),
317
-                jimFsRule.getFileSystem().getPath("simple-ircd"),
314
+        Files.copy(jimFsRule.getPath("simple-ircd-extra"), jimFsRule.getPath("simple-ircd"),
318
                 StandardCopyOption.REPLACE_EXISTING);
315
                 StandardCopyOption.REPLACE_EXISTING);
319
         provider.reload();
316
         provider.reload();
320
     }
317
     }
321
 
318
 
322
     private ConfigFileBackedConfigProvider getProvider(final String file)
319
     private ConfigFileBackedConfigProvider getProvider(final String file)
323
             throws IOException, InvalidIdentityFileException {
320
             throws IOException, InvalidIdentityFileException {
324
-        return new ConfigFileBackedConfigProvider(identityManager,
325
-                jimFsRule.getFileSystem().getPath(file), false);
321
+        return new ConfigFileBackedConfigProvider(identityManager, jimFsRule.getPath(file), false);
326
     }
322
     }
327
 
323
 
328
 }
324
 }

+ 1
- 2
test/com/dmdirc/config/IdentityManagerTest.java View File

54
     private Path identitiesDirectory;
54
     private Path identitiesDirectory;
55
 
55
 
56
     @Before
56
     @Before
57
-    @SuppressWarnings("resource")
58
     public void setUp() throws Exception {
57
     public void setUp() throws Exception {
59
-        baseDirectory = jimFsRule.getFileSystem().getPath("config");
58
+        baseDirectory = jimFsRule.getPath("config");
60
         identitiesDirectory = baseDirectory.resolve("identities");
59
         identitiesDirectory = baseDirectory.resolve("identities");
61
     }
60
     }
62
 
61
 

+ 7
- 8
test/com/dmdirc/logger/DiskLoggingErrorManagerTest.java View File

45
 import static org.mockito.Mockito.verify;
45
 import static org.mockito.Mockito.verify;
46
 import static org.mockito.Mockito.when;
46
 import static org.mockito.Mockito.when;
47
 
47
 
48
-@SuppressWarnings("resource")
49
 @RunWith(MockitoJUnitRunner.class)
48
 @RunWith(MockitoJUnitRunner.class)
50
 public class DiskLoggingErrorManagerTest {
49
 public class DiskLoggingErrorManagerTest {
51
 
50
 
67
         when(programError.getThrowableAsString()).thenReturn(Optional.of("test"));
66
         when(programError.getThrowableAsString()).thenReturn(Optional.of("test"));
68
         when(programError.getLevel()).thenReturn(ErrorLevel.MEDIUM);
67
         when(programError.getLevel()).thenReturn(ErrorLevel.MEDIUM);
69
         when(config.getBinder()).thenReturn(configBinder);
68
         when(config.getBinder()).thenReturn(configBinder);
70
-        instance = new DiskLoggingErrorManager(jimFsRule.getFileSystem().getPath("/errors"),
69
+        instance = new DiskLoggingErrorManager(jimFsRule.getPath("/errors"),
71
                 eventBus);
70
                 eventBus);
72
     }
71
     }
73
 
72
 
74
     @Test
73
     @Test
75
     public void testInitialise() throws Exception {
74
     public void testInitialise() throws Exception {
76
-        assertFalse(Files.exists(jimFsRule.getFileSystem().getPath("/errors")));
75
+        assertFalse(Files.exists(jimFsRule.getPath("/errors")));
77
         instance.initialise(config);
76
         instance.initialise(config);
78
         verify(configBinder).bind(instance, DiskLoggingErrorManager.class);
77
         verify(configBinder).bind(instance, DiskLoggingErrorManager.class);
79
-        assertTrue(Files.exists(jimFsRule.getFileSystem().getPath("/errors")));
78
+        assertTrue(Files.exists(jimFsRule.getPath("/errors")));
80
         assertFalse(instance.isDirectoryError());
79
         assertFalse(instance.isDirectoryError());
81
     }
80
     }
82
 
81
 
90
         instance.initialise(config);
89
         instance.initialise(config);
91
         instance.handleLoggingSetting(true);
90
         instance.handleLoggingSetting(true);
92
         final String logName = error.getTimestamp() + "-" + error.getError().getLevel() + ".log";;
91
         final String logName = error.getTimestamp() + "-" + error.getError().getLevel() + ".log";;
93
-        assertFalse(Files.exists(jimFsRule.getFileSystem().getPath("/errors", logName)));
92
+        assertFalse(Files.exists(jimFsRule.getPath("/errors", logName)));
94
         instance.handleErrorEvent(error);
93
         instance.handleErrorEvent(error);
95
-        final Path errorPath = jimFsRule.getFileSystem().getPath("/errors", logName);
94
+        final Path errorPath = jimFsRule.getPath("/errors", logName);
96
         assertTrue(Files.exists(errorPath));
95
         assertTrue(Files.exists(errorPath));
97
         assertTrue(Files.readAllLines(errorPath).contains("Level: Medium"));
96
         assertTrue(Files.readAllLines(errorPath).contains("Level: Medium"));
98
     }
97
     }
102
         instance.initialise(config);
101
         instance.initialise(config);
103
         instance.handleLoggingSetting(false);
102
         instance.handleLoggingSetting(false);
104
         final String logName = error.getTimestamp() + "-" + error.getError().getLevel() + ".log";;
103
         final String logName = error.getTimestamp() + "-" + error.getError().getLevel() + ".log";;
105
-        assertFalse(Files.exists(jimFsRule.getFileSystem().getPath("/errors", logName)));
104
+        assertFalse(Files.exists(jimFsRule.getPath("/errors", logName)));
106
         instance.handleErrorEvent(error);
105
         instance.handleErrorEvent(error);
107
-        assertFalse(Files.exists(jimFsRule.getFileSystem().getPath("/errors", logName)));
106
+        assertFalse(Files.exists(jimFsRule.getPath("/errors", logName)));
108
     }
107
     }
109
 
108
 
110
     @Test
109
     @Test

+ 17
- 3
test/com/dmdirc/tests/JimFsRule.java View File

26
 import com.google.common.jimfs.Jimfs;
26
 import com.google.common.jimfs.Jimfs;
27
 
27
 
28
 import java.nio.file.FileSystem;
28
 import java.nio.file.FileSystem;
29
-
30
-import javax.annotation.WillCloseWhenClosed;
29
+import java.nio.file.Path;
31
 
30
 
32
 import org.junit.rules.TestRule;
31
 import org.junit.rules.TestRule;
33
 import org.junit.runner.Description;
32
 import org.junit.runner.Description;
55
         };
54
         };
56
     }
55
     }
57
 
56
 
58
-    @WillCloseWhenClosed
57
+    /**
58
+     * Gets the file system for use in the test.
59
+     *
60
+     * @return The JimFS filesystem.
61
+     */
59
     public FileSystem getFileSystem() {
62
     public FileSystem getFileSystem() {
60
         return fileSystem;
63
         return fileSystem;
61
     }
64
     }
62
 
65
 
66
+    /**
67
+     * Utility method to get a path from the file system.
68
+     *
69
+     * @param first The first component of the path
70
+     * @param more Any additional path components
71
+     * @return A corresponding {@link Path} object from the JimFS file system.
72
+     */
73
+    public Path getPath(final String first, final String ... more) {
74
+        return fileSystem.getPath(first, more);
75
+    }
76
+
63
 }
77
 }

+ 1
- 3
test/com/dmdirc/util/URLBuilderTest.java View File

59
     @Mock private DMDircMBassador eventBus;
59
     @Mock private DMDircMBassador eventBus;
60
 
60
 
61
     @Before
61
     @Before
62
-    @SuppressWarnings("resource")
63
     public void setup() throws MalformedURLException {
62
     public void setup() throws MalformedURLException {
64
         when(pluginManagerProvider.get()).thenReturn(pluginManager);
63
         when(pluginManagerProvider.get()).thenReturn(pluginManager);
65
         when(themeManagerProvider.get()).thenReturn(themeManager);
64
         when(themeManagerProvider.get()).thenReturn(themeManager);
66
         when(pluginManager.getPluginInfoByName(Matchers.anyString())).thenReturn(pluginInfo);
65
         when(pluginManager.getPluginInfoByName(Matchers.anyString())).thenReturn(pluginInfo);
67
         when(themeManager.getDirectory()).thenReturn("/themes/");
66
         when(themeManager.getDirectory()).thenReturn("/themes/");
68
         when(pluginInfo.getMetaData()).thenReturn(pluginMetaData);
67
         when(pluginInfo.getMetaData()).thenReturn(pluginMetaData);
69
-        when(pluginMetaData.getPluginPath()).thenReturn(
70
-                jimFsRule.getFileSystem().getPath("file://testPlugin"));
68
+        when(pluginMetaData.getPluginPath()).thenReturn(jimFsRule.getPath("file://testPlugin"));
71
     }
69
     }
72
 
70
 
73
     @Test
71
     @Test

Loading…
Cancel
Save