Run Gradle integration tests against multiple versions of Gradle
Closes gh-9365pull/9343/merge
parent
5f3462f358
commit
a685d9ec92
@ -0,0 +1,94 @@
|
||||
/*
|
||||
* Copyright 2012-2017 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.gradle.junit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.gradle.api.Rule;
|
||||
import org.junit.runner.Runner;
|
||||
import org.junit.runners.BlockJUnit4ClassRunner;
|
||||
import org.junit.runners.Suite;
|
||||
import org.junit.runners.model.FrameworkMethod;
|
||||
import org.junit.runners.model.InitializationError;
|
||||
|
||||
import org.springframework.boot.gradle.testkit.GradleBuild;
|
||||
|
||||
/**
|
||||
* Custom {@link Suite} that runs tests against multiple version of Gradle. Test classes
|
||||
* using the suite must have a public {@link GradleBuild} field named {@code gradleBuild}
|
||||
* and annotated with {@link Rule}.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public final class GradleCompatibilitySuite extends Suite {
|
||||
|
||||
private static final List<String> GRADLE_VERSIONS = Arrays.asList("default", "3.4.1",
|
||||
"3.5", "4.0-rc-1");
|
||||
|
||||
public GradleCompatibilitySuite(Class<?> clazz) throws InitializationError {
|
||||
super(clazz, createRunners(clazz));
|
||||
}
|
||||
|
||||
private static List<Runner> createRunners(Class<?> clazz) throws InitializationError {
|
||||
List<Runner> runners = new ArrayList<>();
|
||||
for (String version : GRADLE_VERSIONS) {
|
||||
runners.add(new GradleCompatibilityClassRunner(clazz, version));
|
||||
}
|
||||
return runners;
|
||||
}
|
||||
|
||||
private static final class GradleCompatibilityClassRunner
|
||||
extends BlockJUnit4ClassRunner {
|
||||
|
||||
private final String gradleVersion;
|
||||
|
||||
private GradleCompatibilityClassRunner(Class<?> klass, String gradleVersion)
|
||||
throws InitializationError {
|
||||
super(klass);
|
||||
this.gradleVersion = gradleVersion;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object createTest() throws Exception {
|
||||
Object test = super.createTest();
|
||||
configureTest(test);
|
||||
return test;
|
||||
}
|
||||
|
||||
private void configureTest(Object test) throws Exception {
|
||||
GradleBuild gradleBuild = new GradleBuild();
|
||||
if (!"default".equals(this.gradleVersion)) {
|
||||
gradleBuild = gradleBuild.gradleVersion(this.gradleVersion);
|
||||
}
|
||||
test.getClass().getField("gradleBuild").set(test, gradleBuild);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getName() {
|
||||
return "Gradle " + this.gradleVersion;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String testName(FrameworkMethod method) {
|
||||
return method.getName() + " [" + getName() + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue