diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/junit/GradleCompatibilitySuite.java b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/junit/GradleCompatibilitySuite.java new file mode 100644 index 0000000000..b0fde7cd17 --- /dev/null +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/junit/GradleCompatibilitySuite.java @@ -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 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 createRunners(Class clazz) throws InitializationError { + List 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() + "]"; + } + + } + +} diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests.java b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests.java index 9346dd10ee..dccde4f345 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/ApplicationPluginActionIntegrationTests.java @@ -30,7 +30,9 @@ import org.apache.commons.compress.archivers.tar.TarArchiveInputStream; import org.gradle.testkit.runner.TaskOutcome; import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.gradle.junit.GradleCompatibilitySuite; import org.springframework.boot.gradle.testkit.GradleBuild; import static org.assertj.core.api.Assertions.assertThat; @@ -40,10 +42,11 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Andy Wilkinson */ +@RunWith(GradleCompatibilitySuite.class) public class ApplicationPluginActionIntegrationTests { @Rule - public GradleBuild gradleBuild = new GradleBuild(); + public GradleBuild gradleBuild; @Test public void noBootDistributionWithoutApplicationPluginApplied() { diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/DependencyManagementPluginActionIntegrationTests.java b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/DependencyManagementPluginActionIntegrationTests.java index b22c0a718d..5f65094e40 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/DependencyManagementPluginActionIntegrationTests.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/DependencyManagementPluginActionIntegrationTests.java @@ -23,7 +23,9 @@ import org.gradle.testkit.runner.BuildResult; import org.gradle.testkit.runner.TaskOutcome; import org.junit.Rule; 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.util.FileSystemUtils; @@ -35,10 +37,11 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Andy Wilkinson */ +@RunWith(GradleCompatibilitySuite.class) public class DependencyManagementPluginActionIntegrationTests { @Rule - public GradleBuild gradleBuild = new GradleBuild(); + public GradleBuild gradleBuild; @Test public void noDependencyManagementIsAppliedByDefault() { diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/JavaPluginActionIntegrationTests.java b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/JavaPluginActionIntegrationTests.java index d4ab90408a..7591d7f8e4 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/JavaPluginActionIntegrationTests.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/JavaPluginActionIntegrationTests.java @@ -22,7 +22,9 @@ import org.gradle.testkit.runner.BuildResult; import org.gradle.testkit.runner.TaskOutcome; import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.gradle.junit.GradleCompatibilitySuite; import org.springframework.boot.gradle.testkit.GradleBuild; import static org.assertj.core.api.Assertions.assertThat; @@ -32,10 +34,11 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Andy Wilkinson */ +@RunWith(GradleCompatibilitySuite.class) public class JavaPluginActionIntegrationTests { @Rule - public GradleBuild gradleBuild = new GradleBuild(); + public GradleBuild gradleBuild; @Test public void noBootJarTaskWithoutJavaPluginApplied() { diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/MavenPluginActionIntegrationTests.java b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/MavenPluginActionIntegrationTests.java index efc756eb55..fa053c71ce 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/MavenPluginActionIntegrationTests.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/MavenPluginActionIntegrationTests.java @@ -18,7 +18,9 @@ package org.springframework.boot.gradle.plugin; import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.gradle.junit.GradleCompatibilitySuite; import org.springframework.boot.gradle.testkit.GradleBuild; import static org.assertj.core.api.Assertions.assertThat; @@ -28,10 +30,11 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Andy Wilkinson */ +@RunWith(GradleCompatibilitySuite.class) public class MavenPluginActionIntegrationTests { @Rule - public GradleBuild gradleBuild = new GradleBuild(); + public GradleBuild gradleBuild; @Test public void clearsConf2ScopeMappingsOfUploadBootArchivesTask() { diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/SpringBootPluginIntegrationTests.java b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/SpringBootPluginIntegrationTests.java index 57e673c15d..0fff853803 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/SpringBootPluginIntegrationTests.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/SpringBootPluginIntegrationTests.java @@ -43,7 +43,7 @@ public class SpringBootPluginIntegrationTests { @Test public void succeedWithVersionOfGradleHigherThanRequired() { - this.gradleBuild.gradleVersion("3.5-rc-3").build(); + this.gradleBuild.gradleVersion("3.5").build(); } @Test diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/WarPluginActionIntegrationTests.java b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/WarPluginActionIntegrationTests.java index bbde1264cd..84aac096a2 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/WarPluginActionIntegrationTests.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/plugin/WarPluginActionIntegrationTests.java @@ -22,7 +22,9 @@ import org.gradle.testkit.runner.BuildResult; import org.gradle.testkit.runner.TaskOutcome; import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.gradle.junit.GradleCompatibilitySuite; import org.springframework.boot.gradle.testkit.GradleBuild; import static org.assertj.core.api.Assertions.assertThat; @@ -32,10 +34,11 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Andy Wilkinson */ +@RunWith(GradleCompatibilitySuite.class) public class WarPluginActionIntegrationTests { @Rule - public GradleBuild gradleBuild = new GradleBuild(); + public GradleBuild gradleBuild; @Test public void noBootWarTaskWithoutWarPluginApplied() { diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.java b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.java index fac2498efa..2e0a8eb6fb 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/buildinfo/BuildInfoIntegrationTests.java @@ -24,7 +24,9 @@ import java.util.Properties; import org.gradle.testkit.runner.TaskOutcome; import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.gradle.junit.GradleCompatibilitySuite; import org.springframework.boot.gradle.testkit.GradleBuild; import static org.assertj.core.api.Assertions.assertThat; @@ -34,10 +36,11 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Andy Wilkinson */ +@RunWith(GradleCompatibilitySuite.class) public class BuildInfoIntegrationTests { @Rule - public final GradleBuild gradleBuild = new GradleBuild(); + public GradleBuild gradleBuild; @Test public void defaultValues() { diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveIntegrationTests.java b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveIntegrationTests.java index 72d835d9af..13d85d4744 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveIntegrationTests.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/AbstractBootArchiveIntegrationTests.java @@ -25,7 +25,9 @@ import org.gradle.testkit.runner.TaskOutcome; import org.gradle.testkit.runner.UnexpectedBuildFailure; import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.gradle.junit.GradleCompatibilitySuite; import org.springframework.boot.gradle.testkit.GradleBuild; import static org.assertj.core.api.Assertions.assertThat; @@ -35,10 +37,11 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Andy Wilkinson */ +@RunWith(GradleCompatibilitySuite.class) public abstract class AbstractBootArchiveIntegrationTests { @Rule - public final GradleBuild gradleBuild = new GradleBuild(); + public GradleBuild gradleBuild; private final String taskName; diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/MavenIntegrationTests.java b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/MavenIntegrationTests.java index 5b1ac3088f..a507593c7f 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/MavenIntegrationTests.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/MavenIntegrationTests.java @@ -24,7 +24,9 @@ import org.gradle.testkit.runner.BuildResult; import org.gradle.testkit.runner.TaskOutcome; import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.gradle.junit.GradleCompatibilitySuite; import org.springframework.boot.gradle.testkit.GradleBuild; import static org.assertj.core.api.Assertions.assertThat; @@ -34,10 +36,11 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Andy Wilkinson */ +@RunWith(GradleCompatibilitySuite.class) public class MavenIntegrationTests { @Rule - public final GradleBuild gradleBuild = new GradleBuild(); + public GradleBuild gradleBuild; @Test public void bootJarCanBeUploaded() throws FileNotFoundException, IOException { diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/MavenPublishingIntegrationTests.java b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/MavenPublishingIntegrationTests.java index a2dce669f9..2c604ffdb6 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/MavenPublishingIntegrationTests.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/bundling/MavenPublishingIntegrationTests.java @@ -24,7 +24,9 @@ import org.gradle.testkit.runner.BuildResult; import org.gradle.testkit.runner.TaskOutcome; import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.gradle.junit.GradleCompatibilitySuite; import org.springframework.boot.gradle.testkit.GradleBuild; import static org.assertj.core.api.Assertions.assertThat; @@ -35,10 +37,11 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Andy Wilkinson */ +@RunWith(GradleCompatibilitySuite.class) public class MavenPublishingIntegrationTests { @Rule - public final GradleBuild gradleBuild = new GradleBuild(); + public GradleBuild gradleBuild; @Test public void bootJarCanBePublished() throws FileNotFoundException, IOException { diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/run/BootRunIntegrationTests.java b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/run/BootRunIntegrationTests.java index a22a9db72a..617ac378a7 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/run/BootRunIntegrationTests.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/tasks/run/BootRunIntegrationTests.java @@ -23,7 +23,9 @@ import org.gradle.testkit.runner.BuildResult; import org.gradle.testkit.runner.TaskOutcome; import org.junit.Rule; 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.util.FileSystemUtils; @@ -34,10 +36,11 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Andy Wilkinson */ +@RunWith(GradleCompatibilitySuite.class) public class BootRunIntegrationTests { @Rule - public final GradleBuild gradleBuild = new GradleBuild(); + public GradleBuild gradleBuild; @Test public void basicExecution() throws IOException { @@ -48,7 +51,7 @@ public class BootRunIntegrationTests { new File(this.gradleBuild.getProjectDir(), "src/main/resources").mkdirs(); BuildResult result = this.gradleBuild.build("bootRun"); 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()).doesNotContain(urlOf("src/main/resources")); } @@ -62,7 +65,7 @@ public class BootRunIntegrationTests { BuildResult result = this.gradleBuild.build("bootRun"); assertThat(result.task(":bootRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS); 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")); } @@ -84,6 +87,13 @@ public class BootRunIntegrationTests { .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 { return new File(this.gradleBuild.getProjectDir().getCanonicalFile(), path).toURI() .toURL().toString(); diff --git a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/GradleBuild.java b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/GradleBuild.java index dad62321a4..fbf71feb09 100644 --- a/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/GradleBuild.java +++ b/spring-boot-tools/spring-boot-gradle-plugin/src/test/java/org/springframework/boot/gradle/testkit/GradleBuild.java @@ -88,9 +88,13 @@ public class GradleBuild implements TestRule { } private URL getScriptForTestMethod(Description description) { - return description.getTestClass() - .getResource(description.getTestClass().getSimpleName() + "-" - + description.getMethodName() + ".gradle"); + String name = description.getTestClass().getSimpleName() + "-" + + removeGradleVersion(description.getMethodName()) + ".gradle"; + return description.getTestClass().getResource(name); + } + + private String removeGradleVersion(String methodName) { + return methodName.replaceAll("\\[Gradle .+\\]", "").trim(); } private URL getScriptForTestClass(Class testClass) { @@ -157,6 +161,7 @@ public class GradleBuild implements TestRule { List allArguments = new ArrayList(); allArguments.add("-PpluginClasspath=" + pluginClasspath()); allArguments.add("-PbootVersion=" + getBootVersion()); + allArguments.add("--stacktrace"); allArguments.addAll(Arrays.asList(arguments)); return gradleRunner.withArguments(allArguments); } @@ -174,6 +179,10 @@ public class GradleBuild implements TestRule { return this; } + public String getGradleVersion() { + return this.gradleVersion; + } + private static String getBootVersion() { return evaluateExpression( "/*[local-name()='project']/*[local-name()='parent']/*[local-name()='version']"