Browse Source

Use SystemInfo in IdentClient.

This allows the properties to be mocked out in tests, instead
of trying to set global system properties which screw up the
JVM...
pull/246/head
Chris Smith 9 years ago
parent
commit
c8d7982cae

+ 10
- 12
identd/src/com/dmdirc/addons/identd/IdentClient.java View File

@@ -30,6 +30,7 @@ import com.dmdirc.interfaces.User;
30 30
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
31 31
 import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
32 32
 import com.dmdirc.logger.ErrorLevel;
33
+import com.dmdirc.util.SystemInfo;
33 34
 import com.dmdirc.util.io.StreamUtils;
34 35
 
35 36
 import java.io.BufferedReader;
@@ -57,26 +58,23 @@ public class IdentClient implements Runnable {
57 58
     private final AggregateConfigProvider config;
58 59
     /** This plugin's settings domain. */
59 60
     private final String domain;
61
+    /** System wrapper to use. */
62
+    private final SystemInfo systemInfo;
60 63
 
61 64
     /**
62 65
      * Create the IdentClient.
63
-     *
64
-     * @param eventBus      The event bus to post errors on
65
-     * @param server        The server that owns this
66
-     * @param socket        The socket we are handing
67
-     * @param connectionManager Server manager to retrieve servers from
68
-     * @param config        Global config to read settings from
69
-     * @param domain        This plugin's settings domain
70 66
      */
71
-    public IdentClient(final DMDircMBassador eventBus, final IdentdServer server, final Socket socket,
72
-            final ConnectionManager connectionManager, final AggregateConfigProvider config,
73
-            final String domain) {
67
+    public IdentClient(final DMDircMBassador eventBus, final IdentdServer server,
68
+            final Socket socket, final ConnectionManager connectionManager,
69
+            final AggregateConfigProvider config, final String domain,
70
+            final SystemInfo systemInfo) {
74 71
         this.eventBus = eventBus;
75 72
         this.server = server;
76 73
         this.socket = socket;
77 74
         this.connectionManager = connectionManager;
78 75
         this.config = config;
79 76
         this.domain = domain;
77
+        this.systemInfo = systemInfo;
80 78
     }
81 79
 
82 80
     /**
@@ -149,7 +147,7 @@ public class IdentClient implements Runnable {
149 147
             return String.format("%d , %d : ERROR : HIDDEN-USER", myPort, theirPort);
150 148
         }
151 149
 
152
-        final String osName = System.getProperty("os.name").toLowerCase();
150
+        final String osName = systemInfo.getProperty("os.name").toLowerCase();
153 151
         final String os;
154 152
 
155 153
         final String customSystem = config.getOption(domain, "advanced.customSystem");
@@ -189,7 +187,7 @@ public class IdentClient implements Runnable {
189 187
         } else if (connection != null && config.getOptionBool(domain, "general.useUsername")) {
190 188
             username = connection.getLocalUser().flatMap(User::getUsername).orElse("Unknown");
191 189
         } else {
192
-            username = System.getProperty("user.name");
190
+            username = systemInfo.getProperty("user.name");
193 191
         }
194 192
 
195 193
         return String.format("%d , %d : USERID : %s : %s", myPort, theirPort, escapeString(os),

+ 7
- 2
identd/src/com/dmdirc/addons/identd/IdentdServer.java View File

@@ -29,6 +29,7 @@ import com.dmdirc.interfaces.ConnectionManager;
29 29
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
30 30
 import com.dmdirc.logger.ErrorLevel;
31 31
 import com.dmdirc.plugins.PluginDomain;
32
+import com.dmdirc.util.SystemInfo;
32 33
 
33 34
 import java.io.IOException;
34 35
 import java.net.ServerSocket;
@@ -59,6 +60,8 @@ public final class IdentdServer implements Runnable {
59 60
     private final AggregateConfigProvider config;
60 61
     /** This plugin's config settings domain. */
61 62
     private final String domain;
63
+    /** System info wrapper to use for the ident client. */
64
+    private final SystemInfo systemInfo;
62 65
 
63 66
     /**
64 67
      * Create the IdentdServer.
@@ -72,11 +75,13 @@ public final class IdentdServer implements Runnable {
72 75
     public IdentdServer(final DMDircMBassador eventBus,
73 76
             final ConnectionManager connectionManager,
74 77
             @GlobalConfig final AggregateConfigProvider config,
75
-            @PluginDomain(IdentdPlugin.class) final String domain) {
78
+            @PluginDomain(IdentdPlugin.class) final String domain,
79
+            final SystemInfo systemInfo) {
76 80
         this.eventBus = eventBus;
77 81
         this.connectionManager = connectionManager;
78 82
         this.config = config;
79 83
         this.domain = domain;
84
+        this.systemInfo = systemInfo;
80 85
     }
81 86
 
82 87
     /**
@@ -89,7 +94,7 @@ public final class IdentdServer implements Runnable {
89 94
             try {
90 95
                 final Socket clientSocket = serverSocket.accept();
91 96
                 final IdentClient client = new IdentClient(eventBus, this, clientSocket,
92
-                        connectionManager, config, domain);
97
+                        connectionManager, config, domain, systemInfo);
93 98
                 client.start();
94 99
                 addClient(client);
95 100
             } catch (IOException e) {

+ 33
- 36
identd/test/com/dmdirc/addons/identd/IdentClientTest.java View File

@@ -29,6 +29,7 @@ import com.dmdirc.interfaces.User;
29 29
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
30 30
 import com.dmdirc.parser.irc.IRCClientInfo;
31 31
 import com.dmdirc.parser.irc.IRCParser;
32
+import com.dmdirc.util.SystemInfo;
32 33
 
33 34
 import java.util.ArrayList;
34 35
 import java.util.List;
@@ -54,6 +55,7 @@ public class IdentClientTest {
54 55
     @Mock private User user;
55 56
     @Mock private AggregateConfigProvider config;
56 57
     @Mock private DMDircMBassador eventBus;
58
+    @Mock private SystemInfo systemInfo;
57 59
 
58 60
     protected IdentClient getClient() {
59 61
         final List<Connection> servers = new ArrayList<>();
@@ -69,7 +71,7 @@ public class IdentClientTest {
69 71
         when(user.getNickname()).thenReturn("nickname");
70 72
         when(user.getUsername()).thenReturn(Optional.of("username"));
71 73
 
72
-        return new IdentClient(eventBus, null, null, sm, config, "plugin-Identd");
74
+        return new IdentClient(eventBus, null, null, sm, config, "plugin-Identd", systemInfo);
73 75
     }
74 76
 
75 77
     @Test
@@ -156,6 +158,8 @@ public class IdentClientTest {
156 158
         when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("a:b\\c,d");
157 159
         when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(false);
158 160
         when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("");
161
+        when(systemInfo.getProperty("user.name")).thenReturn("test");
162
+        when(systemInfo.getProperty("os.name")).thenReturn("test");
159 163
 
160 164
         final String response = getClient().getIdentResponse("50, 50", acp);
161 165
         assertContains("Special characters must be quoted in system names",
@@ -170,6 +174,8 @@ public class IdentClientTest {
170 174
         when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("");
171 175
         when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
172 176
         when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("a:b\\c,d");
177
+        when(systemInfo.getProperty("user.name")).thenReturn("test");
178
+        when(systemInfo.getProperty("os.name")).thenReturn("test");
173 179
 
174 180
         final String response = getClient().getIdentResponse("50, 50", acp);
175 181
         assertContains("Special characters must be quoted in custom names",
@@ -184,6 +190,8 @@ public class IdentClientTest {
184 190
         when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("system");
185 191
         when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
186 192
         when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("name");
193
+        when(systemInfo.getProperty("user.name")).thenReturn("test");
194
+        when(systemInfo.getProperty("os.name")).thenReturn("test");
187 195
 
188 196
         final String response = getClient().getIdentResponse("50, 60", acp);
189 197
         final String[] bits = response.split(":");
@@ -200,8 +208,8 @@ public class IdentClientTest {
200 208
     public void testOSWindows() {
201 209
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
202 210
         when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
203
-        System.setProperty("user.name", "test");
204
-        System.setProperty("os.name", "windows");
211
+        when(systemInfo.getProperty("user.name")).thenReturn("test");
212
+        when(systemInfo.getProperty("os.name")).thenReturn("windows");
205 213
 
206 214
         final String response = getClient().getIdentResponse("50, 50", acp);
207 215
         assertEquals("50 , 50 : USERID : WIN32 : test", response);
@@ -211,8 +219,8 @@ public class IdentClientTest {
211 219
     public void testOSMac() {
212 220
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
213 221
         when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
214
-        System.setProperty("user.name", "test");
215
-        System.setProperty("os.name", "mac");
222
+        when(systemInfo.getProperty("user.name")).thenReturn("test");
223
+        when(systemInfo.getProperty("os.name")).thenReturn("mac");
216 224
 
217 225
         final String response = getClient().getIdentResponse("50, 50", acp);
218 226
         assertEquals("50 , 50 : USERID : MACOS : test", response);
@@ -222,8 +230,8 @@ public class IdentClientTest {
222 230
     public void testOSLinux() {
223 231
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
224 232
         when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
225
-        System.setProperty("user.name", "test");
226
-        System.setProperty("os.name", "linux");
233
+        when(systemInfo.getProperty("user.name")).thenReturn("test");
234
+        when(systemInfo.getProperty("os.name")).thenReturn("linux");
227 235
 
228 236
         final String response = getClient().getIdentResponse("50, 50", acp);
229 237
         assertEquals("50 , 50 : USERID : UNIX : test", response);
@@ -233,8 +241,8 @@ public class IdentClientTest {
233 241
     public void testOSBSD() {
234 242
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
235 243
         when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
236
-        System.setProperty("user.name", "test");
237
-        System.setProperty("os.name", "bsd");
244
+        when(systemInfo.getProperty("user.name")).thenReturn("test");
245
+        when(systemInfo.getProperty("os.name")).thenReturn("bsd");
238 246
 
239 247
         final String response = getClient().getIdentResponse("50, 50", acp);
240 248
         assertEquals("50 , 50 : USERID : UNIX-BSD : test", response);
@@ -244,8 +252,8 @@ public class IdentClientTest {
244 252
     public void testOSOS2() {
245 253
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
246 254
         when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
247
-        System.setProperty("user.name", "test");
248
-        System.setProperty("os.name", "os/2");
255
+        when(systemInfo.getProperty("user.name")).thenReturn("test");
256
+        when(systemInfo.getProperty("os.name")).thenReturn("os/2");
249 257
 
250 258
         final String response = getClient().getIdentResponse("50, 50", acp);
251 259
         assertEquals("50 , 50 : USERID : OS/2 : test", response);
@@ -255,8 +263,8 @@ public class IdentClientTest {
255 263
     public void testOSUnix() {
256 264
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
257 265
         when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
258
-        System.setProperty("user.name", "test");
259
-        System.setProperty("os.name", "unix");
266
+        when(systemInfo.getProperty("user.name")).thenReturn("test");
267
+        when(systemInfo.getProperty("os.name")).thenReturn("unix");
260 268
 
261 269
         final String response = getClient().getIdentResponse("50, 50", acp);
262 270
         assertEquals("50 , 50 : USERID : UNIX : test", response);
@@ -266,8 +274,8 @@ public class IdentClientTest {
266 274
     public void testOSIrix() {
267 275
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
268 276
         when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
269
-        System.setProperty("user.name", "test");
270
-        System.setProperty("os.name", "irix");
277
+        when(systemInfo.getProperty("user.name")).thenReturn("test");
278
+        when(systemInfo.getProperty("os.name")).thenReturn("irix");
271 279
 
272 280
         final String response = getClient().getIdentResponse("50, 50", acp);
273 281
         assertEquals("50 , 50 : USERID : IRIX : test", response);
@@ -277,19 +285,8 @@ public class IdentClientTest {
277 285
     public void testOSUnknown() {
278 286
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
279 287
         when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
280
-        System.setProperty("user.name", "test");
281
-        System.setProperty("os.name", "test");
282
-
283
-        final String response = getClient().getIdentResponse("50, 50", acp);
284
-        assertEquals("50 , 50 : USERID : UNKNOWN : test", response);
285
-    }
286
-
287
-    @Test
288
-    public void testNameSystem() {
289
-        when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
290
-        when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
291
-        System.setProperty("user.name", "test");
292
-        System.setProperty("os.name", "test");
288
+        when(systemInfo.getProperty("user.name")).thenReturn("test");
289
+        when(systemInfo.getProperty("os.name")).thenReturn("test");
293 290
 
294 291
         final String response = getClient().getIdentResponse("50, 50", acp);
295 292
         assertEquals("50 , 50 : USERID : UNKNOWN : test", response);
@@ -300,8 +297,8 @@ public class IdentClientTest {
300 297
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
301 298
         when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
302 299
         when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("name");
303
-        System.setProperty("user.name", "test");
304
-        System.setProperty("os.name", "test");
300
+        when(systemInfo.getProperty("user.name")).thenReturn("test");
301
+        when(systemInfo.getProperty("os.name")).thenReturn("test");
305 302
 
306 303
         final String response = getClient().getIdentResponse("50, 50", acp);
307 304
         assertEquals("50 , 50 : USERID : UNKNOWN : name", response);
@@ -310,8 +307,8 @@ public class IdentClientTest {
310 307
     @Test
311 308
     public void testNameNickname() {
312 309
         when(acp.getOptionBool("plugin-Identd", "general.useNickname")).thenReturn(true);
313
-        System.setProperty("user.name", "test");
314
-        System.setProperty("os.name", "test");
310
+        when(systemInfo.getProperty("user.name")).thenReturn("test");
311
+        when(systemInfo.getProperty("os.name")).thenReturn("test");
315 312
 
316 313
         final String response = getClient().getIdentResponse("60, 50", acp);
317 314
         assertEquals("60 , 50 : USERID : UNKNOWN : nickname", response);
@@ -320,16 +317,16 @@ public class IdentClientTest {
320 317
     @Test
321 318
     public void testNameUsername() {
322 319
         when(acp.getOptionBool("plugin-Identd", "general.useUsername")).thenReturn(true);
323
-        System.setProperty("user.name", "test");
324
-        System.setProperty("os.name", "test");
320
+        when(systemInfo.getProperty("user.name")).thenReturn("test");
321
+        when(systemInfo.getProperty("os.name")).thenReturn("test");
325 322
 
326 323
         final String response = getClient().getIdentResponse("60, 50", acp);
327 324
         assertEquals("60 , 50 : USERID : UNKNOWN : username", response);
328 325
     }
329 326
 
330 327
     private static void assertContains(final String msg, final String haystack,
331
-            final String needle) {
332
-        assertTrue(msg, haystack.indexOf(needle) > -1);
328
+            final CharSequence needle) {
329
+        assertTrue(msg, haystack.contains(needle));
333 330
     }
334 331
 
335 332
     private static void assertStartsWith(final String msg, final String haystack,

Loading…
Cancel
Save