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

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