Add bootTestRun to run app using test source set output and classpath
Closes gh-35248pull/35286/head
parent
9cadc6ffbb
commit
19d7973776
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -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 {
|
||||||
|
|
||||||
|
}
|
@ -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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
plugins {
|
||||||
|
id 'org.springframework.boot' version '{version}'
|
||||||
|
id 'java'
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
plugins {
|
||||||
|
id 'org.springframework.boot' version '{version}'
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
plugins {
|
||||||
|
id 'application'
|
||||||
|
id 'org.springframework.boot' version '{version}'
|
||||||
|
}
|
||||||
|
|
||||||
|
application {
|
||||||
|
applicationDefaultJvmArgs = ['-Dcom.foo=bar', '-Dcom.bar=baz']
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
plugins {
|
||||||
|
id 'java'
|
||||||
|
id 'org.springframework.boot' version '{version}'
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
plugins {
|
||||||
|
id 'application'
|
||||||
|
id 'org.springframework.boot' version '{version}'
|
||||||
|
}
|
@ -0,0 +1,4 @@
|
|||||||
|
plugins {
|
||||||
|
id 'java'
|
||||||
|
id 'org.springframework.boot' version '{version}'
|
||||||
|
}
|
@ -0,0 +1,15 @@
|
|||||||
|
plugins {
|
||||||
|
id 'java'
|
||||||
|
id 'org.springframework.boot' version '{version}'
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
flatDir {
|
||||||
|
dirs 'repository'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation(name: "standard")
|
||||||
|
implementation(name: "starter")
|
||||||
|
}
|
@ -0,0 +1,8 @@
|
|||||||
|
plugins {
|
||||||
|
id 'application'
|
||||||
|
id 'org.springframework.boot' version '{version}'
|
||||||
|
}
|
||||||
|
|
||||||
|
bootTestRun {
|
||||||
|
optimizedLaunch = false
|
||||||
|
}
|
Loading…
Reference in New Issue