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.0KB

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