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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2006-2015 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.commandparser.CommandArguments;
  24. import com.dmdirc.commandparser.commands.context.CommandContext;
  25. import com.dmdirc.config.profiles.Profile;
  26. import com.dmdirc.config.profiles.ProfileManager;
  27. import com.dmdirc.events.CommandErrorEvent;
  28. import com.dmdirc.interfaces.CommandController;
  29. import com.dmdirc.interfaces.Connection;
  30. import com.dmdirc.interfaces.ConnectionFactory;
  31. import com.dmdirc.interfaces.EventBus;
  32. import com.dmdirc.interfaces.WindowModel;
  33. import com.dmdirc.plugins.ServiceManager;
  34. import com.dmdirc.util.URIParser;
  35. import java.net.URI;
  36. import java.net.URISyntaxException;
  37. import java.util.Collections;
  38. import org.junit.Before;
  39. import org.junit.Test;
  40. import org.junit.runner.RunWith;
  41. import org.mockito.Mock;
  42. import org.mockito.runners.MockitoJUnitRunner;
  43. import static org.mockito.Matchers.any;
  44. import static org.mockito.Matchers.eq;
  45. import static org.mockito.Matchers.isA;
  46. import static org.mockito.Mockito.verify;
  47. import static org.mockito.Mockito.when;
  48. @RunWith(MockitoJUnitRunner.class)
  49. public class NewServerTest {
  50. @Mock private EventBus eventBus;
  51. @Mock private CommandController controller;
  52. @Mock private ProfileManager profileManager;
  53. @Mock private Profile identity;
  54. @Mock private WindowModel container;
  55. @Mock private ServiceManager serviceManager;
  56. @Mock private ConnectionFactory factory;
  57. @Mock private Connection connection;
  58. private NewServer command;
  59. @Before
  60. public void setup() {
  61. when(container.getEventBus()).thenReturn(eventBus);
  62. when(factory.createServer(any(URI.class), any(Profile.class))).thenReturn(connection);
  63. when(profileManager.getProfiles()).thenReturn(
  64. Collections.singletonList(identity));
  65. command = new NewServer(controller, factory, serviceManager, profileManager, new URIParser());
  66. }
  67. @Test
  68. public void testBasicUsage() throws URISyntaxException {
  69. command.execute(container, new CommandArguments(controller, "/foo irc.foo.com"),
  70. new CommandContext(null, NewServer.INFO));
  71. verify(factory).createServer(eq(new URI("irc://irc.foo.com")), any(Profile.class));
  72. verify(connection).connect();
  73. }
  74. @Test
  75. public void testPortUsage() throws URISyntaxException {
  76. command.execute(container, new CommandArguments(controller, "/foo irc.foo.com:1234"),
  77. new CommandContext(null, NewServer.INFO));
  78. verify(factory).createServer(eq(new URI("irc://irc.foo.com:1234")), any(Profile.class));
  79. verify(connection).connect();
  80. }
  81. @Test
  82. public void testUriUsage() throws URISyntaxException {
  83. command.execute(container, new CommandArguments(controller, "/foo otheruri://foo.com:123/blah"),
  84. new CommandContext(null, NewServer.INFO));
  85. verify(factory).createServer(eq(new URI("otheruri://foo.com:123/blah")), any(Profile.class));
  86. verify(connection).connect();
  87. }
  88. @Test
  89. public void testUsageNoArgs() {
  90. command.execute(container, new CommandArguments(controller, "/foo"),
  91. new CommandContext(null, NewServer.INFO));
  92. verify(eventBus).publishAsync(isA(CommandErrorEvent.class));
  93. }
  94. @Test
  95. public void testInvalidPort() {
  96. command.execute(container, new CommandArguments(controller, "/foo foo:abc"),
  97. new CommandContext(null, NewServer.INFO));
  98. verify(eventBus).publishAsync(isA(CommandErrorEvent.class));
  99. }
  100. @Test
  101. public void testOutOfRangePort1() {
  102. command.execute(container, new CommandArguments(controller, "/foo foo:0"),
  103. new CommandContext(null, NewServer.INFO));
  104. verify(eventBus).publishAsync(isA(CommandErrorEvent.class));
  105. }
  106. @Test
  107. public void testOutOfRangePort2() {
  108. command.execute(container, new CommandArguments(controller, "/foo foo:65537"),
  109. new CommandContext(null, NewServer.INFO));
  110. verify(eventBus).publishAsync(isA(CommandErrorEvent.class));
  111. }
  112. }