Clean up TestCommand paraphenalia

This commit is contained in:
Dave Syer
2013-10-08 15:39:37 -04:00
parent 1ce13cc2c2
commit b5f0f97110
13 changed files with 283 additions and 301 deletions

View File

@@ -0,0 +1,68 @@
/*
* 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.
*/
import org.junit.runner.JUnitCore
import org.junit.runner.Result
import org.springframework.boot.cli.command.tester.Failure
import org.springframework.boot.cli.command.tester.TestResults
import java.lang.annotation.Annotation
import java.lang.reflect.Method
/**
* Groovy script to run JUnit tests inside the {@link TestCommand}.
* Needs to be compiled along with the actual code to work properly.
*
* @author Greg Turnquist
*/
class JUnitTester extends AbstractTester {
@Override
protected Set<Class<?>> findTestableClasses(List<Class<?>> compiled) {
// Look for @Test methods
Set<Class<?>> testable = new LinkedHashSet<Class<?>>()
for (Class<?> clazz : compiled) {
for (Method method : clazz.getMethods()) {
for (Annotation annotation : method.getAnnotations()) {
if (annotation.toString().contains("Test")) {
testable.add(clazz)
}
}
}
}
return testable
}
@Override
protected TestResults test(Class<?>[] testable) {
Result results = JUnitCore.runClasses(testable)
TestResults testResults = new TestResults()
testResults.setFailureCount(results.getFailureCount())
testResults.setRunCount(results.getRunCount())
List<org.springframework.boot.cli.command.tester.Failure> failures =
new ArrayList<Failure>()
for (org.junit.runner.notification.Failure failure : results.getFailures()) {
failures.add(new Failure(failure.getDescription().toString(), failure.getTrace()))
}
testResults.setFailures(failures.toArray(new Failure[0]))
return testResults
}
}

View File

@@ -0,0 +1,62 @@
/*
* 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.
*/
import org.junit.runner.Result
import org.springframework.boot.cli.command.tester.Failure
import org.springframework.boot.cli.command.tester.TestResults
import spock.lang.Specification
import spock.util.EmbeddedSpecRunner
/**
* Groovy script to run Spock tests inside the {@link TestCommand}.
* Needs to be compiled along with the actual code to work properly.
*
* @author Greg Turnquist
*/
class SpockTester extends AbstractTester {
@Override
protected Set<Class<?>> findTestableClasses(List<Class<?>> compiled) {
// Look for classes that implement spock.lang.Specification
Set<Class<?>> testable = new LinkedHashSet<Class<?>>()
for (Class<?> clazz : compiled) {
if (Specification.class.isAssignableFrom(clazz)) {
testable.add(clazz)
}
}
return testable
}
@Override
protected TestResults test(Class<?>[] testable) {
Result results = new EmbeddedSpecRunner().runClasses(Arrays.asList(testable))
TestResults testResults = new TestResults()
testResults.setFailureCount(results.getFailureCount())
testResults.setRunCount(results.getRunCount())
List<org.springframework.boot.cli.command.tester.Failure> failures =
new ArrayList<Failure>()
for (org.junit.runner.notification.Failure failure : results.getFailures()) {
failures.add(new Failure(failure.getDescription().toString(), failure.getTrace()))
}
testResults.setFailures(failures.toArray(new Failure[0]))
return testResults
}
}

View File

@@ -0,0 +1,41 @@
/*
* 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.
*/
import org.springframework.boot.cli.command.tester.TestResults
/**
* Groovy script define abstract basis for automated testers for {@link TestCommand}.
* Needs to be compiled along with the actual code to work properly.
*
* @author Greg Turnquist
*/
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<?>[0]))
}
abstract protected Set<Class<?>> findTestableClasses(List<Class<?>> compiled)
abstract protected TestResults test(Class<?>[] testable)
}