You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IrcAddressTest.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2006-2008 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. package com.dmdirc.util;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.ServerManager;
  25. import com.dmdirc.config.IdentityManager;
  26. import com.dmdirc.ui.dummy.DummyController;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import static org.junit.Assert.*;
  30. public class IrcAddressTest extends junit.framework.TestCase {
  31. @Before
  32. public void setUp() {
  33. IdentityManager.load();
  34. Main.setUI(new DummyController());
  35. }
  36. @Test
  37. public void testInvalidProtocol() {
  38. boolean exception = false;
  39. try {
  40. final IrcAddress address = new IrcAddress("http://moo!");
  41. } catch (InvalidAddressException ex) {
  42. exception = true;
  43. }
  44. assertTrue(exception);
  45. }
  46. @Test
  47. public void testNoProtocol() {
  48. boolean exception = false;
  49. try {
  50. final IrcAddress address = new IrcAddress("moo!");
  51. } catch (InvalidAddressException ex) {
  52. exception = true;
  53. }
  54. assertTrue(exception);
  55. }
  56. @Test
  57. public void testBasic() {
  58. try {
  59. final IrcAddress address = new IrcAddress("irc://servername");
  60. assertEquals("servername", address.getServer());
  61. assertEquals("", address.getPassword());
  62. assertEquals(6667, address.getPort());
  63. assertFalse(address.isSSL());
  64. } catch (InvalidAddressException ex) {
  65. assertFalse(true);
  66. }
  67. }
  68. @Test
  69. public void testPasswordSSL() {
  70. try {
  71. final IrcAddress address = new IrcAddress("ircs://password@servername");
  72. assertEquals("servername", address.getServer());
  73. assertEquals("password", address.getPassword());
  74. assertEquals(6667, address.getPort());
  75. assertTrue(address.isSSL());
  76. } catch (InvalidAddressException ex) {
  77. assertFalse(true);
  78. }
  79. }
  80. @Test
  81. public void testPort() {
  82. try {
  83. final IrcAddress address = new IrcAddress("irc://servername:7000/");
  84. assertEquals("servername", address.getServer());
  85. assertEquals("", address.getPassword());
  86. assertEquals(7000, address.getPort());
  87. assertFalse(address.isSSL());
  88. } catch (InvalidAddressException ex) {
  89. assertFalse(true);
  90. }
  91. }
  92. @Test
  93. public void testInvalidPort() {
  94. boolean except = false;
  95. try {
  96. new IrcAddress("irc://servername:port/");
  97. } catch (InvalidAddressException ex) {
  98. except = true;
  99. }
  100. assertTrue(except);
  101. }
  102. @Test
  103. public void testPortSSL() {
  104. try {
  105. final IrcAddress address = new IrcAddress("ircs://servername:+7000/");
  106. assertEquals("servername", address.getServer());
  107. assertEquals("", address.getPassword());
  108. assertEquals(7000, address.getPort());
  109. assertTrue(address.isSSL());
  110. } catch (InvalidAddressException ex) {
  111. assertFalse(true);
  112. }
  113. }
  114. @Test
  115. public void testComplex() {
  116. try {
  117. final IrcAddress address = new IrcAddress("ircs://password@servername:+7000/c1,c2,c3");
  118. assertEquals("servername", address.getServer());
  119. assertEquals("password", address.getPassword());
  120. assertEquals(7000, address.getPort());
  121. assertEquals(3, address.getChannels().size());
  122. assertTrue(address.isSSL());
  123. } catch (InvalidAddressException ex) {
  124. assertFalse(true);
  125. }
  126. }
  127. @Test
  128. public void testConnect() {
  129. try {
  130. final IrcAddress address = new IrcAddress("irc://255.255.255.205/a,b,c");
  131. int initial = ServerManager.getServerManager().numServers();
  132. address.connect();
  133. assertEquals(initial + 1, ServerManager.getServerManager().numServers());
  134. address.connect();
  135. assertEquals(initial + 1, ServerManager.getServerManager().numServers());
  136. } catch (InvalidAddressException ex) {
  137. assertFalse(true);
  138. }
  139. }
  140. @Test
  141. public void testReservedChanNames() {
  142. try {
  143. final IrcAddress address1 = new IrcAddress("irc://server/,needpass");
  144. assertEquals(0, address1.getChannels().size());
  145. final IrcAddress address2 = new IrcAddress("irc://server/MDbot,needkey");
  146. assertEquals(1, address2.getChannels().size());
  147. assertEquals("MDbot", address2.getChannels().get(0));
  148. final IrcAddress address3 = new IrcAddress("irc://server/MDbot,isnick");
  149. assertEquals(1, address3.getChannels().size());
  150. assertEquals("MDbot", address3.getChannels().get(0));
  151. } catch (InvalidAddressException ex) {
  152. assertFalse(true);
  153. }
  154. }
  155. @Test
  156. public void testChannels() throws InvalidAddressException {
  157. try {
  158. final IrcAddress address3 = new IrcAddress("irc://server/#MDbot");
  159. assertEquals(1, address3.getChannels().size());
  160. assertEquals("#MDbot", address3.getChannels().get(0));
  161. } catch (InvalidAddressException ex) {
  162. fail();
  163. }
  164. }
  165. @Test
  166. public void testEncoding() {
  167. try {
  168. final IrcAddress address1 = new IrcAddress("irc://server/%23DMDirc");
  169. assertEquals(1, address1.getChannels().size());
  170. assertEquals("#DMDirc", address1.getChannels().get(0));
  171. } catch (InvalidAddressException ex) {
  172. assertFalse(true);
  173. }
  174. }
  175. }