You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ChannelInfoTest.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.parser.irc;
  23. import com.dmdirc.harness.parser.TestParser;
  24. import com.dmdirc.parser.interfaces.ChannelInfo;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. import org.junit.Ignore;
  28. import org.junit.Test;
  29. import static org.junit.Assert.*;
  30. public class ChannelInfoTest {
  31. final IRCChannelInfo ci = new IRCChannelInfo(null, "name");
  32. @Test
  33. public void testGetName() {
  34. assertEquals("name", ci.getName());
  35. }
  36. @Test
  37. public void testAddingNames() {
  38. assertTrue(ci.isAddingNames());
  39. ci.setAddingNames(false);
  40. assertFalse(ci.isAddingNames());
  41. }
  42. @Test
  43. public void testMap() {
  44. final Map map = new HashMap();
  45. ci.setMap(map);
  46. assertEquals(map, ci.getMap());
  47. }
  48. @Test
  49. public void testCreateTime() {
  50. ci.setCreateTime(12345l);
  51. assertEquals(12345l, ci.getCreateTime());
  52. }
  53. @Test
  54. public void testTopicTime() {
  55. ci.setTopicTime(12345l);
  56. assertEquals(12345l, ci.getTopicTime());
  57. }
  58. @Test
  59. public void testTopic() {
  60. ci.setInternalTopic("abcdef");
  61. assertEquals("abcdef", ci.getTopic());
  62. }
  63. @Test
  64. public void testSendMessage() {
  65. final TestParser parser = new TestParser();
  66. getChannel(parser).sendMessage("hello");
  67. assertEquals("PRIVMSG #DMDirc_testing :hello", parser.sentLines.get(0));
  68. }
  69. @Test
  70. public void testSendNotice() {
  71. final TestParser parser = new TestParser();
  72. getChannel(parser).sendNotice("hello");
  73. assertEquals("NOTICE #DMDirc_testing :hello", parser.sentLines.get(0));
  74. }
  75. @Test
  76. public void testSendCTCP() {
  77. final TestParser parser = new TestParser();
  78. getChannel(parser).sendCTCP("type", "hello");
  79. assertEquals("PRIVMSG #DMDirc_testing :" + ((char) 1) + "TYPE hello" + ((char) 1),
  80. parser.sentLines.get(0));
  81. }
  82. @Test
  83. public void testSendCTCPEmpty() {
  84. final TestParser parser = new TestParser();
  85. getChannel(parser).sendCTCP("type", "");
  86. assertEquals("PRIVMSG #DMDirc_testing :" + ((char) 1) + "TYPE" + ((char) 1),
  87. parser.sentLines.get(0));
  88. }
  89. @Test
  90. public void testSendAction() {
  91. final TestParser parser = new TestParser();
  92. getChannel(parser).sendAction("moo");
  93. assertEquals("PRIVMSG #DMDirc_testing :" + ((char) 1) + "ACTION moo" + ((char) 1),
  94. parser.sentLines.get(0));
  95. }
  96. @Test
  97. public void testSendCTCPReply() {
  98. final TestParser parser = new TestParser();
  99. getChannel(parser).sendCTCPReply("type", "moo");
  100. assertEquals("NOTICE #DMDirc_testing :" + ((char) 1) + "TYPE moo" + ((char) 1),
  101. parser.sentLines.get(0));
  102. }
  103. @Test
  104. public void testSendCTCPReplyEmpty() {
  105. final TestParser parser = new TestParser();
  106. getChannel(parser).sendCTCPReply("type", "");
  107. assertEquals("NOTICE #DMDirc_testing :" + ((char) 1) + "TYPE" + ((char) 1),
  108. parser.sentLines.get(0));
  109. }
  110. @Test
  111. public void testSendEmptyMessages() {
  112. final TestParser parser = new TestParser();
  113. final IRCChannelInfo info = getChannel(parser);
  114. info.sendAction("");
  115. info.sendCTCP("", "");
  116. info.sendCTCPReply("", "");
  117. info.sendMessage("");
  118. info.sendNotice("");
  119. assertEquals(0, parser.sentLines.size());
  120. }
  121. @Test
  122. public void testGetSetParamMode() {
  123. final TestParser parser = new TestParser();
  124. final ChannelInfo info = getChannel(parser);
  125. parser.injectLine(":server 324 nick #DMDirc_testing +k lalala");
  126. parser.sentLines.clear();
  127. assertEquals("lalala", info.getMode('k'));
  128. assertEquals("", info.getMode('z'));
  129. parser.injectLine(":server MODE #DMDirc_testing -k *");
  130. assertEquals("", info.getMode('k'));
  131. }
  132. @Test
  133. public void testModeSendFull() {
  134. final TestParser parser = new TestParser();
  135. final ChannelInfo info = getChannel(parser);
  136. parser.sentLines.clear();
  137. info.alterMode(true, 'i', null);
  138. info.alterMode(true, 'm', null);
  139. info.alterMode(true, 'n', null);
  140. info.alterMode(true, 'p', null);
  141. info.alterMode(true, 't', null);
  142. info.alterMode(true, 'r', null);
  143. assertEquals("Parser must send modes as soon as the max number is reached",
  144. 1, parser.sentLines.size());
  145. final String modes = getModes(parser.sentLines.get(0));
  146. assertTrue(modes.indexOf('i') > -1);
  147. assertTrue(modes.indexOf('m') > -1);
  148. assertTrue(modes.indexOf('n') > -1);
  149. assertTrue(modes.indexOf('p') > -1);
  150. assertTrue(modes.indexOf('t') > -1);
  151. assertTrue(modes.indexOf('r') > -1);
  152. }
  153. @Test
  154. public void testModeSendExtra() {
  155. final TestParser parser = new TestParser();
  156. final ChannelInfo info = getChannel(parser);
  157. parser.sentLines.clear();
  158. info.alterMode(true, 'i', null);
  159. info.alterMode(true, 'm', null);
  160. info.alterMode(true, 'n', null);
  161. info.alterMode(true, 'p', null);
  162. info.alterMode(true, 't', null);
  163. info.alterMode(true, 'r', null);
  164. info.alterMode(true, 'N', null);
  165. info.flushModes();
  166. assertEquals("sendModes must send modes",
  167. 2, parser.sentLines.size());
  168. final String modes = getModes(parser.sentLines.get(0))
  169. + getModes(parser.sentLines.get(1));
  170. assertTrue(modes.indexOf('i') > -1);
  171. assertTrue(modes.indexOf('m') > -1);
  172. assertTrue(modes.indexOf('n') > -1);
  173. assertTrue(modes.indexOf('p') > -1);
  174. assertTrue(modes.indexOf('t') > -1);
  175. assertTrue(modes.indexOf('r') > -1);
  176. assertTrue(modes.indexOf('N') > -1);
  177. }
  178. @Test
  179. public void testModeSendOptimisation1() {
  180. final TestParser parser = new TestParser();
  181. final ChannelInfo info = getChannel(parser);
  182. parser.sentLines.clear();
  183. info.alterMode(true, 'i', null);
  184. info.alterMode(true, 'm', null);
  185. info.alterMode(true, 'n', null);
  186. info.alterMode(true, 'n', null);
  187. info.alterMode(false, 'i', null);
  188. info.flushModes();
  189. assertEquals("sendModes must send modes in one go",
  190. 1, parser.sentLines.size());
  191. final String modes = getModes(parser.sentLines.get(0));
  192. assertEquals("Setting a negative mode should cancel a positive one",
  193. -1, modes.indexOf('i'));
  194. assertTrue(modes.indexOf('m') > -1);
  195. }
  196. @Test
  197. public void testModeSendOptimisation2() {
  198. final TestParser parser = new TestParser();
  199. final ChannelInfo info = getChannel(parser);
  200. parser.sentLines.clear();
  201. info.alterMode(true, 'm', null);
  202. info.alterMode(true, 'n', null);
  203. info.alterMode(true, 'n', null);
  204. info.flushModes();
  205. assertEquals("sendModes must send modes in one go",
  206. 1, parser.sentLines.size());
  207. final String modes = getModes(parser.sentLines.get(0));
  208. assertEquals("Setting a mode twice should have no effect",
  209. modes.indexOf('n'), modes.lastIndexOf('n'));
  210. assertTrue(modes.indexOf('m') > -1);
  211. }
  212. @Test
  213. public void testModeUnsetKey() {
  214. final TestParser parser = new TestParser();
  215. final ChannelInfo info = getChannel(parser);
  216. parser.injectLine(":server 324 nick #DMDirc_testing +k lalala");
  217. parser.sentLines.clear();
  218. info.alterMode(true, 'k', "foobar");
  219. info.flushModes();
  220. assertEquals("sendModes must send modes in one go",
  221. 1, parser.sentLines.size());
  222. assertEquals("Setting +k must set -k first",
  223. "-k+k lalala foobar", getModes(parser.sentLines.get(0)));
  224. }
  225. @Test
  226. public void testIssue1410() {
  227. final TestParser parser = new TestParser();
  228. parser.injectConnectionStrings();
  229. parser.injectLine(":nick JOIN #DMDirc_testing");
  230. parser.injectLine(":foo!bar@baz JOIN #DMDirc_testing");
  231. parser.injectLine(":flub!floo@fleeee JOIN #DMDirc_testing");
  232. assertNotSame(parser.getChannel("#DMDirc_testing").getChannelClients(),
  233. parser.getChannel("#DMDirc_testing").getChannelClients());
  234. assertEquals(parser.getChannel("#DMDirc_testing").getChannelClients(),
  235. parser.getChannel("#DMDirc_testing").getChannelClients());
  236. }
  237. @Test @Ignore
  238. public void testModeUnsetKeyMultiple() {
  239. final TestParser parser = new TestParser();
  240. final ChannelInfo info = getChannel(parser);
  241. parser.injectLine(":server 324 nick #DMDirc_testing +k lalala");
  242. parser.sentLines.clear();
  243. info.alterMode(true, 'k', "foobar");
  244. info.alterMode(true, 'k', "blahblah");
  245. info.alterMode(true, 'k', "unittest");
  246. info.flushModes();
  247. assertEquals("sendModes must send modes in one go",
  248. 1, parser.sentLines.size());
  249. assertEquals("Setting a mode multiple times should have no effect",
  250. "-k+k lalala unittest", getModes(parser.sentLines.get(0)));
  251. }
  252. @Test @Ignore
  253. public void testModeUnsetLimitMultiple() {
  254. final TestParser parser = new TestParser();
  255. final ChannelInfo info = getChannel(parser);
  256. parser.injectLine(":server 324 nick #DMDirc_testing +l 73");
  257. parser.sentLines.clear();
  258. info.alterMode(true, 'l', "74");
  259. info.alterMode(true, 'l', "75");
  260. info.alterMode(true, 'l', "76");
  261. info.flushModes();
  262. assertEquals("sendModes must send modes in one go",
  263. 1, parser.sentLines.size());
  264. assertEquals("Setting a mode multiple times should have no effect",
  265. "+l 76", getModes(parser.sentLines.get(0)));
  266. }
  267. private String getModes(final String line) {
  268. final String res = line.substring("MODE #DMDirc_testing ".length());
  269. if (res.charAt(0) == '+') {
  270. return res.substring(1);
  271. }
  272. return res;
  273. }
  274. private IRCChannelInfo getChannel(final TestParser parser) {
  275. parser.injectConnectionStrings();
  276. parser.injectLine(":nick JOIN #DMDirc_testing");
  277. parser.sentLines.clear();
  278. return parser.getChannels().iterator().next();
  279. }
  280. }