您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

IrcAddressTest.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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("irc", address.getProtocol());
  54. assertEquals("servername", address.getServer());
  55. assertEquals("", address.getPassword());
  56. assertEquals(-1, address.getPort());
  57. assertFalse(address.isSSL());
  58. }
  59. @Test
  60. public void testPasswordSSL() throws InvalidAddressException {
  61. final IrcAddress address = new IrcAddress("ircs://password@servername");
  62. assertEquals("irc", address.getProtocol());
  63. assertEquals("servername", address.getServer());
  64. assertEquals("password", address.getPassword());
  65. assertEquals(-1, address.getPort());
  66. assertTrue(address.isSSL());
  67. }
  68. @Test
  69. public void testPort() throws InvalidAddressException {
  70. final IrcAddress address = new IrcAddress("irc://servername:7000/");
  71. assertEquals("irc", address.getProtocol());
  72. assertEquals("servername", address.getServer());
  73. assertEquals("", address.getPassword());
  74. assertEquals(7000, address.getPort());
  75. assertFalse(address.isSSL());
  76. }
  77. @Test(expected=InvalidAddressException.class)
  78. public void testInvalidPort() throws InvalidAddressException {
  79. new IrcAddress("irc://servername:port/");
  80. }
  81. @Test
  82. public void testPortSSL() throws InvalidAddressException {
  83. final IrcAddress address = new IrcAddress("ircs://servername:+7000/");
  84. assertEquals("irc", address.getProtocol());
  85. assertEquals("servername", address.getServer());
  86. assertEquals("", address.getPassword());
  87. assertEquals(7000, address.getPort());
  88. assertTrue(address.isSSL());
  89. }
  90. @Test
  91. public void testComplex() throws InvalidAddressException {
  92. final IrcAddress address = new IrcAddress("ircs://password@servername:+7000/c1,c2,c3");
  93. assertEquals("irc", address.getProtocol());
  94. assertEquals("servername", address.getServer());
  95. assertEquals("password", address.getPassword());
  96. assertEquals(7000, address.getPort());
  97. assertEquals(3, address.getChannels().size());
  98. assertTrue(address.isSSL());
  99. }
  100. @Test
  101. public void testConnect() throws InvalidAddressException {
  102. final IrcAddress address = new IrcAddress("irc://255.255.255.205/a,b,c");
  103. int initial = ServerManager.getServerManager().numServers();
  104. address.connect();
  105. assertEquals(initial + 1, ServerManager.getServerManager().numServers());
  106. address.connect();
  107. assertEquals(initial + 1, ServerManager.getServerManager().numServers());
  108. }
  109. @Test
  110. public void testReservedChanNames() throws InvalidAddressException {
  111. final IrcAddress address1 = new IrcAddress("irc://server/,needpass");
  112. assertEquals(0, address1.getChannels().size());
  113. final IrcAddress address2 = new IrcAddress("irc://server/MDbot,needkey");
  114. assertEquals(1, address2.getChannels().size());
  115. assertEquals("MDbot", address2.getChannels().get(0));
  116. final IrcAddress address3 = new IrcAddress("irc://server/MDbot,isnick");
  117. assertEquals(1, address3.getChannels().size());
  118. assertEquals("MDbot", address3.getChannels().get(0));
  119. }
  120. @Test
  121. public void testChannels() throws InvalidAddressException {
  122. final IrcAddress address3 = new IrcAddress("irc://server/#MDbot");
  123. assertEquals(1, address3.getChannels().size());
  124. assertEquals("#MDbot", address3.getChannels().get(0));
  125. }
  126. @Test
  127. public void testChannelsQuery() throws InvalidAddressException {
  128. final IrcAddress address3 = new IrcAddress("irc://server/MDbot?moo");
  129. assertEquals(1, address3.getChannels().size());
  130. assertEquals("MDbot?moo", address3.getChannels().get(0));
  131. }
  132. @Test
  133. public void testEncoding() throws InvalidAddressException {
  134. final IrcAddress address1 = new IrcAddress("irc://server/%23DMDirc");
  135. assertEquals(1, address1.getChannels().size());
  136. assertEquals("#DMDirc", address1.getChannels().get(0));
  137. }
  138. }