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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.addons.debug;
  23. import com.dmdirc.FrameContainer;
  24. import com.dmdirc.commandparser.CommandArguments;
  25. import com.dmdirc.commandparser.commands.context.CommandContext;
  26. import com.dmdirc.config.IdentityManager;
  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.Matchers.*;
  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.getIdentityManager().initialise();
  39. }
  40. /** Checks the debug command with no arguments shows usage. */
  41. @Test
  42. public void testNoArgs() {
  43. final CommandArguments arguments = mock(CommandArguments.class);
  44. final FrameContainer container = mock(FrameContainer.class);
  45. when(arguments.isCommand()).thenReturn(true);
  46. when(arguments.getArguments()).thenReturn(new String[0]);
  47. final Debug debug = new Debug(null);
  48. debug.execute(container, arguments, null);
  49. verify(container).addLine(eq("commandUsage"), anyObject(),
  50. eq("debug"), anyObject());
  51. }
  52. /** Checks the debug command with an invalid subcommand shows an error. */
  53. @Test
  54. public void testInvalidArg() {
  55. final DebugPlugin plugin = mock(DebugPlugin.class);
  56. final CommandArguments arguments = mock(CommandArguments.class);
  57. final FrameContainer container = mock(FrameContainer.class);
  58. when(plugin.getCommand("test")).thenReturn(null);
  59. when(arguments.isCommand()).thenReturn(true);
  60. when(arguments.getArguments()).thenReturn(new String[]{"test"});
  61. final Debug debug = new Debug(plugin);
  62. debug.execute(container, arguments, null);
  63. verify(container).addLine(eq("commandError"), anyObject());
  64. }
  65. /** Checks the debug command executes a subcommand with no args. */
  66. @Test
  67. public void testCommandNoArgs() {
  68. final DebugPlugin plugin = mock(DebugPlugin.class);
  69. final CommandArguments arguments = mock(CommandArguments.class);
  70. final FrameContainer container = mock(FrameContainer.class);
  71. final DebugCommand command = mock(DebugCommand.class);
  72. final CommandContext context = mock(CommandContext.class);
  73. when(plugin.getCommand("test")).thenReturn(command);
  74. when(arguments.isCommand()).thenReturn(true);
  75. when(arguments.getArguments()).thenReturn(new String[]{"test"});
  76. when(arguments.getArgumentsAsString(1)).thenReturn("");
  77. when(command.getName()).thenReturn("test");
  78. final Debug debug = new Debug(plugin);
  79. debug.execute(container, arguments, context);
  80. verify(container, never()).addLine(anyString(), anyObject());
  81. verify(command).execute(same(container), eqLine("/test"), same(context));
  82. }
  83. /** Checks the debug command executes a subcommand with args. */
  84. @Test
  85. public void testCommandWithArgs() {
  86. final DebugPlugin plugin = mock(DebugPlugin.class);
  87. final CommandArguments arguments = mock(CommandArguments.class);
  88. final FrameContainer container = mock(FrameContainer.class);
  89. final DebugCommand command = mock(DebugCommand.class);
  90. final CommandContext context = mock(CommandContext.class);
  91. when(plugin.getCommand("test")).thenReturn(command);
  92. when(arguments.isCommand()).thenReturn(true);
  93. when(arguments.getArguments()).thenReturn(new String[]{"test", "1", "2", "3"});
  94. when(arguments.getArgumentsAsString(1)).thenReturn("1 2 3");
  95. when(command.getName()).thenReturn("test");
  96. final Debug debug = new Debug(plugin);
  97. debug.execute(container, arguments, context);
  98. verify(container, never()).addLine(anyString(), anyObject());
  99. verify(command).execute(same(container), eqLine("/test 1 2 3"), same(context));
  100. }
  101. }