Add bootTestRun to run app using test source set output and classpath

Closes gh-35248
This commit is contained in:
Andy Wilkinson
2023-05-03 09:52:09 +01:00
parent 9cadc6ffbb
commit 19d7973776
19 changed files with 409 additions and 11 deletions

View File

@@ -0,0 +1,41 @@
/*
* Copyright 2012-2023 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.boottestrun.classpath;
import java.io.File;
import java.lang.management.ManagementFactory;
/**
* Application used for testing {@code bootTestRun}'s classpath handling.
*
* @author Andy Wilkinson
*/
public class BootTestRunClasspathApplication {
protected BootTestRunClasspathApplication() {
}
public static void main(String[] args) {
System.out.println("Main class name = " + BootTestRunClasspathApplication.class.getName());
int i = 1;
for (String entry : ManagementFactory.getRuntimeMXBean().getClassPath().split(File.pathSeparator)) {
System.out.println(i++ + ". " + entry);
}
}
}

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2012-2023 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.boottestrun.jvmargs;
import java.lang.management.ManagementFactory;
/**
* Application used for testing {@code bootTestRun}'s JVM argument handling.
*
* @author Andy Wilkinson
*/
public class BootTestRunJvmArgsApplication {
protected BootTestRunJvmArgsApplication() {
}
public static void main(String[] args) {
int i = 1;
for (String entry : ManagementFactory.getRuntimeMXBean().getInputArguments()) {
System.out.println(i++ + ". " + entry);
}
}
}

View File

@@ -0,0 +1,26 @@
/*
* Copyright 2012-2023 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.boottestrun.nomain;
/**
* Application used for testing {@code bootTestRun}'s handling of no test main method
*
* @author Andy Wilkinson
*/
public class BootTestRunNoMain {
}

View File

@@ -60,11 +60,21 @@ class JavaPluginActionIntegrationTests {
assertThat(this.gradleBuild.build("tasks").getOutput()).doesNotContain("bootRun");
}
@TestTemplate
void noBootTestRunTaskWithoutJavaPluginApplied() {
assertThat(this.gradleBuild.build("tasks").getOutput()).doesNotContain("bootTestRun");
}
@TestTemplate
void applyingJavaPluginCreatesBootRunTask() {
assertThat(this.gradleBuild.build("tasks").getOutput()).contains("bootRun");
}
@TestTemplate
void applyingJavaPluginCreatesBootTestRunTask() {
assertThat(this.gradleBuild.build("tasks").getOutput()).contains("bootTestRun");
}
@TestTemplate
void javaCompileTasksUseUtf8Encoding() {
assertThat(this.gradleBuild.build("build").getOutput()).contains("compileJava = UTF-8")

View File

@@ -0,0 +1,156 @@
/*
* Copyright 2012-2023 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.tasks.run;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.function.Consumer;
import java.util.jar.Attributes;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import org.assertj.core.api.Assumptions;
import org.gradle.testkit.runner.BuildResult;
import org.gradle.testkit.runner.TaskOutcome;
import org.gradle.util.GradleVersion;
import org.junit.jupiter.api.TestTemplate;
import org.springframework.boot.gradle.junit.GradleCompatibility;
import org.springframework.boot.testsupport.gradle.testkit.GradleBuild;
import org.springframework.util.FileSystemUtils;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for the {@link BootRun} task configured to use the test source set.
*
* @author Andy Wilkinson
*/
@GradleCompatibility(configurationCache = true)
class BootTestRunIntegrationTests {
GradleBuild gradleBuild;
@TestTemplate
void basicExecution() throws IOException {
copyClasspathApplication();
BuildResult result = this.gradleBuild.build("bootTestRun");
assertThat(result.task(":bootTestRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("1. " + canonicalPathOf("build/classes/java/test"))
.contains("2. " + canonicalPathOf("build/resources/test"))
.contains("3. " + canonicalPathOf("build/classes/java/main"))
.contains("4. " + canonicalPathOf("build/resources/main"));
}
@TestTemplate
void defaultJvmArgs() throws IOException {
copyJvmArgsApplication();
BuildResult result = this.gradleBuild.build("bootTestRun");
assertThat(result.task(":bootTestRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("-XX:TieredStopAtLevel=1");
}
@TestTemplate
void optimizedLaunchDisabledJvmArgs() throws IOException {
copyJvmArgsApplication();
BuildResult result = this.gradleBuild.build("bootTestRun");
assertThat(result.task(":bootTestRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).doesNotContain("-Xverify:none").doesNotContain("-XX:TieredStopAtLevel=1");
}
@TestTemplate
void applicationPluginJvmArgumentsAreUsed() throws IOException {
if (this.gradleBuild.isConfigurationCache()) {
// https://github.com/gradle/gradle/pull/23924
GradleVersion gradleVersion = GradleVersion.version(this.gradleBuild.getGradleVersion());
Assumptions.assumeThat(gradleVersion)
.isLessThan(GradleVersion.version("8.0"))
.isGreaterThanOrEqualTo(GradleVersion.version("8.1-rc-1"));
}
copyJvmArgsApplication();
BuildResult result = this.gradleBuild.build("bootTestRun");
assertThat(result.task(":bootTestRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("-Dcom.bar=baz")
.contains("-Dcom.foo=bar")
.contains("-XX:TieredStopAtLevel=1");
}
@TestTemplate
void jarTypeFilteringIsAppliedToTheClasspath() throws IOException {
copyClasspathApplication();
File flatDirRepository = new File(this.gradleBuild.getProjectDir(), "repository");
createDependenciesStarterJar(new File(flatDirRepository, "starter.jar"));
createStandardJar(new File(flatDirRepository, "standard.jar"));
BuildResult result = this.gradleBuild.build("bootTestRun");
assertThat(result.task(":bootTestRun").getOutcome()).isEqualTo(TaskOutcome.SUCCESS);
assertThat(result.getOutput()).contains("standard.jar").doesNotContain("starter.jar");
}
@TestTemplate
void failsGracefullyWhenNoTestMainMethodIsFound() throws IOException {
copyApplication("nomain");
BuildResult result = this.gradleBuild.buildAndFail("bootTestRun");
assertThat(result.task(":bootTestRun").getOutcome()).isEqualTo(TaskOutcome.FAILED);
if (this.gradleBuild.isConfigurationCache() && this.gradleBuild.gradleVersionIsAtLeast("8.0")) {
assertThat(result.getOutput())
.contains("Main class name has not been configured and it could not be resolved from classpath");
}
else {
assertThat(result.getOutput())
.contains("Main class name has not been configured and it could not be resolved from classpath "
+ "build/classes/java/test");
}
}
private void copyClasspathApplication() throws IOException {
copyApplication("classpath");
}
private void copyJvmArgsApplication() throws IOException {
copyApplication("jvmargs");
}
private void copyApplication(String name) throws IOException {
File output = new File(this.gradleBuild.getProjectDir(), "src/test/java/com/example/boottestrun/" + name);
output.mkdirs();
FileSystemUtils.copyRecursively(new File("src/test/java/com/example/boottestrun/" + name), output);
}
private String canonicalPathOf(String path) throws IOException {
return new File(this.gradleBuild.getProjectDir(), path).getCanonicalPath();
}
private void createStandardJar(File location) throws IOException {
createJar(location, (attributes) -> {
});
}
private void createDependenciesStarterJar(File location) throws IOException {
createJar(location, (attributes) -> attributes.putValue("Spring-Boot-Jar-Type", "dependencies-starter"));
}
private void createJar(File location, Consumer<Attributes> attributesConfigurer) throws IOException {
location.getParentFile().mkdirs();
Manifest manifest = new Manifest();
Attributes attributes = manifest.getMainAttributes();
attributes.put(Attributes.Name.MANIFEST_VERSION, "1.0");
attributesConfigurer.accept(attributes);
new JarOutputStream(new FileOutputStream(location), manifest).close();
}
}