[bs-135] Plugin model for spring commands

* Added CommandFactory and a ServiceLoader model for providing
implementations
* Added ScriptCommand (wrapping groovy script). Service providers
are recommended to implement OptionHandler in their script, but a
regulare Script or a Runnable will also work.

[#50427095]
This commit is contained in:
Dave Syer
2013-05-28 09:44:36 +01:00
parent 51e6c8c8b6
commit d950d15f9b
21 changed files with 836 additions and 400 deletions

View File

@@ -28,6 +28,7 @@ import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.bootstrap.cli.command.RunCommand;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

View File

@@ -11,6 +11,8 @@ import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.bootstrap.cli.SpringBootstrapCli.NoArgumentsException;
import org.springframework.bootstrap.cli.SpringBootstrapCli.NoHelpCommandArgumentsException;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

View File

@@ -0,0 +1,54 @@
/*
* 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.bootstrap.cli.command;
import groovy.lang.Script;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/**
* @author Dave Syer
*
*/
public class ScriptCommandTests {
public static boolean executed = false;
@Test
public void testScript() throws Exception {
ScriptCommand command = new ScriptCommand("script", "Run a test command");
command.run("World");
assertEquals("World",
((String[]) ((Script) command.getMain()).getProperty("args"))[0]);
}
@Test
public void testOptions() throws Exception {
ScriptCommand command = new ScriptCommand("test", "Run a test command");
command.run("World", "--foo");
ByteArrayOutputStream out = new ByteArrayOutputStream();
((OptionHandler) command.getMain()).printHelp(new PrintStream(out));
assertTrue("Wrong output: " + out, out.toString().contains("--foo"));
assertTrue(executed);
}
}

View File

@@ -0,0 +1 @@
println "Hello ${args[0]}"

View File

@@ -0,0 +1,19 @@
package org.test.command
@Grab("org.eclipse.jgit:org.eclipse.jgit:2.3.1.201302201838-r")
import org.eclipse.jgit.api.Git
class TestCommand extends OptionHandler {
void options() {
option "foo", "Foo set"
}
void run(OptionSet options) {
// Demonstrate use of Grape.grab to load dependencies before running
println "Clean : " + Git.open(".." as File).status().call().isClean()
org.springframework.bootstrap.cli.command.ScriptCommandTests.executed = true
println "Hello ${options.nonOptionArguments()}: ${options.has('foo')}"
}
}