Polish "Use NBT plugin version from gradle.properties in image tests"

See gh-32643
This commit is contained in:
Scott Frederick 2022-10-13 11:26:55 -05:00
parent 42ab82c0b1
commit 3ea18f07fa
2 changed files with 28 additions and 11 deletions

View File

@ -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;
}
}
}

View File

@ -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();