Browse Source

Fix up debug command tests.

Change-Id: I62dfab20d1d457af0af44ccdddd0b584d2406da7
Reviewed-on: http://gerrit.dmdirc.com/2683
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.8
Chris Smith 10 years ago
parent
commit
5fdc06f334

+ 10
- 5
src/com/dmdirc/addons/debug/Debug.java View File

@@ -26,11 +26,11 @@ import com.dmdirc.FrameContainer;
26 26
 import com.dmdirc.commandparser.BaseCommandInfo;
27 27
 import com.dmdirc.commandparser.CommandArguments;
28 28
 import com.dmdirc.commandparser.CommandInfo;
29
-import com.dmdirc.commandparser.CommandManager;
30 29
 import com.dmdirc.commandparser.CommandType;
31 30
 import com.dmdirc.commandparser.commands.Command;
32 31
 import com.dmdirc.commandparser.commands.IntelligentCommand;
33 32
 import com.dmdirc.commandparser.commands.context.CommandContext;
33
+import com.dmdirc.interfaces.CommandController;
34 34
 import com.dmdirc.ui.input.AdditionalTabTargets;
35 35
 
36 36
 import java.util.Arrays;
@@ -48,15 +48,20 @@ public class Debug extends Command implements IntelligentCommand {
48 48
     /** Parent debug plugin. */
49 49
     private final DebugPlugin plugin;
50 50
 
51
+    /** The command controller to use to lookup command information. */
52
+    private final CommandController controller;
53
+
51 54
     /**
52 55
      * Creates a new debug command with the specified parent plugin.
53 56
      *
54
-     * @param plugin Parent debug plugin
57
+     * @param plugin Parent debug plugin.
58
+     * @param controller The command controller to use to lookup command information.
55 59
      */
56
-    public Debug(final DebugPlugin plugin) {
57
-        super();
60
+    public Debug(final DebugPlugin plugin, final CommandController controller) {
61
+        super(controller);
58 62
 
59 63
         this.plugin = plugin;
64
+        this.controller = controller;
60 65
     }
61 66
 
62 67
     /** {@inheritDoc} */
@@ -74,7 +79,7 @@ public class Debug extends Command implements IntelligentCommand {
74 79
                         "Unknown debug action.");
75 80
             } else {
76 81
                 final CommandArguments newArgs = new CommandArguments(
77
-                        Arrays.asList((CommandManager.getCommandManager().getCommandChar()
82
+                        Arrays.asList((controller.getCommandChar()
78 83
                         + command.getName() + " "
79 84
                         + args.getArgumentsAsString(1)).split(" ")));
80 85
                 command.execute(origin, newArgs, context);

+ 4
- 7
src/com/dmdirc/addons/debug/DebugPlugin.java View File

@@ -77,8 +77,8 @@ public class DebugPlugin extends BaseCommandPlugin {
77 77
         super(commandController);
78 78
         this.identityManager = identityManager;
79 79
         this.pluginManager = pluginManager;
80
-        commands = new HashMap<String, DebugCommand>();
81
-        debugCommand = new Debug(this);
80
+        commands = new HashMap<>();
81
+        debugCommand = new Debug(this, commandController);
82 82
         registerCommand(debugCommand, Debug.INFO);
83 83
     }
84 84
 
@@ -90,10 +90,7 @@ public class DebugPlugin extends BaseCommandPlugin {
90 90
             try {
91 91
                 addCommand(type.getConstructor(DebugPlugin.class, Debug.class)
92 92
                         .newInstance(this, debugCommand));
93
-            } catch (LinkageError e) {
94
-                Logger.appError(ErrorLevel.HIGH,
95
-                        "Unable to load debug command", e);
96
-            } catch (Exception e) {
93
+            } catch (LinkageError | Exception e) {
97 94
                 Logger.appError(ErrorLevel.HIGH,
98 95
                         "Unable to load debug command", e);
99 96
             }
@@ -132,7 +129,7 @@ public class DebugPlugin extends BaseCommandPlugin {
132 129
      * @return List of command names
133 130
      */
134 131
     public List<String> getCommandNames() {
135
-        final List<String> names = new ArrayList<String>(commands.size());
132
+        final List<String> names = new ArrayList<>(commands.size());
136 133
 
137 134
         for (DebugCommand command : commands.values()) {
138 135
             names.add(command.getName());

+ 26
- 44
test/com/dmdirc/addons/debug/DebugTest.java View File

@@ -22,38 +22,42 @@
22 22
 
23 23
 package com.dmdirc.addons.debug;
24 24
 
25
-import com.dmdirc.TestMain;
26 25
 import com.dmdirc.FrameContainer;
27 26
 import com.dmdirc.commandparser.CommandArguments;
28 27
 import com.dmdirc.commandparser.commands.context.CommandContext;
29
-import com.dmdirc.config.InvalidIdentityFileException;
28
+import com.dmdirc.interfaces.CommandController;
30 29
 
31
-import org.junit.BeforeClass;
30
+import org.junit.Before;
32 31
 import org.junit.Test;
32
+import org.junit.runner.RunWith;
33
+import org.mockito.Mock;
34
+import org.mockito.runners.MockitoJUnitRunner;
33 35
 
34 36
 import static com.dmdirc.harness.CommandArgsMatcher.*;
35 37
 import static org.mockito.Mockito.*;
36 38
 
39
+@RunWith(MockitoJUnitRunner.class)
37 40
 public class DebugTest {
38 41
 
39
-    @BeforeClass
40
-    public static void setUp() throws InvalidIdentityFileException {
41
-        // Command has a dependency on CommandManager (to get the command char)
42
-        // And CommandManager has a dependency on IdentityManager for the same
43
-        TestMain.getTestMain();
42
+    @Mock private CommandArguments arguments;
43
+    @Mock private FrameContainer container;
44
+    @Mock private DebugPlugin plugin;
45
+    @Mock private CommandController controller;
46
+    @Mock private DebugCommand debugCommand;
47
+    @Mock private CommandContext commandContext;
48
+    private Debug debug;
49
+
50
+    @Before
51
+    public void setup() {
52
+        when(controller.getCommandChar()).thenReturn('/');
53
+        debug = new Debug(plugin, controller);
44 54
     }
45 55
 
46 56
     /** Checks the debug command with no arguments shows usage. */
47 57
     @Test
48 58
     public void testNoArgs() {
49
-        final CommandArguments arguments = mock(CommandArguments.class);
50
-        final FrameContainer container = mock(FrameContainer.class);
51
-
52 59
         when(arguments.isCommand()).thenReturn(true);
53 60
         when(arguments.getArguments()).thenReturn(new String[0]);
54
-
55
-        final Debug debug = new Debug(null);
56
-
57 61
         debug.execute(container, arguments, null);
58 62
         verify(container).addLine(eq("commandUsage"), anyObject(),
59 63
                 eq("debug"), anyObject());
@@ -62,16 +66,10 @@ public class DebugTest {
62 66
     /** Checks the debug command with an invalid subcommand shows an error. */
63 67
     @Test
64 68
     public void testInvalidArg() {
65
-        final DebugPlugin plugin = mock(DebugPlugin.class);
66
-        final CommandArguments arguments = mock(CommandArguments.class);
67
-        final FrameContainer container = mock(FrameContainer.class);
68
-
69 69
         when(plugin.getCommand("test")).thenReturn(null);
70 70
         when(arguments.isCommand()).thenReturn(true);
71 71
         when(arguments.getArguments()).thenReturn(new String[]{"test"});
72 72
 
73
-        final Debug debug = new Debug(plugin);
74
-
75 73
         debug.execute(container, arguments, null);
76 74
         verify(container).addLine(eq("commandError"), anyObject());
77 75
     }
@@ -79,47 +77,31 @@ public class DebugTest {
79 77
     /** Checks the debug command executes a subcommand with no args. */
80 78
     @Test
81 79
     public void testCommandNoArgs() {
82
-        final DebugPlugin plugin = mock(DebugPlugin.class);
83
-        final CommandArguments arguments = mock(CommandArguments.class);
84
-        final FrameContainer container = mock(FrameContainer.class);
85
-        final DebugCommand command = mock(DebugCommand.class);
86
-        final CommandContext context = mock(CommandContext.class);
87
-
88
-        when(plugin.getCommand("test")).thenReturn(command);
80
+        when(plugin.getCommand("test")).thenReturn(debugCommand);
89 81
         when(arguments.isCommand()).thenReturn(true);
90 82
         when(arguments.getArguments()).thenReturn(new String[]{"test"});
91 83
         when(arguments.getArgumentsAsString(1)).thenReturn("");
92
-        when(command.getName()).thenReturn("test");
93
-
94
-        final Debug debug = new Debug(plugin);
84
+        when(debugCommand.getName()).thenReturn("test");
95 85
 
96
-        debug.execute(container, arguments, context);
86
+        debug.execute(container, arguments, commandContext);
97 87
 
98 88
         verify(container, never()).addLine(anyString(), anyObject());
99
-        verify(command).execute(same(container), eqLine("/test"), same(context));
89
+        verify(debugCommand).execute(same(container), eqLine("/test"), same(commandContext));
100 90
     }
101 91
 
102 92
     /** Checks the debug command executes a subcommand with args. */
103 93
     @Test
104 94
     public void testCommandWithArgs() {
105
-        final DebugPlugin plugin = mock(DebugPlugin.class);
106
-        final CommandArguments arguments = mock(CommandArguments.class);
107
-        final FrameContainer container = mock(FrameContainer.class);
108
-        final DebugCommand command = mock(DebugCommand.class);
109
-        final CommandContext context = mock(CommandContext.class);
110
-
111
-        when(plugin.getCommand("test")).thenReturn(command);
95
+        when(plugin.getCommand("test")).thenReturn(debugCommand);
112 96
         when(arguments.isCommand()).thenReturn(true);
113 97
         when(arguments.getArguments()).thenReturn(new String[]{"test", "1", "2", "3"});
114 98
         when(arguments.getArgumentsAsString(1)).thenReturn("1 2 3");
115
-        when(command.getName()).thenReturn("test");
116
-
117
-        final Debug debug = new Debug(plugin);
99
+        when(debugCommand.getName()).thenReturn("test");
118 100
 
119
-        debug.execute(container, arguments, context);
101
+        debug.execute(container, arguments, commandContext);
120 102
 
121 103
         verify(container, never()).addLine(anyString(), anyObject());
122
-        verify(command).execute(same(container), eqLine("/test 1 2 3"), same(context));
104
+        verify(debugCommand).execute(same(container), eqLine("/test 1 2 3"), same(commandContext));
123 105
     }
124 106
 
125 107
 }

Loading…
Cancel
Save