Add 'spring test [files]' command to compile and test code automatically

- Look for JUnit test symbols, and add JUnit automatically
- Look for Spock test symbols, and add Spock automatically
- Based on what test libraries were used, invoke relevant embedded testers
  and accumulate results
- Make it so that multiple testers can be invoked through a single 'test' command
- Print out total results and write out detailed trace errors in results.txt
- Update based on the new artifact resolution mechanism
This commit is contained in:
Greg Turnquist
2013-10-03 08:16:48 -05:00
parent 4655eb3a94
commit 1ce13cc2c2
26 changed files with 990 additions and 12 deletions

View File

@@ -31,7 +31,7 @@ import org.springframework.boot.cli.CommandFactory;
public class DefaultCommandFactory implements CommandFactory {
private static final List<Command> DEFAULT_COMMANDS = Arrays.<Command> asList(
new VersionCommand(), new RunCommand(), new CleanCommand());
new VersionCommand(), new RunCommand(), new CleanCommand(), new TestCommand());
@Override
public Collection<Command> getCommands() {

View File

@@ -0,0 +1,264 @@
/*
* 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 groovy.lang.GroovyObject;
import joptsimple.OptionSet;
import org.apache.ivy.util.FileUtil;
import org.springframework.boot.cli.Log;
import org.springframework.boot.cli.command.tester.Failure;
import org.springframework.boot.cli.command.tester.TestResults;
import org.springframework.boot.cli.compiler.GroovyCompiler;
import org.springframework.boot.cli.compiler.GroovyCompilerConfiguration;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
import java.util.logging.Level;
/**
* Invokes testing for autocompiled scripts
*
* @author Greg Turnquist
*/
public class TestCommand extends OptionParsingCommand {
private TestOptionHandler testOptionHandler;
public TestCommand() {
super("test", "Test a groovy script", new TestOptionHandler());
this.testOptionHandler = (TestOptionHandler)this.getHandler();
}
@Override
public String getUsageHelp() {
return "[options] <files>";
}
public TestResults getResults() {
return testOptionHandler.results;
}
private static class TestGroovyCompilerConfiguration implements GroovyCompilerConfiguration {
@Override
public boolean isGuessImports() {
return true;
}
@Override
public boolean isGuessDependencies() {
return true;
}
@Override
public String getClasspath() {
return "";
}
public Level getLogLevel() {
return Level.INFO;
}
}
private static class TestOptionHandler extends OptionHandler {
private final GroovyCompiler compiler;
private TestResults results;
public TestOptionHandler() {
TestGroovyCompilerConfiguration configuration = new TestGroovyCompilerConfiguration();
this.compiler = new GroovyCompiler(configuration);
if (configuration.getLogLevel().intValue() <= Level.FINE.intValue()) {
System.setProperty("groovy.grape.report.downloads", "true");
}
}
@Override
protected void run(OptionSet options) throws Exception {
List<?> nonOptionArguments = options.nonOptionArguments();
Set<File> testerFiles = new HashSet<File>();
File[] files = getFileArguments(nonOptionArguments, testerFiles);
/*
* Need to compile the code twice: The first time automatically
* pulls in autoconfigured libraries including test tools. Then
* the compiled code can be scanned to see what libraries were
* activated. Then it can be recompiled, with appropriate tester
* groovy scripts included in the same classloading context. Then
* the testers can be fetched and invoked through reflection against
* the composite AST.
*/
// Compile - Pass 1
Object[] sources = this.compiler.sources(files);
boolean testing = false;
try {
check("org.junit.Test", sources);
testerFiles.add(locateSourceFromUrl("junit", "testers/junit.groovy"));
testing = true;
} catch (ClassNotFoundException e) {
}
try {
check("spock.lang.Specification", sources);
testerFiles.add(locateSourceFromUrl("spock", "testers/spock.groovy"));
testing = true;
} catch (ClassNotFoundException e) {
}
if (testing) {
testerFiles.add(locateSourceFromUrl("tester", "testers/tester.groovy"));
}
// Compile - Pass 2 - with appropriate testers added in
files = getFileArguments(nonOptionArguments, testerFiles);
sources = this.compiler.sources(files);
if (sources.length == 0) {
throw new RuntimeException("No classes found in '" + files + "'");
}
List<Class<?>> testers = new ArrayList<Class<?>>();
// Extract list of compiled classes
List<Class<?>> compiled = new ArrayList<Class<?>>();
for (Object source : sources) {
if (source.getClass() == Class.class) {
Class<?> sourceClass = (Class<?>)source;
if (sourceClass.getSuperclass().getName().equals("AbstractTester")) {
testers.add(sourceClass);
} else {
compiled.add((Class<?>)source);
}
}
}
this.results = new TestResults();
for (Class<?> tester : testers) {
GroovyObject obj = (GroovyObject)tester.newInstance();
this.results.add((TestResults)obj.invokeMethod("findAndTest", compiled));
}
printReport(this.results);
}
private File locateSourceFromUrl(String name, String path) {
try {
File file = File.createTempFile(name, ".groovy");
file.deleteOnExit();
FileUtil.copy(getClass().getClassLoader().getResourceAsStream(path), file, null);
return file;
}
catch (IOException ex) {
throw new IllegalStateException("Could not create temp file for source: "
+ name);
}
}
private Class<?> check(String className, Object[] sources) throws ClassNotFoundException {
Class<?> classToReturn = null;
ClassNotFoundException classNotFoundException = null;
for (Object source : sources) {
try {
classToReturn = ((Class<?>)source).getClassLoader().loadClass(className);
} catch (ClassNotFoundException e) {
classNotFoundException = e;
}
}
if (classToReturn != null) {
return classToReturn;
}
throw classNotFoundException;
}
private File[] getFileArguments(List<?> nonOptionArguments, Set<File> testerFiles) {
List<File> files = new ArrayList<File>();
for (Object option : nonOptionArguments) {
if (option instanceof String) {
String filename = (String) option;
if ("--".equals(filename)) {
break;
}
if (filename.endsWith(".groovy") || filename.endsWith(".java")) {
File file = new File(filename);
if (file.isFile() && file.canRead()) {
files.add(file);
}
else {
URL url = getClass().getClassLoader().getResource(filename);
if (url != null) {
if (url.toString().startsWith("file:")) {
files.add(new File(url.toString().substring("file:".length())));
}
}
else {
throw new RuntimeException("Can't find " + filename);
}
}
}
}
}
if (files.size() == 0) {
throw new RuntimeException("Please specify a file to run");
}
for (File testerFile : testerFiles) {
files.add(testerFile);
}
return files.toArray(new File[files.size()]);
}
private void printReport(TestResults results) throws FileNotFoundException {
PrintWriter writer = new PrintWriter("results.txt");
String header = "Total: " + results.getRunCount() +
", Success: " + (results.getRunCount()-results.getFailureCount()) +
", : Failures: " + results.getFailureCount() + "\n" +
"Passed? " + results.wasSuccessful();
String trailer = "";
String trace = "";
for (Failure failure : results.getFailures()) {
trailer += failure.getDescription().toString();
trace += failure.getTrace() + "\n";
}
writer.println(header);
writer.println(trace);
writer.close();
Log.info(header);
Log.info(trailer);
}
}
}

View File

@@ -0,0 +1,39 @@
/*
* 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.tester;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Set;
public abstract class AbstractTester {
public TestResults findAndTest(List<Class<?>> compiled) throws FileNotFoundException {
Set<Class<?>> testable = findTestableClasses(compiled);
if (testable.size() == 0) {
return TestResults.none;
}
return test(testable.toArray(new Class<?>[]{}));
}
abstract protected Set<Class<?>> findTestableClasses(List<Class<?>> compiled);
abstract protected TestResults test(Class<?>[] testable);
}

View File

@@ -0,0 +1,48 @@
/*
* 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.tester;
/**
* Platform neutral way to capture a test failure
*
* NOTE: This is needed to avoid having to add JUnit jar file to the deployable artifacts
*/
public class Failure {
private String description;
private String trace;
public Failure(String description, String trace) {
this.description = description;
this.trace = trace;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTrace() {
return trace;
}
public void setTrace(String trace) {
this.trace = trace;
}
}

View File

@@ -0,0 +1,94 @@
/*
* 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.tester;
import java.util.Arrays;
import java.util.List;
/**
* Platform neutral way to collect test results
*
* NOTE: This is needed to avoid having to add JUnit's jar file to the deployable artifacts
*/
public class TestResults {
public static final NoTestResults none = new NoTestResults();
private int runCount;
private int failureCount;
private Failure[] failures = new Failure[0];
public void add(TestResults results) {
this.runCount += results.getRunCount();
this.failureCount += results.getFailureCount();
List<Failure> failures = Arrays.asList(this.failures);
failures.addAll(Arrays.asList(results.getFailures()));
this.failures = failures.toArray(new Failure[]{});
}
public boolean wasSuccessful() {
return this.failureCount == 0;
}
public int getRunCount() {
return runCount;
}
public void setRunCount(int runCount) {
this.runCount = runCount;
}
public int getFailureCount() {
return failureCount;
}
public void setFailureCount(int failureCount) {
this.failureCount = failureCount;
}
public Failure[] getFailures() {
return failures;
}
public void setFailures(Failure[] failures) {
this.failures = failures;
}
private static class NoTestResults extends TestResults {
@Override
public int getRunCount() {
return 0;
}
@Override
public int getFailureCount() {
return 0;
}
@Override
public Failure[] getFailures() {
return new Failure[0];
}
@Override
public boolean wasSuccessful() {
return true;
}
}
}

View File

@@ -32,6 +32,7 @@ import org.codehaus.groovy.ast.MethodNode;
*
* @author Phillip Webb
* @author Dave Syer
* @author Greg Turnquist
*/
public abstract class AstUtils {
@@ -49,12 +50,40 @@ public abstract class AstUtils {
}
}
}
return false;
}
/**
* Determine if a {@link ClassNode} has one or more of the specified annotations on the class
* or any of its methods.
* N.B. the type names are not normally fully qualified.
*/
public static boolean hasAtLeastOneAnnotation(ClassNode node, String... annotations) {
for (AnnotationNode annotationNode : node.getAnnotations()) {
for (String annotation : annotations) {
if (annotation.equals(annotationNode.getClassNode().getName())) {
return true;
}
}
}
List<MethodNode> methods = node.getMethods();
for (MethodNode method : methods) {
for (AnnotationNode annotationNode : method.getAnnotations()) {
for (String annotation : annotations) {
if (annotation.equals(annotationNode.getClassNode().getName())) {
return true;
}
}
}
}
return false;
}
/**
* Determine if an {@link ClassNode} has one or more fields of the specified types or
* Determine if a {@link ClassNode} has one or more fields of the specified types or
* method returning one or more of the specified types. N.B. the type names are not
* normally fully qualified.
*/
@@ -73,8 +102,23 @@ public abstract class AstUtils {
return true;
}
}
return false;
}
/**
* Determine if a {@link ClassNode} subclasses any of the specified types
* N.B. the type names are not normally fully qualified.
*/
public static boolean subclasses(ClassNode node, String... types) {
for (String type : types) {
if (node.getSuperClass().getName().equals(type)) {
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,52 @@
/*
* 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.compiler.autoconfigure;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.springframework.boot.cli.compiler.AstUtils;
import org.springframework.boot.cli.compiler.CompilerAutoConfiguration;
import org.springframework.boot.cli.compiler.DependencyCustomizer;
/**
* {@link CompilerAutoConfiguration} for JUnit
*
* @author Greg Turnquist
*/
public class JUnitCompilerAutoConfiguration extends CompilerAutoConfiguration {
@Override
public boolean matches(ClassNode classNode) {
return AstUtils.hasAtLeastOneAnnotation(classNode, "Test");
}
@Override
public void applyDependencies(DependencyCustomizer dependencies)
throws CompilationFailedException {
dependencies.add("junit").add("spring-test").add("hamcrest-library");
}
@Override
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
imports.addStarImports("org.junit")
.addStaticStars("org.junit.Assert").addImports()
.addStaticStars("org.hamcrest.MatcherAssert")
.addStaticStars("org.hamcrest.Matchers");
}
}

View File

@@ -0,0 +1,49 @@
/*
* 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.compiler.autoconfigure;
import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.springframework.boot.cli.compiler.AstUtils;
import org.springframework.boot.cli.compiler.CompilerAutoConfiguration;
import org.springframework.boot.cli.compiler.DependencyCustomizer;
/**
* {@link CompilerAutoConfiguration} for Spock test framework
*
* @author Greg Turnquist
*/
public class SpockCompilerAutoConfiguration extends CompilerAutoConfiguration {
@Override
public boolean matches(ClassNode classNode) {
return AstUtils.subclasses(classNode, "Specification");
}
@Override
public void applyDependencies(DependencyCustomizer dependencies)
throws CompilationFailedException {
dependencies.add("spock-core");
}
@Override
public void applyImports(ImportCustomizer imports) throws CompilationFailedException {
imports.addStarImports("spock.lang");
}
}