|
| 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 | +} |
0 commit comments