Skip to content

Commit d71889e

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 7e09589 commit d71889e

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
@@ -30,15 +30,13 @@ public class FileUtil {
3030

3131
static public String findFirstString(String string, File file) {
3232
String str;
33-
try {
34-
BufferedReader in = new BufferedReader(new FileReader(file) );
33+
try (BufferedReader in = new BufferedReader(new FileReader(file))) {
3534
while ( (str = in.readLine() ) != null ) {
3635
if(str.indexOf(string)>=0) {
3736
break;
3837
}
3938
}
40-
in.close();
41-
}
39+
}
4240
catch (IOException e) {
4341
throw new RuntimeException("trouble with searching in " + file,e);
4442
}

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
@@ -30,13 +30,14 @@ public class ResourceUtil {
3030
public static void createResources(Object test, String[] resources, File resourcesDir) {
3131
try {
3232
for (String resource : resources) {
33-
InputStream inputStream = resolveResourceLocation(test.getClass(), resource);
34-
File resourceFile = new File(resourcesDir, resource);
35-
File parent = resourceFile.getParentFile();
36-
if (!parent.exists()) {
37-
parent.mkdirs();
33+
try (InputStream inputStream = resolveResourceLocation(test.getClass(), resource)) {
34+
File resourceFile = new File(resourcesDir, resource);
35+
File parent = resourceFile.getParentFile();
36+
if (!parent.exists()) {
37+
parent.mkdirs();
38+
}
39+
Files.copy(inputStream, resourceFile.toPath());
3840
}
39-
Files.copy(inputStream, resourceFile.toPath());
4041
}
4142
} catch (IOException e) {
4243
throw new RuntimeException(e);

0 commit comments

Comments
 (0)