aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDanny van Heumen <danny@dannyvanheumen.nl>2014-11-09 20:53:39 +0100
committerDanny van Heumen <danny@dannyvanheumen.nl>2014-11-09 20:57:43 +0100
commitda53b1f81012fa99d5090c2c587fef370d3c0de7 (patch)
tree17998d9bebb724e7c06e24b0298e4096770c78db
parent35a0fe89b41f76bf09d5333e0decb11d36e63cd7 (diff)
downloadjitsi-da53b1f81012fa99d5090c2c587fef370d3c0de7.zip
jitsi-da53b1f81012fa99d5090c2c587fef370d3c0de7.tar.gz
jitsi-da53b1f81012fa99d5090c2c587fef370d3c0de7.tar.bz2
Fine-tuned commands Mode, Join + added unit tests.
-rw-r--r--src/net/java/sip/communicator/impl/protocol/irc/command/Join.java17
-rw-r--r--src/net/java/sip/communicator/impl/protocol/irc/command/Mode.java11
-rw-r--r--test/net/java/sip/communicator/impl/protocol/irc/command/JoinTest.java98
-rw-r--r--test/net/java/sip/communicator/impl/protocol/irc/command/ModeTest.java94
4 files changed, 214 insertions, 6 deletions
diff --git a/src/net/java/sip/communicator/impl/protocol/irc/command/Join.java b/src/net/java/sip/communicator/impl/protocol/irc/command/Join.java
index 47b25c5..cf2aa77 100644
--- a/src/net/java/sip/communicator/impl/protocol/irc/command/Join.java
+++ b/src/net/java/sip/communicator/impl/protocol/irc/command/Join.java
@@ -17,6 +17,11 @@ import net.java.sip.communicator.impl.protocol.irc.*;
public class Join implements Command
{
/**
+ * Index of end of command prefix.
+ */
+ private static final int END_OF_COMMAND_PREFIX = 6;
+
+ /**
* Instance of the IRC connection.
*/
private IrcConnection connection;
@@ -47,7 +52,11 @@ public class Join implements Command
@Override
public void execute(final String source, final String line)
{
- final String part = line.substring(6);
+ if (line.length() < END_OF_COMMAND_PREFIX)
+ {
+ return;
+ }
+ final String part = line.substring(END_OF_COMMAND_PREFIX);
final String channel;
final String password;
int indexOfSep = part.indexOf(' ');
@@ -61,9 +70,11 @@ public class Join implements Command
channel = part.substring(0, indexOfSep);
password = part.substring(indexOfSep + 1);
}
- if (channel.matches("[^,\\n\\r\\s\\a]+"))
+ if (!channel.matches("[^,\\n\\r\\s\\a]+"))
{
- this.connection.getClient().joinChannel(channel, password);
+ throw new IllegalArgumentException(
+ "Invalid chat room name specified.");
}
+ this.connection.getClient().joinChannel(channel, password);
}
}
diff --git a/src/net/java/sip/communicator/impl/protocol/irc/command/Mode.java b/src/net/java/sip/communicator/impl/protocol/irc/command/Mode.java
index 0225f16..57ab19f 100644
--- a/src/net/java/sip/communicator/impl/protocol/irc/command/Mode.java
+++ b/src/net/java/sip/communicator/impl/protocol/irc/command/Mode.java
@@ -58,8 +58,13 @@ public class Mode implements Command
// results.
return;
}
- final String rawModeString = line.substring(END_OF_MODE_COMMAND_PREFIX);
- this.connection.getClient().rawMessage(
- "MODE " + source + " " + rawModeString);
+ final String rawModeString =
+ line.substring(END_OF_MODE_COMMAND_PREFIX);
+ if (rawModeString.trim().isEmpty())
+ {
+ throw new IllegalArgumentException(
+ "The mode command needs mode parameters to function.");
+ }
+ this.connection.getClient().changeMode(source + " " + rawModeString);
}
}
diff --git a/test/net/java/sip/communicator/impl/protocol/irc/command/JoinTest.java b/test/net/java/sip/communicator/impl/protocol/irc/command/JoinTest.java
new file mode 100644
index 0000000..3819f33
--- /dev/null
+++ b/test/net/java/sip/communicator/impl/protocol/irc/command/JoinTest.java
@@ -0,0 +1,98 @@
+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 JoinTest
+ extends TestCase
+{
+
+ public void testConstruction()
+ {
+ new Join();
+ }
+
+ public void testGoodInit()
+ {
+ IrcConnection connection = EasyMock.createMock(IrcConnection.class);
+ EasyMock.replay(connection);
+
+ Join join = new Join();
+ join.init(null, connection);
+ }
+
+ public void testBadInit()
+ {
+ ProtocolProviderServiceIrcImpl provider = EasyMock.createMock(ProtocolProviderServiceIrcImpl.class);
+ EasyMock.replay(provider);
+
+ Join join = new Join();
+ try
+ {
+ join.init(provider, null);
+ Assert.fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ }
+
+ public void testEmptyJoin()
+ {
+ IrcConnection connection = EasyMock.createMock(IrcConnection.class);
+ EasyMock.replay(connection);
+
+ Join join = new Join();
+ join.init(null, connection);
+ join.execute("#test", "/join");
+ }
+
+ public void testJoinEmptyChannelNoPassword()
+ {
+ IrcConnection connection = EasyMock.createMock(IrcConnection.class);
+ EasyMock.replay(connection);
+
+ Join join = new Join();
+ join.init(null, connection);
+ try
+ {
+ join.execute("#test", "/join ");
+ Assert.fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ }
+
+ public void testJoinWithChannelNoPassword()
+ {
+ IrcConnection connection = EasyMock.createMock(IrcConnection.class);
+ IRCApi client = EasyMock.createMock(IRCApi.class);
+ EasyMock.expect(connection.getClient()).andReturn(client);
+ client.joinChannel(EasyMock.eq("#test"), EasyMock.eq(""));
+ EasyMock.expectLastCall();
+ EasyMock.replay(connection, client);
+
+ Join join = new Join();
+ join.init(null, connection);
+ join.execute("#test", "/join #test");
+ }
+
+ public void testJoinWithChannelWithPassword()
+ {
+ IrcConnection connection = EasyMock.createMock(IrcConnection.class);
+ IRCApi client = EasyMock.createMock(IRCApi.class);
+ EasyMock.expect(connection.getClient()).andReturn(client);
+ client.joinChannel(EasyMock.eq("#test"), EasyMock.eq("top-secret"));
+ EasyMock.expectLastCall();
+ EasyMock.replay(connection, client);
+
+ Join join = new Join();
+ join.init(null, connection);
+ join.execute("#test", "/join #test top-secret");
+ }
+}
diff --git a/test/net/java/sip/communicator/impl/protocol/irc/command/ModeTest.java b/test/net/java/sip/communicator/impl/protocol/irc/command/ModeTest.java
new file mode 100644
index 0000000..9c582e3
--- /dev/null
+++ b/test/net/java/sip/communicator/impl/protocol/irc/command/ModeTest.java
@@ -0,0 +1,94 @@
+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 ModeTest
+ extends TestCase
+{
+
+ public void testConstruction()
+ {
+ new Mode();
+ }
+
+ public void testGoodInit()
+ {
+ IrcConnection connection = EasyMock.createMock(IrcConnection.class);
+ EasyMock.replay(connection);
+
+ Mode mode = new Mode();
+ mode.init(null, connection);
+ }
+
+ public void testBadInit()
+ {
+ ProtocolProviderServiceIrcImpl provider =
+ EasyMock.createMock(ProtocolProviderServiceIrcImpl.class);
+ EasyMock.replay(provider);
+
+ Mode mode = new Mode();
+ try
+ {
+ mode.init(provider, null);
+ Assert.fail();
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ }
+
+ public void testEmptyCommand()
+ {
+ IrcConnection connection = EasyMock.createMock(IrcConnection.class);
+ EasyMock.replay(connection);
+
+ Mode mode = new Mode();
+ mode.init(null, connection);
+ mode.execute("#test", "/mode");
+ }
+
+ public void testEmptyModeLine()
+ {
+ IrcConnection connection = EasyMock.createMock(IrcConnection.class);
+ EasyMock.replay(connection);
+
+ Mode mode = new Mode();
+ mode.init(null, connection);
+ mode.execute("#test", "/mode ");
+ }
+
+ public void testSpacesModeLine()
+ {
+ IrcConnection connection = EasyMock.createMock(IrcConnection.class);
+ EasyMock.replay(connection);
+
+ Mode mode = new Mode();
+ mode.init(null, connection);
+ try
+ {
+ mode.execute("#test", "/mode ");
+ }
+ catch (IllegalArgumentException e)
+ {
+ }
+ }
+
+ public void testCorrectModeCommand()
+ {
+ IrcConnection connection = EasyMock.createMock(IrcConnection.class);
+ IRCApi client = EasyMock.createMock(IRCApi.class);
+ EasyMock.expect(connection.getClient()).andReturn(client);
+ client.changeMode(EasyMock.eq("#test +o ThaDud3"));
+ EasyMock.expectLastCall();
+ EasyMock.replay(connection, client);
+
+ Mode mode = new Mode();
+ mode.init(null, connection);
+ mode.execute("#test", "/mode +o ThaDud3");
+ }
+}