00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.commands;
00023
00024 import com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection;
00025 import com.realtime.crossfire.jxclient.server.crossfire.TestCrossfireServerConnection;
00026 import java.util.Arrays;
00027 import java.util.Collection;
00028 import org.jetbrains.annotations.NotNull;
00029 import org.junit.Assert;
00030 import org.junit.Test;
00031
00036 public class CommandExpanderTest {
00037
00042 @Test
00043 public void testExpandSingle() {
00044 final Commands commands = new Commands();
00045 check("xyz", commands, new CommandExec(null, "xyz"));
00046 }
00047
00052 @Test
00053 public void testExpandMultiple() {
00054 final Commands commands = new Commands();
00055 check("xyz;abc;abc", commands, new CommandExec(null, "xyz"), new CommandExec(null, "abc"), new CommandExec(null, "abc"));
00056 }
00057
00062 @Test
00063 public void testExpandIgnoreWhitespace() {
00064 final Commands commands = new Commands();
00065 check("xyz;abc", commands, new CommandExec(null, "xyz"), new CommandExec(null, "abc"));
00066 check("xyz ;abc", commands, new CommandExec(null, "xyz"), new CommandExec(null, "abc"));
00067 check("xyz; abc", commands, new CommandExec(null, "xyz"), new CommandExec(null, "abc"));
00068 check("xyz ; abc", commands, new CommandExec(null, "xyz"), new CommandExec(null, "abc"));
00069 }
00070
00075 @Test
00076 public void testExpandIgnoreEmpty() {
00077 final Commands commands = new Commands();
00078 check("", commands);
00079 check(";;;", commands);
00080 check(" ; xyz ; abc ; ", commands, new CommandExec(null, "xyz"), new CommandExec(null, "abc"));
00081 }
00082
00087 @Test
00088 public void testExpandPredefined() {
00089 final CrossfireServerConnection crossfireServerConnection = new TestCrossfireServerConnection();
00090 final Commands commands = new Commands();
00091 final TestCommand cmd = new TestCommand("cmd", false, crossfireServerConnection);
00092 commands.addCommand(cmd);
00093 check("cmd", commands, new CommandExec(cmd, ""));
00094 check("cmd abc", commands, new CommandExec(cmd, "abc"));
00095 check("cm", commands, new CommandExec(null, "cm"));
00096 check("cmd2", commands, new CommandExec(null, "cmd2"));
00097 check("cmd;cmd;cm;cmd;cmd2", commands, new CommandExec(cmd, ""), new CommandExec(cmd, ""), new CommandExec(null, "cm"), new CommandExec(cmd, ""), new CommandExec(null, "cmd2"));
00098 }
00099
00104 @Test
00105 public void testExpandArguments() {
00106 final CrossfireServerConnection crossfireServerConnection = new TestCrossfireServerConnection();
00107 final Commands commands = new Commands();
00108 final TestCommand cmd = new TestCommand("cmd", false, crossfireServerConnection);
00109 commands.addCommand(cmd);
00110 check("cmd", commands, new CommandExec(cmd, ""));
00111 check("cmd abc", commands, new CommandExec(cmd, "abc"));
00112 check("cmd abc", commands, new CommandExec(cmd, "abc"));
00113 check("cm", commands, new CommandExec(null, "cm"));
00114 check("cm abc", commands, new CommandExec(null, "cm abc"));
00115 check("cm abc", commands, new CommandExec(null, "cm abc"));
00116 }
00117
00123 @Test
00124 public void testExpandCaseInsensitiveCommandNames() {
00125 final CrossfireServerConnection crossfireServerConnection = new TestCrossfireServerConnection();
00126 final Commands commands = new Commands();
00127 final TestCommand cmd = new TestCommand("cmd", false, crossfireServerConnection);
00128 commands.addCommand(cmd);
00129 check("cmd", commands, new CommandExec(cmd, ""));
00130 check("Cmd", commands, new CommandExec(cmd, ""));
00131 check("cmD", commands, new CommandExec(cmd, ""));
00132 check("CMD", commands, new CommandExec(cmd, ""));
00133 }
00134
00139 @Test
00140 public void testExpandAllArguments() {
00141 final CrossfireServerConnection crossfireServerConnection = new TestCrossfireServerConnection();
00142 final Commands commands = new Commands();
00143 final TestCommand cmd = new TestCommand("cmd", false, crossfireServerConnection);
00144 final TestCommand bind = new TestCommand("bind", true, crossfireServerConnection);
00145 commands.addCommand(cmd);
00146 commands.addCommand(bind);
00147 check("cmd bind", commands, new CommandExec(cmd, "bind"));
00148 check("bind cmd", commands, new CommandExec(bind, "cmd"));
00149 check("cmd bind;bind cmd", commands, new CommandExec(cmd, "bind"), new CommandExec(bind, "cmd"));
00150 check("bind cmd;cmd bind", commands, new CommandExec(bind, "cmd;cmd bind"));
00151 check("cmd bind;bind cmd;cmd bind", commands, new CommandExec(cmd, "bind"), new CommandExec(bind, "cmd;cmd bind"));
00152 check("bind cmd;cmd bind;bind cmd", commands, new CommandExec(bind, "cmd;cmd bind;bind cmd"));
00153 check("cmd bind ; bind cmd ; cmd bind", commands, new CommandExec(cmd, "bind"), new CommandExec(bind, "cmd ; cmd bind"));
00154 check("bind cmd ; cmd bind ; bind cmd", commands, new CommandExec(bind, "cmd ; cmd bind ; bind cmd"));
00155 }
00156
00164 private static void check(@NotNull final CharSequence command, @NotNull final Commands commands, @NotNull final CommandExec... commandList) {
00165 final Collection<CommandExec> expandedCommands = CommandExpander.expand(command, commands);
00166 Assert.assertEquals(Arrays.asList(commandList), expandedCommands);
00167 }
00168
00169 }