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.

CoreGlobalAutoCommandsDialogModelTest.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2006-2015 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.ui.core.autocommands;
  23. import com.dmdirc.commandparser.auto.AutoCommand;
  24. import com.dmdirc.commandparser.auto.AutoCommandManager;
  25. import com.dmdirc.interfaces.CommandController;
  26. import java.util.Optional;
  27. import org.junit.Before;
  28. import org.junit.Test;
  29. import org.junit.runner.RunWith;
  30. import org.mockito.Mock;
  31. import org.mockito.runners.MockitoJUnitRunner;
  32. import static org.junit.Assert.assertEquals;
  33. import static org.junit.Assert.assertFalse;
  34. import static org.junit.Assert.assertNotNull;
  35. import static org.junit.Assert.assertTrue;
  36. import static org.mockito.ArgumentMatchers.eq;
  37. import static org.mockito.Mockito.verify;
  38. import static org.mockito.Mockito.when;
  39. @RunWith(MockitoJUnitRunner.class)
  40. public class CoreGlobalAutoCommandsDialogModelTest {
  41. @Mock private AutoCommandManager autoCommandManager;
  42. @Mock private CommandController commandController;
  43. @Mock private AutoCommand autoCommand;
  44. private CoreGlobalAutoCommandsDialogModel instance;
  45. @Before
  46. public void setUp() throws Exception {
  47. when(autoCommand.getServer()).thenReturn(Optional.empty());
  48. when(autoCommand.getNetwork()).thenReturn(Optional.empty());
  49. when(autoCommand.getProfile()).thenReturn(Optional.empty());
  50. when(autoCommand.getResponse()).thenReturn("response");
  51. when(commandController.getCommandChar()).thenReturn('/');
  52. when(autoCommandManager.getGlobalAutoCommand()).thenReturn(Optional.of(autoCommand));
  53. instance = new CoreGlobalAutoCommandsDialogModel(autoCommandManager,
  54. new ResponseValidator(commandController));
  55. }
  56. @Test(expected = IllegalStateException.class)
  57. public void testLoad_NotCalled() throws Exception {
  58. instance = new CoreGlobalAutoCommandsDialogModel(autoCommandManager,
  59. new ResponseValidator(commandController));
  60. instance.getResponse();
  61. }
  62. @Test
  63. public void testGetResponse_Empty() throws Exception {
  64. when(autoCommandManager.getGlobalAutoCommand()).thenReturn(Optional.empty());
  65. instance.load();
  66. assertEquals("", instance.getResponse());
  67. }
  68. @Test
  69. public void testGetResponse_Complete() throws Exception {
  70. instance.load();
  71. assertEquals("response", instance.getResponse());
  72. }
  73. @Test(expected = NullPointerException.class)
  74. public void testSetResponse_Null() throws Exception {
  75. instance.load();
  76. assertEquals("response", instance.getResponse());
  77. instance.setResponse(null);
  78. }
  79. @Test
  80. public void testSetResponse_empty() throws Exception {
  81. instance.load();
  82. assertEquals("response", instance.getResponse());
  83. instance.setResponse("");
  84. assertEquals("", instance.getResponse());
  85. }
  86. @Test
  87. public void testSetResponse() throws Exception {
  88. instance.load();
  89. assertEquals("response", instance.getResponse());
  90. instance.setResponse("test");
  91. assertEquals("test", instance.getResponse());
  92. }
  93. @Test
  94. public void testGetResponseValidator() throws Exception {
  95. instance.load();
  96. assertNotNull(instance.getResponseValidator());
  97. }
  98. @Test
  99. public void testIsResponseValid_Initially() throws Exception {
  100. instance.load();
  101. assertTrue(instance.isResponseValid());
  102. }
  103. @Test
  104. public void testIsResponseValid_CommandChar() throws Exception {
  105. instance.load();
  106. instance.setResponse("/moo");
  107. assertFalse(instance.isResponseValid());
  108. }
  109. @Test
  110. public void testIsResponseValid() throws Exception {
  111. instance.load();
  112. instance.setResponse("moo");
  113. assertTrue(instance.isResponseValid());
  114. }
  115. @Test
  116. public void testIsSaveAllowed() throws Exception {
  117. instance.load();
  118. assertTrue(instance.isSaveAllowed());
  119. instance.setResponse("test");
  120. assertTrue(instance.isSaveAllowed());
  121. }
  122. @Test
  123. public void testSave_EmptyGlobal() throws Exception {
  124. when(autoCommandManager.getGlobalAutoCommand()).thenReturn(Optional.empty());
  125. instance.load();
  126. instance.setResponse("test");
  127. assertTrue(instance.isSaveAllowed());
  128. instance.save();
  129. verify(autoCommandManager).addAutoCommand(eq(AutoCommand
  130. .create(Optional.empty(), Optional.empty(), Optional.empty(), "test")));
  131. }
  132. @Test
  133. public void testSave_EmptyResponse() throws Exception {
  134. instance.load();
  135. instance.setResponse("");
  136. assertTrue(instance.isSaveAllowed());
  137. instance.save();
  138. verify(autoCommandManager).removeAutoCommand(eq(AutoCommand
  139. .create(Optional.empty(), Optional.empty(), Optional.empty(), "")));
  140. }
  141. @Test
  142. public void testSave() throws Exception {
  143. instance.load();
  144. instance.setResponse("test");
  145. assertTrue(instance.isSaveAllowed());
  146. instance.save();
  147. verify(autoCommandManager).replaceAutoCommand(eq(autoCommand), eq(AutoCommand
  148. .create(Optional.empty(), Optional.empty(), Optional.empty(), "test")));
  149. }
  150. }