Browse Source

Allow injecting Paths not Files.

Make the ActionAliasMigrator use a Path, and add a basic
unit test. Need to make ConfigFile use paths to do anything
sensible.

Also refresh gradle libs in idea so JimFs shows up.

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

+ 1
- 1
.idea/inspectionProfiles/Project_Default.xml View File

@@ -316,4 +316,4 @@
316 316
       <option name="ignoreNonEmtpyLoops" value="false" />
317 317
     </inspection_tool>
318 318
   </profile>
319
-</component>
319
+</component>

+ 2
- 0
.idea/parser.iml View File

@@ -21,6 +21,8 @@
21 21
     <orderEntry type="library" scope="TEST" name="Gradle: junit:junit:4.12-beta-1" level="project" />
22 22
     <orderEntry type="library" scope="TEST" name="Gradle: org.mockito:mockito-all:1.9.5" level="project" />
23 23
     <orderEntry type="library" scope="TEST" name="Gradle: org.hamcrest:hamcrest-core:1.3" level="project" />
24
+    <orderEntry type="library" scope="TEST" name="Gradle: com.google.jimfs:jimfs:1.0" level="project" />
25
+    <orderEntry type="library" scope="TEST" name="Gradle: com.google.guava:guava:16.0.1" level="project" />
24 26
   </component>
25 27
 </module>
26 28
 

+ 2
- 3
.idea/plugins.iml View File

@@ -7,9 +7,6 @@
7 7
     <content url="file://$MODULE_DIR$/modules/plugins">
8 8
       <excludeFolder url="file://$MODULE_DIR$/modules/plugins/.gradle" />
9 9
       <excludeFolder url="file://$MODULE_DIR$/modules/plugins/build" />
10
-      <excludeFolder url="file://$MODULE_DIR$/modules/plugins/etc" />
11
-      <excludeFolder url="file://$MODULE_DIR$/modules/plugins/gradle" />
12
-      <excludeFolder url="file://$MODULE_DIR$/modules/plugins/lib" />
13 10
     </content>
14 11
     <orderEntry type="inheritedJdk" />
15 12
     <orderEntry type="sourceFolder" forTests="false" />
@@ -19,6 +16,8 @@
19 16
     <orderEntry type="library" scope="TEST" name="Gradle: junit:junit:4.12-beta-1" level="project" />
20 17
     <orderEntry type="library" scope="TEST" name="Gradle: org.mockito:mockito-all:1.9.5" level="project" />
21 18
     <orderEntry type="library" scope="TEST" name="Gradle: org.hamcrest:hamcrest-core:1.3" level="project" />
19
+    <orderEntry type="library" scope="TEST" name="Gradle: com.google.jimfs:jimfs:1.0" level="project" />
20
+    <orderEntry type="library" scope="TEST" name="Gradle: com.google.guava:guava:16.0.1" level="project" />
22 21
   </component>
23 22
 </module>
24 23
 

+ 2
- 3
.idea/util.iml View File

@@ -15,15 +15,14 @@
15 15
       <sourceFolder url="file://$MODULE_DIR$/modules/util/test-res" type="java-test-resource" />
16 16
       <excludeFolder url="file://$MODULE_DIR$/modules/util/.gradle" />
17 17
       <excludeFolder url="file://$MODULE_DIR$/modules/util/build" />
18
-      <excludeFolder url="file://$MODULE_DIR$/modules/util/gradle" />
19
-      <excludeFolder url="file://$MODULE_DIR$/modules/util/lib" />
20
-      <excludeFolder url="file://$MODULE_DIR$/modules/util/nbproject" />
21 18
     </content>
22 19
     <orderEntry type="inheritedJdk" />
23 20
     <orderEntry type="sourceFolder" forTests="false" />
24 21
     <orderEntry type="library" scope="TEST" name="Gradle: junit:junit:4.12-beta-1" level="project" />
25 22
     <orderEntry type="library" scope="TEST" name="Gradle: org.mockito:mockito-all:1.9.5" level="project" />
26 23
     <orderEntry type="library" scope="TEST" name="Gradle: org.hamcrest:hamcrest-core:1.3" level="project" />
24
+    <orderEntry type="library" scope="TEST" name="Gradle: com.google.jimfs:jimfs:1.0" level="project" />
25
+    <orderEntry type="library" scope="TEST" name="Gradle: com.google.guava:guava:16.0.1" level="project" />
27 26
   </component>
28 27
 </module>
29 28
 

+ 1
- 7
dmdirc.iml View File

@@ -13,15 +13,8 @@
13 13
       <sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
14 14
       <sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
15 15
       <sourceFolder url="file://$MODULE_DIR$/test-res" type="java-test-resource" />
16
-      <excludeFolder url="file://$MODULE_DIR$/.clover" />
17 16
       <excludeFolder url="file://$MODULE_DIR$/.gradle" />
18
-      <excludeFolder url="file://$MODULE_DIR$/.idea" />
19 17
       <excludeFolder url="file://$MODULE_DIR$/build" />
20
-      <excludeFolder url="file://$MODULE_DIR$/etc" />
21
-      <excludeFolder url="file://$MODULE_DIR$/gradle" />
22
-      <excludeFolder url="file://$MODULE_DIR$/installer" />
23
-      <excludeFolder url="file://$MODULE_DIR$/junitreports" />
24
-      <excludeFolder url="file://$MODULE_DIR$/lib" />
25 18
     </content>
26 19
     <orderEntry type="inheritedJdk" />
27 20
     <orderEntry type="sourceFolder" forTests="false" />
@@ -42,6 +35,7 @@
42 35
     <orderEntry type="library" exported="" name="Gradle: com.squareup:javawriter:2.3.0" level="project" />
43 36
     <orderEntry type="library" scope="TEST" name="Gradle: junit:junit:4.12-beta-1" level="project" />
44 37
     <orderEntry type="library" scope="TEST" name="Gradle: org.mockito:mockito-all:1.9.5" level="project" />
38
+    <orderEntry type="library" scope="TEST" name="Gradle: com.google.jimfs:jimfs:1.0" level="project" />
45 39
   </component>
46 40
 </module>
47 41
 

+ 2
- 0
modules/modules.iml View File

@@ -21,6 +21,8 @@
21 21
     <orderEntry type="library" scope="TEST" name="Gradle: junit:junit:4.12-beta-1" level="project" />
22 22
     <orderEntry type="library" scope="TEST" name="Gradle: org.mockito:mockito-all:1.9.5" level="project" />
23 23
     <orderEntry type="library" scope="TEST" name="Gradle: org.hamcrest:hamcrest-core:1.3" level="project" />
24
+    <orderEntry type="library" scope="TEST" name="Gradle: com.google.jimfs:jimfs:1.0" level="project" />
25
+    <orderEntry type="library" scope="TEST" name="Gradle: com.google.guava:guava:16.0.1" level="project" />
24 26
   </component>
25 27
 </module>
26 28
 

+ 43
- 0
src/com/dmdirc/commandline/CommandLineOptionsModule.java View File

@@ -23,6 +23,7 @@
23 23
 package com.dmdirc.commandline;
24 24
 
25 25
 import java.io.File;
26
+import java.nio.file.Path;
26 27
 
27 28
 import javax.inject.Qualifier;
28 29
 import javax.inject.Singleton;
@@ -192,4 +193,46 @@ public class CommandLineOptionsModule {
192 193
         return baseDirectory;
193 194
     }
194 195
 
196
+    @Provides
197
+    @Singleton
198
+    @Directory(DirectoryType.BASE)
199
+    public Path getBasePath(@Directory(DirectoryType.BASE) final String directory) {
200
+        return new File(directory).toPath();
201
+    }
202
+
203
+    @Provides
204
+    @Singleton
205
+    @Directory(DirectoryType.PLUGINS)
206
+    public Path getPluginsPath(@Directory(DirectoryType.PLUGINS) final String directory) {
207
+        return new File(directory).toPath();
208
+    }
209
+
210
+    @Provides
211
+    @Singleton
212
+    @Directory(DirectoryType.ACTIONS)
213
+    public Path getActionsPath(@Directory(DirectoryType.ACTIONS) final String directory) {
214
+        return new File(directory).toPath();
215
+    }
216
+
217
+    @Provides
218
+    @Singleton
219
+    @Directory(DirectoryType.IDENTITIES)
220
+    public Path getIdentitiesPath(@Directory(DirectoryType.IDENTITIES) final String directory) {
221
+        return new File(directory).toPath();
222
+    }
223
+
224
+    @Provides
225
+    @Singleton
226
+    @Directory(DirectoryType.ERRORS)
227
+    public Path getErrorsPath(@Directory(DirectoryType.ERRORS) final String directory) {
228
+        return new File(directory).toPath();
229
+    }
230
+
231
+    @Provides
232
+    @Singleton
233
+    @Directory(DirectoryType.THEMES)
234
+    public Path getThemesPath(@Directory(DirectoryType.THEMES) final String directory) {
235
+        return new File(directory).toPath();
236
+    }
237
+
195 238
 }

+ 26
- 15
src/com/dmdirc/commandparser/aliases/ActionAliasMigrator.java View File

@@ -22,19 +22,23 @@
22 22
 
23 23
 package com.dmdirc.commandparser.aliases;
24 24
 
25
+import com.dmdirc.DMDircMBassador;
25 26
 import com.dmdirc.commandline.CommandLineOptionsModule.Directory;
26 27
 import com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
28
+import com.dmdirc.events.AppErrorEvent;
27 29
 import com.dmdirc.interfaces.Migrator;
30
+import com.dmdirc.logger.ErrorLevel;
28 31
 import com.dmdirc.util.io.ConfigFile;
29 32
 import com.dmdirc.util.io.InvalidConfigFileException;
30 33
 
31 34
 import com.google.common.base.Joiner;
32 35
 
33
-import java.io.File;
34 36
 import java.io.IOException;
37
+import java.nio.file.DirectoryStream;
38
+import java.nio.file.Files;
39
+import java.nio.file.Path;
35 40
 import java.util.Map;
36 41
 
37
-import javax.annotation.Nullable;
38 42
 import javax.inject.Inject;
39 43
 import javax.inject.Singleton;
40 44
 
@@ -44,9 +48,10 @@ import javax.inject.Singleton;
44 48
 @Singleton
45 49
 public class ActionAliasMigrator implements Migrator {
46 50
 
47
-    private final File directory;
51
+    private final Path directory;
48 52
     private final AliasFactory aliasFactory;
49 53
     private final AliasManager aliasManager;
54
+    private final DMDircMBassador eventBus;
50 55
 
51 56
     /**
52 57
      * Creates a new alias migrator.
@@ -54,33 +59,38 @@ public class ActionAliasMigrator implements Migrator {
54 59
      * @param directory    The base directory to read alias actions from.
55 60
      * @param aliasFactory The factory to use to create new aliases.
56 61
      * @param aliasManager The manager to add aliases to.
62
+     * @param eventBus     Bus to report errors on.
57 63
      */
58 64
     @Inject
59 65
     public ActionAliasMigrator(
60
-            @Directory(DirectoryType.ACTIONS) final String directory,
66
+            @Directory(DirectoryType.ACTIONS) final Path directory,
61 67
             final AliasFactory aliasFactory,
62
-            final AliasManager aliasManager) {
63
-        this.directory = new File(directory, "aliases");
68
+            final AliasManager aliasManager,
69
+            final DMDircMBassador eventBus) {
70
+        this.directory = directory.resolve("aliases");
64 71
         this.aliasFactory = aliasFactory;
65 72
         this.aliasManager = aliasManager;
73
+        this.eventBus = eventBus;
66 74
     }
67 75
 
68 76
     @Override
69 77
     public boolean needsMigration() {
70
-        return directory.exists();
78
+        return Files.isDirectory(directory);
71 79
     }
72 80
 
73 81
     @Override
74 82
     public void migrate() {
75
-        @Nullable final File[] files = directory.listFiles();
76
-        if (files != null) {
77
-            for (File child : files) {
78
-                if (migrate(child)) {
79
-                    child.delete();
83
+        try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) {
84
+            for (Path path : directoryStream) {
85
+                if (migrate(path)) {
86
+                    Files.delete(path);
80 87
                 }
81 88
             }
89
+            Files.delete(directory);
90
+        } catch (IOException ex) {
91
+            eventBus.publish(new AppErrorEvent(ErrorLevel.MEDIUM, ex,
92
+                    "Unable to migrate aliases", ex.getMessage()));
82 93
         }
83
-        directory.delete();
84 94
     }
85 95
 
86 96
     /**
@@ -90,9 +100,10 @@ public class ActionAliasMigrator implements Migrator {
90 100
      *
91 101
      * @return True if the file was migrated successfully, false otherwise.
92 102
      */
93
-    private boolean migrate(final File file) {
103
+    private boolean migrate(final Path file) {
94 104
         try {
95
-            final ConfigFile configFile = new ConfigFile(file);
105
+            // TODO: Make ConfigFile take a Path.
106
+            final ConfigFile configFile = new ConfigFile(file.toFile());
96 107
             configFile.read();
97 108
 
98 109
             final String response = Joiner.on('\n').join(configFile.getFlatDomain("response"));

+ 0
- 1
src/com/dmdirc/config/ConfigModule.java View File

@@ -56,7 +56,6 @@ import static com.dmdirc.commandline.CommandLineOptionsModule.DirectoryType;
56 56
 @Module(library = true, complete = false)
57 57
 public class ConfigModule {
58 58
 
59
-
60 59
     @Provides
61 60
     @Singleton
62 61
     public IdentityManager getIdentityManager(

+ 37
- 0
test-res/com/dmdirc/commandparser/aliases/op-greater-0 View File

@@ -0,0 +1,37 @@
1
+# This is a DMDirc configuration file.
2
+# Written on: Sun Nov 25 13:32:17 GMT 2007
3
+
4
+# This section indicates which sections below take key/value
5
+# pairs, rather than a simple list. It should be placed above
6
+# any sections that take key/values.
7
+keysections:
8
+  condition 0
9
+  condition 1
10
+  condition 2
11
+
12
+triggers:
13
+  UNKNOWN_COMMAND
14
+
15
+response:
16
+  /mode ooooooooooooooo $1-
17
+
18
+format:
19
+
20
+
21
+condition 0:
22
+  argument=1
23
+  component=STRING_STRING
24
+  target=op
25
+  comparison=STRING_EQUALS
26
+
27
+condition 1:
28
+  argument=2
29
+  component=STRINGARRAY_LENGTH
30
+  target=0
31
+  comparison=INT_GREATER
32
+
33
+condition 2:
34
+  argument=2
35
+  component=STRINGARRAY_LENGTH
36
+  target=16
37
+  comparison=INT_LESS

+ 30
- 0
test-res/com/dmdirc/commandparser/aliases/unset-equals-2 View File

@@ -0,0 +1,30 @@
1
+# This is a DMDirc configuration file.
2
+# Written on: Sun Nov 25 13:32:17 GMT 2007
3
+
4
+# This section indicates which sections below take key/value
5
+# pairs, rather than a simple list. It should be placed above
6
+# any sections that take key/values.
7
+keysections:
8
+  condition 0
9
+  condition 1
10
+
11
+triggers:
12
+  UNKNOWN_COMMAND
13
+
14
+response:
15
+  /set --unset $1-
16
+
17
+format:
18
+
19
+
20
+condition 0:
21
+  argument=1
22
+  component=STRING_STRING
23
+  target=unset
24
+  comparison=STRING_EQUALS
25
+
26
+condition 1:
27
+  argument=2
28
+  component=STRINGARRAY_LENGTH
29
+  target=2
30
+  comparison=INT_EQUALS

+ 93
- 0
test/com/dmdirc/commandparser/aliases/ActionAliasMigratorTest.java View File

@@ -0,0 +1,93 @@
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.aliases;
24
+
25
+import com.dmdirc.DMDircMBassador;
26
+
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
+
34
+import org.junit.Before;
35
+import org.junit.Ignore;
36
+import org.junit.Test;
37
+import org.junit.runner.RunWith;
38
+import org.mockito.Mock;
39
+import org.mockito.runners.MockitoJUnitRunner;
40
+
41
+import static org.junit.Assert.assertFalse;
42
+import static org.junit.Assert.assertTrue;
43
+
44
+@RunWith(MockitoJUnitRunner.class)
45
+public class ActionAliasMigratorTest {
46
+
47
+    @Mock private AliasManager aliasManager;
48
+    @Mock private AliasFactory aliasFactory;
49
+    @Mock private DMDircMBassador eventBus;
50
+
51
+    private FileSystem fs;
52
+    private ActionAliasMigrator migrator1;
53
+    private ActionAliasMigrator migrator2;
54
+
55
+    @Before
56
+    public void setup() throws IOException {
57
+        fs = Jimfs.newFileSystem(Configuration.unix());
58
+        Files.createDirectories(fs.getPath("test1/aliases"));
59
+        Files.createDirectories(fs.getPath("test2/other-stuff/aliases"));
60
+        Files.copy(getClass().getResource("op-greater-0").openStream(),
61
+                fs.getPath("test1/aliases/op"));
62
+        Files.copy(getClass().getResource("unset-equals-2").openStream(),
63
+                fs.getPath("test1/aliases/unset"));
64
+
65
+        migrator1 = new ActionAliasMigrator(fs.getPath("test1"), aliasFactory,
66
+                aliasManager, eventBus);
67
+        migrator2 = new ActionAliasMigrator(fs.getPath("test2"), aliasFactory,
68
+                aliasManager, eventBus);
69
+    }
70
+
71
+    @Test
72
+    public void testNeedMigration() {
73
+        assertTrue(migrator1.needsMigration());
74
+        assertFalse(migrator2.needsMigration());
75
+    }
76
+
77
+    @Test
78
+    @Ignore("Converts to a file, throws UnsupportedOperationException under JimFs")
79
+    public void testDeletesFilesAfterMigration() {
80
+        migrator1.migrate();
81
+        assertFalse(Files.exists(fs.getPath("test1/aliases/unset")));
82
+        assertFalse(Files.exists(fs.getPath("test1/aliases/op")));
83
+        assertFalse(Files.exists(fs.getPath("test1/aliases")));
84
+        assertTrue(Files.exists(fs.getPath("test1")));
85
+    }
86
+
87
+    @Test
88
+    public void testLeavesOtherFilesDuringMigration() {
89
+        migrator2.migrate();
90
+        assertTrue(Files.exists(fs.getPath("test2/other-stuff/aliases")));
91
+    }
92
+
93
+}

Loading…
Cancel
Save