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