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.

LoggingPluginTest.java 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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.addons.logging;
  23. import com.dmdirc.Channel;
  24. import com.dmdirc.Main;
  25. import com.dmdirc.Server;
  26. import com.dmdirc.Query;
  27. import com.dmdirc.actions.CoreActionType;
  28. import com.dmdirc.config.IdentityManager;
  29. import com.dmdirc.harness.TestLoggingPlugin;
  30. import com.dmdirc.parser.irc.IRCChannelInfo;
  31. import com.dmdirc.addons.ui_dummy.DummyController;
  32. import com.dmdirc.parser.irc.IRCParser;
  33. import com.dmdirc.util.ConfigFile;
  34. import java.util.Map;
  35. import org.junit.BeforeClass;
  36. import org.junit.Test;
  37. import static org.junit.Assert.*;
  38. public class LoggingPluginTest {
  39. private static Server server;
  40. private static Channel channel;
  41. private static Query query;
  42. private static TestLoggingPlugin lp;
  43. @BeforeClass
  44. public static void setUp() throws Exception {
  45. Main.setUI(new DummyController());
  46. IdentityManager.load();
  47. server = new Server("irc-test://255.255.255.255",
  48. IdentityManager.getProfiles().get(0));
  49. server.connect();
  50. channel = new Channel(server, new IRCChannelInfo((IRCParser) server.getParser(), "#test"));
  51. query = new Query(server, "foo!bar@baz");
  52. final ConfigFile file = new ConfigFile(LoggingPlugin.class
  53. .getResourceAsStream("plugin.config"));
  54. file.read();
  55. for (Map.Entry<String, String> entry : file.getKeyDomain("defaults").entrySet()) {
  56. IdentityManager.getAddonIdentity().setOption("temp-plugin-logging",
  57. entry.getKey(), entry.getValue());
  58. }
  59. lp = new TestLoggingPlugin();
  60. lp.setDomain("temp-plugin-logging");
  61. lp.domainUpdated();
  62. lp.onLoad();
  63. }
  64. @Test
  65. public void testChannelOpened() {
  66. lp.processEvent(CoreActionType.CHANNEL_OPENED, new StringBuffer(),
  67. channel);
  68. assertTrue(lp.lines.containsKey("#test"));
  69. assertEquals(2, lp.lines.get("#test").size());
  70. assertTrue(lp.lines.get("#test").get(1).isEmpty());
  71. assertTrue(lp.lines.get("#test").get(0).indexOf("opened") > -1);
  72. lp.lines.clear();
  73. }
  74. @Test
  75. public void testChannelClosed() {
  76. lp.processEvent(CoreActionType.CHANNEL_CLOSED, new StringBuffer(),
  77. channel);
  78. assertTrue(lp.lines.containsKey("#test"));
  79. assertEquals(1, lp.lines.get("#test").size());
  80. assertTrue(lp.lines.get("#test").get(0).indexOf("closed") > -1);
  81. lp.lines.clear();
  82. }
  83. @Test
  84. public void testQueryOpened() {
  85. lp.processEvent(CoreActionType.QUERY_OPENED, new StringBuffer(),
  86. query);
  87. assertTrue(lp.lines.containsKey("foo!bar@baz"));
  88. assertEquals(3, lp.lines.get("foo!bar@baz").size());
  89. assertTrue(lp.lines.get("foo!bar@baz").get(2).isEmpty());
  90. assertTrue(lp.lines.get("foo!bar@baz").get(1).indexOf("foo!bar@baz") > -1);
  91. assertTrue(lp.lines.get("foo!bar@baz").get(0).indexOf("opened") > -1);
  92. lp.lines.clear();
  93. }
  94. @Test
  95. public void testQueryClosed() {
  96. lp.processEvent(CoreActionType.QUERY_CLOSED, new StringBuffer(),
  97. query);
  98. assertTrue(lp.lines.containsKey("foo!bar@baz"));
  99. assertEquals(1, lp.lines.get("foo!bar@baz").size());
  100. assertTrue(lp.lines.get("foo!bar@baz").get(0).indexOf("closed") > -1);
  101. lp.lines.clear();
  102. }
  103. }