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.

DebugTest.java 5.1KB

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.addons.debug;
  23. import com.dmdirc.TestMain;
  24. import com.dmdirc.FrameContainer;
  25. import com.dmdirc.commandparser.CommandArguments;
  26. import com.dmdirc.commandparser.commands.context.CommandContext;
  27. import com.dmdirc.config.InvalidIdentityFileException;
  28. import org.junit.BeforeClass;
  29. import org.junit.Test;
  30. import static com.dmdirc.harness.CommandArgsMatcher.*;
  31. import static org.mockito.Mockito.*;
  32. public class DebugTest {
  33. @BeforeClass
  34. public static void setUp() throws InvalidIdentityFileException {
  35. // Command has a dependency on CommandManager (to get the command char)
  36. // And CommandManager has a dependency on IdentityManager for the same
  37. TestMain.getTestMain();
  38. }
  39. /** Checks the debug command with no arguments shows usage. */
  40. @Test
  41. public void testNoArgs() {
  42. final CommandArguments arguments = mock(CommandArguments.class);
  43. final FrameContainer container = mock(FrameContainer.class);
  44. when(arguments.isCommand()).thenReturn(true);
  45. when(arguments.getArguments()).thenReturn(new String[0]);
  46. final Debug debug = new Debug(null);
  47. debug.execute(container, arguments, null);
  48. verify(container).addLine(eq("commandUsage"), anyObject(),
  49. eq("debug"), anyObject());
  50. }
  51. /** Checks the debug command with an invalid subcommand shows an error. */
  52. @Test
  53. public void testInvalidArg() {
  54. final DebugPlugin plugin = mock(DebugPlugin.class);
  55. final CommandArguments arguments = mock(CommandArguments.class);
  56. final FrameContainer container = mock(FrameContainer.class);
  57. when(plugin.getCommand("test")).thenReturn(null);
  58. when(arguments.isCommand()).thenReturn(true);
  59. when(arguments.getArguments()).thenReturn(new String[]{"test"});
  60. final Debug debug = new Debug(plugin);
  61. debug.execute(container, arguments, null);
  62. verify(container).addLine(eq("commandError"), anyObject());
  63. }
  64. /** Checks the debug command executes a subcommand with no args. */
  65. @Test
  66. public void testCommandNoArgs() {
  67. final DebugPlugin plugin = mock(DebugPlugin.class);
  68. final CommandArguments arguments = mock(CommandArguments.class);
  69. final FrameContainer container = mock(FrameContainer.class);
  70. final DebugCommand command = mock(DebugCommand.class);
  71. final CommandContext context = mock(CommandContext.class);
  72. when(plugin.getCommand("test")).thenReturn(command);
  73. when(arguments.isCommand()).thenReturn(true);
  74. when(arguments.getArguments()).thenReturn(new String[]{"test"});
  75. when(arguments.getArgumentsAsString(1)).thenReturn("");
  76. when(command.getName()).thenReturn("test");
  77. final Debug debug = new Debug(plugin);
  78. debug.execute(container, arguments, context);
  79. verify(container, never()).addLine(anyString(), anyObject());
  80. verify(command).execute(same(container), eqLine("/test"), same(context));
  81. }
  82. /** Checks the debug command executes a subcommand with args. */
  83. @Test
  84. public void testCommandWithArgs() {
  85. final DebugPlugin plugin = mock(DebugPlugin.class);
  86. final CommandArguments arguments = mock(CommandArguments.class);
  87. final FrameContainer container = mock(FrameContainer.class);
  88. final DebugCommand command = mock(DebugCommand.class);
  89. final CommandContext context = mock(CommandContext.class);
  90. when(plugin.getCommand("test")).thenReturn(command);
  91. when(arguments.isCommand()).thenReturn(true);
  92. when(arguments.getArguments()).thenReturn(new String[]{"test", "1", "2", "3"});
  93. when(arguments.getArgumentsAsString(1)).thenReturn("1 2 3");
  94. when(command.getName()).thenReturn("test");
  95. final Debug debug = new Debug(plugin);
  96. debug.execute(container, arguments, context);
  97. verify(container, never()).addLine(anyString(), anyObject());
  98. verify(command).execute(same(container), eqLine("/test 1 2 3"), same(context));
  99. }
  100. }