From b96cbb4e656da2d765e9290fdff5dfc3fa65fcee Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 23 Aug 2019 12:56:47 +0200 Subject: [PATCH] Close streams in tests This commit ensures that file streams are properly closed in tests. This seems to cause issues on Windows as the OS cannot delete temp folders. This is similar to spring-io/initializr#862 See gh-23507 --- .../CandidateComponentsIndexerTests.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/spring-context-indexer/src/test/java/org/springframework/context/index/processor/CandidateComponentsIndexerTests.java b/spring-context-indexer/src/test/java/org/springframework/context/index/processor/CandidateComponentsIndexerTests.java index 9b359989cd..a8d649e381 100644 --- a/spring-context-indexer/src/test/java/org/springframework/context/index/processor/CandidateComponentsIndexerTests.java +++ b/spring-context-indexer/src/test/java/org/springframework/context/index/processor/CandidateComponentsIndexerTests.java @@ -244,17 +244,18 @@ class CandidateComponentsIndexerTests { } private CandidateComponentsMetadata readGeneratedMetadata(File outputLocation) { - try { - File metadataFile = new File(outputLocation, MetadataStore.METADATA_PATH); - if (metadataFile.isFile()) { - return PropertiesMarshaller.read(new FileInputStream(metadataFile)); + File metadataFile = new File(outputLocation, MetadataStore.METADATA_PATH); + if (metadataFile.isFile()) { + try (FileInputStream fileInputStream = new FileInputStream(metadataFile)) { + CandidateComponentsMetadata metadata = PropertiesMarshaller.read(fileInputStream); + return metadata; } - else { - return new CandidateComponentsMetadata(); + catch (IOException ex) { + throw new IllegalStateException("Failed to read metadata from disk", ex); } } - catch (IOException ex) { - throw new IllegalStateException("Failed to read metadata from disk", ex); + else { + return new CandidateComponentsMetadata(); } }