/tesla-shell/tesla-shell-core/src/test/java/org/sonatype/gshell/command/resolver/NodePathCompleterTest.java

https://github.com/olamy/tesla · Java · 229 lines · 172 code · 37 blank · 20 comment · 1 complexity · 978f417c9ec45119d0dc9327dccecf31 MD5 · raw file

  1. /**
  2. * Copyright (c) 2009-2011 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.sonatype.gshell.command.resolver;
  17. import com.google.inject.AbstractModule;
  18. import com.google.inject.Guice;
  19. import com.google.inject.Injector;
  20. import com.google.inject.Provides;
  21. import com.google.inject.Stage;
  22. import org.junit.After;
  23. import org.junit.Before;
  24. import org.junit.Test;
  25. import org.sonatype.gshell.command.DummyAction;
  26. import org.sonatype.gshell.command.registry.CommandRegistry;
  27. import org.sonatype.gshell.command.registry.CommandRegistryImpl;
  28. import org.sonatype.gshell.event.EventManager;
  29. import org.sonatype.gshell.event.EventManagerImpl;
  30. import org.sonatype.gshell.variables.Variables;
  31. import org.sonatype.gshell.variables.VariablesImpl;
  32. import java.util.ArrayList;
  33. import java.util.List;
  34. import static junit.framework.Assert.*;
  35. import static org.sonatype.gshell.variables.VariableNames.*;
  36. /**
  37. * Tests for {@link NodePathCompleter}.
  38. *
  39. * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
  40. */
  41. public class NodePathCompleterTest
  42. {
  43. private Variables variables;
  44. private CommandResolver resolver;
  45. private Node root;
  46. private NodePathCompleter completer;
  47. @Before
  48. public void setUp() throws Exception {
  49. variables = new VariablesImpl();
  50. variables.set(SHELL_GROUP, "/");
  51. variables.set(SHELL_GROUP_PATH, ".:/");
  52. Injector injector = Guice.createInjector(Stage.DEVELOPMENT, new AbstractModule() {
  53. @Override
  54. protected void configure() {
  55. bind(EventManager.class).to(EventManagerImpl.class);
  56. bind(CommandRegistry.class).to(CommandRegistryImpl.class);
  57. bind(CommandResolver.class).to(CommandResolverImpl.class);
  58. }
  59. @Provides
  60. private Variables provideVariables() {
  61. return variables;
  62. }
  63. });
  64. resolver = injector.getInstance(CommandResolver.class);
  65. completer = injector.getInstance(NodePathCompleter.class);
  66. root = resolver.root();
  67. root.add("foo/a1", new DummyAction());
  68. root.add("foo/a2", new DummyAction());
  69. root.add("foo/b1", new DummyAction());
  70. root.add("bar", new DummyAction());
  71. root.add("baz", new DummyAction());
  72. }
  73. @After
  74. public void tearDown() {
  75. resolver = null;
  76. variables = null;
  77. root = null;
  78. completer = null;
  79. }
  80. protected void assertCompletes(final String input, final String... expected) {
  81. System.out.println(">");
  82. try {
  83. List<CharSequence> candidates = new ArrayList<CharSequence>();
  84. int result = completer.complete(input, 0, candidates);
  85. System.out.println("Result: " + result + ", Candidates: " + candidates);
  86. assertEquals(expected.length, candidates.size());
  87. for (int i=0; i<expected.length;i++) {
  88. assertEquals(expected[i], candidates.get(i));
  89. }
  90. }
  91. finally {
  92. System.out.println("<");
  93. System.out.flush();
  94. }
  95. }
  96. @Test
  97. public void test1a() {
  98. assertCompletes("b", "bar", "baz");
  99. }
  100. @Test
  101. public void test1b() {
  102. assertCompletes("bar", "bar ");
  103. }
  104. @Test
  105. public void test1c() {
  106. assertCompletes("../bar", "../bar ");
  107. }
  108. @Test
  109. public void test2a() {
  110. assertCompletes("f", "foo/");
  111. }
  112. @Test
  113. public void test2b() {
  114. assertCompletes("foo/a", "foo/a1", "foo/a2");
  115. }
  116. @Test
  117. public void test2c() {
  118. assertCompletes("foo", "foo/");
  119. }
  120. @Test
  121. public void test2d() {
  122. assertCompletes("foo/", "foo/a1", "foo/a2", "foo/b1");
  123. }
  124. @Test
  125. public void test3a() {
  126. assertCompletes("", "foo/", "bar", "baz");
  127. }
  128. @Test
  129. public void test3b() {
  130. assertCompletes("/", "/foo/", "/bar", "/baz");
  131. }
  132. @Test
  133. public void test3c() {
  134. assertCompletes("/foo/", "/foo/a1", "/foo/a2", "/foo/b1");
  135. }
  136. @Test
  137. public void test3d() {
  138. assertCompletes("/foo/a", "/foo/a1", "/foo/a2");
  139. }
  140. @Test
  141. public void test3e() {
  142. assertCompletes("./foo/", "./foo/a1", "./foo/a2", "./foo/b1");
  143. }
  144. @Test
  145. public void test3f() {
  146. assertCompletes("./foo/a", "./foo/a1", "./foo/a2");
  147. }
  148. @Test
  149. public void test4a() {
  150. variables.set(SHELL_GROUP, "/foo");
  151. variables.set(SHELL_GROUP_PATH, ".");
  152. assertCompletes("a1", "a1 ");
  153. }
  154. @Test
  155. public void test4b() {
  156. variables.set(SHELL_GROUP, "/foo");
  157. variables.set(SHELL_GROUP_PATH, ".");
  158. assertCompletes("", "..", "a1", "a2", "b1");
  159. }
  160. @Test
  161. public void test4c() {
  162. variables.set(SHELL_GROUP, "/foo");
  163. variables.set(SHELL_GROUP_PATH, ".:/");
  164. assertCompletes("", "..", "a1", "a2", "b1", "foo/", "bar", "baz");
  165. }
  166. @Test
  167. public void test4d() {
  168. variables.set(SHELL_GROUP, "/foo");
  169. variables.set(SHELL_GROUP_PATH, ".");
  170. assertCompletes("a", "a1", "a2");
  171. }
  172. @Test
  173. public void test4e() {
  174. variables.set(SHELL_GROUP, "/foo");
  175. variables.set(SHELL_GROUP_PATH, ".:/");
  176. assertCompletes("a", "a1", "a2");
  177. }
  178. @Test
  179. public void test5a() {
  180. assertCompletes(".", "./", "../");
  181. }
  182. @Test
  183. public void test5b() {
  184. assertCompletes("..", "../");
  185. }
  186. @Test
  187. public void test5c() {
  188. variables.set(SHELL_GROUP, "/foo");
  189. variables.set(SHELL_GROUP_PATH, ".:/");
  190. assertCompletes(".", "./", "../");
  191. }
  192. }