Browse Source

Fix error with channel commands without args.

Previously /mode #channel failed with an exception trying to
get the arguments. Now it works as expected.

Also fix an issue with silence chars not being propagated to
channel command - /.topic #channel will now work silently.

Change-Id: Ifa314b0daf81a30003d5879b9484e759668bc763
Fixes-Issue: CLIENT-455
Reviewed-on: http://gerrit.dmdirc.com/3208
Automatic-Compile: DMDirc Build Manager
Reviewed-by: Greg Holmes <greg@dmdirc.com>
tags/0.8rc1
Chris Smith 10 years ago
parent
commit
2270000cd3

+ 16
- 17
src/com/dmdirc/commandparser/parsers/CommandParser.java View File

@@ -51,19 +51,11 @@ import java.util.Map;
51 51
  */
52 52
 public abstract class CommandParser implements Serializable {
53 53
 
54
-    /**
55
-     * A version number for this class. It should be changed whenever the class structure is changed
56
-     * (or anything else that would prevent serialized objects being unserialized with the new
57
-     * class).
58
-     */
54
+    /** A version number for this class. */
59 55
     private static final long serialVersionUID = 1;
60
-    /**
61
-     * Commands that are associated with this parser.
62
-     */
56
+    /** Commands that are associated with this parser. */
63 57
     private final Map<String, CommandInfoPair> commands;
64
-    /**
65
-     * A history of commands that have been entered into this parser.
66
-     */
58
+    /** A history of commands that have been entered into this parser. */
67 59
     private final RollingList<PreviousCommand> history;
68 60
     /** Command manager to use. */
69 61
     protected final CommandController commandManager;
@@ -82,6 +74,10 @@ public abstract class CommandParser implements Serializable {
82 74
         loadCommands();
83 75
     }
84 76
 
77
+    /**
78
+     * @deprecated Callers should obtain their own instance of command controller.
79
+     */
80
+    @Deprecated
85 81
     public CommandController getCommandManager() {
86 82
         return commandManager;
87 83
     }
@@ -141,7 +137,7 @@ public abstract class CommandParser implements Serializable {
141 137
      */
142 138
     public final void parseCommand(final FrameContainer origin,
143 139
             final String line, final boolean parseChannel) {
144
-        final CommandArguments args = new CommandArguments(getCommandManager(), line);
140
+        final CommandArguments args = new CommandArguments(commandManager, line);
145 141
 
146 142
         if (args.isCommand()) {
147 143
             if (handleChannelCommand(origin, args, parseChannel)) {
@@ -200,10 +196,14 @@ public abstract class CommandParser implements Serializable {
200 196
                     continue;
201 197
                 }
202 198
 
199
+                final String newCommandString = commandManager.getCommandChar()
200
+                        + (silent ? String.valueOf(commandManager.getSilenceChar()) : "")
201
+                        + args.getCommandName()
202
+                        + (cargs.length > 1 ? " " + args.getArgumentsAsString(1) : "");
203
+
203 204
                 if (server.hasChannel(channel)) {
204
-                    server.getChannel(channel).getCommandParser()
205
-                            .parseCommand(origin, commandManager.getCommandChar()
206
-                            + args.getCommandName() + " " + args.getWordsAsString(2), false);
205
+                    server.getChannel(channel).getCommandParser().parseCommand(origin,
206
+                            newCommandString, false);
207 207
                 } else {
208 208
                     final Map.Entry<CommandInfo, Command> actCommand = commandManager.getCommand(
209 209
                             CommandType.TYPE_CHANNEL, command);
@@ -211,8 +211,7 @@ public abstract class CommandParser implements Serializable {
211 211
                     if (actCommand != null && actCommand.getValue() instanceof ExternalCommand) {
212 212
                         ((ExternalCommand) actCommand.getValue()).execute(
213 213
                                 origin, (Server) server, channel, silent,
214
-                                new CommandArguments(commandManager, args.getCommandName()
215
-                                + " " + args.getWordsAsString(2)));
214
+                                new CommandArguments(commandManager, newCommandString));
216 215
                     }
217 216
                 }
218 217
             }

+ 165
- 102
test/com/dmdirc/commandparser/parsers/CommandParserTest.java View File

@@ -19,155 +19,218 @@
19 19
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 20
  * SOFTWARE.
21 21
  */
22
+
22 23
 package com.dmdirc.commandparser.parsers;
23 24
 
24
-import com.dmdirc.ServerManager;
25
-import com.dmdirc.commandparser.CommandManager;
26
-import com.dmdirc.commandparser.commands.global.Echo;
27
-import com.dmdirc.config.ConfigBinder;
28
-import com.dmdirc.config.InvalidIdentityFileException;
25
+import com.dmdirc.Channel;
26
+import com.dmdirc.FrameContainer;
27
+import com.dmdirc.commandparser.CommandInfo;
28
+import com.dmdirc.commandparser.commands.Command;
29 29
 import com.dmdirc.harness.TestCommandParser;
30
+import com.dmdirc.interfaces.CommandController;
31
+import com.dmdirc.interfaces.Connection;
30 32
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
31
-import com.dmdirc.ui.WindowManager;
32 33
 
33 34
 import org.junit.Before;
34 35
 import org.junit.Test;
36
+import org.junit.runner.RunWith;
35 37
 import org.mockito.Mock;
36
-import org.mockito.MockitoAnnotations;
38
+import org.mockito.runners.MockitoJUnitRunner;
37 39
 
38
-import static org.junit.Assert.*;
39
-import static org.mockito.Mockito.*;
40
+import static org.junit.Assert.assertEquals;
41
+import static org.junit.Assert.assertFalse;
42
+import static org.junit.Assert.assertNull;
43
+import static org.junit.Assert.assertSame;
44
+import static org.junit.Assert.assertTrue;
45
+import static org.mockito.Mockito.when;
40 46
 
47
+@RunWith(MockitoJUnitRunner.class)
41 48
 public class CommandParserTest {
42 49
 
43
-    @Mock private ServerManager serverManager;
44
-    @Mock private AggregateConfigProvider cm;
45
-    @Mock private WindowManager windowManager;
46
-    private CommandManager commands;
50
+    @Mock private AggregateConfigProvider configProvider;
51
+    @Mock private CommandController commandController;
52
+    @Mock private CommandInfo commandInfo;
53
+    @Mock private CommandInfo channelCommandInfo;
54
+    @Mock private Command command;
55
+    @Mock private Command channelCommand;
56
+    @Mock private FrameContainer container;
57
+    @Mock private Channel channel;
58
+    @Mock private Connection connection;
59
+    private TestCommandParser commandParser;
60
+    private TestCommandParser channelCommandParser;
47 61
 
48 62
     @Before
49
-    public void setup() throws InvalidIdentityFileException {
50
-        MockitoAnnotations.initMocks(this);
51
-        when(cm.getOptionChar("general", "silencechar")).thenReturn('.');
52
-        when(cm.getOptionInt("general", "commandhistory")).thenReturn(10);
53
-        when(cm.getOptionChar("general", "commandchar")).thenReturn('/');
54
-        final ConfigBinder binder = new ConfigBinder(cm);
55
-        when(cm.getBinder()).thenReturn(binder);
56
-        commands = new CommandManager(serverManager);
57
-        commands.initialise(cm);
58
-        commands.registerCommand(new Echo(commands, windowManager), Echo.INFO);
63
+    public void setup() {
64
+        when(commandController.getCommandChar()).thenReturn('/');
65
+        when(commandController.getSilenceChar()).thenReturn('.');
66
+        when(commandController.isChannelCommand("channel")).thenReturn(true);
67
+
68
+        when(commandInfo.getName()).thenReturn("command");
69
+        when(channelCommandInfo.getName()).thenReturn("channel");
70
+
71
+        when(configProvider.getOptionInt("general", "commandhistory")).thenReturn(10);
72
+
73
+        when(container.getConnection()).thenReturn(connection);
74
+        when(connection.isValidChannelName("#channel1")).thenReturn(true);
75
+        when(connection.isValidChannelName("#channel2")).thenReturn(true);
76
+        when(connection.hasChannel("#channel1")).thenReturn(true);
77
+        when(connection.getChannel("#channel1")).thenReturn(channel);
78
+
79
+        commandParser = new TestCommandParser(configProvider, commandController);
80
+        commandParser.registerCommand(command, commandInfo);
81
+        commandParser.registerCommand(channelCommand, channelCommandInfo);
82
+
83
+        channelCommandParser = new TestCommandParser(configProvider, commandController);
84
+        channelCommandParser.registerCommand(channelCommand, channelCommandInfo);
85
+
86
+        when(channel.getCommandParser()).thenReturn(channelCommandParser);
59 87
     }
60 88
 
61 89
     @Test
62
-    public void testBasicCommand() {
63
-        final TestCommandParser tcp = new TestCommandParser(cm, commands);
64
-        tcp.parseCommand(null, "/echo this is a test");
65
-
66
-        assertNull(tcp.nonCommandLine);
67
-        assertNull(tcp.invalidCommand);
68
-        assertNotNull(tcp.executedCommand);
69
-        assertFalse(tcp.wasSilent);
70
-        assertTrue(tcp.executedCommand instanceof Echo);
90
+    public void testParseCommandWithArguments() {
91
+        commandParser.parseCommand(null, "/command this is a test");
92
+
93
+        assertNull(commandParser.nonCommandLine);
94
+        assertNull(commandParser.invalidCommand);
95
+        assertFalse(commandParser.wasSilent);
96
+        assertSame(command, commandParser.executedCommand);
97
+        assertEquals("this is a test", commandParser.commandArgs.getArgumentsAsString());
71 98
     }
72 99
 
73 100
     @Test
74
-    public void testBasicNoArgs() {
75
-        final TestCommandParser tcp = new TestCommandParser(cm, commands);
76
-        tcp.parseCommand(null, "/echo");
77
-
78
-        assertNull(tcp.nonCommandLine);
79
-        assertNull(tcp.invalidCommand);
80
-        assertNotNull(tcp.executedCommand);
81
-        assertFalse(tcp.wasSilent);
82
-        assertTrue(tcp.executedCommand instanceof Echo);
101
+    public void testParseCommandWithoutArguments() {
102
+        commandParser.parseCommand(null, "/command");
103
+
104
+        assertNull(commandParser.nonCommandLine);
105
+        assertNull(commandParser.invalidCommand);
106
+        assertFalse(commandParser.wasSilent);
107
+        assertSame(command, commandParser.executedCommand);
108
+        assertEquals("", commandParser.commandArgs.getArgumentsAsString());
83 109
     }
84 110
 
85 111
     @Test
86
-    public void testSilentNoArgs() {
87
-        final TestCommandParser tcp = new TestCommandParser(cm, commands);
88
-        tcp.parseCommand(null, "/.echo");
89
-
90
-        assertNull(tcp.nonCommandLine);
91
-        assertNull(tcp.invalidCommand);
92
-        assertNotNull(tcp.executedCommand);
93
-        assertTrue(tcp.wasSilent);
94
-        assertTrue(tcp.executedCommand instanceof Echo);
112
+    public void testParseSilentCommandWithoutArguments() {
113
+        commandParser.parseCommand(null, "/.command");
114
+
115
+        assertNull(commandParser.nonCommandLine);
116
+        assertNull(commandParser.invalidCommand);
117
+        assertTrue(commandParser.wasSilent);
118
+        assertSame(command, commandParser.executedCommand);
119
+        assertEquals("", commandParser.commandArgs.getArgumentsAsString());
95 120
     }
96 121
 
97 122
     @Test
98
-    public void testSilentCommand() {
99
-        final TestCommandParser tcp = new TestCommandParser(cm, commands);
100
-        tcp.parseCommand(null, "/.echo this is a test");
101
-
102
-        assertNull(tcp.nonCommandLine);
103
-        assertNull(tcp.invalidCommand);
104
-        assertNotNull(tcp.executedCommand);
105
-        assertTrue(tcp.wasSilent);
106
-        assertTrue(tcp.executedCommand instanceof Echo);
123
+    public void testParseSilentCommandWithArguments() {
124
+        commandParser.parseCommand(null, "/.command this is a test");
125
+
126
+        assertNull(commandParser.nonCommandLine);
127
+        assertNull(commandParser.invalidCommand);
128
+        assertTrue(commandParser.wasSilent);
129
+        assertSame(command, commandParser.executedCommand);
130
+        assertEquals("this is a test", commandParser.commandArgs.getArgumentsAsString());
107 131
     }
108 132
 
109 133
     @Test
110
-    public void testNonExistantCommand() {
111
-        final TestCommandParser tcp = new TestCommandParser(cm, commands);
112
-        tcp.parseCommand(null, "/foobar moo bar");
113
-
114
-        assertNull(tcp.nonCommandLine);
115
-        assertEquals("foobar", tcp.invalidCommand);
116
-        assertNotNull(tcp.invalidCommand);
117
-        assertNull(tcp.executedCommand);
118
-        assertFalse(tcp.wasSilent);
134
+    public void testParseUnknownCommand() {
135
+        commandParser.parseCommand(null, "/foobar moo bar");
136
+
137
+        assertNull(commandParser.nonCommandLine);
138
+        assertEquals("foobar", commandParser.invalidCommand);
139
+        assertNull(commandParser.executedCommand);
140
+        assertFalse(commandParser.wasSilent);
119 141
     }
120 142
 
121 143
     @Test
122
-    public void testEmptyCommand() {
123
-        final TestCommandParser tcp = new TestCommandParser(cm, commands);
124
-        tcp.parseCommand(null, "/ moo bar");
125
-
126
-        assertNull(tcp.nonCommandLine);
127
-        assertEquals("", tcp.invalidCommand);
128
-        assertNotNull(tcp.invalidCommand);
129
-        assertNull(tcp.executedCommand);
130
-        assertFalse(tcp.wasSilent);
144
+    public void testParseEmptyCommand() {
145
+        commandParser.parseCommand(null, "/ moo bar");
146
+
147
+        assertNull(commandParser.nonCommandLine);
148
+        assertEquals("", commandParser.invalidCommand);
149
+        assertNull(commandParser.executedCommand);
150
+        assertFalse(commandParser.wasSilent);
131 151
     }
132 152
 
133 153
     @Test
134
-    public void testEmptySilentCommand() {
135
-        final TestCommandParser tcp = new TestCommandParser(cm, commands);
136
-        tcp.parseCommand(null, "/. moo bar");
137
-
138
-        assertNull(tcp.nonCommandLine);
139
-        assertEquals("", tcp.invalidCommand);
140
-        assertNotNull(tcp.invalidCommand);
141
-        assertNull(tcp.executedCommand);
142
-        assertFalse(tcp.wasSilent);
154
+    public void testParseEmptySilenceCommand() {
155
+        commandParser.parseCommand(null, "/. moo bar");
156
+
157
+        assertNull(commandParser.nonCommandLine);
158
+        assertEquals("", commandParser.invalidCommand);
159
+        assertNull(commandParser.executedCommand);
160
+        assertFalse(commandParser.wasSilent);
143 161
     }
144 162
 
145 163
     @Test
146
-    public void testNonCommand() {
147
-        final TestCommandParser tcp = new TestCommandParser(cm, commands);
148
-        tcp.parseCommand(null, "Foobar baz");
149
-
150
-        assertNotNull(tcp.nonCommandLine);
151
-        assertEquals("Foobar baz", tcp.nonCommandLine);
152
-        assertNull(tcp.invalidCommand);
153
-        assertNull(tcp.executedCommand);
154
-        assertFalse(tcp.wasSilent);
164
+    public void testParseNonCommand() {
165
+        commandParser.parseCommand(null, "Foobar baz");
166
+
167
+        assertEquals("Foobar baz", commandParser.nonCommandLine);
168
+        assertNull(commandParser.invalidCommand);
169
+        assertNull(commandParser.executedCommand);
170
+        assertFalse(commandParser.wasSilent);
155 171
     }
156 172
 
157 173
     @Test
158
-    public void testCommandHistory() {
159
-        final TestCommandParser tcp = new TestCommandParser(cm, commands);
160
-        tcp.parseCommand(null, "/echo this is a test");
174
+    public void testGetCommandTime() {
175
+        commandParser.parseCommand(null, "/command this is a test");
161 176
 
162
-        final long time1 = tcp.getCommandTime("echo this is a test");
177
+        final long time1 = commandParser.getCommandTime("command this is a test");
163 178
         assertTrue(time1 > 0);
164 179
 
165
-        tcp.parseCommand( null, "/echo this is a test");
166
-        final long time2 = tcp.getCommandTime("echo this is a test");
180
+        commandParser.parseCommand(null, "/command this is a test");
181
+        final long time2 = commandParser.getCommandTime("command this is a test");
167 182
         assertTrue(time2 > 0);
168 183
         assertTrue(time2 >= time1);
169 184
 
170
-        assertEquals(0L, tcp.getCommandTime("echo"));
185
+        assertEquals(0L, commandParser.getCommandTime("command"));
186
+    }
187
+
188
+    @Test
189
+    public void testParseChannelCommandWithArguments() {
190
+        when(container.getConnection()).thenReturn(connection);
191
+        commandParser.parseCommand(container, "/channel #channel1 this is a test");
192
+
193
+        assertNull(channelCommandParser.nonCommandLine);
194
+        assertNull(channelCommandParser.invalidCommand);
195
+        assertFalse(channelCommandParser.wasSilent);
196
+        assertSame(channelCommand, channelCommandParser.executedCommand);
197
+        assertEquals("this is a test", channelCommandParser.commandArgs.getArgumentsAsString());
198
+    }
199
+
200
+    @Test
201
+    public void testParseChannelCommandWithoutArguments() {
202
+        when(container.getConnection()).thenReturn(connection);
203
+        commandParser.parseCommand(container, "/channel #channel1");
204
+
205
+        assertNull(channelCommandParser.nonCommandLine);
206
+        assertNull(channelCommandParser.invalidCommand);
207
+        assertFalse(channelCommandParser.wasSilent);
208
+        assertSame(channelCommand, channelCommandParser.executedCommand);
209
+        assertEquals("", channelCommandParser.commandArgs.getArgumentsAsString());
210
+    }
211
+
212
+    @Test
213
+    public void testParseSilencedChannelCommandWithArguments() {
214
+        when(container.getConnection()).thenReturn(connection);
215
+        commandParser.parseCommand(container, "/.channel #channel1 this is a test");
216
+
217
+        assertNull(channelCommandParser.nonCommandLine);
218
+        assertNull(channelCommandParser.invalidCommand);
219
+        assertTrue(channelCommandParser.wasSilent);
220
+        assertSame(channelCommand, channelCommandParser.executedCommand);
221
+        assertEquals("this is a test", channelCommandParser.commandArgs.getArgumentsAsString());
222
+    }
223
+
224
+    @Test
225
+    public void testParseSilencedChannelCommandWithoutArguments() {
226
+        when(container.getConnection()).thenReturn(connection);
227
+        commandParser.parseCommand(container, "/.channel #channel1");
228
+
229
+        assertNull(channelCommandParser.nonCommandLine);
230
+        assertNull(channelCommandParser.invalidCommand);
231
+        assertTrue(channelCommandParser.wasSilent);
232
+        assertSame(channelCommand, channelCommandParser.executedCommand);
233
+        assertEquals("", channelCommandParser.commandArgs.getArgumentsAsString());
171 234
     }
172 235
 
173 236
 }

+ 4
- 3
test/com/dmdirc/harness/TestCommandParser.java View File

@@ -25,7 +25,6 @@ package com.dmdirc.harness;
25 25
 import com.dmdirc.FrameContainer;
26 26
 import com.dmdirc.commandparser.CommandArguments;
27 27
 import com.dmdirc.commandparser.CommandInfo;
28
-import com.dmdirc.commandparser.CommandType;
29 28
 import com.dmdirc.commandparser.commands.Command;
30 29
 import com.dmdirc.commandparser.commands.context.CommandContext;
31 30
 import com.dmdirc.commandparser.parsers.CommandParser;
@@ -33,6 +32,7 @@ import com.dmdirc.interfaces.CommandController;
33 32
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
34 33
 
35 34
 public class TestCommandParser extends CommandParser {
35
+
36 36
     private static final long serialVersionUID = 7073002401375438532L;
37 37
 
38 38
     public String nonCommandLine;
@@ -52,7 +52,7 @@ public class TestCommandParser extends CommandParser {
52 52
 
53 53
     @Override
54 54
     protected void loadCommands() {
55
-        commandManager.loadCommands(this, CommandType.TYPE_GLOBAL);
55
+        // Do nothing
56 56
     }
57 57
 
58 58
     @Override
@@ -76,7 +76,7 @@ public class TestCommandParser extends CommandParser {
76 76
 
77 77
     @Override
78 78
     protected void handleInvalidCommand(FrameContainer origin,
79
-                                        CommandArguments args) {
79
+            CommandArguments args) {
80 80
         invalidCommand = args.getCommandName();
81 81
     }
82 82
 
@@ -84,4 +84,5 @@ public class TestCommandParser extends CommandParser {
84 84
     public void setOwner(FrameContainer owner) {
85 85
         // Don't care
86 86
     }
87
+
87 88
 }

Loading…
Cancel
Save