Use AutoClosables with try-with-resources

Closes gh-33538
This commit is contained in:
Moritz Halbritter
2022-12-16 15:43:15 +01:00
parent 725337f976
commit f36e2ecb7b
16 changed files with 121 additions and 77 deletions

View File

@@ -57,35 +57,43 @@ class BuildpackCoordinatesTests extends AbstractJsonTests {
}
@Test
void fromTomlWhenMissingIDThrowsException() {
InputStream coordinates = createTomlStream(null, null, true, false);
assertThatIllegalArgumentException().isThrownBy(() -> BuildpackCoordinates.fromToml(coordinates, this.archive))
.withMessageContaining("Buildpack descriptor must contain ID")
.withMessageContaining(this.archive.toString());
void fromTomlWhenMissingIDThrowsException() throws IOException {
try (InputStream coordinates = createTomlStream(null, null, true, false)) {
assertThatIllegalArgumentException()
.isThrownBy(() -> BuildpackCoordinates.fromToml(coordinates, this.archive))
.withMessageContaining("Buildpack descriptor must contain ID")
.withMessageContaining(this.archive.toString());
}
}
@Test
void fromTomlWhenMissingVersionThrowsException() {
InputStream coordinates = createTomlStream("example/buildpack1", null, true, false);
assertThatIllegalArgumentException().isThrownBy(() -> BuildpackCoordinates.fromToml(coordinates, this.archive))
.withMessageContaining("Buildpack descriptor must contain version")
.withMessageContaining(this.archive.toString());
void fromTomlWhenMissingVersionThrowsException() throws IOException {
try (InputStream coordinates = createTomlStream("example/buildpack1", null, true, false)) {
assertThatIllegalArgumentException()
.isThrownBy(() -> BuildpackCoordinates.fromToml(coordinates, this.archive))
.withMessageContaining("Buildpack descriptor must contain version")
.withMessageContaining(this.archive.toString());
}
}
@Test
void fromTomlWhenMissingStacksAndOrderThrowsException() {
InputStream coordinates = createTomlStream("example/buildpack1", "0.0.1", false, false);
assertThatIllegalArgumentException().isThrownBy(() -> BuildpackCoordinates.fromToml(coordinates, this.archive))
.withMessageContaining("Buildpack descriptor must contain either 'stacks' or 'order'")
.withMessageContaining(this.archive.toString());
void fromTomlWhenMissingStacksAndOrderThrowsException() throws IOException {
try (InputStream coordinates = createTomlStream("example/buildpack1", "0.0.1", false, false)) {
assertThatIllegalArgumentException()
.isThrownBy(() -> BuildpackCoordinates.fromToml(coordinates, this.archive))
.withMessageContaining("Buildpack descriptor must contain either 'stacks' or 'order'")
.withMessageContaining(this.archive.toString());
}
}
@Test
void fromTomlWhenContainsBothStacksAndOrderThrowsException() {
InputStream coordinates = createTomlStream("example/buildpack1", "0.0.1", true, true);
assertThatIllegalArgumentException().isThrownBy(() -> BuildpackCoordinates.fromToml(coordinates, this.archive))
.withMessageContaining("Buildpack descriptor must not contain both 'stacks' and 'order'")
.withMessageContaining(this.archive.toString());
void fromTomlWhenContainsBothStacksAndOrderThrowsException() throws IOException {
try (InputStream coordinates = createTomlStream("example/buildpack1", "0.0.1", true, true)) {
assertThatIllegalArgumentException()
.isThrownBy(() -> BuildpackCoordinates.fromToml(coordinates, this.archive))
.withMessageContaining("Buildpack descriptor must not contain both 'stacks' and 'order'")
.withMessageContaining(this.archive.toString());
}
}
@Test

View File

@@ -585,8 +585,12 @@ abstract class AbstractBootArchiveIntegrationTests {
for (String layerName : layerNames) {
File layer = new File(root, layerName);
assertThat(layer).isDirectory();
extractedLayers.put(layerName, Files.walk(layer.toPath()).filter((path) -> path.toFile().isFile())
.map(layer.toPath()::relativize).map(Path::toString).map(StringUtils::cleanPath).toList());
List<String> files;
try (Stream<Path> pathStream = Files.walk(layer.toPath())) {
files = pathStream.filter((path) -> path.toFile().isFile()).map(layer.toPath()::relativize)
.map(Path::toString).map(StringUtils::cleanPath).toList();
}
extractedLayers.put(layerName, files);
}
return extractedLayers;
}

View File

@@ -294,9 +294,10 @@ class JarFileTests {
void getEntryUrlStream() throws Exception {
URL url = new URL(this.jarFile.getUrl(), "1.dat");
url.openConnection();
InputStream stream = url.openStream();
assertThat(stream.read()).isEqualTo(1);
assertThat(stream.read()).isEqualTo(-1);
try (InputStream stream = url.openStream()) {
assertThat(stream.read()).isEqualTo(1);
assertThat(stream.read()).isEqualTo(-1);
}
}
@Test

View File

@@ -20,6 +20,7 @@ import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;
import org.junit.jupiter.api.TestTemplate;
@@ -173,10 +174,10 @@ public class AotTests {
});
}
Stream<Path> collectRelativePaths(Path sourceDirectory) {
try {
return Files.walk(sourceDirectory).filter(Files::isRegularFile)
.map((path) -> path.subpath(sourceDirectory.getNameCount(), path.getNameCount()));
List<Path> collectRelativePaths(Path sourceDirectory) {
try (Stream<Path> pathStream = Files.walk(sourceDirectory)) {
return pathStream.filter(Files::isRegularFile)
.map((path) -> path.subpath(sourceDirectory.getNameCount(), path.getNameCount())).toList();
}
catch (IOException ex) {
throw new IllegalStateException(ex);

View File

@@ -47,6 +47,8 @@ class MavenBuildExtension implements TestTemplateInvocationContextProvider {
@Override
public Stream<TestTemplateInvocationContext> provideTestTemplateInvocationContexts(ExtensionContext context) {
try {
// Returning a stream which must be closed here is fine, as JUnit will take
// care of closing it
return Files.list(Paths.get("build/maven-binaries")).map(MavenVersionTestTemplateInvocationContext::new);
}
catch (IOException ex) {

View File

@@ -28,6 +28,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Stream;
import javax.tools.Diagnostic;
import javax.tools.DiagnosticListener;
@@ -124,7 +125,10 @@ public abstract class AbstractAotMojo extends AbstractDependencyFilterMojo {
protected final void compileSourceFiles(URL[] classPath, File sourcesDirectory, File outputDirectory)
throws Exception {
List<Path> sourceFiles = Files.walk(sourcesDirectory.toPath()).filter(Files::isRegularFile).toList();
List<Path> sourceFiles;
try (Stream<Path> pathStream = Files.walk(sourcesDirectory.toPath())) {
sourceFiles = pathStream.filter(Files::isRegularFile).toList();
}
if (sourceFiles.isEmpty()) {
return;
}
@@ -167,8 +171,13 @@ public abstract class AbstractAotMojo extends AbstractDependencyFilterMojo {
}
protected final void copyAll(Path from, Path to) throws IOException {
List<Path> files = (Files.exists(from)) ? Files.walk(from).filter(Files::isRegularFile).toList()
: Collections.emptyList();
if (!Files.exists(from)) {
return;
}
List<Path> files;
try (Stream<Path> pathStream = Files.walk(from)) {
files = pathStream.filter(Files::isRegularFile).toList();
}
for (Path file : files) {
String relativeFileName = file.subpath(from.getNameCount(), file.getNameCount()).toString();
getLog().debug("Copying '" + relativeFileName + "' to " + to);