Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

LoggingPluginTest.java 5.1KB

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