Update BootRun to support Gradle's configuration cache
See gh-22922
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* Copyright 2012-2020 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 com.example.main;
|
||||
|
||||
/**
|
||||
* Application used for testing {@code BootRun}'s main class configuration.
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
public class CustomMainClass {
|
||||
|
||||
protected CustomMainClass() {
|
||||
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(CustomMainClass.class.getName());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,7 +17,9 @@
|
||||
package org.springframework.boot.gradle.docs;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import org.junit.jupiter.api.TestTemplate;
|
||||
import org.junit.jupiter.api.condition.DisabledForJreRange;
|
||||
@@ -43,20 +45,23 @@ class RunningDocumentationTests {
|
||||
@TestTemplate
|
||||
@DisabledForJreRange(min = JRE.JAVA_13)
|
||||
void bootRunMain() throws IOException {
|
||||
assertThat(this.gradleBuild.script("src/docs/gradle/running/boot-run-main").build("configuredMainClass")
|
||||
.getOutput()).contains("com.example.ExampleApplication");
|
||||
writeMainClass();
|
||||
assertThat(this.gradleBuild.script("src/docs/gradle/running/boot-run-main").build("bootRun").getOutput())
|
||||
.contains("com.example.ExampleApplication");
|
||||
}
|
||||
|
||||
@TestTemplate
|
||||
void applicationPluginMainClassName() {
|
||||
void applicationPluginMainClassName() throws IOException {
|
||||
writeMainClass();
|
||||
assertThat(this.gradleBuild.script("src/docs/gradle/running/application-plugin-main-class-name")
|
||||
.build("configuredMainClass").getOutput()).contains("com.example.ExampleApplication");
|
||||
.build("bootRun").getOutput()).contains("com.example.ExampleApplication");
|
||||
}
|
||||
|
||||
@TestTemplate
|
||||
void springBootDslMainClassName() throws IOException {
|
||||
assertThat(this.gradleBuild.script("src/docs/gradle/running/spring-boot-dsl-main-class-name")
|
||||
.build("configuredMainClass").getOutput()).contains("com.example.ExampleApplication");
|
||||
writeMainClass();
|
||||
assertThat(this.gradleBuild.script("src/docs/gradle/running/spring-boot-dsl-main-class-name").build("bootRun")
|
||||
.getOutput()).contains("com.example.ExampleApplication");
|
||||
}
|
||||
|
||||
@TestTemplate
|
||||
@@ -84,4 +89,18 @@ class RunningDocumentationTests {
|
||||
.contains("com.example.property = custom");
|
||||
}
|
||||
|
||||
private void writeMainClass() throws IOException {
|
||||
File exampleApplication = new File(this.gradleBuild.getProjectDir(),
|
||||
"src/main/java/com/example/ExampleApplication.java");
|
||||
exampleApplication.getParentFile().mkdirs();
|
||||
try (PrintWriter writer = new PrintWriter(new FileWriter(exampleApplication))) {
|
||||
writer.println("package com.example;");
|
||||
writer.println("public class ExampleApplication {");
|
||||
writer.println(" public static void main(String[] args) {");
|
||||
writer.println(" System.out.println(ExampleApplication.class.getName());");
|
||||
writer.println(" }");
|
||||
writer.println("}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -66,7 +66,8 @@ final class GradleCompatibilityExtension implements TestTemplateInvocationContex
|
||||
boolean configurationCache = AnnotationUtils
|
||||
.findAnnotation(context.getRequiredTestClass(), GradleCompatibility.class).get()
|
||||
.configurationCache();
|
||||
if (configurationCache && GradleVersion.version(version).compareTo(GradleVersion.version("6.6")) >= 0) {
|
||||
if (configurationCache
|
||||
&& GradleVersion.version(version).compareTo(GradleVersion.version("6.7-rc-1")) >= 0) {
|
||||
invocationContexts.add(new GradleVersionTestTemplateInvocationContext(version, true));
|
||||
}
|
||||
return invocationContexts.stream();
|
||||
|
||||
@@ -40,7 +40,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
*
|
||||
* @author Andy Wilkinson
|
||||
*/
|
||||
@GradleCompatibility
|
||||
@GradleCompatibility(configurationCache = true)
|
||||
class BootRunIntegrationTests {
|
||||
|
||||
GradleBuild gradleBuild;
|
||||
@@ -68,23 +68,25 @@ class BootRunIntegrationTests {
|
||||
|
||||
@TestTemplate
|
||||
void springBootExtensionMainClassNameIsUsed() throws IOException {
|
||||
BuildResult result = this.gradleBuild.build("echoMainClassName");
|
||||
assertThat(result.task(":echoMainClassName").getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE);
|
||||
assertThat(result.getOutput()).contains("Main class name = com.example.CustomMainClass");
|
||||
copyMainClassApplication();
|
||||
BuildResult result = this.gradleBuild.build("bootRun");
|
||||
assertThat(result.task(":bootRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
|
||||
assertThat(result.getOutput()).contains("com.example.main.CustomMainClass");
|
||||
}
|
||||
|
||||
@TestTemplate
|
||||
void applicationPluginMainClassNameIsUsed() throws IOException {
|
||||
BuildResult result = this.gradleBuild.build("echoMainClassName");
|
||||
assertThat(result.task(":echoMainClassName").getOutcome()).isEqualTo(TaskOutcome.UP_TO_DATE);
|
||||
assertThat(result.getOutput()).contains("Main class name = com.example.CustomMainClass");
|
||||
copyMainClassApplication();
|
||||
BuildResult result = this.gradleBuild.build("bootRun");
|
||||
assertThat(result.task(":bootRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
|
||||
assertThat(result.getOutput()).contains("com.example.main.CustomMainClass");
|
||||
}
|
||||
|
||||
@TestTemplate
|
||||
void applicationPluginMainClassNameIsNotUsedWhenItIsNull() throws IOException {
|
||||
copyClasspathApplication();
|
||||
BuildResult result = this.gradleBuild.build("echoMainClassName");
|
||||
assertThat(result.task(":echoMainClassName").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
|
||||
BuildResult result = this.gradleBuild.build("bootRun");
|
||||
assertThat(result.task(":bootRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
|
||||
assertThat(result.getOutput()).contains("Main class name = com.example.classpath.BootRunClasspathApplication");
|
||||
}
|
||||
|
||||
@@ -135,6 +137,10 @@ class BootRunIntegrationTests {
|
||||
assertThat(result.getOutput()).contains("standard.jar").doesNotContain("starter.jar");
|
||||
}
|
||||
|
||||
private void copyMainClassApplication() throws IOException {
|
||||
copyApplication("main");
|
||||
}
|
||||
|
||||
private void copyClasspathApplication() throws IOException {
|
||||
copyApplication("classpath");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user