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.

CommandParserTest.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (c) 2006-2013 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.parsers;
  23. import com.dmdirc.TestMain;
  24. import com.dmdirc.Main;
  25. import com.dmdirc.ServerManager;
  26. import com.dmdirc.commandparser.CommandManager;
  27. import com.dmdirc.commandparser.commands.global.Echo;
  28. import com.dmdirc.config.ConfigBinder;
  29. import com.dmdirc.config.ConfigManager;
  30. import com.dmdirc.config.InvalidIdentityFileException;
  31. import com.dmdirc.harness.TestCommandParser;
  32. import org.junit.Before;
  33. import org.junit.Test;
  34. import org.mockito.Mock;
  35. import org.mockito.MockitoAnnotations;
  36. import static org.junit.Assert.*;
  37. import static org.mockito.Mockito.*;
  38. public class CommandParserTest {
  39. @Mock private ServerManager serverManager;
  40. @Mock private ConfigManager cm;
  41. private CommandManager commands;
  42. @Before
  43. public void setup() throws InvalidIdentityFileException {
  44. MockitoAnnotations.initMocks(this);
  45. when(cm.getOptionChar("general", "silencechar")).thenReturn('.');
  46. when(cm.getOptionInt("general", "commandhistory")).thenReturn(10);
  47. when(cm.getOptionChar("general", "commandchar")).thenReturn('/');
  48. final ConfigBinder binder = new ConfigBinder(cm);
  49. when(cm.getBinder()).thenReturn(binder);
  50. commands = new CommandManager(serverManager);
  51. commands.initialise(cm);
  52. commands.registerCommand(new Echo(commands), Echo.INFO);
  53. }
  54. @Test
  55. public void testBasicCommand() {
  56. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  57. tcp.parseCommand(null, "/echo this is a test");
  58. assertNull(tcp.nonCommandLine);
  59. assertNull(tcp.invalidCommand);
  60. assertNotNull(tcp.executedCommand);
  61. assertFalse(tcp.wasSilent);
  62. assertTrue(tcp.executedCommand instanceof Echo);
  63. }
  64. @Test
  65. public void testBasicNoArgs() {
  66. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  67. tcp.parseCommand(null, "/echo");
  68. assertNull(tcp.nonCommandLine);
  69. assertNull(tcp.invalidCommand);
  70. assertNotNull(tcp.executedCommand);
  71. assertFalse(tcp.wasSilent);
  72. assertTrue(tcp.executedCommand instanceof Echo);
  73. }
  74. @Test
  75. public void testSilentNoArgs() {
  76. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  77. tcp.parseCommand(null, "/.echo");
  78. assertNull(tcp.nonCommandLine);
  79. assertNull(tcp.invalidCommand);
  80. assertNotNull(tcp.executedCommand);
  81. assertTrue(tcp.wasSilent);
  82. assertTrue(tcp.executedCommand instanceof Echo);
  83. }
  84. @Test
  85. public void testSilentCommand() {
  86. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  87. tcp.parseCommand(null, "/.echo this is a test");
  88. assertNull(tcp.nonCommandLine);
  89. assertNull(tcp.invalidCommand);
  90. assertNotNull(tcp.executedCommand);
  91. assertTrue(tcp.wasSilent);
  92. assertTrue(tcp.executedCommand instanceof Echo);
  93. }
  94. @Test
  95. public void testNonExistantCommand() {
  96. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  97. tcp.parseCommand(null, "/foobar moo bar");
  98. assertNull(tcp.nonCommandLine);
  99. assertEquals("foobar", tcp.invalidCommand);
  100. assertNotNull(tcp.invalidCommand);
  101. assertNull(tcp.executedCommand);
  102. assertFalse(tcp.wasSilent);
  103. }
  104. @Test
  105. public void testEmptyCommand() {
  106. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  107. tcp.parseCommand(null, "/ moo bar");
  108. assertNull(tcp.nonCommandLine);
  109. assertEquals("", tcp.invalidCommand);
  110. assertNotNull(tcp.invalidCommand);
  111. assertNull(tcp.executedCommand);
  112. assertFalse(tcp.wasSilent);
  113. }
  114. @Test
  115. public void testEmptySilentCommand() {
  116. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  117. tcp.parseCommand(null, "/. moo bar");
  118. assertNull(tcp.nonCommandLine);
  119. assertEquals("", tcp.invalidCommand);
  120. assertNotNull(tcp.invalidCommand);
  121. assertNull(tcp.executedCommand);
  122. assertFalse(tcp.wasSilent);
  123. }
  124. @Test
  125. public void testNonCommand() {
  126. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  127. tcp.parseCommand(null, "Foobar baz");
  128. assertNotNull(tcp.nonCommandLine);
  129. assertEquals("Foobar baz", tcp.nonCommandLine);
  130. assertNull(tcp.invalidCommand);
  131. assertNull(tcp.executedCommand);
  132. assertFalse(tcp.wasSilent);
  133. }
  134. @Test
  135. public void testCommandHistory() {
  136. final TestCommandParser tcp = new TestCommandParser(cm, commands);
  137. tcp.parseCommand(null, "/echo this is a test");
  138. final long time1 = tcp.getCommandTime("echo this is a test");
  139. assertTrue(time1 > 0);
  140. tcp.parseCommand( null, "/echo this is a test");
  141. final long time2 = tcp.getCommandTime("echo this is a test");
  142. assertTrue(time2 > 0);
  143. assertTrue(time2 >= time1);
  144. assertEquals(0L, tcp.getCommandTime("echo"));
  145. }
  146. }