Skip to content

Commit 6adf790

Browse files
committed
HBX-3313: Fix file handle leaks that cause test failures on Windows
- Use try-with-resources for InputStream in ResourceUtil.createResources() - Use try-with-resources for BufferedReader in FileUtil.findFirstString() Signed-off-by: Koen Aers <koen.aers@gmail.com>
1 parent 66ee986 commit 6adf790

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

test/utils/src/main/java/org/hibernate/tools/test/util/FileUtil.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,13 @@ public class FileUtil {
2828

2929
static public String findFirstString(String string, File file) {
3030
String str;
31-
try {
32-
BufferedReader in = new BufferedReader(new FileReader(file) );
31+
try (BufferedReader in = new BufferedReader(new FileReader(file))) {
3332
while ( (str = in.readLine() ) != null ) {
3433
if(str.indexOf(string)>=0) {
3534
break;
3635
}
3736
}
38-
in.close();
39-
}
37+
}
4038
catch (IOException e) {
4139
throw new RuntimeException("trouble with searching in " + file,e);
4240
}

test/utils/src/main/java/org/hibernate/tools/test/util/ResourceUtil.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ public class ResourceUtil {
2828
public static void createResources(Object test, String[] resources, File resourcesDir) {
2929
try {
3030
for (String resource : resources) {
31-
InputStream inputStream = resolveResourceLocation(test.getClass(), resource);
32-
File resourceFile = new File(resourcesDir, resource);
33-
File parent = resourceFile.getParentFile();
34-
if (!parent.exists()) {
35-
parent.mkdirs();
31+
try (InputStream inputStream = resolveResourceLocation(test.getClass(), resource)) {
32+
File resourceFile = new File(resourcesDir, resource);
33+
File parent = resourceFile.getParentFile();
34+
if (!parent.exists()) {
35+
parent.mkdirs();
36+
}
37+
Files.copy(inputStream, resourceFile.toPath());
3638
}
37-
Files.copy(inputStream, resourceFile.toPath());
3839
}
3940
} catch (IOException e) {
4041
throw new RuntimeException(e);

0 commit comments

Comments
 (0)