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

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