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 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (c) 2006-2008 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;
  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 testModeSendFull() {
  122. final TestParser parser = new TestParser();
  123. final ChannelInfo info = getChannelInfo(parser);
  124. parser.sentLines.clear();
  125. info.alterMode(true, 'i', null);
  126. info.alterMode(true, 'm', null);
  127. info.alterMode(true, 'n', null);
  128. info.alterMode(true, 'p', null);
  129. info.alterMode(true, 't', null);
  130. info.alterMode(true, 'r', null);
  131. assertTrue("Parser must send modes as soon as the max number is reached",
  132. parser.sentLines.size() == 1);
  133. final String modes = getModes(parser.sentLines.get(0));
  134. assertTrue(modes.indexOf('i') > -1);
  135. assertTrue(modes.indexOf('m') > -1);
  136. assertTrue(modes.indexOf('n') > -1);
  137. assertTrue(modes.indexOf('p') > -1);
  138. assertTrue(modes.indexOf('t') > -1);
  139. assertTrue(modes.indexOf('r') > -1);
  140. }
  141. @Test
  142. public void testModeSendExtra() {
  143. final TestParser parser = new TestParser();
  144. final ChannelInfo info = getChannelInfo(parser);
  145. parser.sentLines.clear();
  146. info.alterMode(true, 'i', null);
  147. info.alterMode(true, 'm', null);
  148. info.alterMode(true, 'n', null);
  149. info.alterMode(true, 'p', null);
  150. info.alterMode(true, 't', null);
  151. info.alterMode(true, 'r', null);
  152. info.alterMode(true, 'N', null);
  153. info.sendModes();
  154. assertTrue("sendModes must send modes",
  155. parser.sentLines.size() == 2);
  156. final String modes = getModes(parser.sentLines.get(0))
  157. + getModes(parser.sentLines.get(1));
  158. assertTrue(modes.indexOf('i') > -1);
  159. assertTrue(modes.indexOf('m') > -1);
  160. assertTrue(modes.indexOf('n') > -1);
  161. assertTrue(modes.indexOf('p') > -1);
  162. assertTrue(modes.indexOf('t') > -1);
  163. assertTrue(modes.indexOf('r') > -1);
  164. assertTrue(modes.indexOf('N') > -1);
  165. }
  166. @Test
  167. public void testModeSendOptimisation1() {
  168. final TestParser parser = new TestParser();
  169. final ChannelInfo info = getChannelInfo(parser);
  170. parser.sentLines.clear();
  171. info.alterMode(true, 'i', null);
  172. info.alterMode(true, 'm', null);
  173. info.alterMode(true, 'n', null);
  174. info.alterMode(true, 'n', null);
  175. info.alterMode(false, 'i', null);
  176. info.sendModes();
  177. assertTrue("sendModes must send modes",
  178. parser.sentLines.size() == 1);
  179. final String modes = getModes(parser.sentLines.get(0));
  180. assertEquals("Setting a negative mode should cancel a positive one",
  181. -1, modes.indexOf('i'));
  182. assertTrue(modes.indexOf('m') > -1);
  183. }
  184. @Test
  185. public void testModeSendOptimisation2() {
  186. final TestParser parser = new TestParser();
  187. final ChannelInfo info = getChannelInfo(parser);
  188. parser.sentLines.clear();
  189. info.alterMode(true, 'm', null);
  190. info.alterMode(true, 'n', null);
  191. info.alterMode(true, 'n', null);
  192. info.sendModes();
  193. assertTrue("sendModes must send modes",
  194. parser.sentLines.size() == 1);
  195. final String modes = getModes(parser.sentLines.get(0));
  196. assertEquals("Setting a mode twice should have no effect",
  197. modes.indexOf('n'), modes.lastIndexOf('n'));
  198. assertTrue(modes.indexOf('m') > -1);
  199. }
  200. private String getModes(final String line) {
  201. final String res = line.substring("MODE #DMDirc_testing ".length());
  202. if (res.charAt(0) == '+') {
  203. return res.substring(1);
  204. }
  205. return res;
  206. }
  207. private ChannelInfo getChannelInfo(final TestParser parser) {
  208. parser.injectConnectionStrings();
  209. parser.injectLine(":nick JOIN #DMDirc_testing");
  210. parser.sentLines.clear();
  211. return parser.getChannels().iterator().next();
  212. }
  213. }