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

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