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.8KB

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