Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

LoggingPluginTest.java 5.1KB

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