Skip to content

Commit 16291b0

Browse files
committed
Merge branch '3.2.x'
Closes gh-40609
2 parents 59dd34f + 6fd3ebb commit 16291b0

File tree

8 files changed

+255
-3
lines changed

8 files changed

+255
-3
lines changed

settings.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ include "spring-boot-project:spring-boot-test"
7575
include "spring-boot-project:spring-boot-testcontainers"
7676
include "spring-boot-project:spring-boot-test-autoconfigure"
7777
include "spring-boot-tests:spring-boot-integration-tests:spring-boot-configuration-processor-tests"
78+
include "spring-boot-tests:spring-boot-integration-tests:spring-boot-console-tests"
7879
include "spring-boot-tests:spring-boot-integration-tests:spring-boot-launch-script-tests"
7980
include "spring-boot-tests:spring-boot-integration-tests:spring-boot-loader-tests"
8081
include "spring-boot-tests:spring-boot-integration-tests:spring-boot-loader-classic-tests"

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ansi/AnsiOutput.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,15 +16,19 @@
1616

1717
package org.springframework.boot.ansi;
1818

19+
import java.io.Console;
20+
import java.lang.reflect.Method;
1921
import java.util.Locale;
2022

2123
import org.springframework.util.Assert;
24+
import org.springframework.util.ClassUtils;
2225

2326
/**
2427
* Generates ANSI encoded output, automatically attempting to detect if the terminal
2528
* supports ANSI.
2629
*
2730
* @author Phillip Webb
31+
* @author Yong-Hyun Kim
2832
* @since 1.0.0
2933
*/
3034
public abstract class AnsiOutput {
@@ -152,8 +156,18 @@ private static boolean detectIfAnsiCapable() {
152156
if (Boolean.FALSE.equals(consoleAvailable)) {
153157
return false;
154158
}
155-
if ((consoleAvailable == null) && (System.console() == null)) {
156-
return false;
159+
if (consoleAvailable == null) {
160+
Console console = System.console();
161+
if (console == null) {
162+
return false;
163+
}
164+
Method isTerminalMethod = ClassUtils.getMethodIfAvailable(Console.class, "isTerminal");
165+
if (isTerminalMethod != null) {
166+
Boolean isTerminal = (Boolean) isTerminalMethod.invoke(console);
167+
if (Boolean.FALSE.equals(isTerminal)) {
168+
return false;
169+
}
170+
}
157171
}
158172
return !(OPERATING_SYSTEM_NAME.contains("win"));
159173
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
plugins {
2+
id "java"
3+
id "org.springframework.boot.conventions"
4+
id "org.springframework.boot.integration-test"
5+
}
6+
7+
description = "Spring Boot Console Integration Tests"
8+
9+
configurations {
10+
app
11+
}
12+
13+
dependencies {
14+
app project(path: ":spring-boot-project:spring-boot-dependencies", configuration: "mavenRepository")
15+
app project(path: ":spring-boot-project:spring-boot-tools:spring-boot-gradle-plugin", configuration: "mavenRepository")
16+
app project(path: ":spring-boot-project:spring-boot-starters:spring-boot-starter", configuration: "mavenRepository")
17+
18+
intTestImplementation(enforcedPlatform(project(":spring-boot-project:spring-boot-parent")))
19+
intTestImplementation(project(":spring-boot-project:spring-boot-tools:spring-boot-test-support"))
20+
intTestImplementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-test"))
21+
intTestImplementation("org.testcontainers:junit-jupiter")
22+
intTestImplementation("org.testcontainers:testcontainers")
23+
}
24+
25+
task syncMavenRepository(type: Sync) {
26+
from configurations.app
27+
into "${buildDir}/int-test-maven-repository"
28+
}
29+
30+
task syncAppSource(type: org.springframework.boot.build.SyncAppSource) {
31+
sourceDirectory = file("spring-boot-console-tests-app")
32+
destinationDirectory = file("${buildDir}/spring-boot-console-tests-app")
33+
}
34+
35+
task buildApp(type: GradleBuild) {
36+
dependsOn syncAppSource, syncMavenRepository
37+
dir = "${buildDir}/spring-boot-console-tests-app"
38+
startParameter.buildCacheEnabled = false
39+
tasks = ["build"]
40+
}
41+
42+
intTest {
43+
dependsOn buildApp
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
plugins {
2+
id "java"
3+
id "org.springframework.boot"
4+
}
5+
6+
apply plugin: "io.spring.dependency-management"
7+
8+
repositories {
9+
maven { url "file:${rootDir}/../int-test-maven-repository"}
10+
mavenCentral()
11+
maven { url "https://repo.spring.io/snapshot" }
12+
maven { url "https://repo.spring.io/milestone" }
13+
}
14+
15+
dependencies {
16+
implementation("org.springframework.boot:spring-boot-starter")
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
pluginManagement {
2+
repositories {
3+
maven { url "file:${rootDir}/../int-test-maven-repository"}
4+
mavenCentral()
5+
maven { url "https://repo.spring.io/snapshot" }
6+
maven { url "https://repo.spring.io/milestone" }
7+
}
8+
resolutionStrategy {
9+
eachPlugin {
10+
if (requested.id.id == "org.springframework.boot") {
11+
useModule "org.springframework.boot:spring-boot-gradle-plugin:${requested.version}"
12+
}
13+
}
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.consoleapp;
18+
19+
import org.springframework.boot.SpringApplication;
20+
import org.springframework.boot.autoconfigure.SpringBootApplication;
21+
import org.springframework.boot.ansi.AnsiOutput;
22+
import org.springframework.boot.ansi.AnsiOutput.Enabled;
23+
24+
@SpringBootApplication
25+
public class ConsoleTestApplication {
26+
27+
public static void main(String[] args) {
28+
System.out.println("System.console() is " + System.console());
29+
SpringApplication.run(ConsoleTestApplication.class, args);
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.console;
18+
19+
import java.io.File;
20+
import java.nio.charset.StandardCharsets;
21+
import java.time.Duration;
22+
import java.util.function.Supplier;
23+
24+
import org.junit.jupiter.api.Test;
25+
import org.testcontainers.containers.GenericContainer;
26+
import org.testcontainers.containers.output.ToStringConsumer;
27+
import org.testcontainers.containers.startupcheck.OneShotStartupCheckStrategy;
28+
import org.testcontainers.utility.DockerImageName;
29+
import org.testcontainers.utility.MountableFile;
30+
31+
import org.springframework.boot.system.JavaVersion;
32+
import org.springframework.boot.testsupport.testcontainers.DisabledIfDockerUnavailable;
33+
import org.springframework.util.Assert;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
37+
/**
38+
* Integration tests that checks ANSI output is not turned on in JDK 22 or later.
39+
*
40+
* @author Yong-Hyun Kim
41+
*/
42+
@DisabledIfDockerUnavailable
43+
class ConsoleIntegrationTests {
44+
45+
private static final String ENCODE_START = "\033[";
46+
47+
private static final JavaRuntime JDK_17_RUNTIME = JavaRuntime.openJdk(JavaVersion.SEVENTEEN);
48+
49+
private static final JavaRuntime JDK_22_RUNTIME = JavaRuntime.openJdk(JavaVersion.TWENTY_TWO);
50+
51+
private final ToStringConsumer output = new ToStringConsumer().withRemoveAnsiCodes(false);
52+
53+
@Test
54+
void runJarOn17() {
55+
try (GenericContainer<?> container = createContainer(JDK_17_RUNTIME)) {
56+
container.start();
57+
assertThat(this.output.toString(StandardCharsets.UTF_8)).contains("System.console() is null")
58+
.doesNotContain(ENCODE_START);
59+
}
60+
}
61+
62+
@Test
63+
void runJarOn22() {
64+
try (GenericContainer<?> container = createContainer(JDK_22_RUNTIME)) {
65+
container.start();
66+
assertThat(this.output.toString(StandardCharsets.UTF_8)).doesNotContain("System.console() is null")
67+
.doesNotContain(ENCODE_START);
68+
}
69+
}
70+
71+
private GenericContainer<?> createContainer(JavaRuntime javaRuntime) {
72+
return javaRuntime.getContainer()
73+
.withLogConsumer(this.output)
74+
.withCopyFileToContainer(findApplication(), "/app.jar")
75+
.withStartupCheckStrategy(new OneShotStartupCheckStrategy().withTimeout(Duration.ofMinutes(5)))
76+
.withCommand("java", "-jar", "app.jar");
77+
}
78+
79+
private MountableFile findApplication() {
80+
return MountableFile.forHostPath(findJarFile().toPath());
81+
}
82+
83+
private File findJarFile() {
84+
String path = String.format("build/%1$s/build/libs/%1$s.jar", "spring-boot-console-tests-app");
85+
File jar = new File(path);
86+
Assert.state(jar.isFile(), () -> "Could not find " + path + ". Have you built it?");
87+
return jar;
88+
}
89+
90+
static final class JavaRuntime {
91+
92+
private final String name;
93+
94+
private final JavaVersion version;
95+
96+
private final Supplier<GenericContainer<?>> container;
97+
98+
private JavaRuntime(String name, JavaVersion version, Supplier<GenericContainer<?>> container) {
99+
this.name = name;
100+
this.version = version;
101+
this.container = container;
102+
}
103+
104+
private boolean isCompatible() {
105+
return this.version.isEqualOrNewerThan(JavaVersion.getJavaVersion());
106+
}
107+
108+
GenericContainer<?> getContainer() {
109+
return this.container.get();
110+
}
111+
112+
@Override
113+
public String toString() {
114+
return this.name;
115+
}
116+
117+
static JavaRuntime openJdk(JavaVersion version) {
118+
String imageVersion = version.toString();
119+
DockerImageName image = DockerImageName.parse("bellsoft/liberica-openjdk-debian:" + imageVersion);
120+
return new JavaRuntime("OpenJDK " + imageVersion, version, () -> new GenericContainer<>(image));
121+
}
122+
123+
}
124+
125+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
<include resource="org/springframework/boot/logging/logback/base.xml"/>
4+
</configuration>

0 commit comments

Comments
 (0)