diff options
author | Ahmed Ashour <asashour@yahoo.com> | 2017-03-07 09:38:05 +0100 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-03-07 10:38:05 +0200 |
commit | 628ff25ae5eb55a3a8e3cdd09e07044e356b2d72 (patch) | |
tree | 94ddf8a3d3053f7c24b6305e16dc7e4b0b8b6ff5 | |
parent | 2adb8f9837980fece3ea79bbcfccbcfb1cfc0f3e (diff) | |
download | vaadin-framework-628ff25ae5eb55a3a8e3cdd09e07044e356b2d72.tar.gz vaadin-framework-628ff25ae5eb55a3a8e3cdd09e07044e356b2d72.zip |
Use try-with-resources (#8757)
To ensure the resource is automatically closed
5 files changed, 43 insertions, 42 deletions
diff --git a/server/src/main/java/com/vaadin/server/themeutils/SASSAddonImportFileCreator.java b/server/src/main/java/com/vaadin/server/themeutils/SASSAddonImportFileCreator.java index a087a1ed31..79d87f960a 100644 --- a/server/src/main/java/com/vaadin/server/themeutils/SASSAddonImportFileCreator.java +++ b/server/src/main/java/com/vaadin/server/themeutils/SASSAddonImportFileCreator.java @@ -85,9 +85,8 @@ public class SASSAddonImportFileCreator { LocationInfo info = ClassPathExplorer .getAvailableWidgetSetsAndStylesheets(); - try { - PrintStream printStream = new PrintStream( - new FileOutputStream(addonImports)); + try (PrintStream printStream = new PrintStream( + new FileOutputStream(addonImports))) { printStream.println("/* " + ADDON_IMPORTS_FILE_TEXT + " */"); diff --git a/server/src/test/java/com/vaadin/tests/server/ClassesSerializableTest.java b/server/src/test/java/com/vaadin/tests/server/ClassesSerializableTest.java index a3375585c7..5659ec7c56 100644 --- a/server/src/test/java/com/vaadin/tests/server/ClassesSerializableTest.java +++ b/server/src/test/java/com/vaadin/tests/server/ClassesSerializableTest.java @@ -352,15 +352,16 @@ public class ClassesSerializableTest { private Collection<String> findClassesInJar(File file) throws IOException { Collection<String> classes = new ArrayList<>(); - JarFile jar = new JarFile(file); - Enumeration<JarEntry> e = jar.entries(); - while (e.hasMoreElements()) { - JarEntry entry = e.nextElement(); - if (entry.getName().endsWith(".class")) { - String nameWithoutExtension = entry.getName() - .replaceAll("\\.class", ""); - String className = nameWithoutExtension.replace('/', '.'); - classes.add(className); + try (JarFile jar = new JarFile(file)) { + Enumeration<JarEntry> e = jar.entries(); + while (e.hasMoreElements()) { + JarEntry entry = e.nextElement(); + if (entry.getName().endsWith(".class")) { + String nameWithoutExtension = entry.getName() + .replaceAll("\\.class", ""); + String className = nameWithoutExtension.replace('/', '.'); + classes.add(className); + } } } return classes; diff --git a/server/src/test/java/com/vaadin/tests/server/SimpleMultiPartInputStreamTest.java b/server/src/test/java/com/vaadin/tests/server/SimpleMultiPartInputStreamTest.java index 14b839be6a..06ae1570b2 100644 --- a/server/src/test/java/com/vaadin/tests/server/SimpleMultiPartInputStreamTest.java +++ b/server/src/test/java/com/vaadin/tests/server/SimpleMultiPartInputStreamTest.java @@ -22,22 +22,23 @@ public class SimpleMultiPartInputStreamTest { protected void checkBoundaryDetection(byte[] input, String boundary, byte[] expected) throws Exception { ByteArrayInputStream bais = new ByteArrayInputStream(input); - SimpleMultiPartInputStream smpis = new SimpleMultiPartInputStream(bais, - boundary); - ByteArrayOutputStream resultStream = new ByteArrayOutputStream(); - int outbyte; - try { - while ((outbyte = smpis.read()) != -1) { - resultStream.write(outbyte); + try (SimpleMultiPartInputStream smpis = new SimpleMultiPartInputStream(bais, + boundary)) { + ByteArrayOutputStream resultStream = new ByteArrayOutputStream(); + int outbyte; + try { + while ((outbyte = smpis.read()) != -1) { + resultStream.write(outbyte); + } + } catch (IOException e) { + throw new IOException( + e.getMessage() + "; expected " + new String(expected) + + " but got " + resultStream.toString()); + } + if (!Arrays.equals(expected, resultStream.toByteArray())) { + throw new Exception("Mismatch: expected " + new String(expected) + + " but got " + resultStream.toString()); } - } catch (IOException e) { - throw new IOException( - e.getMessage() + "; expected " + new String(expected) - + " but got " + resultStream.toString()); - } - if (!Arrays.equals(expected, resultStream.toByteArray())) { - throw new Exception("Mismatch: expected " + new String(expected) - + " but got " + resultStream.toString()); } } diff --git a/uitest/src/main/java/com/vaadin/tests/components/table/EditableTableLeak.java b/uitest/src/main/java/com/vaadin/tests/components/table/EditableTableLeak.java index 7b837553cb..156caf3437 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/table/EditableTableLeak.java +++ b/uitest/src/main/java/com/vaadin/tests/components/table/EditableTableLeak.java @@ -45,15 +45,14 @@ public class EditableTableLeak extends TestBase { } public static long getSize(Object object) { - ByteCountNullOutputStream os = new ByteCountNullOutputStream(); - ObjectOutputStream oos; - try { - oos = new ObjectOutputStream(os); + try (ByteCountNullOutputStream os = new ByteCountNullOutputStream()) { + ObjectOutputStream oos = new ObjectOutputStream(os); oos.writeObject(object); + return os.getBytes(); } catch (IOException e) { e.printStackTrace(); + return 0; } - return os.getBytes(); } } diff --git a/uitest/src/test/java/com/vaadin/tests/tb3/AbstractTB3Test.java b/uitest/src/test/java/com/vaadin/tests/tb3/AbstractTB3Test.java index 0322d76949..c151e57e9c 100644 --- a/uitest/src/test/java/com/vaadin/tests/tb3/AbstractTB3Test.java +++ b/uitest/src/test/java/com/vaadin/tests/tb3/AbstractTB3Test.java @@ -942,16 +942,17 @@ public abstract class AbstractTB3Test extends ParallelTest { String hostName = ce.getAddressOfRemoteServer().getHost(); int port = ce.getAddressOfRemoteServer().getPort(); HttpHost host = new HttpHost(hostName, port); - DefaultHttpClient client = new DefaultHttpClient(); - URL sessionURL = new URL("http://" + hostName + ":" + port - + "/grid/api/testsession?session=" + d.getSessionId()); - BasicHttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest( - "POST", sessionURL.toExternalForm()); - HttpResponse response = client.execute(host, r); - JsonObject object = extractObject(response); - URL myURL = new URL(object.getString("proxyId")); - if ((myURL.getHost() != null) && (myURL.getPort() != -1)) { - return myURL.getHost(); + try (DefaultHttpClient client = new DefaultHttpClient()) { + URL sessionURL = new URL("http://" + hostName + ":" + port + + "/grid/api/testsession?session=" + d.getSessionId()); + BasicHttpEntityEnclosingRequest r = new BasicHttpEntityEnclosingRequest( + "POST", sessionURL.toExternalForm()); + HttpResponse response = client.execute(host, r); + JsonObject object = extractObject(response); + URL myURL = new URL(object.getString("proxyId")); + if ((myURL.getHost() != null) && (myURL.getPort() != -1)) { + return myURL.getHost(); + } } } catch (Exception e) { e.printStackTrace(); |