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