Sfoglia il codice sorgente

Add an injectable wrapper around System.

Not 100% sure this is a good idea though.
pull/309/head
Greg Holmes 9 anni fa
parent
commit
749233b293

+ 4
- 2
src/com/dmdirc/LauncherUtils.java Vedi File

@@ -25,6 +25,7 @@ package com.dmdirc;
25 25
 import com.dmdirc.commandline.BaseDirectoryLocator;
26 26
 import com.dmdirc.commandline.CommandLineParser;
27 27
 import com.dmdirc.updater.Version;
28
+import com.dmdirc.util.SystemInfo;
28 29
 
29 30
 /**
30 31
  * Utility class to help launcher interface with client.
@@ -39,8 +40,9 @@ public class LauncherUtils {
39 40
      * @return Returns the config directory to use
40 41
      */
41 42
     public static String getDirectory(final String... args) {
42
-        final CommandLineParser cliParser = new CommandLineParser(null, null, null);
43
-        final BaseDirectoryLocator locator = new BaseDirectoryLocator();
43
+        final SystemInfo systemInfo = new SystemInfo();
44
+        final CommandLineParser cliParser = new CommandLineParser(null, null, null, systemInfo);
45
+        final BaseDirectoryLocator locator = new BaseDirectoryLocator(systemInfo);
44 46
         cliParser.parse(args);
45 47
         final String configDirectory = cliParser.getConfigDirectory();
46 48
         return configDirectory == null ? locator.getDefaultBaseDirectory() : configDirectory;

+ 18
- 12
src/com/dmdirc/commandline/BaseDirectoryLocator.java Vedi File

@@ -22,6 +22,8 @@
22 22
 
23 23
 package com.dmdirc.commandline;
24 24
 
25
+import com.dmdirc.util.SystemInfo;
26
+
25 27
 import java.io.File;
26 28
 
27 29
 import javax.inject.Inject;
@@ -31,8 +33,12 @@ import javax.inject.Inject;
31 33
  */
32 34
 public class BaseDirectoryLocator {
33 35
 
36
+    private final SystemInfo systemInfo;
37
+
34 38
     @Inject
35
-    public BaseDirectoryLocator(){}
39
+    public BaseDirectoryLocator(final SystemInfo systemInfo) {
40
+        this.systemInfo = systemInfo;
41
+    }
36 42
 
37 43
     /**
38 44
      * Initialises the location of the configuration directory.
@@ -40,28 +46,28 @@ public class BaseDirectoryLocator {
40 46
      * @return Directory
41 47
      */
42 48
     public String getDefaultBaseDirectory() {
43
-        final String fs = System.getProperty("file.separator");
44
-        final String osName = System.getProperty("os.name");
49
+        final String fs = systemInfo.getProperty("file.separator");
50
+        final String osName = systemInfo.getProperty("os.name");
45 51
         String configdir;
46 52
 
47
-        if (System.getenv("DMDIRC_HOME") != null) {
48
-            configdir = System.getenv("DMDIRC_HOME");
53
+        if (systemInfo.getenv("DMDIRC_HOME") != null) {
54
+            configdir = systemInfo.getenv("DMDIRC_HOME");
49 55
         } else if (osName.startsWith("Mac OS")) {
50
-            configdir = System.getProperty("user.home") + fs + "Library"
56
+            configdir = systemInfo.getProperty("user.home") + fs + "Library"
51 57
                     + fs + "Preferences" + fs + "DMDirc" + fs;
52 58
         } else if (osName.startsWith("Windows")) {
53
-            if (System.getenv("APPDATA") == null) {
54
-                configdir = System.getProperty("user.home") + fs + "DMDirc" + fs;
59
+            if (systemInfo.getenv("APPDATA") == null) {
60
+                configdir = systemInfo.getProperty("user.home") + fs + "DMDirc" + fs;
55 61
             } else {
56
-                configdir = System.getenv("APPDATA") + fs + "DMDirc" + fs;
62
+                configdir = systemInfo.getenv("APPDATA") + fs + "DMDirc" + fs;
57 63
             }
58 64
         } else {
59
-            configdir = System.getProperty("user.home") + fs + ".DMDirc" + fs;
65
+            configdir = systemInfo.getProperty("user.home") + fs + ".DMDirc" + fs;
60 66
             final File testFile = new File(configdir);
61 67
             if (!testFile.exists()) {
62
-                final String configHome = System.getenv("XDG_CONFIG_HOME");
68
+                final String configHome = systemInfo.getenv("XDG_CONFIG_HOME");
63 69
                 configdir = (configHome == null || configHome.isEmpty())
64
-                        ? System.getProperty("user.home") + fs + ".config" + fs
70
+                        ? systemInfo.getProperty("user.home") + fs + ".config" + fs
65 71
                         : configHome;
66 72
                 configdir += fs + "DMDirc" + fs;
67 73
             }

+ 7
- 2
src/com/dmdirc/commandline/CommandLineParser.java Vedi File

@@ -26,6 +26,7 @@ import com.dmdirc.ClientModule.GlobalConfig;
26 26
 import com.dmdirc.interfaces.ConnectionManager;
27 27
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
28 28
 import com.dmdirc.util.InvalidURIException;
29
+import com.dmdirc.util.SystemInfo;
29 30
 import com.dmdirc.util.URIParser;
30 31
 
31 32
 import java.io.File;
@@ -72,6 +73,8 @@ public class CommandLineParser {
72 73
     @Nullable private final Provider<AggregateConfigProvider> globalConfigProvider;
73 74
     /** The parser to use for URIs. */
74 75
     @Nullable private final URIParser uriParser;
76
+    /** Used to retrieve informationa about the running system. */
77
+    private final SystemInfo systemInfo;
75 78
     /** Whether to disable error reporting or not. */
76 79
     private boolean disablereporting;
77 80
     /** The version string passed for the launcher. */
@@ -92,10 +95,12 @@ public class CommandLineParser {
92 95
     public CommandLineParser(
93 96
             @Nullable final Provider<ConnectionManager> serverManagerProvider,
94 97
             @Nullable @GlobalConfig final Provider<AggregateConfigProvider> globalConfigProvider,
95
-            @Nullable final URIParser uriParser) {
98
+            @Nullable final URIParser uriParser,
99
+            final SystemInfo systemInfo) {
96 100
         this.serverManagerProvider = serverManagerProvider;
97 101
         this.globalConfigProvider = globalConfigProvider;
98 102
         this.uriParser = uriParser;
103
+        this.systemInfo = systemInfo;
99 104
     }
100 105
 
101 106
     /**
@@ -237,7 +242,7 @@ public class CommandLineParser {
237 242
                 launcherVersion = param;
238 243
                 break;
239 244
             case 'p':
240
-                doDirectory(Paths.get(System.getProperty("user.dir")));
245
+                doDirectory(Paths.get(systemInfo.getProperty("user.dir")));
241 246
                 break;
242 247
             case 'r':
243 248
                 disablereporting = true;

+ 3
- 2
src/com/dmdirc/config/profiles/ProfileManager.java Vedi File

@@ -25,6 +25,7 @@ package com.dmdirc.config.profiles;
25 25
 import com.dmdirc.DMDircMBassador;
26 26
 import com.dmdirc.events.ProfileAddedEvent;
27 27
 import com.dmdirc.events.ProfileDeletedEvent;
28
+import com.dmdirc.util.SystemInfo;
28 29
 
29 30
 import com.google.common.collect.Iterables;
30 31
 import com.google.common.collect.Lists;
@@ -49,10 +50,10 @@ public class ProfileManager {
49 50
     private final Profile defaultProfile;
50 51
 
51 52
     @Inject
52
-    public ProfileManager(final DMDircMBassador eventBus) {
53
+    public ProfileManager(final DMDircMBassador eventBus, final SystemInfo systemInfo) {
53 54
         this.eventBus = eventBus;
54 55
         profiles = new ArrayList<>();
55
-        final String nick = System.getProperty("user.name").replace(' ', '_');
56
+        final String nick = systemInfo.getProperty("user.name").replace(' ', '_');
56 57
         defaultProfile = new Profile(nick, nick, Optional.empty(), Lists.newArrayList(nick));
57 58
     }
58 59
 

+ 43
- 0
src/com/dmdirc/util/SystemInfo.java Vedi File

@@ -0,0 +1,43 @@
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.util;
24
+
25
+import javax.inject.Inject;
26
+
27
+/**
28
+ * Injectable wrapper around {@link System}
29
+ */
30
+public class SystemInfo {
31
+
32
+    @Inject
33
+    public SystemInfo() {
34
+    }
35
+
36
+    public String getProperty(final String key) {
37
+        return System.getProperty(key);
38
+    }
39
+
40
+    public String getenv(final String name) {
41
+        return System.getenv(name);
42
+    }
43
+}

+ 6
- 2
test/com/dmdirc/config/profiles/ProfileManagerTest.java Vedi File

@@ -25,6 +25,7 @@ package com.dmdirc.config.profiles;
25 25
 import com.dmdirc.DMDircMBassador;
26 26
 import com.dmdirc.events.ProfileAddedEvent;
27 27
 import com.dmdirc.events.ProfileDeletedEvent;
28
+import com.dmdirc.util.SystemInfo;
28 29
 
29 30
 import com.google.common.collect.Lists;
30 31
 
@@ -42,6 +43,7 @@ import static junit.framework.TestCase.assertTrue;
42 43
 import static org.mockito.Matchers.any;
43 44
 import static org.mockito.Mockito.reset;
44 45
 import static org.mockito.Mockito.verify;
46
+import static org.mockito.Mockito.when;
45 47
 
46 48
 @RunWith(MockitoJUnitRunner.class)
47 49
 public class ProfileManagerTest {
@@ -49,12 +51,14 @@ public class ProfileManagerTest {
49 51
     @Mock private DMDircMBassador eventBus;
50 52
     @Mock private Profile profile1;
51 53
     @Mock private Profile profile2;
54
+    @Mock private SystemInfo systemInfo;
52 55
 
53 56
     private ProfileManager instance;
54 57
 
55 58
     @Before
56 59
     public void setUp() throws Exception {
57
-        instance = new ProfileManager(eventBus);
60
+        when(systemInfo.getProperty("user.name")).thenReturn("UserName");
61
+        instance = new ProfileManager(eventBus, systemInfo);
58 62
     }
59 63
 
60 64
     @Test
@@ -95,7 +99,7 @@ public class ProfileManagerTest {
95 99
 
96 100
     @Test
97 101
     public void testGetDefaultProfile_EmptyList() {
98
-        final String nick = System.getProperty("user.name").replace(' ', '_');
102
+        final String nick = "UserName";
99 103
         final Profile profile = new Profile(nick, nick, Optional.empty(), Lists.newArrayList(nick));
100 104
         assertEquals(profile, instance.getDefault());
101 105
     }

Loading…
Annulla
Salva