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
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
30
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
31
 import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
31
 import com.dmdirc.interfaces.config.ReadOnlyConfigProvider;
32
 import com.dmdirc.logger.ErrorLevel;
32
 import com.dmdirc.logger.ErrorLevel;
33
+import com.dmdirc.util.SystemInfo;
33
 import com.dmdirc.util.io.StreamUtils;
34
 import com.dmdirc.util.io.StreamUtils;
34
 
35
 
35
 import java.io.BufferedReader;
36
 import java.io.BufferedReader;
57
     private final AggregateConfigProvider config;
58
     private final AggregateConfigProvider config;
58
     /** This plugin's settings domain. */
59
     /** This plugin's settings domain. */
59
     private final String domain;
60
     private final String domain;
61
+    /** System wrapper to use. */
62
+    private final SystemInfo systemInfo;
60
 
63
 
61
     /**
64
     /**
62
      * Create the IdentClient.
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
         this.eventBus = eventBus;
71
         this.eventBus = eventBus;
75
         this.server = server;
72
         this.server = server;
76
         this.socket = socket;
73
         this.socket = socket;
77
         this.connectionManager = connectionManager;
74
         this.connectionManager = connectionManager;
78
         this.config = config;
75
         this.config = config;
79
         this.domain = domain;
76
         this.domain = domain;
77
+        this.systemInfo = systemInfo;
80
     }
78
     }
81
 
79
 
82
     /**
80
     /**
149
             return String.format("%d , %d : ERROR : HIDDEN-USER", myPort, theirPort);
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
         final String os;
151
         final String os;
154
 
152
 
155
         final String customSystem = config.getOption(domain, "advanced.customSystem");
153
         final String customSystem = config.getOption(domain, "advanced.customSystem");
189
         } else if (connection != null && config.getOptionBool(domain, "general.useUsername")) {
187
         } else if (connection != null && config.getOptionBool(domain, "general.useUsername")) {
190
             username = connection.getLocalUser().flatMap(User::getUsername).orElse("Unknown");
188
             username = connection.getLocalUser().flatMap(User::getUsername).orElse("Unknown");
191
         } else {
189
         } else {
192
-            username = System.getProperty("user.name");
190
+            username = systemInfo.getProperty("user.name");
193
         }
191
         }
194
 
192
 
195
         return String.format("%d , %d : USERID : %s : %s", myPort, theirPort, escapeString(os),
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
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
29
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
30
 import com.dmdirc.logger.ErrorLevel;
30
 import com.dmdirc.logger.ErrorLevel;
31
 import com.dmdirc.plugins.PluginDomain;
31
 import com.dmdirc.plugins.PluginDomain;
32
+import com.dmdirc.util.SystemInfo;
32
 
33
 
33
 import java.io.IOException;
34
 import java.io.IOException;
34
 import java.net.ServerSocket;
35
 import java.net.ServerSocket;
59
     private final AggregateConfigProvider config;
60
     private final AggregateConfigProvider config;
60
     /** This plugin's config settings domain. */
61
     /** This plugin's config settings domain. */
61
     private final String domain;
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
      * Create the IdentdServer.
67
      * Create the IdentdServer.
72
     public IdentdServer(final DMDircMBassador eventBus,
75
     public IdentdServer(final DMDircMBassador eventBus,
73
             final ConnectionManager connectionManager,
76
             final ConnectionManager connectionManager,
74
             @GlobalConfig final AggregateConfigProvider config,
77
             @GlobalConfig final AggregateConfigProvider config,
75
-            @PluginDomain(IdentdPlugin.class) final String domain) {
78
+            @PluginDomain(IdentdPlugin.class) final String domain,
79
+            final SystemInfo systemInfo) {
76
         this.eventBus = eventBus;
80
         this.eventBus = eventBus;
77
         this.connectionManager = connectionManager;
81
         this.connectionManager = connectionManager;
78
         this.config = config;
82
         this.config = config;
79
         this.domain = domain;
83
         this.domain = domain;
84
+        this.systemInfo = systemInfo;
80
     }
85
     }
81
 
86
 
82
     /**
87
     /**
89
             try {
94
             try {
90
                 final Socket clientSocket = serverSocket.accept();
95
                 final Socket clientSocket = serverSocket.accept();
91
                 final IdentClient client = new IdentClient(eventBus, this, clientSocket,
96
                 final IdentClient client = new IdentClient(eventBus, this, clientSocket,
92
-                        connectionManager, config, domain);
97
+                        connectionManager, config, domain, systemInfo);
93
                 client.start();
98
                 client.start();
94
                 addClient(client);
99
                 addClient(client);
95
             } catch (IOException e) {
100
             } catch (IOException e) {

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

29
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
29
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
30
 import com.dmdirc.parser.irc.IRCClientInfo;
30
 import com.dmdirc.parser.irc.IRCClientInfo;
31
 import com.dmdirc.parser.irc.IRCParser;
31
 import com.dmdirc.parser.irc.IRCParser;
32
+import com.dmdirc.util.SystemInfo;
32
 
33
 
33
 import java.util.ArrayList;
34
 import java.util.ArrayList;
34
 import java.util.List;
35
 import java.util.List;
54
     @Mock private User user;
55
     @Mock private User user;
55
     @Mock private AggregateConfigProvider config;
56
     @Mock private AggregateConfigProvider config;
56
     @Mock private DMDircMBassador eventBus;
57
     @Mock private DMDircMBassador eventBus;
58
+    @Mock private SystemInfo systemInfo;
57
 
59
 
58
     protected IdentClient getClient() {
60
     protected IdentClient getClient() {
59
         final List<Connection> servers = new ArrayList<>();
61
         final List<Connection> servers = new ArrayList<>();
69
         when(user.getNickname()).thenReturn("nickname");
71
         when(user.getNickname()).thenReturn("nickname");
70
         when(user.getUsername()).thenReturn(Optional.of("username"));
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
     @Test
77
     @Test
156
         when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("a:b\\c,d");
158
         when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("a:b\\c,d");
157
         when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(false);
159
         when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(false);
158
         when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("");
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
         final String response = getClient().getIdentResponse("50, 50", acp);
164
         final String response = getClient().getIdentResponse("50, 50", acp);
161
         assertContains("Special characters must be quoted in system names",
165
         assertContains("Special characters must be quoted in system names",
170
         when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("");
174
         when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("");
171
         when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
175
         when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
172
         when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("a:b\\c,d");
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
         final String response = getClient().getIdentResponse("50, 50", acp);
180
         final String response = getClient().getIdentResponse("50, 50", acp);
175
         assertContains("Special characters must be quoted in custom names",
181
         assertContains("Special characters must be quoted in custom names",
184
         when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("system");
190
         when(acp.getOption("plugin-Identd", "advanced.customSystem")).thenReturn("system");
185
         when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
191
         when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
186
         when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("name");
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
         final String response = getClient().getIdentResponse("50, 60", acp);
196
         final String response = getClient().getIdentResponse("50, 60", acp);
189
         final String[] bits = response.split(":");
197
         final String[] bits = response.split(":");
200
     public void testOSWindows() {
208
     public void testOSWindows() {
201
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
209
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
202
         when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
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
         final String response = getClient().getIdentResponse("50, 50", acp);
214
         final String response = getClient().getIdentResponse("50, 50", acp);
207
         assertEquals("50 , 50 : USERID : WIN32 : test", response);
215
         assertEquals("50 , 50 : USERID : WIN32 : test", response);
211
     public void testOSMac() {
219
     public void testOSMac() {
212
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
220
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
213
         when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
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
         final String response = getClient().getIdentResponse("50, 50", acp);
225
         final String response = getClient().getIdentResponse("50, 50", acp);
218
         assertEquals("50 , 50 : USERID : MACOS : test", response);
226
         assertEquals("50 , 50 : USERID : MACOS : test", response);
222
     public void testOSLinux() {
230
     public void testOSLinux() {
223
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
231
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
224
         when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
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
         final String response = getClient().getIdentResponse("50, 50", acp);
236
         final String response = getClient().getIdentResponse("50, 50", acp);
229
         assertEquals("50 , 50 : USERID : UNIX : test", response);
237
         assertEquals("50 , 50 : USERID : UNIX : test", response);
233
     public void testOSBSD() {
241
     public void testOSBSD() {
234
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
242
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
235
         when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
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
         final String response = getClient().getIdentResponse("50, 50", acp);
247
         final String response = getClient().getIdentResponse("50, 50", acp);
240
         assertEquals("50 , 50 : USERID : UNIX-BSD : test", response);
248
         assertEquals("50 , 50 : USERID : UNIX-BSD : test", response);
244
     public void testOSOS2() {
252
     public void testOSOS2() {
245
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
253
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
246
         when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
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
         final String response = getClient().getIdentResponse("50, 50", acp);
258
         final String response = getClient().getIdentResponse("50, 50", acp);
251
         assertEquals("50 , 50 : USERID : OS/2 : test", response);
259
         assertEquals("50 , 50 : USERID : OS/2 : test", response);
255
     public void testOSUnix() {
263
     public void testOSUnix() {
256
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
264
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
257
         when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
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
         final String response = getClient().getIdentResponse("50, 50", acp);
269
         final String response = getClient().getIdentResponse("50, 50", acp);
262
         assertEquals("50 , 50 : USERID : UNIX : test", response);
270
         assertEquals("50 , 50 : USERID : UNIX : test", response);
266
     public void testOSIrix() {
274
     public void testOSIrix() {
267
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
275
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
268
         when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
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
         final String response = getClient().getIdentResponse("50, 50", acp);
280
         final String response = getClient().getIdentResponse("50, 50", acp);
273
         assertEquals("50 , 50 : USERID : IRIX : test", response);
281
         assertEquals("50 , 50 : USERID : IRIX : test", response);
277
     public void testOSUnknown() {
285
     public void testOSUnknown() {
278
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
286
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
279
         when(acp.getOptionBool("plugin-Identd", "advanced.useCustomSystem")).thenReturn(false);
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
         final String response = getClient().getIdentResponse("50, 50", acp);
291
         final String response = getClient().getIdentResponse("50, 50", acp);
295
         assertEquals("50 , 50 : USERID : UNKNOWN : test", response);
292
         assertEquals("50 , 50 : USERID : UNKNOWN : test", response);
300
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
297
         when(acp.getOptionBool("plugin-Identd", "advanced.alwaysOn")).thenReturn(true);
301
         when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
298
         when(acp.getOptionBool("plugin-Identd", "general.useCustomName")).thenReturn(true);
302
         when(acp.getOption("plugin-Identd", "general.customName")).thenReturn("name");
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
         final String response = getClient().getIdentResponse("50, 50", acp);
303
         final String response = getClient().getIdentResponse("50, 50", acp);
307
         assertEquals("50 , 50 : USERID : UNKNOWN : name", response);
304
         assertEquals("50 , 50 : USERID : UNKNOWN : name", response);
310
     @Test
307
     @Test
311
     public void testNameNickname() {
308
     public void testNameNickname() {
312
         when(acp.getOptionBool("plugin-Identd", "general.useNickname")).thenReturn(true);
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
         final String response = getClient().getIdentResponse("60, 50", acp);
313
         final String response = getClient().getIdentResponse("60, 50", acp);
317
         assertEquals("60 , 50 : USERID : UNKNOWN : nickname", response);
314
         assertEquals("60 , 50 : USERID : UNKNOWN : nickname", response);
320
     @Test
317
     @Test
321
     public void testNameUsername() {
318
     public void testNameUsername() {
322
         when(acp.getOptionBool("plugin-Identd", "general.useUsername")).thenReturn(true);
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
         final String response = getClient().getIdentResponse("60, 50", acp);
323
         final String response = getClient().getIdentResponse("60, 50", acp);
327
         assertEquals("60 , 50 : USERID : UNKNOWN : username", response);
324
         assertEquals("60 , 50 : USERID : UNKNOWN : username", response);
328
     }
325
     }
329
 
326
 
330
     private static void assertContains(final String msg, final String haystack,
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
     private static void assertStartsWith(final String msg, final String haystack,
332
     private static void assertStartsWith(final String msg, final String haystack,

Loading…
Cancel
Save