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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. package com.dmdirc.util;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.ServerManager;
  25. import com.dmdirc.config.IdentityManager;
  26. import com.dmdirc.addons.ui_dummy.DummyController;
  27. import org.junit.BeforeClass;
  28. import org.junit.Ignore;
  29. import org.junit.Test;
  30. import static org.junit.Assert.*;
  31. public class IrcAddressTest {
  32. @BeforeClass
  33. public static void setUp() {
  34. IdentityManager.load();
  35. Main.setUI(new DummyController());
  36. }
  37. @Test(expected=InvalidAddressException.class)
  38. public void testInvalidProtocol() throws InvalidAddressException {
  39. new IrcAddress("http://moo!");
  40. }
  41. @Test(expected=InvalidAddressException.class)
  42. public void testNoProtocol() throws InvalidAddressException {
  43. new IrcAddress("moo!");
  44. }
  45. @Test(expected=InvalidAddressException.class)
  46. public void testInvalidURL() throws InvalidAddressException {
  47. new IrcAddress(":");
  48. }
  49. @Test
  50. public void testBasic() throws InvalidAddressException {
  51. final IrcAddress address = new IrcAddress("irc://servername");
  52. assertEquals("servername", address.getServer());
  53. assertEquals("", address.getPassword());
  54. assertEquals(6667, address.getPort());
  55. assertFalse(address.isSSL());
  56. }
  57. @Test
  58. public void testPasswordSSL() throws InvalidAddressException {
  59. final IrcAddress address = new IrcAddress("ircs://password@servername");
  60. assertEquals("servername", address.getServer());
  61. assertEquals("password", address.getPassword());
  62. assertEquals(6667, address.getPort());
  63. assertTrue(address.isSSL());
  64. }
  65. @Test
  66. public void testPort() throws InvalidAddressException {
  67. final IrcAddress address = new IrcAddress("irc://servername:7000/");
  68. assertEquals("servername", address.getServer());
  69. assertEquals("", address.getPassword());
  70. assertEquals(7000, address.getPort());
  71. assertFalse(address.isSSL());
  72. }
  73. @Test(expected=InvalidAddressException.class)
  74. public void testInvalidPort() throws InvalidAddressException {
  75. new IrcAddress("irc://servername:port/");
  76. }
  77. @Test
  78. public void testPortSSL() throws InvalidAddressException {
  79. final IrcAddress address = new IrcAddress("ircs://servername:+7000/");
  80. assertEquals("servername", address.getServer());
  81. assertEquals("", address.getPassword());
  82. assertEquals(7000, address.getPort());
  83. assertTrue(address.isSSL());
  84. }
  85. @Test
  86. public void testComplex() throws InvalidAddressException {
  87. final IrcAddress address = new IrcAddress("ircs://password@servername:+7000/c1,c2,c3");
  88. assertEquals("servername", address.getServer());
  89. assertEquals("password", address.getPassword());
  90. assertEquals(7000, address.getPort());
  91. assertEquals(3, address.getChannels().size());
  92. assertTrue(address.isSSL());
  93. }
  94. @Test
  95. public void testConnect() throws InvalidAddressException {
  96. final IrcAddress address = new IrcAddress("irc://255.255.255.205/a,b,c");
  97. int initial = ServerManager.getServerManager().numServers();
  98. address.connect();
  99. assertEquals(initial + 1, ServerManager.getServerManager().numServers());
  100. address.connect();
  101. assertEquals(initial + 1, ServerManager.getServerManager().numServers());
  102. }
  103. @Test
  104. public void testReservedChanNames() throws InvalidAddressException {
  105. final IrcAddress address1 = new IrcAddress("irc://server/,needpass");
  106. assertEquals(0, address1.getChannels().size());
  107. final IrcAddress address2 = new IrcAddress("irc://server/MDbot,needkey");
  108. assertEquals(1, address2.getChannels().size());
  109. assertEquals("MDbot", address2.getChannels().get(0));
  110. final IrcAddress address3 = new IrcAddress("irc://server/MDbot,isnick");
  111. assertEquals(1, address3.getChannels().size());
  112. assertEquals("MDbot", address3.getChannels().get(0));
  113. }
  114. @Test
  115. public void testChannels() throws InvalidAddressException {
  116. final IrcAddress address3 = new IrcAddress("irc://server/#MDbot");
  117. assertEquals(1, address3.getChannels().size());
  118. assertEquals("#MDbot", address3.getChannels().get(0));
  119. }
  120. @Test
  121. public void testChannelsQuery() throws InvalidAddressException {
  122. final IrcAddress address3 = new IrcAddress("irc://server/MDbot?moo");
  123. assertEquals(1, address3.getChannels().size());
  124. assertEquals("MDbot?moo", address3.getChannels().get(0));
  125. }
  126. @Test
  127. public void testEncoding() throws InvalidAddressException {
  128. final IrcAddress address1 = new IrcAddress("irc://server/%23DMDirc");
  129. assertEquals(1, address1.getChannels().size());
  130. assertEquals("#DMDirc", address1.getChannels().get(0));
  131. }
  132. }