Extend programming model for script commands

This commit is contained in:
Dave Syer
2014-01-04 19:24:55 +00:00
parent b5db4d3ff4
commit 1e75c0a55b
17 changed files with 469 additions and 281 deletions

View File

@@ -0,0 +1,131 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cli.command;
import groovy.lang.Closure;
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.OutputCapture;
import org.springframework.boot.cli.Command;
import org.springframework.boot.cli.command.InitCommand.Commands;
import org.springframework.boot.cli.command.OptionHandler;
import org.springframework.boot.cli.command.ScriptCompilationCustomizer;
import org.springframework.boot.cli.compiler.GroovyCompiler;
import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration;
import org.springframework.boot.cli.compiler.GroovyCompilerScope;
import org.springframework.boot.cli.compiler.grape.RepositoryConfiguration;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Dave Syer
*/
public class ScriptCompilationCustomizerTests {
private TestGroovyCompilerConfiguration configuration = new TestGroovyCompilerConfiguration();
private GroovyCompiler compiler = new GroovyCompiler(this.configuration);
@Rule
public OutputCapture output = new OutputCapture();
@Before
public void init() {
this.compiler.addCompilationCustomizers(new ScriptCompilationCustomizer());
}
@Test
public void simpleCompile() throws Exception {
Class<?>[] types = this.compiler.compile(new File(
"src/test/resources/scripts/command.groovy"));
Class<?> main = types[0];
assertEquals("org.test.command.TestCommand", main.getName());
assertTrue(Command.class.isAssignableFrom(main));
}
@Test
public void addsOptionHandler() throws Exception {
Class<?>[] types = this.compiler.compile(new File(
"src/test/resources/scripts/handler.groovy"));
Class<?> main = types[0];
assertTrue(OptionHandler.class.isAssignableFrom(main));
}
@Test
public void addsCommands() throws Exception {
Class<?>[] types = this.compiler.compile(new File(
"src/test/resources/scripts/options.groovy"));
Class<?> main = types[0];
assertTrue(Commands.class.isAssignableFrom(main));
}
@Test
public void commandsExecutable() throws Exception {
Class<?>[] types = this.compiler.compile(new File(
"src/test/resources/scripts/options.groovy"));
Class<?> main = types[0];
Map<String, Closure<?>> commands = ((Commands) main.newInstance()).getCommands();
assertEquals(1, commands.size());
assertEquals("foo", commands.keySet().iterator().next());
Closure<?> closure = commands.values().iterator().next();
closure.call(); // what about args?
assertTrue(this.output.toString().contains("Hello Command"));
}
private static class TestGroovyCompilerConfiguration implements
GroovyCompilerConfiguration {
@Override
public GroovyCompilerScope getScope() {
return GroovyCompilerScope.EXTENSION;
}
@Override
public boolean isGuessImports() {
return true;
}
@Override
public boolean isGuessDependencies() {
return false;
}
@Override
public boolean isAutoconfigure() {
return true;
}
@Override
public String[] getClasspath() {
return new String[0];
}
@Override
public List<RepositoryConfiguration> getRepositoryConfiguration() {
return Collections.emptyList();
}
}
}

View File

@@ -69,6 +69,12 @@ public class InitCommandTests {
assertTrue(this.output.toString().contains("Hello Grab"));
}
@Test
public void initCommand() throws Exception {
this.command.run("src/test/resources/command.groovy");
verify(this.cli, times(this.defaultCount + 1)).register(any(Command.class));
}
@Test(expected = IllegalArgumentException.class)
public void initNonExistentScript() throws Exception {
this.command.run("nonexistent.groovy");
@@ -78,7 +84,7 @@ public class InitCommandTests {
@Test
public void initDefault() throws Exception {
this.command.run();
assertTrue(this.output.toString().contains("Hello World"));
assertTrue(this.output.toString().contains("Hello Init"));
}
}

View File

@@ -0,0 +1,79 @@
/*
* Copyright 2012-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.cli.command;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.boot.OutputCapture;
import org.springframework.boot.cli.SpringCli;
import static org.junit.Assert.assertTrue;
/**
* @author Dave Syer
*/
public class ScriptCommandTests {
@Rule
public OutputCapture output = new OutputCapture();
public static boolean executed = false;
private SpringCli cli;
private InitCommand init;
private ClassLoader classLoader;
@Before
public void init() {
this.classLoader = Thread.currentThread().getContextClassLoader();
this.cli = new SpringCli();
this.init = this.cli.getInitCommand();
executed = false;
}
@After
public void close() {
Thread.currentThread().setContextClassLoader(this.classLoader);
}
@Test
public void command() throws Exception {
this.init.run("src/test/resources/commands/command.groovy");
this.cli.find("foo").run("Foo");
assertTrue(this.output.toString().contains("Hello Foo"));
}
@Test
public void handler() throws Exception {
this.init.run("src/test/resources/commands/handler.groovy");
this.cli.find("foo").run("Foo", "--foo=bar");
assertTrue(executed);
}
@Test
@Ignore
public void options() throws Exception {
this.init.run("src/test/resources/commands/options.groovy");
this.cli.find("foo").run("Foo", "--foo=bar");
assertTrue(executed);
}
}