PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tesla-shell/tesla-shell-core/src/test/java/org/sonatype/gshell/command/support/CommandTestSupport.java

https://github.com/olamy/tesla
Java | 248 lines | 176 code | 44 blank | 28 comment | 6 complexity | 2e907c29c1f4734ff1786796873cc272 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.support;
  17. import com.google.inject.AbstractModule;
  18. import com.google.inject.Guice;
  19. import com.google.inject.Injector;
  20. import com.google.inject.Module;
  21. import com.google.inject.Stage;
  22. import org.fusesource.jansi.Ansi;
  23. import org.junit.After;
  24. import org.junit.Assert;
  25. import org.junit.Before;
  26. import org.junit.Test;
  27. import org.sonatype.gshell.alias.AliasRegistry;
  28. import org.sonatype.gshell.branding.Branding;
  29. import org.sonatype.gshell.branding.TestBranding;
  30. import org.sonatype.gshell.command.Command;
  31. import org.sonatype.gshell.command.CommandAction;
  32. import org.sonatype.gshell.command.IO;
  33. import org.sonatype.gshell.command.registry.CommandRegistrar;
  34. import org.sonatype.gshell.command.registry.CommandRegistry;
  35. import org.sonatype.gshell.console.ConsoleErrorHandler;
  36. import org.sonatype.gshell.console.ConsolePrompt;
  37. import org.sonatype.gshell.guice.CoreModule;
  38. import org.sonatype.gshell.logging.LoggingSystem;
  39. import org.sonatype.gshell.shell.Shell;
  40. import org.sonatype.gshell.shell.ShellErrorHandler;
  41. import org.sonatype.gshell.shell.ShellImpl;
  42. import org.sonatype.gshell.shell.ShellPrompt;
  43. import org.sonatype.gshell.util.Strings;
  44. import org.sonatype.gshell.variables.Variables;
  45. import org.sonatype.gshell.variables.VariablesImpl;
  46. import org.sonatype.guice.bean.binders.SpaceModule;
  47. import org.sonatype.guice.bean.binders.WireModule;
  48. import org.sonatype.guice.bean.locators.DefaultBeanLocator;
  49. import org.sonatype.guice.bean.locators.MutableBeanLocator;
  50. import org.sonatype.guice.bean.reflect.URLClassSpace;
  51. import org.sonatype.inject.BeanScanning;
  52. import java.util.ArrayList;
  53. import java.util.HashMap;
  54. import java.util.List;
  55. import java.util.Map;
  56. import static com.google.inject.name.Names.named;
  57. import static org.junit.Assert.*;
  58. /**
  59. * Support for testing {@link org.sonatype.gshell.command.CommandAction} instances.
  60. *
  61. * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
  62. * @since 2.5
  63. */
  64. public abstract class CommandTestSupport
  65. {
  66. protected final String name;
  67. private final TestUtil util = new TestUtil(this);
  68. private DefaultBeanLocator container;
  69. private TestIO io;
  70. private Shell shell;
  71. protected AliasRegistry aliasRegistry;
  72. protected CommandRegistry commandRegistry;
  73. protected Variables vars;
  74. protected final Map<String,Class> requiredCommands = new HashMap<String,Class>();
  75. protected CommandTestSupport(final String name, final Class<?> type) {
  76. assertNotNull(name);
  77. assertNotNull(type);
  78. this.name = name;
  79. requiredCommands.put(name, type);
  80. }
  81. protected CommandTestSupport(final Class<?> type) {
  82. this(type.getAnnotation(Command.class).name(), type);
  83. }
  84. @Before
  85. public void setUp() throws Exception {
  86. container = new DefaultBeanLocator();
  87. io = new TestIO();
  88. vars = new VariablesImpl();
  89. Module boot = new AbstractModule()
  90. {
  91. @Override
  92. protected void configure() {
  93. bind(MutableBeanLocator.class).toInstance(container);
  94. bind(LoggingSystem.class).to(TestLoggingSystem.class);
  95. bind(ConsolePrompt.class).to(ShellPrompt.class);
  96. bind(ConsoleErrorHandler.class).to(ShellErrorHandler.class);
  97. bind(Branding.class).toInstance(new TestBranding(util.resolveFile("target/shell-home")));
  98. bind(IO.class).annotatedWith(named("main")).toInstance(io);
  99. bind(Variables.class).annotatedWith(named("main")).toInstance(vars);
  100. }
  101. };
  102. List<Module> modules = new ArrayList<Module>();
  103. modules.add(boot);
  104. configureModules(modules);
  105. Injector injector = Guice.createInjector(Stage.DEVELOPMENT, new WireModule(modules));
  106. container.add(injector, 0);
  107. CommandRegistrar registrar = injector.getInstance(CommandRegistrar.class);
  108. for (Map.Entry<String,Class> entry : requiredCommands.entrySet()) {
  109. registrar.registerCommand(entry.getKey(), entry.getValue().getName());
  110. }
  111. shell = injector.getInstance(ShellImpl.class);
  112. // For simplicity of output verification disable ANSI
  113. Ansi.setEnabled(false);
  114. vars = shell.getVariables();
  115. aliasRegistry = injector.getInstance(AliasRegistry.class);
  116. commandRegistry = injector.getInstance(CommandRegistry.class);
  117. }
  118. protected void configureModules(final List<Module> modules) {
  119. assert modules != null;
  120. modules.add(createSpaceModule());
  121. modules.add(new CoreModule());
  122. }
  123. protected SpaceModule createSpaceModule() {
  124. URLClassSpace space = new URLClassSpace(getClass().getClassLoader());
  125. return new SpaceModule(space, BeanScanning.INDEX);
  126. }
  127. @After
  128. public void tearDown() throws Exception {
  129. commandRegistry = null;
  130. aliasRegistry = null;
  131. vars = null;
  132. io = null;
  133. if (shell != null) {
  134. shell.close();
  135. }
  136. shell = null;
  137. requiredCommands.clear();
  138. if (container != null) {
  139. container.clear();
  140. container = null;
  141. }
  142. }
  143. protected Shell getShell() {
  144. assertNotNull(shell);
  145. return shell;
  146. }
  147. protected TestIO getIo() {
  148. assertNotNull(io);
  149. return io;
  150. }
  151. protected Object execute(final String line) throws Exception {
  152. assertNotNull(line);
  153. return getShell().execute(line);
  154. }
  155. protected Object execute() throws Exception {
  156. return execute(name);
  157. }
  158. protected Object execute(final String... args) throws Exception {
  159. return execute(Strings.join(args, " "));
  160. }
  161. protected Object executeWithArgs(final String args) throws Exception {
  162. assertNotNull(args);
  163. return execute(name, args);
  164. }
  165. protected Object executeWithArgs(final String... args) throws Exception {
  166. assertNotNull(args);
  167. return execute(name, Strings.join(args, " "));
  168. }
  169. //
  170. // Assertion helpers
  171. //
  172. protected void assertEqualsSuccess(final Object result) {
  173. Assert.assertEquals(CommandAction.Result.SUCCESS, result);
  174. }
  175. protected void assertEqualsFailure(final Object result) {
  176. assertEquals(CommandAction.Result.FAILURE, result);
  177. }
  178. protected void assertOutputEquals(final String expected) {
  179. assertEquals(getIo().getOutputString(), expected);
  180. }
  181. protected void assertErrorOutputEquals(final String expected) {
  182. assertEquals(getIo().getErrorString(), expected);
  183. }
  184. //
  185. // Some default tests for all commands
  186. //
  187. @Test
  188. public void testRegistered() throws Exception {
  189. assertTrue(commandRegistry.containsCommand(name));
  190. }
  191. @Test
  192. public void testHelp() throws Exception {
  193. Object result;
  194. result = executeWithArgs("--help");
  195. assertEqualsSuccess(result);
  196. result = executeWithArgs("-h");
  197. assertEqualsSuccess(result);
  198. }
  199. @Test
  200. public void testDefault() throws Exception {
  201. Object result = execute();
  202. assertEqualsSuccess(result);
  203. }
  204. }