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.

ChangeServerTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2006-2013 DMDirc Developers
  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.commandparser.commands.server;
  23. import com.dmdirc.TestMain;
  24. import com.dmdirc.FrameContainer;
  25. import com.dmdirc.Server;
  26. import com.dmdirc.commandparser.CommandArguments;
  27. import com.dmdirc.commandparser.commands.context.ServerCommandContext;
  28. import com.dmdirc.config.Identity;
  29. import java.net.URI;
  30. import java.net.URISyntaxException;
  31. import org.junit.Before;
  32. import org.junit.BeforeClass;
  33. import org.junit.Test;
  34. import static org.mockito.Mockito.*;
  35. public class ChangeServerTest {
  36. private final ChangeServer command = new ChangeServer();
  37. private FrameContainer tiw;
  38. private Identity profile;
  39. private Server server;
  40. @BeforeClass
  41. public static void setUpClass() throws Exception {
  42. TestMain.getTestMain();
  43. }
  44. @Before
  45. public void setUp() {
  46. tiw = mock(FrameContainer.class);
  47. profile = mock(Identity.class);
  48. server = mock(Server.class);
  49. when(server.getProfile()).thenReturn(profile);
  50. }
  51. @Test
  52. public void testUsageNoArgs() {
  53. command.execute(tiw, new CommandArguments("/server"),
  54. new ServerCommandContext(null, ChangeServer.INFO, server));
  55. verify(tiw).addLine(eq("commandUsage"), anyChar(), anyString(), anyString());
  56. }
  57. @Test
  58. public void testInvalidPort() {
  59. command.execute(tiw, new CommandArguments("/server foo:abc"),
  60. new ServerCommandContext(null, ChangeServer.INFO, server));
  61. verify(tiw).addLine(eq("commandError"), anyString());
  62. }
  63. @Test
  64. public void testOutOfRangePort1() {
  65. command.execute(tiw, new CommandArguments("/server foo:0"),
  66. new ServerCommandContext(null, ChangeServer.INFO, server));
  67. verify(tiw).addLine(eq("commandError"), anyString());
  68. }
  69. @Test
  70. public void testOutOfRangePort2() {
  71. command.execute(tiw, new CommandArguments("/server foo:65537"),
  72. new ServerCommandContext(null, ChangeServer.INFO, server));
  73. verify(tiw).addLine(eq("commandError"), anyString());
  74. }
  75. @Test
  76. public void testExecuteBasic() throws URISyntaxException {
  77. command.execute(tiw, new CommandArguments("/server foo:1234"),
  78. new ServerCommandContext(null, ChangeServer.INFO, server));
  79. verify(server).connect(eq(new URI("irc://foo:1234")), same(profile));
  80. }
  81. @Test
  82. public void testExecuteNoPort() throws URISyntaxException {
  83. command.execute(tiw, new CommandArguments("/server foo"),
  84. new ServerCommandContext(null, ChangeServer.INFO, server));
  85. verify(server).connect(eq(new URI("irc://foo:6667")), same(profile));
  86. }
  87. @Test
  88. public void testDeprecatedSSL() throws URISyntaxException {
  89. command.execute(tiw, new CommandArguments("/server --ssl foo"),
  90. new ServerCommandContext(null, ChangeServer.INFO, server));
  91. verify(server).connect(eq(new URI("ircs://foo:6667")), same(profile));
  92. }
  93. @Test
  94. public void testExecuteComplex() throws URISyntaxException {
  95. command.execute(tiw, new CommandArguments("/server foo:+1234 password"),
  96. new ServerCommandContext(null, ChangeServer.INFO, server));
  97. verify(server).connect(eq(new URI("ircs://password@foo:1234")), same(profile));
  98. }
  99. }