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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 java.util.HashMap;
  25. import java.util.Map;
  26. import org.junit.Ignore;
  27. import org.junit.Test;
  28. import static org.junit.Assert.*;
  29. public class ChannelInfoTest {
  30. final ChannelInfo ci = new ChannelInfo(null, "name");
  31. @Test
  32. public void testGetName() {
  33. assertEquals("name", ci.getName());
  34. }
  35. @Test
  36. public void testAddingNames() {
  37. assertTrue(ci.isAddingNames());
  38. ci.setAddingNames(false);
  39. assertFalse(ci.isAddingNames());
  40. }
  41. @Test
  42. public void testMap() {
  43. final Map map = new HashMap();
  44. ci.setMap(map);
  45. assertEquals(map, ci.getMap());
  46. }
  47. @Test
  48. public void testCreateTime() {
  49. ci.setCreateTime(12345l);
  50. assertEquals(12345l, ci.getCreateTime());
  51. }
  52. @Test
  53. public void testTopicTime() {
  54. ci.setTopicTime(12345l);
  55. assertEquals(12345l, ci.getTopicTime());
  56. }
  57. @Test
  58. public void testTopic() {
  59. ci.setTopic("abcdef");
  60. assertEquals("abcdef", ci.getTopic());
  61. }
  62. @Test
  63. public void testSendMessage() {
  64. final TestParser parser = new TestParser();
  65. getChannelInfo(parser).sendMessage("hello");
  66. assertEquals("PRIVMSG #DMDirc_testing :hello", parser.sentLines.get(0));
  67. }
  68. @Test
  69. public void testSendNotice() {
  70. final TestParser parser = new TestParser();
  71. getChannelInfo(parser).sendNotice("hello");
  72. assertEquals("NOTICE #DMDirc_testing :hello", parser.sentLines.get(0));
  73. }
  74. @Test
  75. public void testSendCTCP() {
  76. final TestParser parser = new TestParser();
  77. getChannelInfo(parser).sendCTCP("type", "hello");
  78. assertEquals("PRIVMSG #DMDirc_testing :" + ((char) 1) + "TYPE hello" + ((char) 1),
  79. parser.sentLines.get(0));
  80. }
  81. @Test
  82. public void testSendCTCPEmpty() {
  83. final TestParser parser = new TestParser();
  84. getChannelInfo(parser).sendCTCP("type", "");
  85. assertEquals("PRIVMSG #DMDirc_testing :" + ((char) 1) + "TYPE" + ((char) 1),
  86. parser.sentLines.get(0));
  87. }
  88. @Test
  89. public void testSendAction() {
  90. final TestParser parser = new TestParser();
  91. getChannelInfo(parser).sendAction("moo");
  92. assertEquals("PRIVMSG #DMDirc_testing :" + ((char) 1) + "ACTION moo" + ((char) 1),
  93. parser.sentLines.get(0));
  94. }
  95. @Test
  96. public void testSendCTCPReply() {
  97. final TestParser parser = new TestParser();
  98. getChannelInfo(parser).sendCTCPReply("type", "moo");
  99. assertEquals("NOTICE #DMDirc_testing :" + ((char) 1) + "TYPE moo" + ((char) 1),
  100. parser.sentLines.get(0));
  101. }
  102. @Test
  103. public void testSendCTCPReplyEmpty() {
  104. final TestParser parser = new TestParser();
  105. getChannelInfo(parser).sendCTCPReply("type", "");
  106. assertEquals("NOTICE #DMDirc_testing :" + ((char) 1) + "TYPE" + ((char) 1),
  107. parser.sentLines.get(0));
  108. }
  109. @Test
  110. public void testSendEmptyMessages() {
  111. final TestParser parser = new TestParser();
  112. final ChannelInfo info = getChannelInfo(parser);
  113. info.sendAction("");
  114. info.sendCTCP("", "");
  115. info.sendCTCPReply("", "");
  116. info.sendMessage("");
  117. info.sendNotice("");
  118. assertEquals(0, parser.sentLines.size());
  119. }
  120. @Test
  121. public void testGetSetParamMode() {
  122. final TestParser parser = new TestParser();
  123. final ChannelInfo info = getChannelInfo(parser);
  124. parser.injectLine(":server 324 nick #DMDirc_testing +k lalala");
  125. parser.sentLines.clear();
  126. assertEquals("lalala", info.getModeParam('k'));
  127. assertEquals("", info.getModeParam('z'));
  128. parser.injectLine(":server MODE #DMDirc_testing -k *");
  129. assertEquals("", info.getModeParam('k'));
  130. }
  131. @Test
  132. public void testModeSendFull() {
  133. final TestParser parser = new TestParser();
  134. final ChannelInfo info = getChannelInfo(parser);
  135. parser.sentLines.clear();
  136. info.alterMode(true, 'i', null);
  137. info.alterMode(true, 'm', null);
  138. info.alterMode(true, 'n', null);
  139. info.alterMode(true, 'p', null);
  140. info.alterMode(true, 't', null);
  141. info.alterMode(true, 'r', null);
  142. assertEquals("Parser must send modes as soon as the max number is reached",
  143. 1, parser.sentLines.size());
  144. final String modes = getModes(parser.sentLines.get(0));
  145. assertTrue(modes.indexOf('i') > -1);
  146. assertTrue(modes.indexOf('m') > -1);
  147. assertTrue(modes.indexOf('n') > -1);
  148. assertTrue(modes.indexOf('p') > -1);
  149. assertTrue(modes.indexOf('t') > -1);
  150. assertTrue(modes.indexOf('r') > -1);
  151. }
  152. @Test
  153. public void testModeSendExtra() {
  154. final TestParser parser = new TestParser();
  155. final ChannelInfo info = getChannelInfo(parser);
  156. parser.sentLines.clear();
  157. info.alterMode(true, 'i', null);
  158. info.alterMode(true, 'm', null);
  159. info.alterMode(true, 'n', null);
  160. info.alterMode(true, 'p', null);
  161. info.alterMode(true, 't', null);
  162. info.alterMode(true, 'r', null);
  163. info.alterMode(true, 'N', null);
  164. info.sendModes();
  165. assertEquals("sendModes must send modes",
  166. 2, parser.sentLines.size());
  167. final String modes = getModes(parser.sentLines.get(0))
  168. + getModes(parser.sentLines.get(1));
  169. assertTrue(modes.indexOf('i') > -1);
  170. assertTrue(modes.indexOf('m') > -1);
  171. assertTrue(modes.indexOf('n') > -1);
  172. assertTrue(modes.indexOf('p') > -1);
  173. assertTrue(modes.indexOf('t') > -1);
  174. assertTrue(modes.indexOf('r') > -1);
  175. assertTrue(modes.indexOf('N') > -1);
  176. }
  177. @Test
  178. public void testModeSendOptimisation1() {
  179. final TestParser parser = new TestParser();
  180. final ChannelInfo info = getChannelInfo(parser);
  181. parser.sentLines.clear();
  182. info.alterMode(true, 'i', null);
  183. info.alterMode(true, 'm', null);
  184. info.alterMode(true, 'n', null);
  185. info.alterMode(true, 'n', null);
  186. info.alterMode(false, 'i', null);
  187. info.sendModes();
  188. assertEquals("sendModes must send modes in one go",
  189. 1, parser.sentLines.size());
  190. final String modes = getModes(parser.sentLines.get(0));
  191. assertEquals("Setting a negative mode should cancel a positive one",
  192. -1, modes.indexOf('i'));
  193. assertTrue(modes.indexOf('m') > -1);
  194. }
  195. @Test
  196. public void testModeSendOptimisation2() {
  197. final TestParser parser = new TestParser();
  198. final ChannelInfo info = getChannelInfo(parser);
  199. parser.sentLines.clear();
  200. info.alterMode(true, 'm', null);
  201. info.alterMode(true, 'n', null);
  202. info.alterMode(true, 'n', null);
  203. info.sendModes();
  204. assertEquals("sendModes must send modes in one go",
  205. 1, parser.sentLines.size());
  206. final String modes = getModes(parser.sentLines.get(0));
  207. assertEquals("Setting a mode twice should have no effect",
  208. modes.indexOf('n'), modes.lastIndexOf('n'));
  209. assertTrue(modes.indexOf('m') > -1);
  210. }
  211. @Test
  212. public void testModeUnsetKey() {
  213. final TestParser parser = new TestParser();
  214. final ChannelInfo info = getChannelInfo(parser);
  215. parser.injectLine(":server 324 nick #DMDirc_testing +k lalala");
  216. parser.sentLines.clear();
  217. info.alterMode(true, 'k', "foobar");
  218. info.sendModes();
  219. assertEquals("sendModes must send modes in one go",
  220. 1, parser.sentLines.size());
  221. assertEquals("Setting +k must set -k first",
  222. "-k+k lalala foobar", getModes(parser.sentLines.get(0)));
  223. }
  224. @Test
  225. public void testIssue1410() {
  226. final TestParser parser = new TestParser();
  227. parser.injectConnectionStrings();
  228. parser.injectLine(":nick JOIN #DMDirc_testing");
  229. parser.injectLine(":foo!bar@baz JOIN #DMDirc_testing");
  230. parser.injectLine(":flub!floo@fleeee JOIN #DMDirc_testing");
  231. assertNotSame(parser.getChannelInfo("#DMDirc_testing").getChannelClients(),
  232. parser.getChannelInfo("#DMDirc_testing").getChannelClients());
  233. assertEquals(parser.getChannelInfo("#DMDirc_testing").getChannelClients(),
  234. parser.getChannelInfo("#DMDirc_testing").getChannelClients());
  235. }
  236. @Test @Ignore
  237. public void testModeUnsetKeyMultiple() {
  238. final TestParser parser = new TestParser();
  239. final ChannelInfo info = getChannelInfo(parser);
  240. parser.injectLine(":server 324 nick #DMDirc_testing +k lalala");
  241. parser.sentLines.clear();
  242. info.alterMode(true, 'k', "foobar");
  243. info.alterMode(true, 'k', "blahblah");
  244. info.alterMode(true, 'k', "unittest");
  245. info.sendModes();
  246. assertEquals("sendModes must send modes in one go",
  247. 1, parser.sentLines.size());
  248. assertEquals("Setting a mode multiple times should have no effect",
  249. "-k+k lalala unittest", getModes(parser.sentLines.get(0)));
  250. }
  251. @Test @Ignore
  252. public void testModeUnsetLimitMultiple() {
  253. final TestParser parser = new TestParser();
  254. final ChannelInfo info = getChannelInfo(parser);
  255. parser.injectLine(":server 324 nick #DMDirc_testing +l 73");
  256. parser.sentLines.clear();
  257. info.alterMode(true, 'l', "74");
  258. info.alterMode(true, 'l', "75");
  259. info.alterMode(true, 'l', "76");
  260. info.sendModes();
  261. assertEquals("sendModes must send modes in one go",
  262. 1, parser.sentLines.size());
  263. assertEquals("Setting a mode multiple times should have no effect",
  264. "+l 76", getModes(parser.sentLines.get(0)));
  265. }
  266. private String getModes(final String line) {
  267. final String res = line.substring("MODE #DMDirc_testing ".length());
  268. if (res.charAt(0) == '+') {
  269. return res.substring(1);
  270. }
  271. return res;
  272. }
  273. private ChannelInfo getChannelInfo(final TestParser parser) {
  274. parser.injectConnectionStrings();
  275. parser.injectLine(":nick JOIN #DMDirc_testing");
  276. parser.sentLines.clear();
  277. return parser.getChannels().iterator().next();
  278. }
  279. }