Migrate spring-boot-gradle-plugin's tests to JUnit 5

Closes gh-16959
This commit is contained in:
Andy Wilkinson 2019-05-24 20:16:36 +01:00
parent 5e62faf720
commit babb94c791
35 changed files with 571 additions and 542 deletions

View File

@ -36,6 +36,7 @@ jar {
}
test {
useJUnitPlatform()
testLogging {
events "passed", "skipped", "failed"
}

View File

@ -84,8 +84,8 @@ public class SpringBootPlugin implements Plugin<Project> {
}
private void verifyGradleVersion() {
if (GradleVersion.current().compareTo(GradleVersion.version("4.4")) < 0) {
throw new GradleException("Spring Boot plugin requires Gradle 4.4 or later."
if (GradleVersion.current().compareTo(GradleVersion.version("4.10")) < 0) {
throw new GradleException("Spring Boot plugin requires Gradle 4.10 or later."
+ " The current version is " + GradleVersion.current());
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@ -16,11 +16,10 @@
package org.springframework.boot.gradle.docs;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleMultiDslSuite;
import org.springframework.boot.gradle.junit.GradleMultiDslExtension;
import org.springframework.boot.gradle.testkit.GradleBuild;
/**
@ -29,16 +28,15 @@ import org.springframework.boot.gradle.testkit.GradleBuild;
* @author Andy Wilkinson
* @author Jean-Baptiste Nizet
*/
@RunWith(GradleMultiDslSuite.class)
@ExtendWith(GradleMultiDslExtension.class)
public class GettingStartedDocumentationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
// NOTE: We can't run any `apply-plugin` tests because during a release the
// jar won't be there
@Test
@TestTemplate
public void typicalPluginsAppliesExceptedPlugins() {
this.gradleBuild.script("src/main/gradle/getting-started/typical-plugins")
.build("verify");

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@ -21,11 +21,10 @@ import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleMultiDslSuite;
import org.springframework.boot.gradle.junit.GradleMultiDslExtension;
import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat;
@ -36,13 +35,12 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Andy Wilkinson
* @author Jean-Baptiste Nizet
*/
@RunWith(GradleMultiDslSuite.class)
@ExtendWith(GradleMultiDslExtension.class)
public class IntegratingWithActuatorDocumentationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
@Test
@TestTemplate
public void basicBuildInfo() throws IOException {
this.gradleBuild
.script("src/main/gradle/integrating-with-actuator/build-info-basic")
@ -51,7 +49,7 @@ public class IntegratingWithActuatorDocumentationTests {
"build/resources/main/META-INF/build-info.properties")).isFile();
}
@Test
@TestTemplate
public void buildInfoCustomValues() throws IOException {
this.gradleBuild.script(
"src/main/gradle/integrating-with-actuator/build-info-custom-values")
@ -66,7 +64,7 @@ public class IntegratingWithActuatorDocumentationTests {
assertThat(properties).containsEntry("build.name", "Example application");
}
@Test
@TestTemplate
public void buildInfoAdditional() throws IOException {
this.gradleBuild
.script("src/main/gradle/integrating-with-actuator/build-info-additional")

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@ -17,11 +17,10 @@
package org.springframework.boot.gradle.docs;
import org.junit.Assume;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleMultiDslSuite;
import org.springframework.boot.gradle.junit.GradleMultiDslExtension;
import org.springframework.boot.gradle.testkit.Dsl;
import org.springframework.boot.gradle.testkit.GradleBuild;
@ -33,26 +32,25 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Andy Wilkinson
* @author Jean-Baptiste Nizet
*/
@RunWith(GradleMultiDslSuite.class)
@ExtendWith(GradleMultiDslExtension.class)
public class ManagingDependenciesDocumentationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
@Test
@TestTemplate
public void dependenciesExampleEvaluatesSuccessfully() {
this.gradleBuild.script("src/main/gradle/managing-dependencies/dependencies")
.build();
}
@Test
@TestTemplate
public void customManagedVersions() {
assertThat(this.gradleBuild
.script("src/main/gradle/managing-dependencies/custom-version")
.build("slf4jVersion").getOutput()).contains("1.7.20");
}
@Test
@TestTemplate
public void dependencyManagementInIsolation() {
assertThat(this.gradleBuild
.script("src/main/gradle/managing-dependencies/configure-bom")
@ -60,7 +58,7 @@ public class ManagingDependenciesDocumentationTests {
.contains("org.springframework.boot:spring-boot-starter ");
}
@Test
@TestTemplate
public void dependencyManagementInIsolationWithPluginsBlock() {
Assume.assumeTrue(this.gradleBuild.getDsl() == Dsl.KOTLIN);
assertThat(this.gradleBuild.script(

View File

@ -27,12 +27,10 @@ import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleMultiDslSuite;
import org.springframework.boot.gradle.testkit.Dsl;
import org.springframework.boot.gradle.junit.GradleMultiDslExtension;
import org.springframework.boot.gradle.testkit.GradleBuild;
import org.springframework.util.FileCopyUtils;
@ -44,21 +42,18 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Andy Wilkinson
* @author Jean-Baptiste Nizet
*/
@RunWith(GradleMultiDslSuite.class)
@ExtendWith(GradleMultiDslExtension.class)
public class PackagingDocumentationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
public Dsl dsl;
@Test
@TestTemplate
public void warContainerDependencyEvaluatesSuccessfully() {
this.gradleBuild.script("src/main/gradle/packaging/war-container-dependency")
.build();
}
@Test
@TestTemplate
public void bootJarMainClass() throws IOException {
this.gradleBuild.script("src/main/gradle/packaging/boot-jar-main-class")
.build("bootJar");
@ -71,7 +66,7 @@ public class PackagingDocumentationTests {
}
}
@Test
@TestTemplate
public void bootJarManifestMainClass() throws IOException {
this.gradleBuild.script("src/main/gradle/packaging/boot-jar-manifest-main-class")
.build("bootJar");
@ -84,7 +79,7 @@ public class PackagingDocumentationTests {
}
}
@Test
@TestTemplate
public void applicationPluginMainClass() throws IOException {
this.gradleBuild.script("src/main/gradle/packaging/application-plugin-main-class")
.build("bootJar");
@ -97,7 +92,7 @@ public class PackagingDocumentationTests {
}
}
@Test
@TestTemplate
public void springBootDslMainClass() throws IOException {
this.gradleBuild.script("src/main/gradle/packaging/spring-boot-dsl-main-class")
.build("bootJar");
@ -110,7 +105,7 @@ public class PackagingDocumentationTests {
}
}
@Test
@TestTemplate
public void bootWarIncludeDevtools() throws IOException {
jarFile(new File(this.gradleBuild.getProjectDir(),
"spring-boot-devtools-1.2.3.RELEASE.jar"));
@ -125,7 +120,7 @@ public class PackagingDocumentationTests {
}
}
@Test
@TestTemplate
public void bootJarRequiresUnpack() throws IOException {
this.gradleBuild.script("src/main/gradle/packaging/boot-jar-requires-unpack")
.build("bootJar");
@ -139,7 +134,7 @@ public class PackagingDocumentationTests {
}
}
@Test
@TestTemplate
public void bootJarIncludeLaunchScript() throws IOException {
this.gradleBuild
.script("src/main/gradle/packaging/boot-jar-include-launch-script")
@ -151,7 +146,7 @@ public class PackagingDocumentationTests {
.startsWith("#!/bin/bash");
}
@Test
@TestTemplate
public void bootJarLaunchScriptProperties() throws IOException {
this.gradleBuild
.script("src/main/gradle/packaging/boot-jar-launch-script-properties")
@ -163,7 +158,7 @@ public class PackagingDocumentationTests {
.contains("example-app.log");
}
@Test
@TestTemplate
public void bootJarCustomLaunchScript() throws IOException {
File customScriptFile = new File(this.gradleBuild.getProjectDir(),
"src/custom.script");
@ -177,7 +172,7 @@ public class PackagingDocumentationTests {
assertThat(FileCopyUtils.copyToString(new FileReader(file))).startsWith("custom");
}
@Test
@TestTemplate
public void bootWarPropertiesLauncher() throws IOException {
this.gradleBuild.script("src/main/gradle/packaging/boot-war-properties-launcher")
.build("bootWar");
@ -190,7 +185,7 @@ public class PackagingDocumentationTests {
}
}
@Test
@TestTemplate
public void bootJarAndJar() {
this.gradleBuild.script("src/main/gradle/packaging/boot-jar-and-jar")
.build("assemble");

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@ -18,11 +18,10 @@ package org.springframework.boot.gradle.docs;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleMultiDslSuite;
import org.springframework.boot.gradle.junit.GradleMultiDslExtension;
import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat;
@ -33,20 +32,19 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Andy Wilkinson
* @author Jean-Baptiste Nizet
*/
@RunWith(GradleMultiDslSuite.class)
@ExtendWith(GradleMultiDslExtension.class)
public class PublishingDocumentationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
@Test
@TestTemplate
public void mavenUpload() throws IOException {
assertThat(this.gradleBuild.script("src/main/gradle/publishing/maven")
.build("deployerRepository").getOutput())
.contains("https://repo.example.com");
}
@Test
@TestTemplate
public void mavenPublish() throws IOException {
assertThat(this.gradleBuild.script("src/main/gradle/publishing/maven-publish")
.build("publishingConfiguration").getOutput())

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@ -19,11 +19,10 @@ package org.springframework.boot.gradle.docs;
import java.io.File;
import java.io.IOException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleMultiDslSuite;
import org.springframework.boot.gradle.junit.GradleMultiDslExtension;
import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat;
@ -34,20 +33,19 @@ import static org.assertj.core.api.Assertions.assertThat;
* @author Andy Wilkinson
* @author Jean-Baptiste Nizet
*/
@RunWith(GradleMultiDslSuite.class)
@ExtendWith(GradleMultiDslExtension.class)
public class RunningDocumentationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
@Test
@TestTemplate
public void bootRunMain() throws IOException {
assertThat(this.gradleBuild.script("src/main/gradle/running/boot-run-main")
.build("configuredMainClass").getOutput())
.contains("com.example.ExampleApplication");
}
@Test
@TestTemplate
public void applicationPluginMainClassName() {
assertThat(this.gradleBuild
.script("src/main/gradle/running/application-plugin-main-class-name")
@ -55,7 +53,7 @@ public class RunningDocumentationTests {
.contains("com.example.ExampleApplication");
}
@Test
@TestTemplate
public void springBootDslMainClassName() throws IOException {
assertThat(this.gradleBuild
.script("src/main/gradle/running/spring-boot-dsl-main-class-name")
@ -63,7 +61,7 @@ public class RunningDocumentationTests {
.contains("com.example.ExampleApplication");
}
@Test
@TestTemplate
public void bootRunSourceResources() throws IOException {
assertThat(this.gradleBuild
.script("src/main/gradle/running/boot-run-source-resources")

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@ -22,11 +22,12 @@ import java.io.IOException;
import java.util.Properties;
import org.gradle.testkit.runner.TaskOutcome;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.tasks.buildinfo.BuildInfo;
import org.springframework.boot.gradle.testkit.GradleBuild;
import org.springframework.boot.gradle.testkit.GradleBuildExtension;
import static org.assertj.core.api.Assertions.assertThat;
@ -36,10 +37,10 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
*/
@ExtendWith(GradleBuildExtension.class)
public class BuildInfoDslIntegrationTests {
@Rule
public final GradleBuild gradleBuild = new GradleBuild();
final GradleBuild gradleBuild = new GradleBuild();
@Test
public void basicJar() throws IOException {

View File

@ -0,0 +1,50 @@
/*
* Copyright 2012-2019 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
*
* https://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.lang.reflect.Field;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.springframework.boot.gradle.testkit.GradleBuild;
import org.springframework.util.ReflectionUtils;
/**
* {@link BeforeEachCallback} to set a test class's {@code gradleBuild} field prior to
* test exection.
*
* @author Andy Wilkinson
*/
final class GradleBuildFieldSetter implements BeforeEachCallback {
private final GradleBuild gradleBuild;
GradleBuildFieldSetter(GradleBuild gradleBuild) {
this.gradleBuild = gradleBuild;
}
@Override
public void beforeEach(ExtensionContext context) throws Exception {
Field field = ReflectionUtils.findField(context.getRequiredTestClass(),
"gradleBuild");
field.setAccessible(true);
field.set(context.getRequiredTestInstance(), this.gradleBuild);
}
}

View File

@ -0,0 +1,82 @@
/*
* Copyright 2012-2019 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
*
* https://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.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
import org.springframework.boot.gradle.testkit.GradleBuild;
import org.springframework.boot.gradle.testkit.GradleBuildExtension;
/**
* {@link Extension} that runs {@link TestTemplate templated tests} against multiple
* versions of Gradle. Test classes using the extension must have non-privaten and
* non-final {@link GradleBuild} field named {@code gradleBuild}.
*
* @author Andy Wilkinson
*/
public final class GradleCompatibilityExtension
implements TestTemplateInvocationContextProvider {
private static final List<String> GRADLE_VERSIONS = Arrays.asList("default", "5.0",
"5.1.1", "5.2.1", "5.3.1", "5.4.1");
@Override
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(
ExtensionContext context) {
return GRADLE_VERSIONS.stream()
.map(GradleVersionTestTemplateInvocationContext::new);
}
@Override
public boolean supportsTestTemplate(ExtensionContext context) {
return true;
}
private static final class GradleVersionTestTemplateInvocationContext
implements TestTemplateInvocationContext {
private final String gradleVersion;
GradleVersionTestTemplateInvocationContext(String gradleVersion) {
this.gradleVersion = gradleVersion;
}
@Override
public String getDisplayName(int invocationIndex) {
return "Gradle " + this.gradleVersion;
}
@Override
public List<Extension> getAdditionalExtensions() {
GradleBuild gradleBuild = new GradleBuild();
if (!this.gradleVersion.equals("default")) {
gradleBuild.gradleVersion(this.gradleVersion);
}
return Arrays.asList(new GradleBuildFieldSetter(gradleBuild),
new GradleBuildExtension());
}
}
}

View File

@ -1,94 +0,0 @@
/*
* Copyright 2012-2019 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
*
* https://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 versions 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", "5.0",
"5.1.1", "5.2.1", "5.3.1", "5.4.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() + "]";
}
}
}

View File

@ -0,0 +1,74 @@
/*
* Copyright 2012-2019 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
*
* https://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.Arrays;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.extension.Extension;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContext;
import org.junit.jupiter.api.extension.TestTemplateInvocationContextProvider;
import org.springframework.boot.gradle.testkit.Dsl;
import org.springframework.boot.gradle.testkit.GradleBuild;
import org.springframework.boot.gradle.testkit.GradleBuildExtension;
/**
* {@link Extension} that runs {@link TestTemplate templated tests} against the Groovy and
* Kotlin DSLs. Test classes using the extension most have a non-private non-final
* {@link GradleBuild} field named {@code gradleBuild}.
*
* @author Andy Wilkinson
*/
public class GradleMultiDslExtension implements TestTemplateInvocationContextProvider {
@Override
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(
ExtensionContext context) {
return Stream.of(Dsl.values()).map(DslTestTemplateInvocationContext::new);
}
@Override
public boolean supportsTestTemplate(ExtensionContext context) {
return true;
}
private static final class DslTestTemplateInvocationContext
implements TestTemplateInvocationContext {
private final Dsl dsl;
DslTestTemplateInvocationContext(Dsl dsl) {
this.dsl = dsl;
}
@Override
public List<Extension> getAdditionalExtensions() {
return Arrays.asList(new GradleBuildFieldSetter(new GradleBuild(this.dsl)),
new GradleBuildExtension());
}
@Override
public String getDisplayName(int invocationIndex) {
return this.dsl.getName();
}
}
}

View File

@ -1,86 +0,0 @@
/*
* Copyright 2012-2018 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
*
* https://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.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.Dsl;
import org.springframework.boot.gradle.testkit.GradleBuild;
/**
* Custom {@link Suite} that runs tests against the Groovy and the Kotlin DSLs. Test
* classes using the suite must have a public {@link GradleBuild} field named
* {@code gradleBuild} and annotated with {@link Rule}.
*
* @author Jean-Baptiste Nizet
* @author Andy Wilkinson
*/
public final class GradleMultiDslSuite extends Suite {
public GradleMultiDslSuite(Class<?> clazz) throws InitializationError {
super(clazz, createRunners(clazz));
}
private static List<Runner> createRunners(Class<?> clazz) throws InitializationError {
List<Runner> runners = new ArrayList<>();
runners.add(new GradleDslClassRunner(clazz, new GradleBuild(Dsl.GROOVY)));
runners.add(new GradleDslClassRunner(clazz, new GradleBuild(Dsl.KOTLIN)));
return runners;
}
private static final class GradleDslClassRunner extends BlockJUnit4ClassRunner {
private final GradleBuild gradleBuild;
private GradleDslClassRunner(Class<?> klass, GradleBuild gradleBuild)
throws InitializationError {
super(klass);
this.gradleBuild = gradleBuild;
}
@Override
protected Object createTest() throws Exception {
Object test = super.createTest();
configureTest(test);
return test;
}
private void configureTest(Object test) throws Exception {
test.getClass().getField("gradleBuild").set(test, this.gradleBuild);
}
@Override
protected String getName() {
return this.gradleBuild.getDsl().getName() + " DSL";
}
@Override
protected String testName(FrameworkMethod method) {
return method.getName() + " [" + getName() + "]";
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@ -29,11 +29,10 @@ import java.util.zip.ZipFile;
import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
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.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.junit.GradleCompatibilityExtension;
import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat;
@ -43,38 +42,37 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
*/
@RunWith(GradleCompatibilitySuite.class)
@ExtendWith(GradleCompatibilityExtension.class)
public class ApplicationPluginActionIntegrationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
@Test
@TestTemplate
public void noBootDistributionWithoutApplicationPluginApplied() {
assertThat(this.gradleBuild.build("distributionExists", "-PdistributionName=boot")
.getOutput()).contains("boot exists = false");
}
@Test
@TestTemplate
public void applyingApplicationPluginCreatesBootDistribution() {
assertThat(this.gradleBuild.build("distributionExists", "-PdistributionName=boot",
"-PapplyApplicationPlugin").getOutput()).contains("boot exists = true");
}
@Test
@TestTemplate
public void noBootStartScriptsTaskWithoutApplicationPluginApplied() {
assertThat(this.gradleBuild.build("taskExists", "-PtaskName=bootStartScripts")
.getOutput()).contains("bootStartScripts exists = false");
}
@Test
@TestTemplate
public void applyingApplicationPluginCreatesBootStartScriptsTask() {
assertThat(this.gradleBuild.build("taskExists", "-PtaskName=bootStartScripts",
"-PapplyApplicationPlugin").getOutput())
.contains("bootStartScripts exists = true");
}
@Test
@TestTemplate
public void createsBootStartScriptsTaskUsesApplicationPluginsDefaultJvmOpts() {
assertThat(this.gradleBuild
.build("startScriptsDefaultJvmOpts", "-PapplyApplicationPlugin")
@ -82,7 +80,7 @@ public class ApplicationPluginActionIntegrationTests {
"bootStartScripts defaultJvmOpts = [-Dcom.example.a=alpha, -Dcom.example.b=bravo]");
}
@Test
@TestTemplate
public void zipDistributionForJarCanBeBuilt() throws IOException {
assertThat(
this.gradleBuild.build("bootDistZip").task(":bootDistZip").getOutcome())
@ -97,7 +95,7 @@ public class ApplicationPluginActionIntegrationTests {
name + "-boot/bin/" + name + ".bat");
}
@Test
@TestTemplate
public void tarDistributionForJarCanBeBuilt() throws IOException {
assertThat(
this.gradleBuild.build("bootDistTar").task(":bootDistTar").getOutcome())
@ -112,7 +110,7 @@ public class ApplicationPluginActionIntegrationTests {
name + "-boot/bin/" + name + ".bat");
}
@Test
@TestTemplate
public void zipDistributionForWarCanBeBuilt() throws IOException {
assertThat(
this.gradleBuild.build("bootDistZip").task(":bootDistZip").getOutcome())
@ -127,7 +125,7 @@ public class ApplicationPluginActionIntegrationTests {
name + "-boot/bin/" + name + ".bat");
}
@Test
@TestTemplate
public void tarDistributionForWarCanBeBuilt() throws IOException {
assertThat(
this.gradleBuild.build("bootDistTar").task(":bootDistTar").getOutcome())
@ -142,7 +140,7 @@ public class ApplicationPluginActionIntegrationTests {
name + "-boot/bin/" + name + ".bat");
}
@Test
@TestTemplate
public void applicationNameCanBeUsedToCustomizeDistributionName() throws IOException {
assertThat(
this.gradleBuild.build("bootDistTar").task(":bootDistTar").getOutcome())
@ -157,7 +155,7 @@ public class ApplicationPluginActionIntegrationTests {
"custom-boot/bin/custom.bat");
}
@Test
@TestTemplate
public void scriptsHaveCorrectPermissions() throws IOException {
assertThat(
this.gradleBuild.build("bootDistTar").task(":bootDistTar").getOutcome())

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@ -21,11 +21,10 @@ import java.io.IOException;
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.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.junit.GradleCompatibilityExtension;
import org.springframework.boot.gradle.testkit.GradleBuild;
import org.springframework.util.FileSystemUtils;
@ -37,20 +36,19 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
*/
@RunWith(GradleCompatibilitySuite.class)
@ExtendWith(GradleCompatibilityExtension.class)
public class DependencyManagementPluginActionIntegrationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
@Test
@TestTemplate
public void noDependencyManagementIsAppliedByDefault() {
assertThat(this.gradleBuild.build("doesNotHaveDependencyManagement")
.task(":doesNotHaveDependencyManagement").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
}
@Test
@TestTemplate
public void bomIsImportedWhenDependencyManagementPluginIsApplied() {
assertThat(this.gradleBuild
.build("hasDependencyManagement", "-PapplyDependencyManagementPlugin")
@ -58,7 +56,7 @@ public class DependencyManagementPluginActionIntegrationTests {
.isEqualTo(TaskOutcome.SUCCESS);
}
@Test
@TestTemplate
public void helpfulErrorWhenVersionlessDependencyFailsToResolve() throws IOException {
File examplePackage = new File(this.gradleBuild.getProjectDir(),
"src/main/java/com/example");

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@ -23,11 +23,10 @@ import java.util.jar.JarOutputStream;
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.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.junit.GradleCompatibilityExtension;
import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat;
@ -37,74 +36,73 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
*/
@RunWith(GradleCompatibilitySuite.class)
@ExtendWith(GradleCompatibilityExtension.class)
public class JavaPluginActionIntegrationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
@Test
@TestTemplate
public void noBootJarTaskWithoutJavaPluginApplied() {
assertThat(this.gradleBuild.build("taskExists", "-PtaskName=bootJar").getOutput())
.contains("bootJar exists = false");
}
@Test
@TestTemplate
public void applyingJavaPluginCreatesBootJarTask() {
assertThat(this.gradleBuild
.build("taskExists", "-PtaskName=bootJar", "-PapplyJavaPlugin")
.getOutput()).contains("bootJar exists = true");
}
@Test
@TestTemplate
public void noBootRunTaskWithoutJavaPluginApplied() {
assertThat(this.gradleBuild.build("taskExists", "-PtaskName=bootRun").getOutput())
.contains("bootRun exists = false");
}
@Test
@TestTemplate
public void applyingJavaPluginCreatesBootRunTask() {
assertThat(this.gradleBuild
.build("taskExists", "-PtaskName=bootRun", "-PapplyJavaPlugin")
.getOutput()).contains("bootRun exists = true");
}
@Test
@TestTemplate
public void javaCompileTasksUseUtf8Encoding() {
assertThat(this.gradleBuild.build("javaCompileEncoding", "-PapplyJavaPlugin")
.getOutput()).contains("compileJava = UTF-8")
.contains("compileTestJava = UTF-8");
}
@Test
@TestTemplate
public void javaCompileTasksUseParametersCompilerFlagByDefault() {
assertThat(this.gradleBuild.build("javaCompileTasksCompilerArgs").getOutput())
.contains("compileJava compiler args: [-parameters]")
.contains("compileTestJava compiler args: [-parameters]");
}
@Test
@TestTemplate
public void javaCompileTasksUseParametersAndAdditionalCompilerFlags() {
assertThat(this.gradleBuild.build("javaCompileTasksCompilerArgs").getOutput())
.contains("compileJava compiler args: [-parameters, -Xlint:all]")
.contains("compileTestJava compiler args: [-parameters, -Xlint:all]");
}
@Test
@TestTemplate
public void javaCompileTasksCanOverrideDefaultParametersCompilerFlag() {
assertThat(this.gradleBuild.build("javaCompileTasksCompilerArgs").getOutput())
.contains("compileJava compiler args: [-Xlint:all]")
.contains("compileTestJava compiler args: [-Xlint:all]");
}
@Test
@TestTemplate
public void assembleRunsBootJarAndJarIsSkipped() {
BuildResult result = this.gradleBuild.build("assemble");
assertThat(result.task(":bootJar").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.task(":jar").getOutcome()).isEqualTo(TaskOutcome.SKIPPED);
}
@Test
@TestTemplate
public void errorMessageIsHelpfulWhenMainClassCannotBeResolved() {
BuildResult result = this.gradleBuild.buildAndFail("build", "-PapplyJavaPlugin");
assertThat(result.task(":bootJar").getOutcome()).isEqualTo(TaskOutcome.FAILED);
@ -112,7 +110,7 @@ public class JavaPluginActionIntegrationTests {
"Main class name has not been configured and it could not be resolved");
}
@Test
@TestTemplate
public void jarAndBootJarCanBothBeBuilt() {
BuildResult result = this.gradleBuild.build("assemble");
assertThat(result.task(":bootJar").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
@ -124,7 +122,7 @@ public class JavaPluginActionIntegrationTests {
this.gradleBuild.getProjectDir().getName() + "-boot.jar"));
}
@Test
@TestTemplate
public void additionalMetadataLocationsConfiguredWhenProcessorIsPresent()
throws IOException {
createMinimalMainSource();
@ -142,7 +140,7 @@ public class JavaPluginActionIntegrationTests {
.getCanonicalPath());
}
@Test
@TestTemplate
public void additionalMetadataLocationsNotConfiguredWhenProcessorIsAbsent()
throws IOException {
createMinimalMainSource();

View File

@ -16,11 +16,10 @@
package org.springframework.boot.gradle.plugin;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.junit.GradleCompatibilityExtension;
import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat;
@ -30,33 +29,32 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
*/
@RunWith(GradleCompatibilitySuite.class)
@ExtendWith(GradleCompatibilityExtension.class)
public class KotlinPluginActionIntegrationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
@Test
@TestTemplate
public void noKotlinVersionPropertyWithoutKotlinPlugin() {
assertThat(this.gradleBuild.build("kotlinVersion").getOutput())
.contains("Kotlin version: none");
}
@Test
@TestTemplate
public void kotlinVersionPropertyIsSet() {
String output = this.gradleBuild.build("kotlinVersion", "dependencies",
"--configuration", "compileClasspath").getOutput();
assertThat(output).containsPattern("Kotlin version: [0-9]\\.[0-9]\\.[0-9]+");
}
@Test
@TestTemplate
public void kotlinCompileTasksUseJavaParametersFlagByDefault() {
assertThat(this.gradleBuild.build("kotlinCompileTasksJavaParameters").getOutput())
.contains("compileKotlin java parameters: true")
.contains("compileTestKotlin java parameters: true");
}
@Test
@TestTemplate
public void kotlinCompileTasksCanOverrideDefaultJavaParametersFlag() {
assertThat(this.gradleBuild.build("kotlinCompileTasksJavaParameters").getOutput())
.contains("compileKotlin java parameters: false")

View File

@ -16,15 +16,15 @@
package org.springframework.boot.gradle.plugin;
import java.io.File;
import java.io.IOException;
import org.gradle.api.Project;
import org.gradle.api.plugins.ExtraPropertiesExtension;
import org.gradle.internal.impldep.org.junit.Before;
import org.gradle.internal.impldep.org.junit.Rule;
import org.gradle.internal.impldep.org.junit.Test;
import org.gradle.internal.impldep.org.junit.rules.TemporaryFolder;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.boot.gradle.dsl.SpringBootExtension;
@ -32,17 +32,16 @@ import static org.assertj.core.api.Assertions.assertThat;
public class MainClassConventionTests {
@Rule
public final TemporaryFolder temp = new TemporaryFolder();
@TempDir
File temp;
private Project project;
private MainClassConvention convention;
@Before
@BeforeEach
public void createConvention() throws IOException {
this.project = ProjectBuilder.builder().withProjectDir(this.temp.newFolder())
.build();
this.project = ProjectBuilder.builder().withProjectDir(this.temp).build();
this.convention = new MainClassConvention(this.project, () -> null);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@ -16,11 +16,10 @@
package org.springframework.boot.gradle.plugin;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.junit.GradleCompatibilityExtension;
import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat;
@ -30,13 +29,12 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
*/
@RunWith(GradleCompatibilitySuite.class)
@ExtendWith(GradleCompatibilityExtension.class)
public class MavenPluginActionIntegrationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
@Test
@TestTemplate
public void clearsConf2ScopeMappingsOfUploadBootArchivesTask() {
assertThat(this.gradleBuild.build("conf2ScopeMappings").getOutput())
.contains("Conf2ScopeMappings = 0");

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@ -16,11 +16,10 @@
package org.springframework.boot.gradle.plugin;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.junit.GradleCompatibilityExtension;
import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat;
@ -31,13 +30,12 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
*/
@RunWith(GradleCompatibilitySuite.class)
@ExtendWith(GradleCompatibilityExtension.class)
public class OnlyDependencyManagementIntegrationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
@Test
@TestTemplate
public void dependencyManagementCanBeConfiguredUsingCoordinatesConstant() {
assertThat(this.gradleBuild.build("dependencyManagement").getOutput())
.contains("org.springframework.boot:spring-boot-starter ");

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@ -20,10 +20,11 @@ import java.io.File;
import java.io.IOException;
import org.gradle.testkit.runner.BuildResult;
import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.testkit.GradleBuild;
import org.springframework.boot.gradle.testkit.GradleBuildExtension;
import static org.assertj.core.api.Assertions.assertThat;
@ -32,26 +33,26 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
*/
@ExtendWith(GradleBuildExtension.class)
public class SpringBootPluginIntegrationTests {
@Rule
public final GradleBuild gradleBuild = new GradleBuild();
final GradleBuild gradleBuild = new GradleBuild();
@Test
public void failFastWithVersionOfGradleLowerThanRequired() {
BuildResult result = this.gradleBuild.gradleVersion("4.3").buildAndFail();
assertThat(result.getOutput()).contains("Spring Boot plugin requires Gradle 4.4"
+ " or later. The current version is Gradle 4.3");
BuildResult result = this.gradleBuild.gradleVersion("4.9").buildAndFail();
assertThat(result.getOutput()).contains("Spring Boot plugin requires Gradle 4.10"
+ " or later. The current version is Gradle 4.9");
}
@Test
public void succeedWithVersionOfGradleHigherThanRequired() {
this.gradleBuild.gradleVersion("4.4.1").build();
this.gradleBuild.gradleVersion("4.10.1").build();
}
@Test
public void succeedWithVersionOfGradleMatchingWhatIsRequired() {
this.gradleBuild.gradleVersion("4.4").build();
this.gradleBuild.gradleVersion("4.10").build();
}
@Test

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@ -20,11 +20,10 @@ import java.io.File;
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.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.junit.GradleCompatibilityExtension;
import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat;
@ -34,33 +33,32 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
*/
@RunWith(GradleCompatibilitySuite.class)
@ExtendWith(GradleCompatibilityExtension.class)
public class WarPluginActionIntegrationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
@Test
@TestTemplate
public void noBootWarTaskWithoutWarPluginApplied() {
assertThat(this.gradleBuild.build("taskExists", "-PtaskName=bootWar").getOutput())
.contains("bootWar exists = false");
}
@Test
@TestTemplate
public void applyingWarPluginCreatesBootWarTask() {
assertThat(this.gradleBuild
.build("taskExists", "-PtaskName=bootWar", "-PapplyWarPlugin")
.getOutput()).contains("bootWar exists = true");
}
@Test
@TestTemplate
public void assembleRunsBootWarAndWarIsSkipped() {
BuildResult result = this.gradleBuild.build("assemble");
assertThat(result.task(":bootWar").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.task(":war").getOutcome()).isEqualTo(TaskOutcome.SKIPPED);
}
@Test
@TestTemplate
public void warAndBootWarCanBothBeBuilt() {
BuildResult result = this.gradleBuild.build("assemble");
assertThat(result.task(":bootWar").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
@ -72,7 +70,7 @@ public class WarPluginActionIntegrationTests {
this.gradleBuild.getProjectDir().getName() + "-boot.war"));
}
@Test
@TestTemplate
public void errorMessageIsHelpfulWhenMainClassCannotBeResolved() {
BuildResult result = this.gradleBuild.buildAndFail("build", "-PapplyWarPlugin");
assertThat(result.task(":bootWar").getOutcome()).isEqualTo(TaskOutcome.FAILED);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@ -23,11 +23,10 @@ import java.util.Properties;
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.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.junit.GradleCompatibilityExtension;
import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat;
@ -37,13 +36,12 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
*/
@RunWith(GradleCompatibilitySuite.class)
@ExtendWith(GradleCompatibilityExtension.class)
public class BuildInfoIntegrationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
@Test
@TestTemplate
public void defaultValues() {
assertThat(this.gradleBuild.build("buildInfo").task(":buildInfo").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
@ -56,7 +54,7 @@ public class BuildInfoIntegrationTests {
assertThat(buildInfoProperties).containsEntry("build.version", "unspecified");
}
@Test
@TestTemplate
public void basicExecution() {
assertThat(this.gradleBuild.build("buildInfo").task(":buildInfo").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
@ -69,7 +67,7 @@ public class BuildInfoIntegrationTests {
assertThat(buildInfoProperties).containsEntry("build.version", "1.0");
}
@Test
@TestTemplate
public void notUpToDateWhenExecutedTwiceAsTimeChanges() {
assertThat(this.gradleBuild.build("buildInfo").task(":buildInfo").getOutcome())
.isEqualTo(TaskOutcome.SUCCESS);
@ -77,7 +75,7 @@ public class BuildInfoIntegrationTests {
.isEqualTo(TaskOutcome.SUCCESS);
}
@Test
@TestTemplate
public void upToDateWhenExecutedTwiceWithFixedTime() {
assertThat(this.gradleBuild.build("buildInfo", "-PnullTime").task(":buildInfo")
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
@ -85,7 +83,7 @@ public class BuildInfoIntegrationTests {
.getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE);
}
@Test
@TestTemplate
public void notUpToDateWhenExecutedTwiceWithFixedTimeAndChangedProjectVersion() {
assertThat(this.gradleBuild.build("buildInfo", "-PnullTime").task(":buildInfo")
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@ -25,9 +25,8 @@ import java.util.Properties;
import org.gradle.api.Project;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import static org.assertj.core.api.Assertions.assertThat;
@ -38,8 +37,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class BuildInfoTests {
@Rule
public TemporaryFolder temp = new TemporaryFolder();
@TempDir
File temp;
@Test
public void basicExecution() {
@ -126,14 +125,9 @@ public class BuildInfoTests {
}
private Project createProject(String projectName) {
try {
File projectDir = this.temp.newFolder(projectName);
return ProjectBuilder.builder().withProjectDir(projectDir)
.withName(projectName).build();
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
File projectDir = new File(this.temp, projectName);
return ProjectBuilder.builder().withProjectDir(projectDir).withName(projectName)
.build();
}
private BuildInfo createTask(Project project) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@ -23,11 +23,10 @@ import java.util.jar.JarFile;
import org.gradle.testkit.runner.InvalidRunnerConfigurationException;
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.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.junit.GradleCompatibilityExtension;
import org.springframework.boot.gradle.testkit.GradleBuild;
import org.springframework.boot.loader.tools.FileUtils;
@ -38,11 +37,10 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
*/
@RunWith(GradleCompatibilitySuite.class)
@ExtendWith(GradleCompatibilityExtension.class)
public abstract class AbstractBootArchiveIntegrationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
private final String taskName;
@ -50,14 +48,14 @@ public abstract class AbstractBootArchiveIntegrationTests {
this.taskName = taskName;
}
@Test
@TestTemplate
public void basicBuild() throws InvalidRunnerConfigurationException,
UnexpectedBuildFailure, IOException {
assertThat(this.gradleBuild.build(this.taskName).task(":" + this.taskName)
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}
@Test
@TestTemplate
public void reproducibleArchive() throws InvalidRunnerConfigurationException,
UnexpectedBuildFailure, IOException, InterruptedException {
assertThat(this.gradleBuild.build(this.taskName).task(":" + this.taskName)
@ -72,7 +70,7 @@ public abstract class AbstractBootArchiveIntegrationTests {
assertThat(firstHash).isEqualTo(secondHash);
}
@Test
@TestTemplate
public void upToDateWhenBuiltTwice() throws InvalidRunnerConfigurationException,
UnexpectedBuildFailure, IOException {
assertThat(this.gradleBuild.build(this.taskName).task(":" + this.taskName)
@ -81,7 +79,7 @@ public abstract class AbstractBootArchiveIntegrationTests {
.getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE);
}
@Test
@TestTemplate
public void upToDateWhenBuiltTwiceWithLaunchScriptIncluded()
throws InvalidRunnerConfigurationException, UnexpectedBuildFailure,
IOException {
@ -92,7 +90,7 @@ public abstract class AbstractBootArchiveIntegrationTests {
.isEqualTo(TaskOutcome.UP_TO_DATE);
}
@Test
@TestTemplate
public void notUpToDateWhenLaunchScriptWasNotIncludedAndThenIsIncluded() {
assertThat(this.gradleBuild.build(this.taskName).task(":" + this.taskName)
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
@ -100,7 +98,7 @@ public abstract class AbstractBootArchiveIntegrationTests {
.task(":" + this.taskName).getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}
@Test
@TestTemplate
public void notUpToDateWhenLaunchScriptWasIncludedAndThenIsNotIncluded() {
assertThat(this.gradleBuild.build(this.taskName).task(":" + this.taskName)
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
@ -108,7 +106,7 @@ public abstract class AbstractBootArchiveIntegrationTests {
.task(":" + this.taskName).getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}
@Test
@TestTemplate
public void notUpToDateWhenLaunchScriptPropertyChanges() {
assertThat(this.gradleBuild.build("-PincludeLaunchScript=true",
"-PlaunchScriptProperty=foo", this.taskName).task(":" + this.taskName)
@ -118,7 +116,7 @@ public abstract class AbstractBootArchiveIntegrationTests {
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
}
@Test
@TestTemplate
public void applicationPluginMainClassNameIsUsed() throws IOException {
assertThat(this.gradleBuild.build(this.taskName).task(":" + this.taskName)
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
@ -130,7 +128,7 @@ public abstract class AbstractBootArchiveIntegrationTests {
}
}
@Test
@TestTemplate
public void springBootExtensionMainClassNameIsUsed() throws IOException {
assertThat(this.gradleBuild.build(this.taskName).task(":" + this.taskName)
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
@ -142,7 +140,7 @@ public abstract class AbstractBootArchiveIntegrationTests {
}
}
@Test
@TestTemplate
public void duplicatesAreHandledGracefully() throws IOException {
assertThat(this.gradleBuild.build(this.taskName).task(":" + this.taskName)
.getOutcome()).isEqualTo(TaskOutcome.SUCCESS);

View File

@ -41,10 +41,9 @@ import org.gradle.api.Project;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.gradle.api.tasks.bundling.Jar;
import org.gradle.testfixtures.ProjectBuilder;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.springframework.boot.loader.tools.DefaultLaunchScript;
@ -58,8 +57,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Rule
public final TemporaryFolder temp = new TemporaryFolder();
@TempDir
File temp;
private final Class<T> taskClass;
@ -81,11 +80,12 @@ public abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
this.classesPath = classesPath;
}
@Before
@BeforeEach
public void createTask() {
try {
this.project = ProjectBuilder.builder().withProjectDir(this.temp.newFolder())
.build();
File projectDir = new File(this.temp, "project");
projectDir.mkdirs();
this.project = ProjectBuilder.builder().withProjectDir(projectDir).build();
this.project
.setDescription("Test project for " + this.taskClass.getSimpleName());
this.task = configure(
@ -130,7 +130,7 @@ public abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
public void classpathFoldersArePackagedBeneathClassesPath() throws IOException {
this.task.setMainClassName("com.example.Main");
File classpathFolder = this.temp.newFolder();
File classpathFolder = new File(this.temp, "classes");
File applicationClass = new File(classpathFolder,
"com/example/Application.class");
applicationClass.getParentFile().mkdirs();
@ -147,7 +147,7 @@ public abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
public void moduleInfoClassIsPackagedInTheRootOfTheArchive() throws IOException {
this.task.setMainClassName("com.example.Main");
File classpathFolder = this.temp.newFolder();
File classpathFolder = new File(this.temp, "classes");
File moduleInfoClass = new File(classpathFolder, "module-info.class");
moduleInfoClass.getParentFile().mkdirs();
moduleInfoClass.createNewFile();
@ -195,7 +195,7 @@ public abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
public void filesOnTheClasspathThatAreNotZipFilesAreSkipped() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.classpath(this.temp.newFile("test.pom"));
this.task.classpath(new File("test.pom"));
this.task.execute();
try (JarFile jarFile = new JarFile(this.task.getArchivePath())) {
assertThat(jarFile.getEntry(this.libPath + "/test.pom")).isNull();
@ -279,7 +279,7 @@ public abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
public void customLaunchScriptCanBePrepended() throws IOException {
this.task.setMainClassName("com.example.Main");
File customScript = this.temp.newFile("custom.script");
File customScript = new File(this.temp, "custom.script");
Files.write(customScript.toPath(), Arrays.asList("custom script"),
StandardOpenOption.CREATE);
this.task.launchScript((configuration) -> configuration.setScript(customScript));
@ -357,8 +357,8 @@ public abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
public void reproducibleOrderingCanBeEnabled() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.from(this.temp.newFile("bravo.txt"), this.temp.newFile("alpha.txt"),
this.temp.newFile("charlie.txt"));
this.task.from(newFile("bravo.txt"), newFile("alpha.txt"),
newFile("charlie.txt"));
this.task.setReproducibleFileOrder(true);
executeTask();
assertThat(this.task.getArchivePath()).exists();
@ -378,7 +378,7 @@ public abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
@Test
public void devtoolsJarIsExcludedByDefault() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.classpath(this.temp.newFile("spring-boot-devtools-0.1.2.jar"));
this.task.classpath(newFile("spring-boot-devtools-0.1.2.jar"));
executeTask();
assertThat(this.task.getArchivePath()).exists();
try (JarFile jarFile = new JarFile(this.task.getArchivePath())) {
@ -404,7 +404,7 @@ public abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
public void allEntriesUseUnixPlatformAndUtf8NameEncoding() throws IOException {
this.task.setMainClassName("com.example.Main");
this.task.setMetadataCharset("UTF-8");
File classpathFolder = this.temp.newFolder();
File classpathFolder = new File(this.temp, "classes");
File resource = new File(classpathFolder, "some-resource.xml");
resource.getParentFile().mkdirs();
resource.createNewFile();
@ -425,7 +425,7 @@ public abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
public void loaderIsWrittenFirstThenApplicationClassesThenLibraries()
throws IOException {
this.task.setMainClassName("com.example.Main");
File classpathFolder = this.temp.newFolder();
File classpathFolder = new File(this.temp, "classes");
File applicationClass = new File(classpathFolder,
"com/example/Application.class");
applicationClass.getParentFile().mkdirs();
@ -442,7 +442,7 @@ public abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
}
protected File jarFile(String name) throws IOException {
File file = this.temp.newFile(name);
File file = newFile(name);
try (JarOutputStream jar = new JarOutputStream(new FileOutputStream(file))) {
jar.putNextEntry(new ZipEntry("META-INF/MANIFEST.MF"));
new Manifest().write(jar);
@ -454,7 +454,9 @@ public abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
private T configure(T task) throws IOException {
AbstractArchiveTask archiveTask = task;
archiveTask.setBaseName("test");
archiveTask.setDestinationDir(this.temp.newFolder());
File destination = new File(this.temp, "destination");
destination.mkdirs();
archiveTask.setDestinationDir(destination);
return task;
}
@ -475,4 +477,10 @@ public abstract class AbstractBootArchiveTests<T extends Jar & BootArchive> {
return entryNames;
}
protected File newFile(String name) throws IOException {
File file = new File(this.temp, name);
file.createNewFile();
return file;
}
}

View File

@ -20,7 +20,7 @@ import java.io.File;
import java.io.IOException;
import java.util.jar.JarFile;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;

View File

@ -20,7 +20,7 @@ import java.io.File;
import java.io.IOException;
import java.util.jar.JarFile;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
@ -75,7 +75,7 @@ public class BootWarTests extends AbstractBootArchiveTests<BootWar> {
public void devtoolsJarIsExcludedByDefaultWhenItsOnTheProvidedClasspath()
throws IOException {
getTask().setMainClassName("com.example.Main");
getTask().providedClasspath(this.temp.newFile("spring-boot-devtools-0.1.2.jar"));
getTask().providedClasspath(newFile("spring-boot-devtools-0.1.2.jar"));
executeTask();
assertThat(getTask().getArchivePath()).exists();
try (JarFile jarFile = new JarFile(getTask().getArchivePath())) {
@ -103,7 +103,8 @@ public class BootWarTests extends AbstractBootArchiveTests<BootWar> {
@Test
public void webappResourcesInDirectoriesThatOverlapWithLoaderCanBePackaged()
throws IOException {
File webappFolder = this.temp.newFolder("src", "main", "webapp");
File webappFolder = new File(this.temp, "src/main/webapp");
webappFolder.mkdirs();
File orgFolder = new File(webappFolder, "org");
orgFolder.mkdir();
new File(orgFolder, "foo.txt").createNewFile();

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 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.
@ -18,8 +18,8 @@ package org.springframework.boot.gradle.tasks.bundling;
import org.gradle.api.Project;
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
@ -36,7 +36,7 @@ public class LaunchScriptConfigurationTests {
private final Project project = mock(Project.class);
@Before
@BeforeEach
public void setUp() {
given(this.task.getProject()).willReturn(this.project);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@ -22,11 +22,10 @@ import java.io.IOException;
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.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.junit.GradleCompatibilityExtension;
import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat;
@ -36,13 +35,12 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
*/
@RunWith(GradleCompatibilitySuite.class)
@ExtendWith(GradleCompatibilityExtension.class)
public class MavenIntegrationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
@Test
@TestTemplate
public void bootJarCanBeUploaded() throws FileNotFoundException, IOException {
BuildResult result = this.gradleBuild.build("uploadBootArchives");
assertThat(result.task(":uploadBootArchives").getOutcome())
@ -53,7 +51,7 @@ public class MavenIntegrationTests {
.noPackaging().noDependencies());
}
@Test
@TestTemplate
public void bootWarCanBeUploaded() throws IOException {
BuildResult result = this.gradleBuild.build("uploadBootArchives");
assertThat(result.task(":uploadBootArchives").getOutcome())

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@ -22,11 +22,10 @@ import java.io.IOException;
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.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.junit.GradleCompatibilityExtension;
import org.springframework.boot.gradle.testkit.GradleBuild;
import static org.assertj.core.api.Assertions.assertThat;
@ -37,13 +36,12 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
*/
@RunWith(GradleCompatibilitySuite.class)
@ExtendWith(GradleCompatibilityExtension.class)
public class MavenPublishingIntegrationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
@Test
@TestTemplate
public void bootJarCanBePublished() throws FileNotFoundException, IOException {
BuildResult result = this.gradleBuild.build("publish");
assertThat(result.task(":publish").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
@ -53,7 +51,7 @@ public class MavenPublishingIntegrationTests {
.noPackaging().noDependencies());
}
@Test
@TestTemplate
public void bootWarCanBePublished() throws IOException {
BuildResult result = this.gradleBuild.build("publish");
assertThat(result.task(":publish").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 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.
@ -21,11 +21,10 @@ import java.io.IOException;
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.junit.jupiter.api.TestTemplate;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.boot.gradle.junit.GradleCompatibilitySuite;
import org.springframework.boot.gradle.junit.GradleCompatibilityExtension;
import org.springframework.boot.gradle.testkit.GradleBuild;
import org.springframework.util.FileSystemUtils;
@ -36,13 +35,12 @@ import static org.assertj.core.api.Assertions.assertThat;
*
* @author Andy Wilkinson
*/
@RunWith(GradleCompatibilitySuite.class)
@ExtendWith(GradleCompatibilityExtension.class)
public class BootRunIntegrationTests {
@Rule
public GradleBuild gradleBuild;
GradleBuild gradleBuild;
@Test
@TestTemplate
public void basicExecution() throws IOException {
copyApplication();
new File(this.gradleBuild.getProjectDir(), "src/main/resources").mkdirs();
@ -56,7 +54,7 @@ public class BootRunIntegrationTests {
.doesNotContain(canonicalPathOf("src/main/resources"));
}
@Test
@TestTemplate
public void sourceResourcesCanBeUsed() throws IOException {
copyApplication();
BuildResult result = this.gradleBuild.build("bootRun");
@ -69,7 +67,7 @@ public class BootRunIntegrationTests {
.doesNotContain(canonicalPathOf("build/resources/main"));
}
@Test
@TestTemplate
public void springBootExtensionMainClassNameIsUsed() throws IOException {
BuildResult result = this.gradleBuild.build("echoMainClassName");
assertThat(result.task(":echoMainClassName").getOutcome())
@ -78,7 +76,7 @@ public class BootRunIntegrationTests {
.contains("Main class name = com.example.CustomMainClass");
}
@Test
@TestTemplate
public void applicationPluginMainClassNameIsUsed() throws IOException {
BuildResult result = this.gradleBuild.build("echoMainClassName");
assertThat(result.task(":echoMainClassName").getOutcome())
@ -87,7 +85,7 @@ public class BootRunIntegrationTests {
.contains("Main class name = com.example.CustomMainClass");
}
@Test
@TestTemplate
public void applicationPluginMainClassNameIsNotUsedWhenItIsNull() throws IOException {
copyApplication();
BuildResult result = this.gradleBuild.build("echoMainClassName");
@ -97,7 +95,7 @@ public class BootRunIntegrationTests {
.contains("Main class name = com.example.BootRunApplication");
}
@Test
@TestTemplate
public void applicationPluginJvmArgumentsAreUsed() throws IOException {
BuildResult result = this.gradleBuild.build("echoJvmArguments");
assertThat(result.task(":echoJvmArguments").getOutcome())

View File

@ -21,11 +21,11 @@ import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.jar.JarFile;
import java.util.regex.Pattern;
import io.spring.gradle.dependencymanagement.DependencyManagementPlugin;
import io.spring.gradle.dependencymanagement.dsl.DependencyManagementExtension;
@ -37,10 +37,6 @@ import org.jetbrains.kotlin.compilerRunner.KotlinLogger;
import org.jetbrains.kotlin.gradle.model.KotlinProject;
import org.jetbrains.kotlin.gradle.plugin.KotlinGradleSubplugin;
import org.jetbrains.kotlin.gradle.plugin.KotlinPlugin;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import org.springframework.asm.ClassVisitor;
import org.springframework.boot.loader.tools.LaunchScript;
@ -48,16 +44,11 @@ import org.springframework.util.FileCopyUtils;
import org.springframework.util.FileSystemUtils;
/**
* A {@link TestRule} for running a Gradle build using {@link GradleRunner}.
* A {@code GradleBuild} is used to run a Gradle build using {@link GradleRunner}.
*
* @author Andy Wilkinson
*/
public class GradleBuild implements TestRule {
private static final Pattern GRADLE_VERSION_PATTERN = Pattern
.compile("\\[Gradle .+\\]");
private final TemporaryFolder temp = new TemporaryFolder();
public class GradleBuild {
private final Dsl dsl;
@ -79,57 +70,13 @@ public class GradleBuild implements TestRule {
return this.dsl;
}
@Override
public Statement apply(Statement base, Description description) {
URL scriptUrl = findDefaultScript(description);
if (scriptUrl != null) {
script(scriptUrl.getFile());
}
return this.temp.apply(new Statement() {
@Override
public void evaluate() throws Throwable {
before();
try {
base.evaluate();
}
finally {
after();
}
}
}, description);
void before() throws IOException {
this.projectDir = Files.createTempDirectory("gradle-").toFile();
}
private URL findDefaultScript(Description description) {
URL scriptUrl = getScriptForTestMethod(description);
if (scriptUrl != null) {
return scriptUrl;
}
return getScriptForTestClass(description.getTestClass());
}
private URL getScriptForTestMethod(Description description) {
String name = description.getTestClass().getSimpleName() + "-"
+ removeGradleVersion(description.getMethodName())
+ this.dsl.getExtension();
return description.getTestClass().getResource(name);
}
private String removeGradleVersion(String methodName) {
return GRADLE_VERSION_PATTERN.matcher(methodName).replaceAll("").trim();
}
private URL getScriptForTestClass(Class<?> testClass) {
return testClass.getResource(testClass.getSimpleName() + this.dsl.getExtension());
}
private void before() throws IOException {
this.projectDir = this.temp.newFolder();
}
private void after() {
void after() {
GradleBuild.this.script = null;
FileSystemUtils.deleteRecursively(this.projectDir);
}
private List<File> pluginClasspath() {

View File

@ -0,0 +1,90 @@
/*
* Copyright 2012-2019 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
*
* https://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.testkit;
import java.lang.reflect.Field;
import java.net.URL;
import java.util.regex.Pattern;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.springframework.util.ReflectionUtils;
/**
* An {@link Extension} for managing the lifecycle of a {@link GradleBuild} stored in a
* field named {@code gradleBuild}.
*
* @author Andy Wilkinson
*/
public class GradleBuildExtension implements BeforeEachCallback, AfterEachCallback {
private static final Pattern GRADLE_VERSION_PATTERN = Pattern
.compile("\\[Gradle .+\\]");
private Dsl dsl = Dsl.GROOVY;
@Override
public void beforeEach(ExtensionContext context) throws Exception {
GradleBuild gradleBuild = extractGradleBuild(context);
URL scriptUrl = findDefaultScript(context);
if (scriptUrl != null) {
gradleBuild.script(scriptUrl.getFile());
}
gradleBuild.before();
}
private GradleBuild extractGradleBuild(ExtensionContext context) throws Exception {
Object testInstance = context.getRequiredTestInstance();
Field gradleBuildField = ReflectionUtils.findField(testInstance.getClass(),
"gradleBuild");
gradleBuildField.setAccessible(true);
GradleBuild gradleBuild = (GradleBuild) gradleBuildField.get(testInstance);
return gradleBuild;
}
private URL findDefaultScript(ExtensionContext context) {
URL scriptUrl = getScriptForTestMethod(context);
if (scriptUrl != null) {
return scriptUrl;
}
return getScriptForTestClass(context.getRequiredTestClass());
}
private URL getScriptForTestMethod(ExtensionContext context) {
Class<?> testClass = context.getRequiredTestClass();
String name = testClass.getSimpleName() + "-"
+ removeGradleVersion(context.getRequiredTestMethod().getName())
+ this.dsl.getExtension();
return testClass.getResource(name);
}
private String removeGradleVersion(String methodName) {
return GRADLE_VERSION_PATTERN.matcher(methodName).replaceAll("").trim();
}
private URL getScriptForTestClass(Class<?> testClass) {
return testClass.getResource(testClass.getSimpleName() + this.dsl.getExtension());
}
@Override
public void afterEach(ExtensionContext context) throws Exception {
extractGradleBuild(context).after();
}
}