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.

NewServerTest.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2006-2012 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.global;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.Server;
  25. import com.dmdirc.commandparser.CommandArguments;
  26. import com.dmdirc.commandparser.commands.context.CommandContext;
  27. import com.dmdirc.config.Identity;
  28. import com.dmdirc.config.IdentityManager;
  29. import com.dmdirc.interfaces.ServerFactory;
  30. import java.net.URI;
  31. import java.net.URISyntaxException;
  32. import org.junit.Before;
  33. import org.junit.BeforeClass;
  34. import org.junit.Test;
  35. import static org.mockito.Matchers.*;
  36. import static org.mockito.Mockito.*;
  37. public class NewServerTest {
  38. @BeforeClass
  39. public static void setUpClass() throws Exception {
  40. IdentityManager.getIdentityManager().initialise();
  41. }
  42. private FrameContainer container;
  43. private ServerFactory factory;
  44. private Server server;
  45. private NewServer command;
  46. @Before
  47. public void setup() {
  48. container = mock(FrameContainer.class);
  49. factory = mock(ServerFactory.class);
  50. server = mock(Server.class);
  51. when(factory.createServer(any(URI.class), any(Identity.class))).thenReturn(server);
  52. command = new NewServer(factory);
  53. }
  54. @Test
  55. public void testBasicUsage() throws URISyntaxException {
  56. command.execute(container, new CommandArguments("/foo irc.foo.com"),
  57. new CommandContext(null, NewServer.INFO));
  58. verify(factory).createServer(eq(new URI("irc://irc.foo.com:6667")), any(Identity.class));
  59. verify(server).connect();
  60. }
  61. @Test
  62. public void testPortUsage() throws URISyntaxException {
  63. command.execute(container, new CommandArguments("/foo irc.foo.com:1234"),
  64. new CommandContext(null, NewServer.INFO));
  65. verify(factory).createServer(eq(new URI("irc://irc.foo.com:1234")), any(Identity.class));
  66. verify(server).connect();
  67. }
  68. @Test
  69. public void testUriUsage() throws URISyntaxException {
  70. command.execute(container, new CommandArguments("/foo otheruri://foo.com:123/blah"),
  71. new CommandContext(null, NewServer.INFO));
  72. verify(factory).createServer(eq(new URI("otheruri://foo.com:123/blah")), any(Identity.class));
  73. verify(server).connect();
  74. }
  75. @Test
  76. public void testUsageNoArgs() {
  77. command.execute(container, new CommandArguments("/foo"),
  78. new CommandContext(null, NewServer.INFO));
  79. verify(container).addLine(eq("commandUsage"), anyChar(), anyString(), anyString());
  80. }
  81. @Test
  82. public void testInvalidPort() {
  83. command.execute(container, new CommandArguments("/foo foo:abc"),
  84. new CommandContext(null, NewServer.INFO));
  85. verify(container).addLine(eq("commandError"), anyString());
  86. }
  87. @Test
  88. public void testOutOfRangePort1() {
  89. command.execute(container, new CommandArguments("/foo foo:0"),
  90. new CommandContext(null, NewServer.INFO));
  91. verify(container).addLine(eq("commandError"), anyString());
  92. }
  93. @Test
  94. public void testOutOfRangePort2() {
  95. command.execute(container, new CommandArguments("/foo foo:65537"),
  96. new CommandContext(null, NewServer.INFO));
  97. verify(container).addLine(eq("commandError"), anyString());
  98. }
  99. }