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.

AutoCommandManagerTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * Copyright (c) 2006-2014 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.commandparser.auto;
  23. import com.dmdirc.DMDircMBassador;
  24. import com.google.common.base.Optional;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import org.junit.runner.RunWith;
  28. import org.mockito.Mock;
  29. import org.mockito.runners.MockitoJUnitRunner;
  30. import static junit.framework.TestCase.assertFalse;
  31. import static org.junit.Assert.assertEquals;
  32. import static org.junit.Assert.assertTrue;
  33. import static org.mockito.Mockito.never;
  34. import static org.mockito.Mockito.verify;
  35. import static org.mockito.Mockito.when;
  36. @RunWith(MockitoJUnitRunner.class)
  37. public class AutoCommandManagerTest {
  38. @Mock private DMDircMBassador eventBus;
  39. @Mock private AutoCommandHandlerFactory factory;
  40. @Mock private AutoCommandHandler globalHandler;
  41. @Mock private AutoCommandHandler connectionHandler;
  42. private AutoCommandManager autoCommandManager;
  43. private AutoCommand global;
  44. private AutoCommand connection;
  45. @Before
  46. public void setup() {
  47. autoCommandManager = new AutoCommandManager(eventBus, factory);
  48. global = new AutoCommand(Optional.<String>absent(), Optional.<String>absent(),
  49. Optional.<String>absent(), "");
  50. connection = new AutoCommand(Optional.<String>absent(),
  51. Optional.fromNullable("Quakenet"), Optional.<String>absent(), "");
  52. when(factory.getAutoCommandHandler(global)).thenReturn(globalHandler);
  53. when(factory.getAutoCommandHandler(connection)).thenReturn(connectionHandler);
  54. }
  55. @Test
  56. public void testRemoveAutoCommand() {
  57. autoCommandManager.addAutoCommand(global);
  58. autoCommandManager.addAutoCommand(connection);
  59. assertEquals(2, autoCommandManager.getAutoCommands().size());
  60. assertTrue(autoCommandManager.getAutoCommands().contains(connection));
  61. autoCommandManager.removeAutoCommand(connection);
  62. assertEquals(1, autoCommandManager.getAutoCommands().size());
  63. assertFalse(autoCommandManager.getAutoCommands().contains(connection));
  64. }
  65. @Test
  66. public void testGetAutoCommands() {
  67. autoCommandManager.addAutoCommand(global);
  68. autoCommandManager.addAutoCommand(connection);
  69. assertEquals(2, autoCommandManager.getAutoCommands().size());
  70. }
  71. @Test
  72. public void testGetGlobalAutoCommands() {
  73. autoCommandManager.addAutoCommand(global);
  74. autoCommandManager.addAutoCommand(connection);
  75. assertEquals(1, autoCommandManager.getGlobalAutoCommands().size());
  76. assertTrue(autoCommandManager.getGlobalAutoCommands().contains(global));
  77. }
  78. @Test
  79. public void testGetConnectionAutoCommands() {
  80. autoCommandManager.addAutoCommand(global);
  81. autoCommandManager.addAutoCommand(connection);
  82. assertEquals(1, autoCommandManager.getConnectionAutoCommands().size());
  83. assertTrue(autoCommandManager.getConnectionAutoCommands().contains(connection));
  84. }
  85. @Test
  86. public void testStart() {
  87. autoCommandManager.addAutoCommand(global);
  88. autoCommandManager.start();
  89. verify(eventBus).subscribe(globalHandler);
  90. }
  91. @Test
  92. public void testStop() {
  93. autoCommandManager.addAutoCommand(global);
  94. autoCommandManager.stop();
  95. verify(eventBus).unsubscribe(globalHandler);
  96. }
  97. @Test
  98. public void testAddWhenStarted() {
  99. autoCommandManager.start();
  100. autoCommandManager.addAutoCommand(global);
  101. verify(eventBus).subscribe(globalHandler);
  102. }
  103. @Test
  104. public void testAddWhenStopped() {
  105. autoCommandManager.addAutoCommand(global);
  106. verify(eventBus, never()).subscribe(globalHandler);
  107. }
  108. @Test
  109. public void testRemoveWhenStarted() {
  110. autoCommandManager.start();
  111. autoCommandManager.addAutoCommand(global);
  112. autoCommandManager.removeAutoCommand(global);
  113. verify(eventBus).subscribe(globalHandler);
  114. verify(eventBus).unsubscribe(globalHandler);
  115. }
  116. @Test
  117. public void testRemoveWhenStopped() {
  118. autoCommandManager.removeAutoCommand(global);
  119. verify(eventBus, never()).subscribe(global);
  120. verify(eventBus, never()).unsubscribe(globalHandler);
  121. }
  122. }