001/* 002 * Copyright 2012-2016 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.boot.cli.command.test; 018 019import joptsimple.OptionSet; 020 021import org.springframework.boot.cli.command.Command; 022import org.springframework.boot.cli.command.OptionParsingCommand; 023import org.springframework.boot.cli.command.options.CompilerOptionHandler; 024import org.springframework.boot.cli.command.options.OptionSetGroovyCompilerConfiguration; 025import org.springframework.boot.cli.command.options.SourceOptions; 026import org.springframework.boot.cli.command.status.ExitStatus; 027 028/** 029 * {@link Command} to run a groovy test script or scripts. 030 * 031 * @author Greg Turnquist 032 * @author Phillip Webb 033 */ 034public class TestCommand extends OptionParsingCommand { 035 036 public TestCommand() { 037 super("test", "Run a spring groovy script test", new TestOptionHandler()); 038 } 039 040 @Override 041 public String getUsageHelp() { 042 return "[options] <files> [--] [args]"; 043 } 044 045 private static class TestOptionHandler extends CompilerOptionHandler { 046 047 private TestRunner runner; 048 049 @Override 050 protected ExitStatus run(OptionSet options) throws Exception { 051 SourceOptions sourceOptions = new SourceOptions(options); 052 TestRunnerConfiguration configuration = new TestRunnerConfigurationAdapter( 053 options, this); 054 this.runner = new TestRunner(configuration, sourceOptions.getSourcesArray(), 055 sourceOptions.getArgsArray()); 056 this.runner.compileAndRunTests(); 057 return ExitStatus.OK.hangup(); 058 } 059 060 /** 061 * Simple adapter class to present the {@link OptionSet} as a 062 * {@link TestRunnerConfiguration}. 063 */ 064 private class TestRunnerConfigurationAdapter extends 065 OptionSetGroovyCompilerConfiguration implements TestRunnerConfiguration { 066 067 TestRunnerConfigurationAdapter(OptionSet options, 068 CompilerOptionHandler optionHandler) { 069 super(options, optionHandler); 070 } 071 072 } 073 074 } 075 076}