Run Gradle integration tests against multiple versions of Gradle

Closes gh-9365
pull/9343/merge
Andy Wilkinson 8 years ago
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() + "]";
}
}
}

@ -30,7 +30,9 @@ import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
import org.gradle.testkit.runner.TaskOutcome; import org.gradle.testkit.runner.TaskOutcome;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.testkit.GradleBuild; import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -40,10 +42,11 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@RunWith(GradleCompatibilitySuite.class)
public class ApplicationPluginActionIntegrationTests { public class ApplicationPluginActionIntegrationTests {
@Rule @Rule
public GradleBuild gradleBuild = new GradleBuild(); public GradleBuild gradleBuild;
@Test @Test
public void noBootDistributionWithoutApplicationPluginApplied() { public void noBootDistributionWithoutApplicationPluginApplied() {

@ -23,7 +23,9 @@ import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.TaskOutcome; import org.gradle.testkit.runner.TaskOutcome;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.testkit.GradleBuild; import org.springframework.boot.gradle.testkit.GradleBuild;
import org.springframework.util.FileSystemUtils; import org.springframework.util.FileSystemUtils;
@ -35,10 +37,11 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@RunWith(GradleCompatibilitySuite.class)
public class DependencyManagementPluginActionIntegrationTests { public class DependencyManagementPluginActionIntegrationTests {
@Rule @Rule
public GradleBuild gradleBuild = new GradleBuild(); public GradleBuild gradleBuild;
@Test @Test
public void noDependencyManagementIsAppliedByDefault() { public void noDependencyManagementIsAppliedByDefault() {

@ -22,7 +22,9 @@ import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.TaskOutcome; import org.gradle.testkit.runner.TaskOutcome;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.testkit.GradleBuild; import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -32,10 +34,11 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@RunWith(GradleCompatibilitySuite.class)
public class JavaPluginActionIntegrationTests { public class JavaPluginActionIntegrationTests {
@Rule @Rule
public GradleBuild gradleBuild = new GradleBuild(); public GradleBuild gradleBuild;
@Test @Test
public void noBootJarTaskWithoutJavaPluginApplied() { public void noBootJarTaskWithoutJavaPluginApplied() {

@ -18,7 +18,9 @@ package org.springframework.boot.gradle.plugin;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.testkit.GradleBuild; import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -28,10 +30,11 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@RunWith(GradleCompatibilitySuite.class)
public class MavenPluginActionIntegrationTests { public class MavenPluginActionIntegrationTests {
@Rule @Rule
public GradleBuild gradleBuild = new GradleBuild(); public GradleBuild gradleBuild;
@Test @Test
public void clearsConf2ScopeMappingsOfUploadBootArchivesTask() { public void clearsConf2ScopeMappingsOfUploadBootArchivesTask() {

@ -43,7 +43,7 @@ public class SpringBootPluginIntegrationTests {
@Test @Test
public void succeedWithVersionOfGradleHigherThanRequired() { public void succeedWithVersionOfGradleHigherThanRequired() {
this.gradleBuild.gradleVersion("3.5-rc-3").build(); this.gradleBuild.gradleVersion("3.5").build();
} }
@Test @Test

@ -22,7 +22,9 @@ import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.TaskOutcome; import org.gradle.testkit.runner.TaskOutcome;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.testkit.GradleBuild; import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -32,10 +34,11 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@RunWith(GradleCompatibilitySuite.class)
public class WarPluginActionIntegrationTests { public class WarPluginActionIntegrationTests {
@Rule @Rule
public GradleBuild gradleBuild = new GradleBuild(); public GradleBuild gradleBuild;
@Test @Test
public void noBootWarTaskWithoutWarPluginApplied() { public void noBootWarTaskWithoutWarPluginApplied() {

@ -24,7 +24,9 @@ import java.util.Properties;
import org.gradle.testkit.runner.TaskOutcome; import org.gradle.testkit.runner.TaskOutcome;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.testkit.GradleBuild; import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -34,10 +36,11 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@RunWith(GradleCompatibilitySuite.class)
public class BuildInfoIntegrationTests { public class BuildInfoIntegrationTests {
@Rule @Rule
public final GradleBuild gradleBuild = new GradleBuild(); public GradleBuild gradleBuild;
@Test @Test
public void defaultValues() { public void defaultValues() {

@ -25,7 +25,9 @@ import org.gradle.testkit.runner.TaskOutcome;
import org.gradle.testkit.runner.UnexpectedBuildFailure; import org.gradle.testkit.runner.UnexpectedBuildFailure;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.testkit.GradleBuild; import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -35,10 +37,11 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@RunWith(GradleCompatibilitySuite.class)
public abstract class AbstractBootArchiveIntegrationTests { public abstract class AbstractBootArchiveIntegrationTests {
@Rule @Rule
public final GradleBuild gradleBuild = new GradleBuild(); public GradleBuild gradleBuild;
private final String taskName; private final String taskName;

@ -24,7 +24,9 @@ import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.TaskOutcome; import org.gradle.testkit.runner.TaskOutcome;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.testkit.GradleBuild; import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -34,10 +36,11 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@RunWith(GradleCompatibilitySuite.class)
public class MavenIntegrationTests { public class MavenIntegrationTests {
@Rule @Rule
public final GradleBuild gradleBuild = new GradleBuild(); public GradleBuild gradleBuild;
@Test @Test
public void bootJarCanBeUploaded() throws FileNotFoundException, IOException { public void bootJarCanBeUploaded() throws FileNotFoundException, IOException {

@ -24,7 +24,9 @@ import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.TaskOutcome; import org.gradle.testkit.runner.TaskOutcome;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.testkit.GradleBuild; import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -35,10 +37,11 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@RunWith(GradleCompatibilitySuite.class)
public class MavenPublishingIntegrationTests { public class MavenPublishingIntegrationTests {
@Rule @Rule
public final GradleBuild gradleBuild = new GradleBuild(); public GradleBuild gradleBuild;
@Test @Test
public void bootJarCanBePublished() throws FileNotFoundException, IOException { public void bootJarCanBePublished() throws FileNotFoundException, IOException {

@ -23,7 +23,9 @@ import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.TaskOutcome; import org.gradle.testkit.runner.TaskOutcome;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.testkit.GradleBuild; import org.springframework.boot.gradle.testkit.GradleBuild;
import org.springframework.util.FileSystemUtils; import org.springframework.util.FileSystemUtils;
@ -34,10 +36,11 @@ import static org.assertj.core.api.Assertions.assertThat;
* *
* @author Andy Wilkinson * @author Andy Wilkinson
*/ */
@RunWith(GradleCompatibilitySuite.class)
public class BootRunIntegrationTests { public class BootRunIntegrationTests {
@Rule @Rule
public final GradleBuild gradleBuild = new GradleBuild(); public GradleBuild gradleBuild;
@Test @Test
public void basicExecution() throws IOException { public void basicExecution() throws IOException {
@ -48,7 +51,7 @@ public class BootRunIntegrationTests {
new File(this.gradleBuild.getProjectDir(), "src/main/resources").mkdirs(); new File(this.gradleBuild.getProjectDir(), "src/main/resources").mkdirs();
BuildResult result = this.gradleBuild.build("bootRun"); BuildResult result = this.gradleBuild.build("bootRun");
assertThat(result.task(":bootRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); assertThat(result.task(":bootRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("1. " + urlOf("build/classes/main")); assertThat(result.getOutput()).contains("1. " + urlOf(mainJavaOutput()));
assertThat(result.getOutput()).contains("2. " + urlOf("build/resources/main")); assertThat(result.getOutput()).contains("2. " + urlOf("build/resources/main"));
assertThat(result.getOutput()).doesNotContain(urlOf("src/main/resources")); assertThat(result.getOutput()).doesNotContain(urlOf("src/main/resources"));
} }
@ -62,7 +65,7 @@ public class BootRunIntegrationTests {
BuildResult result = this.gradleBuild.build("bootRun"); BuildResult result = this.gradleBuild.build("bootRun");
assertThat(result.task(":bootRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); assertThat(result.task(":bootRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("1. " + urlOf("src/main/resources")); assertThat(result.getOutput()).contains("1. " + urlOf("src/main/resources"));
assertThat(result.getOutput()).contains("2. " + urlOf("build/classes/main")); assertThat(result.getOutput()).contains("2. " + urlOf(mainJavaOutput()));
assertThat(result.getOutput()).doesNotContain(urlOf("build/resources/main")); assertThat(result.getOutput()).doesNotContain(urlOf("build/resources/main"));
} }
@ -84,6 +87,13 @@ public class BootRunIntegrationTests {
.contains("JVM arguments = [-Dcom.foo=bar, -Dcom.bar=baz]"); .contains("JVM arguments = [-Dcom.foo=bar, -Dcom.bar=baz]");
} }
private String mainJavaOutput() {
String gradleVersion = this.gradleBuild.getGradleVersion();
return "build/classes/"
+ (gradleVersion != null && gradleVersion.startsWith("4.") ? "java/" : "")
+ "main";
}
private String urlOf(String path) throws IOException { private String urlOf(String path) throws IOException {
return new File(this.gradleBuild.getProjectDir().getCanonicalFile(), path).toURI() return new File(this.gradleBuild.getProjectDir().getCanonicalFile(), path).toURI()
.toURL().toString(); .toURL().toString();

@ -88,9 +88,13 @@ public class GradleBuild implements TestRule {
} }
private URL getScriptForTestMethod(Description description) { private URL getScriptForTestMethod(Description description) {
return description.getTestClass() String name = description.getTestClass().getSimpleName() + "-"
.getResource(description.getTestClass().getSimpleName() + "-" + removeGradleVersion(description.getMethodName()) + ".gradle";
+ description.getMethodName() + ".gradle"); return description.getTestClass().getResource(name);
}
private String removeGradleVersion(String methodName) {
return methodName.replaceAll("\\[Gradle .+\\]", "").trim();
} }
private URL getScriptForTestClass(Class<?> testClass) { private URL getScriptForTestClass(Class<?> testClass) {
@ -157,6 +161,7 @@ public class GradleBuild implements TestRule {
List<String> allArguments = new ArrayList<String>(); List<String> allArguments = new ArrayList<String>();
allArguments.add("-PpluginClasspath=" + pluginClasspath()); allArguments.add("-PpluginClasspath=" + pluginClasspath());
allArguments.add("-PbootVersion=" + getBootVersion()); allArguments.add("-PbootVersion=" + getBootVersion());
allArguments.add("--stacktrace");
allArguments.addAll(Arrays.asList(arguments)); allArguments.addAll(Arrays.asList(arguments));
return gradleRunner.withArguments(allArguments); return gradleRunner.withArguments(allArguments);
} }
@ -174,6 +179,10 @@ public class GradleBuild implements TestRule {
return this; return this;
} }
public String getGradleVersion() {
return this.gradleVersion;
}
private static String getBootVersion() { private static String getBootVersion() {
return evaluateExpression( return evaluateExpression(
"/*[local-name()='project']/*[local-name()='parent']/*[local-name()='version']" "/*[local-name()='project']/*[local-name()='parent']/*[local-name()='version']"

Loading…
Cancel
Save