From bae4377dca11c6c9f3db52e51a55455adf3d43f7 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Thu, 22 May 2014 00:51:27 -0700 Subject: [PATCH] Prefer File.delete over File.deleteOnExit The former has deterministic behavior and avoids resource leaks. --- ...CommandExecutorServiceIntegrationTest.java | 1 - .../internal/RestAnnotationProcessorTest.java | 72 ++++++++++--------- .../ssh/jsch/JschSshClientLiveTest.java | 15 ++-- .../jclouds/sshj/SshjSshClientLiveTest.java | 15 ++-- 4 files changed, 57 insertions(+), 46 deletions(-) diff --git a/core/src/test/java/org/jclouds/http/BaseHttpCommandExecutorServiceIntegrationTest.java b/core/src/test/java/org/jclouds/http/BaseHttpCommandExecutorServiceIntegrationTest.java index c10a354a47..cc2931d979 100644 --- a/core/src/test/java/org/jclouds/http/BaseHttpCommandExecutorServiceIntegrationTest.java +++ b/core/src/test/java/org/jclouds/http/BaseHttpCommandExecutorServiceIntegrationTest.java @@ -275,7 +275,6 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base try { f = File.createTempFile("jclouds", "tmp"); - f.deleteOnExit(); long length = (new Random().nextInt(32) + 1) * 1024 * 1024; byte[] buf = new byte[1024]; Arrays.fill(buf, (byte) 'a'); diff --git a/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java b/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java index c727802d7b..07c480d105 100644 --- a/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java +++ b/core/src/test/java/org/jclouds/rest/internal/RestAnnotationProcessorTest.java @@ -1163,23 +1163,26 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { Invokable method = method(TestMultipartForm.class, "withParamFilePart", String.class, File.class); File file = File.createTempFile("foo", "bar"); - Files.append("foobledata", file, UTF_8); - file.deleteOnExit(); + try { + Files.append("foobledata", file, UTF_8); - GeneratedHttpRequest httpRequest = processor.apply(Invocation.create(method, - ImmutableList. of("name", file))); - assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1"); - assertNonPayloadHeadersEqual(httpRequest, ""); - assertPayloadEquals(httpRequest, - "----JCLOUDS--\r\n" + // - "Content-Disposition: form-data; name=\"name\"\r\n" + // - "\r\n" + // - "name\r\n" + // / - "----JCLOUDS--\r\n" + // - "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n" + // - "\r\n" + // - "foobledata\r\n" + // - "----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false); + GeneratedHttpRequest httpRequest = processor.apply(Invocation.create(method, + ImmutableList. of("name", file))); + assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, ""); + assertPayloadEquals(httpRequest, + "----JCLOUDS--\r\n" + // + "Content-Disposition: form-data; name=\"name\"\r\n" + // + "\r\n" + // + "name\r\n" + // / + "----JCLOUDS--\r\n" + // + "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n" + // + "\r\n" + // + "foobledata\r\n" + // + "----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false); + } finally { + file.delete(); + } } public void testMultipartWithParamByteArrayPart() throws SecurityException, NoSuchMethodException, IOException { @@ -1206,24 +1209,27 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest { Invokable method = method(TestMultipartForm.class, "withParamFileBinaryPart", String.class, File.class); File file = File.createTempFile("foo", "bar"); - Files.write(new byte[] { 17, 26, 39, 40, 50 }, file); - file.deleteOnExit(); + try { + Files.write(new byte[] { 17, 26, 39, 40, 50 }, file); - GeneratedHttpRequest httpRequest = processor.apply(Invocation.create(method, - ImmutableList. of("name", file))); - assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1"); - assertNonPayloadHeadersEqual(httpRequest, ""); - assertPayloadEquals(httpRequest, - "----JCLOUDS--\r\n" + // - "Content-Disposition: form-data; name=\"name\"\r\n" + // - "\r\n" + // - "name\r\n" + // / - "----JCLOUDS--\r\n" + // - "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n" + // - "Content-Type: application/octet-stream\r\n" + // - "\r\n" + // - "'(2\r\n" + // - "----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false); + GeneratedHttpRequest httpRequest = processor.apply(Invocation.create(method, + ImmutableList. of("name", file))); + assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, ""); + assertPayloadEquals(httpRequest, + "----JCLOUDS--\r\n" + // + "Content-Disposition: form-data; name=\"name\"\r\n" + // + "\r\n" + // + "name\r\n" + // / + "----JCLOUDS--\r\n" + // + "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n" + // + "Content-Type: application/octet-stream\r\n" + // + "\r\n" + // + "'(2\r\n" + // + "----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false); + } finally { + file.delete(); + } } public interface TestPut { diff --git a/drivers/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientLiveTest.java b/drivers/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientLiveTest.java index 37d3de09aa..fa009611c2 100644 --- a/drivers/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientLiveTest.java +++ b/drivers/jsch/src/test/java/org/jclouds/ssh/jsch/JschSshClientLiveTest.java @@ -160,12 +160,15 @@ public class JschSshClientLiveTest { @Test public void testPutAndGet() throws IOException { temp = File.createTempFile("foo", "bar"); - temp.deleteOnExit(); - SshClient client = setupClient(); - client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit")); - Payload input = setupClient().get(temp.getAbsolutePath()); - String contents = Strings2.toString(input); - assertEquals(contents, "rabbit"); + try { + SshClient client = setupClient(); + client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit")); + Payload input = setupClient().get(temp.getAbsolutePath()); + String contents = Strings2.toString(input); + assertEquals(contents, "rabbit"); + } finally { + temp.delete(); + } } @Test diff --git a/drivers/sshj/src/test/java/org/jclouds/sshj/SshjSshClientLiveTest.java b/drivers/sshj/src/test/java/org/jclouds/sshj/SshjSshClientLiveTest.java index 1aba367012..d6e2b0f2be 100644 --- a/drivers/sshj/src/test/java/org/jclouds/sshj/SshjSshClientLiveTest.java +++ b/drivers/sshj/src/test/java/org/jclouds/sshj/SshjSshClientLiveTest.java @@ -147,12 +147,15 @@ public class SshjSshClientLiveTest { public void testPutAndGet() throws IOException { temp = File.createTempFile("foo", "bar"); - temp.deleteOnExit(); - SshClient client = setupClient(); - client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit")); - Payload input = client.get(temp.getAbsolutePath()); - String contents = Strings2.toString(input); - assertEquals(contents, "rabbit"); + try { + SshClient client = setupClient(); + client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit")); + Payload input = client.get(temp.getAbsolutePath()); + String contents = Strings2.toString(input); + assertEquals(contents, "rabbit"); + } finally { + temp.delete(); + } } public void testGetEtcPassword() throws IOException {