Browse Source

Issue 2736.

tags/0.6.3m2a1
Chris Smith 15 years ago
parent
commit
f55a6902f2

+ 18
- 3
src/com/dmdirc/ParserFactory.java View File

@@ -22,6 +22,8 @@
22 22
 
23 23
 package com.dmdirc;
24 24
 
25
+import com.dmdirc.logger.ErrorLevel;
26
+import com.dmdirc.logger.Logger;
25 27
 import com.dmdirc.parser.interfaces.Parser;
26 28
 import com.dmdirc.parser.irc.IRCParser;
27 29
 import com.dmdirc.parser.irc.MyInfo;
@@ -45,11 +47,24 @@ public class ParserFactory {
45 47
      * @since 0.6.3m2
46 48
      */
47 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 52
                 address.getPassword());
50 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 View File

@@ -50,10 +50,12 @@ public class IrcAddress implements Serializable {
50 50
 
51 51
     /** Whether or not this address uses SSL. */
52 52
     private boolean usesSSL;
53
+    /** The protocol for this address. */
54
+    private String protocol;
53 55
     /** The server name for this address. */
54 56
     private String server;
55 57
     /** The port number for this address. */
56
-    private int port = 6667;
58
+    private int port;
57 59
     /** A list of channels to auto-connect to. */
58 60
     private List<String> channels = new ArrayList<String>();
59 61
     /** The password for this address. */
@@ -80,12 +82,17 @@ public class IrcAddress implements Serializable {
80 82
         } catch (URISyntaxException ex) {
81 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 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 97
         if (uri.getUserInfo() != null) {
91 98
             doPass(uri.getUserInfo());
@@ -95,9 +102,7 @@ public class IrcAddress implements Serializable {
95 102
             "?" + uri.getQuery()) + (uri.getFragment() == null ? "" :
96 103
             "#" + uri.getFragment()));
97 104
 
98
-        if (uri.getPort() > -1) {
99
-            doPort(uri.getPort());
100
-        }
105
+        doPort(uri.getPort());
101 106
 
102 107
         if (uri.getHost() == null) {
103 108
             throw new InvalidAddressException("Invalid host or port specified");
@@ -155,9 +160,8 @@ public class IrcAddress implements Serializable {
155 160
      * Processes the port part of this address.
156 161
      *
157 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 165
         this.port = port;
162 166
     }
163 167
 
@@ -179,6 +183,16 @@ public class IrcAddress implements Serializable {
179 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 197
      * Retrieves the server from this address.
184 198
      *
@@ -191,12 +205,23 @@ public class IrcAddress implements Serializable {
191 205
     /**
192 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 210
     public int getPort() {
197 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 226
      * Retrieves the password used for this address.
202 227
      *
@@ -239,4 +264,65 @@ public class IrcAddress implements Serializable {
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 View File

@@ -23,10 +23,10 @@
23 23
 package com.dmdirc;
24 24
 
25 25
 import com.dmdirc.config.IdentityManager;
26
-import com.dmdirc.harness.parser.TestParserFactory;
27 26
 import com.dmdirc.addons.ui_dummy.DummyController;
28
-import java.util.ArrayList;
27
+
29 28
 import java.util.Date;
29
+
30 30
 import org.junit.BeforeClass;
31 31
 import org.junit.Test;
32 32
 import static org.junit.Assert.*;
@@ -42,9 +42,7 @@ public class InviteTest {
42 42
         Main.setUI(new DummyController());
43 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 46
         test = new Invite(server, "#channel", "nick!ident@host");
49 47
         server.addInvite(test);
50 48
         ts = new Date().getTime();

+ 6
- 10
test/com/dmdirc/ServerManagerTest.java View File

@@ -23,12 +23,10 @@
23 23
 package com.dmdirc;
24 24
 
25 25
 import com.dmdirc.config.IdentityManager;
26
-import com.dmdirc.harness.parser.TestParserFactory;
27 26
 import com.dmdirc.addons.ui_dummy.DummyController;
28 27
 import com.dmdirc.addons.ui_dummy.DummyQueryWindow;
29 28
 import com.dmdirc.plugins.PluginManager;
30
-
31
-import java.util.ArrayList;
29
+import com.dmdirc.util.InvalidAddressException;
32 30
 
33 31
 import org.junit.After;
34 32
 import org.junit.BeforeClass;
@@ -103,13 +101,11 @@ public class ServerManagerTest {
103 101
     }
104 102
     
105 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 110
         final ServerManager sm = ServerManager.getServerManager();
115 111
         

+ 3
- 5
test/com/dmdirc/ServerTest.java View File

@@ -23,9 +23,8 @@
23 23
 package com.dmdirc;
24 24
 
25 25
 import com.dmdirc.config.IdentityManager;
26
-import com.dmdirc.harness.parser.TestParserFactory;
27 26
 import com.dmdirc.addons.ui_dummy.DummyController;
28
-import java.util.ArrayList;
27
+
29 28
 import org.junit.BeforeClass;
30 29
 import org.junit.Test;
31 30
 import static org.junit.Assert.*;
@@ -38,9 +37,8 @@ public class ServerTest {
38 37
     public static void setUp() throws Exception {
39 38
         Main.setUI(new DummyController());
40 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 44
     @Test

+ 4
- 5
test/com/dmdirc/addons/logging/LoggingPluginTest.java View File

@@ -29,13 +29,13 @@ import com.dmdirc.Query;
29 29
 import com.dmdirc.actions.CoreActionType;
30 30
 import com.dmdirc.config.IdentityManager;
31 31
 import com.dmdirc.harness.TestLoggingPlugin;
32
-import com.dmdirc.harness.parser.TestParserFactory;
33 32
 import com.dmdirc.parser.irc.IRCChannelInfo;
34 33
 import com.dmdirc.addons.ui_dummy.DummyController;
35 34
 import com.dmdirc.parser.irc.IRCParser;
36 35
 import com.dmdirc.util.ConfigFile;
37
-import java.util.ArrayList;
36
+
38 37
 import java.util.Map;
38
+
39 39
 import org.junit.BeforeClass;
40 40
 import org.junit.Test;
41 41
 import static org.junit.Assert.*;
@@ -51,9 +51,8 @@ public class LoggingPluginTest {
51 51
     public static void setUp() throws Exception {
52 52
         Main.setUI(new DummyController());
53 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 56
         channel = new Channel(server, new IRCChannelInfo((IRCParser) server.getParser(), "#test"));
58 57
         query = new Query(server, "foo!bar@baz");
59 58
 

+ 15
- 13
test/com/dmdirc/commandparser/commands/server/ChangeServerTest.java View File

@@ -27,6 +27,8 @@ import com.dmdirc.config.Identity;
27 27
 import com.dmdirc.config.IdentityManager;
28 28
 import com.dmdirc.ui.interfaces.InputWindow;
29 29
 
30
+import com.dmdirc.util.InvalidAddressException;
31
+import com.dmdirc.util.IrcAddress;
30 32
 import org.junit.BeforeClass;
31 33
 import org.junit.Test;
32 34
 import static org.mockito.Mockito.*;
@@ -67,57 +69,57 @@ public class ChangeServerTest {
67 69
     @Test
68 70
     public void testOutOfRangePort2() {
69 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 74
         verify(tiw).addLine(eq("commandError"), anyString());
73 75
     }
74 76
 
75 77
     @Test
76
-    public void testExecuteBasic() {
78
+    public void testExecuteBasic() throws InvalidAddressException {
77 79
         final InputWindow tiw = mock(InputWindow.class);
78 80
         final Identity profile = mock(Identity.class);
79 81
         final Server server = mock(Server.class);
80 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 89
     @Test
88
-    public void testExecuteNoPort() {
90
+    public void testExecuteNoPort() throws InvalidAddressException {
89 91
         final InputWindow tiw = mock(InputWindow.class);
90 92
         final Identity profile = mock(Identity.class);
91 93
         final Server server = mock(Server.class);
92 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 101
     @Test
100
-    public void testDeprecatedSSL() {
102
+    public void testDeprecatedSSL() throws InvalidAddressException {
101 103
         final InputWindow tiw = mock(InputWindow.class);
102 104
         final Identity profile = mock(Identity.class);
103 105
         final Server server = mock(Server.class);
104 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 113
     @Test
112
-    public void testExecuteComplex() {
114
+    public void testExecuteComplex() throws InvalidAddressException {
113 115
         final InputWindow tiw = mock(InputWindow.class);
114 116
         final Identity profile = mock(Identity.class);
115 117
         final Server server = mock(Server.class);
116 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 View File

@@ -1,53 +0,0 @@
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 View File

@@ -33,7 +33,7 @@ public class ClientInfoTest {
33 33
     @Test
34 34
     public void testMap() {
35 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 38
         ci.setMap(map);
39 39
         assertEquals(map, ci.getMap());

Loading…
Cancel
Save