From 3ea18f07fa0751dd709037b1efcb476f176efaea Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Thu, 13 Oct 2022 11:26:55 -0500 Subject: [PATCH] Polish "Use NBT plugin version from gradle.properties in image tests" See gh-32643 --- .../gradle/testkit/GradleBuild.java | 27 +++++++++++++++++++ .../boot/image/paketo/PaketoBuilderTests.java | 12 +-------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-test-support/src/main/java/org/springframework/boot/testsupport/gradle/testkit/GradleBuild.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-test-support/src/main/java/org/springframework/boot/testsupport/gradle/testkit/GradleBuild.java index 72bcd1483f..2884ba17f3 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-test-support/src/main/java/org/springframework/boot/testsupport/gradle/testkit/GradleBuild.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-test-support/src/main/java/org/springframework/boot/testsupport/gradle/testkit/GradleBuild.java @@ -17,6 +17,7 @@ package org.springframework.boot.testsupport.gradle.testkit; import java.io.File; +import java.io.FileInputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; @@ -28,6 +29,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Properties; import java.util.jar.JarFile; import com.fasterxml.jackson.annotation.JsonView; @@ -57,6 +59,7 @@ import org.springframework.util.FileCopyUtils; import org.springframework.util.FileSystemUtils; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.fail; /** * A {@code GradleBuild} is used to run a Gradle build using {@link GradleRunner}. @@ -176,6 +179,11 @@ public class GradleBuild { return this; } + public GradleBuild scriptPropertyFrom(File propertiesFile, String key) { + this.scriptProperties.put(key, getProperty(propertiesFile, key)); + return this; + } + public BuildResult build(String... arguments) { try { BuildResult result = prepareRunner(arguments).build(); @@ -291,4 +299,23 @@ public class GradleBuild { } } + private String getProperty(File propertiesFile, String key) { + try { + assertThat(propertiesFile).withFailMessage("Expecting properties file to exist at path '%s'", + propertiesFile.getCanonicalFile()).exists(); + Properties properties = new Properties(); + try (FileInputStream input = new FileInputStream(propertiesFile)) { + properties.load(input); + String value = properties.getProperty(key); + assertThat(value).withFailMessage("Expecting properties file '%s' to contain the key '%s'", + propertiesFile.getCanonicalFile(), key).isNotEmpty(); + return value; + } + } + catch (IOException ex) { + fail("Error reading properties file '" + propertiesFile + "'"); + return null; + } + } + } diff --git a/spring-boot-system-tests/spring-boot-image-tests/src/systemTest/java/org/springframework/boot/image/paketo/PaketoBuilderTests.java b/spring-boot-system-tests/spring-boot-image-tests/src/systemTest/java/org/springframework/boot/image/paketo/PaketoBuilderTests.java index 4af8b5b2ca..f0cf2d4d57 100644 --- a/spring-boot-system-tests/spring-boot-image-tests/src/systemTest/java/org/springframework/boot/image/paketo/PaketoBuilderTests.java +++ b/spring-boot-system-tests/spring-boot-image-tests/src/systemTest/java/org/springframework/boot/image/paketo/PaketoBuilderTests.java @@ -17,7 +17,6 @@ package org.springframework.boot.image.paketo; import java.io.File; -import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; @@ -25,7 +24,6 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import java.util.Properties; import java.util.function.Consumer; import java.util.function.Predicate; import java.util.jar.Attributes; @@ -73,19 +71,11 @@ class PaketoBuilderTests { void configureGradleBuild() throws IOException { this.gradleBuild.scriptProperty("systemTestMavenRepository", new File("build/system-test-maven-repository").getAbsoluteFile().toURI().toASCIIString()); - this.gradleBuild.scriptProperty("nativeBuildToolsVersion", getNativeBuildToolsVersion()); + this.gradleBuild.scriptPropertyFrom(new File("../../gradle.properties"), "nativeBuildToolsVersion"); this.gradleBuild.expectDeprecationMessages("BPL_SPRING_CLOUD_BINDINGS_ENABLED.*true.*Deprecated"); this.gradleBuild.expectDeprecationMessages("BOM table is deprecated"); } - private String getNativeBuildToolsVersion() throws IOException { - Properties gradleProperties = new Properties(); - try (FileInputStream input = new FileInputStream("../../gradle.properties")) { - gradleProperties.load(input); - return gradleProperties.getProperty("nativeBuildToolsVersion"); - } - } - @Test void executableJarApp() throws Exception { writeMainClass();