Переглянути джерело

Issue 2736.

tags/0.6.3m2a1
Chris Smith 15 роки тому
джерело
коміт
f55a6902f2

+ 18
- 3
src/com/dmdirc/ParserFactory.java Переглянути файл

22
 
22
 
23
 package com.dmdirc;
23
 package com.dmdirc;
24
 
24
 
25
+import com.dmdirc.logger.ErrorLevel;
26
+import com.dmdirc.logger.Logger;
25
 import com.dmdirc.parser.interfaces.Parser;
27
 import com.dmdirc.parser.interfaces.Parser;
26
 import com.dmdirc.parser.irc.IRCParser;
28
 import com.dmdirc.parser.irc.IRCParser;
27
 import com.dmdirc.parser.irc.MyInfo;
29
 import com.dmdirc.parser.irc.MyInfo;
45
      * @since 0.6.3m2
47
      * @since 0.6.3m2
46
      */
48
      */
47
     public Parser getParser(final MyInfo myInfo, final IrcAddress address) {
49
     public Parser getParser(final MyInfo myInfo, final IrcAddress address) {
48
-        final ServerInfo info = new ServerInfo(address.getServer(), address.getPort(),
50
+        // TODO: Hacky Hack McHack
51
+        final ServerInfo info = new ServerInfo(address.getServer(), address.getPort(6667),
49
                 address.getPassword());
52
                 address.getPassword());
50
         info.setSSL(address.isSSL());
53
         info.setSSL(address.isSSL());
51
-        
52
-        return new IRCParser(myInfo, info);
54
+
55
+        if ("irc".equals(address.getProtocol())) {
56
+            return new IRCParser(myInfo, info);
57
+        } else if ("irc-test".equals(address.getProtocol())) {
58
+            try {
59
+                return (Parser) Class.forName("com.dmdirc.harness.parser.TestParser")
60
+                        .getConstructor(MyInfo.class, ServerInfo.class)
61
+                        .newInstance(myInfo, info);
62
+            } catch (Exception ex) {
63
+                Logger.userError(ErrorLevel.UNKNOWN, "Unable to create parser", ex);
64
+            }
65
+        }
66
+
67
+        return null;
53
     }
68
     }
54
 
69
 
55
 }
70
 }

+ 97
- 11
src/com/dmdirc/util/IrcAddress.java Переглянути файл

50
 
50
 
51
     /** Whether or not this address uses SSL. */
51
     /** Whether or not this address uses SSL. */
52
     private boolean usesSSL;
52
     private boolean usesSSL;
53
+    /** The protocol for this address. */
54
+    private String protocol;
53
     /** The server name for this address. */
55
     /** The server name for this address. */
54
     private String server;
56
     private String server;
55
     /** The port number for this address. */
57
     /** The port number for this address. */
56
-    private int port = 6667;
58
+    private int port;
57
     /** A list of channels to auto-connect to. */
59
     /** A list of channels to auto-connect to. */
58
     private List<String> channels = new ArrayList<String>();
60
     private List<String> channels = new ArrayList<String>();
59
     /** The password for this address. */
61
     /** The password for this address. */
80
         } catch (URISyntaxException ex) {
82
         } catch (URISyntaxException ex) {
81
             throw new InvalidAddressException("Unable to parse URI", ex);
83
             throw new InvalidAddressException("Unable to parse URI", ex);
82
         }
84
         }
83
-        
84
-        if (uri.getScheme() != null && uri.getScheme().equalsIgnoreCase("ircs")) {
85
-            usesSSL = true;
86
-        } else if (uri.getScheme() == null || !uri.getScheme().equalsIgnoreCase("irc")) {
85
+
86
+        if (uri.getScheme() == null) {
87
             throw new InvalidAddressException("Invalid protocol specified");
87
             throw new InvalidAddressException("Invalid protocol specified");
88
         }
88
         }
89
+
90
+        protocol = uri.getScheme();
91
+
92
+        if (protocol.endsWith("s")) {
93
+            protocol = protocol.substring(0, protocol.length() - 1);
94
+            usesSSL = true;
95
+        }
89
         
96
         
90
         if (uri.getUserInfo() != null) {
97
         if (uri.getUserInfo() != null) {
91
             doPass(uri.getUserInfo());
98
             doPass(uri.getUserInfo());
95
             "?" + uri.getQuery()) + (uri.getFragment() == null ? "" :
102
             "?" + uri.getQuery()) + (uri.getFragment() == null ? "" :
96
             "#" + uri.getFragment()));
103
             "#" + uri.getFragment()));
97
 
104
 
98
-        if (uri.getPort() > -1) {
99
-            doPort(uri.getPort());
100
-        }
105
+        doPort(uri.getPort());
101
 
106
 
102
         if (uri.getHost() == null) {
107
         if (uri.getHost() == null) {
103
             throw new InvalidAddressException("Invalid host or port specified");
108
             throw new InvalidAddressException("Invalid host or port specified");
155
      * Processes the port part of this address.
160
      * Processes the port part of this address.
156
      *
161
      *
157
      * @param port The port part of this address
162
      * @param port The port part of this address
158
-     * @throws InvalidAddressException if the port is non-numeric
159
      */
163
      */
160
-    private void doPort(final int port) throws InvalidAddressException {
164
+    private void doPort(final int port) {
161
         this.port = port;
165
         this.port = port;
162
     }
166
     }
163
 
167
 
179
         return usesSSL;
183
         return usesSSL;
180
     }
184
     }
181
 
185
 
186
+    /**
187
+     * Retrieves the protocol from this address.
188
+     *
189
+     * @since 0.6.3m2
190
+     * @return This address's protocol
191
+     */
192
+    public String getProtocol() {
193
+        return protocol;
194
+    }
195
+
182
     /**
196
     /**
183
      * Retrieves the server from this address.
197
      * Retrieves the server from this address.
184
      *
198
      *
191
     /**
205
     /**
192
      * Retrieves the port used for this address.
206
      * Retrieves the port used for this address.
193
      *
207
      *
194
-     * @return This address's port
208
+     * @return This address's port, or -1 if none specified.
195
      */
209
      */
196
     public int getPort() {
210
     public int getPort() {
197
         return port;
211
         return port;
198
     }
212
     }
199
 
213
 
214
+    /**
215
+     * Retrieves the port used for this address.
216
+     *
217
+     * @since 0.6.3m2
218
+     * @param fallback The port to fall back to if none was specified
219
+     * @return This address's port, or the fallback value if none is specified
220
+     */
221
+    public int getPort(final int fallback) {
222
+        return port == -1 ? fallback : port;
223
+    }
224
+
200
     /**
225
     /**
201
      * Retrieves the password used for this address.
226
      * Retrieves the password used for this address.
202
      *
227
      *
239
             }
264
             }
240
         }
265
         }
241
     }
266
     }
267
+
268
+    /** {@inheritDoc} */
269
+    @Override
270
+    public boolean equals(final Object obj) {
271
+        if (obj == null) {
272
+            return false;
273
+        }
274
+        if (getClass() != obj.getClass()) {
275
+            return false;
276
+        }
277
+
278
+        final IrcAddress other = (IrcAddress) obj;
279
+
280
+        if (this.usesSSL != other.usesSSL) {
281
+            return false;
282
+        }
283
+        
284
+        if ((this.protocol == null) ? (other.protocol != null)
285
+                : !this.protocol.equals(other.protocol)) {
286
+            return false;
287
+        }
288
+
289
+        if ((this.server == null) ? (other.server != null)
290
+                : !this.server.equals(other.server)) {
291
+            return false;
292
+        }
293
+
294
+        if (this.port != other.port) {
295
+            return false;
296
+        }
297
+
298
+        if (this.channels != other.channels && (this.channels == null
299
+                || !this.channels.equals(other.channels))) {
300
+            return false;
301
+        }
302
+        
303
+        if ((this.pass == null) ? (other.pass != null) : !this.pass.equals(other.pass)) {
304
+            return false;
305
+        }
306
+        
307
+        return true;
308
+    }
309
+
310
+    /** {@inheritDoc} */
311
+    @Override
312
+    public int hashCode() {
313
+        int hash = 5;
314
+        hash = 67 * hash + (this.usesSSL ? 1 : 0);
315
+        hash = 67 * hash + (this.protocol != null ? this.protocol.hashCode() : 0);
316
+        hash = 67 * hash + (this.server != null ? this.server.hashCode() : 0);
317
+        hash = 67 * hash + this.port;
318
+        hash = 67 * hash + (this.channels != null ? this.channels.hashCode() : 0);
319
+        hash = 67 * hash + (this.pass != null ? this.pass.hashCode() : 0);
320
+        return hash;
321
+    }
322
+
323
+    @Override
324
+    public String toString() {
325
+        return protocol + "://" + pass + "@" + server + ":" + (isSSL() ? "+" : "")
326
+                + port + "/" + channels;
327
+    }
242
 }
328
 }

+ 3
- 5
test/com/dmdirc/InviteTest.java Переглянути файл

23
 package com.dmdirc;
23
 package com.dmdirc;
24
 
24
 
25
 import com.dmdirc.config.IdentityManager;
25
 import com.dmdirc.config.IdentityManager;
26
-import com.dmdirc.harness.parser.TestParserFactory;
27
 import com.dmdirc.addons.ui_dummy.DummyController;
26
 import com.dmdirc.addons.ui_dummy.DummyController;
28
-import java.util.ArrayList;
27
+
29
 import java.util.Date;
28
 import java.util.Date;
29
+
30
 import org.junit.BeforeClass;
30
 import org.junit.BeforeClass;
31
 import org.junit.Test;
31
 import org.junit.Test;
32
 import static org.junit.Assert.*;
32
 import static org.junit.Assert.*;
42
         Main.setUI(new DummyController());
42
         Main.setUI(new DummyController());
43
         IdentityManager.load();
43
         IdentityManager.load();
44
         
44
         
45
-        server = new Server("255.255.255.255", 6667, "", false,
46
-                IdentityManager.getProfiles().get(0), new ArrayList<String>(),
47
-                new TestParserFactory());
45
+        server = new Server("irc-test://255.255.255.255", IdentityManager.getProfiles().get(0));
48
         test = new Invite(server, "#channel", "nick!ident@host");
46
         test = new Invite(server, "#channel", "nick!ident@host");
49
         server.addInvite(test);
47
         server.addInvite(test);
50
         ts = new Date().getTime();
48
         ts = new Date().getTime();

+ 6
- 10
test/com/dmdirc/ServerManagerTest.java Переглянути файл

23
 package com.dmdirc;
23
 package com.dmdirc;
24
 
24
 
25
 import com.dmdirc.config.IdentityManager;
25
 import com.dmdirc.config.IdentityManager;
26
-import com.dmdirc.harness.parser.TestParserFactory;
27
 import com.dmdirc.addons.ui_dummy.DummyController;
26
 import com.dmdirc.addons.ui_dummy.DummyController;
28
 import com.dmdirc.addons.ui_dummy.DummyQueryWindow;
27
 import com.dmdirc.addons.ui_dummy.DummyQueryWindow;
29
 import com.dmdirc.plugins.PluginManager;
28
 import com.dmdirc.plugins.PluginManager;
30
-
31
-import java.util.ArrayList;
29
+import com.dmdirc.util.InvalidAddressException;
32
 
30
 
33
 import org.junit.After;
31
 import org.junit.After;
34
 import org.junit.BeforeClass;
32
 import org.junit.BeforeClass;
103
     }
101
     }
104
     
102
     
105
     @Test
103
     @Test
106
-    public void testGetServerFromFrame() {
107
-        final Server serverA = new Server("255.255.255.255", 6667, "", false,
108
-                IdentityManager.getProfiles().get(0), new ArrayList<String>(),
109
-                new TestParserFactory());
110
-        final Server serverB = new Server("255.255.255.254", 6667, "", false,
111
-                IdentityManager.getProfiles().get(0), new ArrayList<String>(),
112
-                new TestParserFactory());
104
+    public void testGetServerFromFrame() throws InvalidAddressException {
105
+        final Server serverA = new Server("irc-test://255.255.255.255",
106
+                IdentityManager.getProfiles().get(0));
107
+        final Server serverB = new Server("irc-test://255.255.255.254",
108
+                IdentityManager.getProfiles().get(0));
113
         
109
         
114
         final ServerManager sm = ServerManager.getServerManager();
110
         final ServerManager sm = ServerManager.getServerManager();
115
         
111
         

+ 3
- 5
test/com/dmdirc/ServerTest.java Переглянути файл

23
 package com.dmdirc;
23
 package com.dmdirc;
24
 
24
 
25
 import com.dmdirc.config.IdentityManager;
25
 import com.dmdirc.config.IdentityManager;
26
-import com.dmdirc.harness.parser.TestParserFactory;
27
 import com.dmdirc.addons.ui_dummy.DummyController;
26
 import com.dmdirc.addons.ui_dummy.DummyController;
28
-import java.util.ArrayList;
27
+
29
 import org.junit.BeforeClass;
28
 import org.junit.BeforeClass;
30
 import org.junit.Test;
29
 import org.junit.Test;
31
 import static org.junit.Assert.*;
30
 import static org.junit.Assert.*;
38
     public static void setUp() throws Exception {
37
     public static void setUp() throws Exception {
39
         Main.setUI(new DummyController());
38
         Main.setUI(new DummyController());
40
         IdentityManager.load();
39
         IdentityManager.load();
41
-        server = new Server("255.255.255.255", 6667, "", false, 
42
-                IdentityManager.getProfiles().get(0), new ArrayList<String>(),
43
-                new TestParserFactory());
40
+        server = new Server("irc-test://255.255.255.255",
41
+                IdentityManager.getProfiles().get(0));
44
     }
42
     }
45
 
43
 
46
     @Test
44
     @Test

+ 4
- 5
test/com/dmdirc/addons/logging/LoggingPluginTest.java Переглянути файл

29
 import com.dmdirc.actions.CoreActionType;
29
 import com.dmdirc.actions.CoreActionType;
30
 import com.dmdirc.config.IdentityManager;
30
 import com.dmdirc.config.IdentityManager;
31
 import com.dmdirc.harness.TestLoggingPlugin;
31
 import com.dmdirc.harness.TestLoggingPlugin;
32
-import com.dmdirc.harness.parser.TestParserFactory;
33
 import com.dmdirc.parser.irc.IRCChannelInfo;
32
 import com.dmdirc.parser.irc.IRCChannelInfo;
34
 import com.dmdirc.addons.ui_dummy.DummyController;
33
 import com.dmdirc.addons.ui_dummy.DummyController;
35
 import com.dmdirc.parser.irc.IRCParser;
34
 import com.dmdirc.parser.irc.IRCParser;
36
 import com.dmdirc.util.ConfigFile;
35
 import com.dmdirc.util.ConfigFile;
37
-import java.util.ArrayList;
36
+
38
 import java.util.Map;
37
 import java.util.Map;
38
+
39
 import org.junit.BeforeClass;
39
 import org.junit.BeforeClass;
40
 import org.junit.Test;
40
 import org.junit.Test;
41
 import static org.junit.Assert.*;
41
 import static org.junit.Assert.*;
51
     public static void setUp() throws Exception {
51
     public static void setUp() throws Exception {
52
         Main.setUI(new DummyController());
52
         Main.setUI(new DummyController());
53
         IdentityManager.load();
53
         IdentityManager.load();
54
-        server = new Server("255.255.255.255", 6667, "", false,
55
-                IdentityManager.getProfiles().get(0), new ArrayList<String>(),
56
-                new TestParserFactory());
54
+        server = new Server("irc-test://255.255.255.255",
55
+                IdentityManager.getProfiles().get(0));
57
         channel = new Channel(server, new IRCChannelInfo((IRCParser) server.getParser(), "#test"));
56
         channel = new Channel(server, new IRCChannelInfo((IRCParser) server.getParser(), "#test"));
58
         query = new Query(server, "foo!bar@baz");
57
         query = new Query(server, "foo!bar@baz");
59
 
58
 

+ 15
- 13
test/com/dmdirc/commandparser/commands/server/ChangeServerTest.java Переглянути файл

27
 import com.dmdirc.config.IdentityManager;
27
 import com.dmdirc.config.IdentityManager;
28
 import com.dmdirc.ui.interfaces.InputWindow;
28
 import com.dmdirc.ui.interfaces.InputWindow;
29
 
29
 
30
+import com.dmdirc.util.InvalidAddressException;
31
+import com.dmdirc.util.IrcAddress;
30
 import org.junit.BeforeClass;
32
 import org.junit.BeforeClass;
31
 import org.junit.Test;
33
 import org.junit.Test;
32
 import static org.mockito.Mockito.*;
34
 import static org.mockito.Mockito.*;
67
     @Test
69
     @Test
68
     public void testOutOfRangePort2() {
70
     public void testOutOfRangePort2() {
69
         final InputWindow tiw = mock(InputWindow.class);
71
         final InputWindow tiw = mock(InputWindow.class);
70
-        command.execute(tiw, null, false,new CommandArguments("/server foo:65537"));
72
+        command.execute(tiw, null, false, new CommandArguments("/server foo:65537"));
71
         
73
         
72
         verify(tiw).addLine(eq("commandError"), anyString());
74
         verify(tiw).addLine(eq("commandError"), anyString());
73
     }
75
     }
74
 
76
 
75
     @Test
77
     @Test
76
-    public void testExecuteBasic() {
78
+    public void testExecuteBasic() throws InvalidAddressException {
77
         final InputWindow tiw = mock(InputWindow.class);
79
         final InputWindow tiw = mock(InputWindow.class);
78
         final Identity profile = mock(Identity.class);
80
         final Identity profile = mock(Identity.class);
79
         final Server server = mock(Server.class);
81
         final Server server = mock(Server.class);
80
         when(server.getProfile()).thenReturn(profile);
82
         when(server.getProfile()).thenReturn(profile);
81
 
83
 
82
-        command.execute(tiw, server, false,new CommandArguments("/server foo:1234"));
84
+        command.execute(tiw, server, false, new CommandArguments("/server foo:1234"));
83
 
85
 
84
-        verify(server).connect("foo", 1234, "", false, profile);
86
+        verify(server).connect(eq(new IrcAddress("foo", 1234, "", false)), same(profile));
85
     }
87
     }
86
 
88
 
87
     @Test
89
     @Test
88
-    public void testExecuteNoPort() {
90
+    public void testExecuteNoPort() throws InvalidAddressException {
89
         final InputWindow tiw = mock(InputWindow.class);
91
         final InputWindow tiw = mock(InputWindow.class);
90
         final Identity profile = mock(Identity.class);
92
         final Identity profile = mock(Identity.class);
91
         final Server server = mock(Server.class);
93
         final Server server = mock(Server.class);
92
         when(server.getProfile()).thenReturn(profile);
94
         when(server.getProfile()).thenReturn(profile);
93
 
95
 
94
-        command.execute(tiw, server, false,new CommandArguments("/server foo"));
96
+        command.execute(tiw, server, false, new CommandArguments("/server foo"));
95
 
97
 
96
-        verify(server).connect("foo", 6667, "", false, profile);
98
+        verify(server).connect(eq(new IrcAddress("foo", 6667, "", false)), same(profile));
97
     }
99
     }
98
 
100
 
99
     @Test
101
     @Test
100
-    public void testDeprecatedSSL() {
102
+    public void testDeprecatedSSL() throws InvalidAddressException {
101
         final InputWindow tiw = mock(InputWindow.class);
103
         final InputWindow tiw = mock(InputWindow.class);
102
         final Identity profile = mock(Identity.class);
104
         final Identity profile = mock(Identity.class);
103
         final Server server = mock(Server.class);
105
         final Server server = mock(Server.class);
104
         when(server.getProfile()).thenReturn(profile);
106
         when(server.getProfile()).thenReturn(profile);
105
 
107
 
106
-        command.execute(tiw, server, false,new CommandArguments("/server --ssl foo"));
108
+        command.execute(tiw, server, false, new CommandArguments("/server --ssl foo"));
107
 
109
 
108
-        verify(server).connect("foo", 6667, "", true, profile);
110
+        verify(server).connect(eq(new IrcAddress("foo", 6667, "", true)), same(profile));
109
     }
111
     }
110
 
112
 
111
     @Test
113
     @Test
112
-    public void testExecuteComplex() {
114
+    public void testExecuteComplex() throws InvalidAddressException {
113
         final InputWindow tiw = mock(InputWindow.class);
115
         final InputWindow tiw = mock(InputWindow.class);
114
         final Identity profile = mock(Identity.class);
116
         final Identity profile = mock(Identity.class);
115
         final Server server = mock(Server.class);
117
         final Server server = mock(Server.class);
116
         when(server.getProfile()).thenReturn(profile);
118
         when(server.getProfile()).thenReturn(profile);
117
 
119
 
118
-        command.execute(tiw, server, false,new CommandArguments("/server foo:+1234 password"));
120
+        command.execute(tiw, server, false, new CommandArguments("/server foo:+1234 password"));
119
 
121
 
120
-        verify(server).connect("foo", 1234, "password", true, profile);
122
+        verify(server).connect(eq(new IrcAddress("foo", 1234, "password", true)), same(profile));
121
     }
123
     }
122
 
124
 
123
 }
125
 }

+ 0
- 53
test/com/dmdirc/harness/parser/TestParserFactory.java Переглянути файл

1
-/*
2
- * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
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.harness.parser;
24
-
25
-import com.dmdirc.ParserFactory;
26
-import com.dmdirc.parser.irc.IRCParser;
27
-import com.dmdirc.parser.irc.MyInfo;
28
-import com.dmdirc.parser.irc.ServerInfo;
29
-
30
-public class TestParserFactory extends ParserFactory {
31
-
32
-    protected String network;
33
-
34
-    public TestParserFactory() {
35
-        this(null);
36
-    }
37
-
38
-    public TestParserFactory(String network) {
39
-        this.network = network;
40
-    }
41
-    
42
-    @Override
43
-    public IRCParser getParser(MyInfo myInfo, ServerInfo serverInfo) {
44
-        final TestParser parser = new TestParser(myInfo, serverInfo);
45
-        
46
-        if (this.network != null) {
47
-            parser.network = network;
48
-        }
49
-        
50
-        return parser;
51
-    }
52
-
53
-}

+ 1
- 1
test/com/dmdirc/parser/irc/ClientInfoTest.java Переглянути файл

33
     @Test
33
     @Test
34
     public void testMap() {
34
     public void testMap() {
35
         final IRCClientInfo ci = new IRCClientInfo(new IRCParser(), "nick!ident@host");
35
         final IRCClientInfo ci = new IRCClientInfo(new IRCParser(), "nick!ident@host");
36
-        final Map map = new HashMap();
36
+        final Map<Object, Object> map = new HashMap<Object, Object>();
37
         
37
         
38
         ci.setMap(map);
38
         ci.setMap(map);
39
         assertEquals(map, ci.getMap());
39
         assertEquals(map, ci.getMap());

Завантаження…
Відмінити
Зберегти