Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ChannelInfoTest.java 11KB

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