Browse Source

Make the RedirectCommandTest work sanely.

Mock out everything that it uses, remove dependency on lots of
global state, and re-implement the actual test which was @Ignored.

Change-Id: I40c15e1a624b810a69c678a53fc0741bcec9e8b6
Depends-On: I63879db5c13517cfa60941fce73a2ba01e63f1cd
Reviewed-on: http://gerrit.dmdirc.com/2682
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.8
Chris Smith 10 years ago
parent
commit
1c2905d0f6
1 changed files with 40 additions and 18 deletions
  1. 40
    18
      test/com/dmdirc/addons/redirect/RedirectCommandTest.java

+ 40
- 18
test/com/dmdirc/addons/redirect/RedirectCommandTest.java View File

@@ -22,42 +22,64 @@
22 22
 
23 23
 package com.dmdirc.addons.redirect;
24 24
 
25
-import com.dmdirc.TestMain;
25
+import com.dmdirc.FrameContainer;
26 26
 import com.dmdirc.MessageTarget;
27
+import com.dmdirc.WritableFrameContainer;
27 28
 import com.dmdirc.commandparser.CommandArguments;
28
-import com.dmdirc.commandparser.CommandManager;
29 29
 import com.dmdirc.commandparser.commands.context.ChatCommandContext;
30 30
 import com.dmdirc.commandparser.commands.global.Echo;
31
-import com.dmdirc.config.IdentityManager;
32
-import com.dmdirc.config.InvalidIdentityFileException;
31
+import com.dmdirc.commandparser.parsers.CommandParser;
32
+import com.dmdirc.config.ConfigManager;
33
+import com.dmdirc.interfaces.CommandController;
33 34
 import com.dmdirc.interfaces.ui.InputWindow;
34 35
 
35
-import org.junit.BeforeClass;
36
-import org.junit.Ignore;
36
+import org.junit.Before;
37 37
 import org.junit.Test;
38
+import org.junit.runner.RunWith;
39
+import org.mockito.Mock;
40
+import org.mockito.invocation.InvocationOnMock;
41
+import org.mockito.runners.MockitoJUnitRunner;
42
+import org.mockito.stubbing.Answer;
38 43
 
39 44
 import static org.mockito.Mockito.*;
40 45
 
46
+@RunWith(MockitoJUnitRunner.class)
41 47
 public class RedirectCommandTest {
42 48
 
43
-    @BeforeClass
44
-    public static void setupClass() throws InvalidIdentityFileException {
45
-        TestMain.getTestMain();
46
-        CommandManager.getCommandManager().registerCommand(new Echo(), Echo.INFO);
49
+    @Mock private MessageTarget target;
50
+    @Mock private InputWindow inputWindow;
51
+    @Mock private CommandController commandController;
52
+    @Mock private WritableFrameContainer frameContainer;
53
+    @Mock private ConfigManager configManager;
54
+    @Mock private CommandParser commandParser;
55
+
56
+    @Before
57
+    public void setup() {
58
+        when(commandController.getCommandChar()).thenReturn('/');
59
+        when(commandController.getSilenceChar()).thenReturn('.');
60
+        when(inputWindow.getContainer()).thenReturn(frameContainer);
61
+        when(target.getConfigManager()).thenReturn(configManager);
62
+        when(target.getCommandParser()).thenReturn(commandParser);
63
+        when(configManager.hasOptionString("formatter", "commandOutput")).thenReturn(true);
64
+        when(configManager.getOption("formatter", "commandOutput")).thenReturn("%1$s");
65
+        doAnswer(new Answer<Void>() {
66
+            @Override
67
+            public Void answer(final InvocationOnMock invocation) throws Throwable {
68
+                new Echo(commandController).execute(
69
+                        ((FrameContainer) invocation.getArguments()[0]),
70
+                        new CommandArguments(commandController, "/echo test"),
71
+                        null);
72
+                return null;
73
+            }
74
+        }).when(commandParser).parseCommand(any(FrameContainer.class), eq("/echo test"));
47 75
     }
48 76
 
49
-    @Ignore
50 77
     @Test
51 78
     public void testExecute() {
52 79
         final RedirectCommand command = new RedirectCommand();
53
-        final MessageTarget target = mock(MessageTarget.class);
54
-        final InputWindow window = mock(InputWindow.class);
55
-        //when(window.getCommandParser()).thenReturn(parser);
56
-        when(window.getContainer().getConfigManager()).thenReturn(
57
-                IdentityManager.getIdentityManager().getGlobalConfiguration());
58 80
 
59
-        command.execute(target, new CommandArguments("/redirect /echo test"),
60
-                new ChatCommandContext(window.getContainer(), RedirectCommand.INFO, target));
81
+        command.execute(target, new CommandArguments(commandController, "/redirect /echo test"),
82
+                new ChatCommandContext(frameContainer, RedirectCommand.INFO, target));
61 83
 
62 84
         verify(target).sendLine("test");
63 85
     }

Loading…
Cancel
Save