Fine-tuned /me command and added unit tests.

cefexperiments
Danny van Heumen 12 years ago
parent f097629933
commit 35a0fe89b4

@ -17,6 +17,11 @@
public class Me
implements Command
{
/**
* Me command prefix index.
*/
private static final int END_OF_ME_COMMAND_PREFIX = 4;
/**
* IRC connection instance.
*/
@ -42,12 +47,22 @@ public void init(final ProtocolProviderServiceIrcImpl provider,
/**
* Execute the /me command: send ACT command.
*
* @param source source chat room or user from which the command originated.
* @param line the command message line
*/
@Override
public void execute(final String source, final String line)
{
final String command = line.substring(4);
this.connection.getClient().act(source, command);
if (line.length() < END_OF_ME_COMMAND_PREFIX)
{
return;
}
final String message = line.substring(4);
if (message.isEmpty())
{
throw new IllegalArgumentException(
"Invalid /me command: message cannot be empty.");
}
this.connection.getClient().act(source, message);
}
}

@ -0,0 +1,86 @@
package net.java.sip.communicator.impl.protocol.irc.command;
import junit.framework.*;
import net.java.sip.communicator.impl.protocol.irc.*;
import org.easymock.*;
import com.ircclouds.irc.api.*;
public class MeTest extends TestCase
{
public void testConstruction()
{
new Me();
}
public void testGoodInit()
{
IrcConnection connection = EasyMock.createMock(IrcConnection.class);
EasyMock.replay(connection);
Me me = new Me();
me.init(null, connection);
}
public void testBadInit()
{
ProtocolProviderServiceIrcImpl provider = EasyMock.createMock(ProtocolProviderServiceIrcImpl.class);
EasyMock.replay(provider);
Me me = new Me();
try
{
me.init(provider, null);
Assert.fail();
}
catch (IllegalArgumentException e)
{
}
}
public void testNoMessage()
{
ProtocolProviderServiceIrcImpl provider = EasyMock.createMock(ProtocolProviderServiceIrcImpl.class);
IrcConnection connection = EasyMock.createMock(IrcConnection.class);
EasyMock.replay(provider, connection);
Me me = new Me();
me.init(provider, connection);
me.execute("#test", "/me");
}
public void testZeroLengthMessage()
{
ProtocolProviderServiceIrcImpl provider = EasyMock.createMock(ProtocolProviderServiceIrcImpl.class);
IrcConnection connection = EasyMock.createMock(IrcConnection.class);
EasyMock.replay(provider, connection);
Me me = new Me();
me.init(provider, connection);
try
{
me.execute("#test", "/me ");
Assert.fail();
}
catch (IllegalArgumentException e)
{
}
}
public void testSendMessage()
{
ProtocolProviderServiceIrcImpl provider = EasyMock.createMock(ProtocolProviderServiceIrcImpl.class);
IrcConnection connection = EasyMock.createMock(IrcConnection.class);
IRCApi client = EasyMock.createMock(IRCApi.class);
EasyMock.expect(connection.getClient()).andReturn(client);
client.act(EasyMock.eq("#test"), EasyMock.eq("says hello world!"));
EasyMock.expectLastCall();
EasyMock.replay(provider, connection, client);
Me me = new Me();
me.init(provider, connection);
me.execute("#test", "/me says hello world!");
}
}
Loading…
Cancel
Save