From ebebe09a9d0f49deb8dac9cd2992d7c79f1b39ec Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 15 Jan 2020 20:38:59 +0000 Subject: [PATCH] Fix file handle leak in JarFileTests The JarFile was not being closed which linked a file handle and caused a test failure on Windows. The local variable has been renamed as, when declared in a try-with-resources, Checkstyle was confused by the shadowing of the jarFile field and required references to jarFile within the try-block the be prefixed with this. See gh-19595 --- .../boot/loader/jar/JarFileTests.java | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java index 97329cb9ef..4c74f6f71c 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-loader/src/test/java/org/springframework/boot/loader/jar/JarFileTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -511,11 +511,12 @@ public class JarFileTests { jarOutputStream.write(new byte[] { (byte) 1 }); jarOutputStream.closeEntry(); } - JarFile jarFile = new JarFile(file); - Enumeration entries = jarFile.entries(); - JarEntry entry = entries.nextElement(); - assertThat(entry.getLastModifiedTime().toInstant()).isEqualTo(Instant.EPOCH); - assertThat(entry.getName()).isEqualTo("1.dat"); + try (JarFile jar = new JarFile(file)) { + Enumeration entries = jar.entries(); + JarEntry entry = entries.nextElement(); + assertThat(entry.getLastModifiedTime().toInstant()).isEqualTo(Instant.EPOCH); + assertThat(entry.getName()).isEqualTo("1.dat"); + } } private int getJavaVersion() {