Prefer File.delete over File.deleteOnExit

The former has deterministic behavior and avoids resource leaks.
This commit is contained in:
Andrew Gaul 2014-05-22 00:51:27 -07:00
parent 26b53e52b7
commit bae4377dca
4 changed files with 57 additions and 46 deletions

View File

@ -275,7 +275,6 @@ public abstract class BaseHttpCommandExecutorServiceIntegrationTest extends Base
try { try {
f = File.createTempFile("jclouds", "tmp"); f = File.createTempFile("jclouds", "tmp");
f.deleteOnExit();
long length = (new Random().nextInt(32) + 1) * 1024 * 1024; long length = (new Random().nextInt(32) + 1) * 1024 * 1024;
byte[] buf = new byte[1024]; byte[] buf = new byte[1024];
Arrays.fill(buf, (byte) 'a'); Arrays.fill(buf, (byte) 'a');

View File

@ -1163,23 +1163,26 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
Invokable<?, ?> method = method(TestMultipartForm.class, "withParamFilePart", String.class, Invokable<?, ?> method = method(TestMultipartForm.class, "withParamFilePart", String.class,
File.class); File.class);
File file = File.createTempFile("foo", "bar"); File file = File.createTempFile("foo", "bar");
Files.append("foobledata", file, UTF_8); try {
file.deleteOnExit(); Files.append("foobledata", file, UTF_8);
GeneratedHttpRequest httpRequest = processor.apply(Invocation.create(method, GeneratedHttpRequest httpRequest = processor.apply(Invocation.create(method,
ImmutableList.<Object> of("name", file))); ImmutableList.<Object> of("name", file)));
assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1"); assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, ""); assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, assertPayloadEquals(httpRequest,
"----JCLOUDS--\r\n" + // "----JCLOUDS--\r\n" + //
"Content-Disposition: form-data; name=\"name\"\r\n" + // "Content-Disposition: form-data; name=\"name\"\r\n" + //
"\r\n" + // "\r\n" + //
"name\r\n" + // / "name\r\n" + // /
"----JCLOUDS--\r\n" + // "----JCLOUDS--\r\n" + //
"Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n" + // "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n" + //
"\r\n" + // "\r\n" + //
"foobledata\r\n" + // "foobledata\r\n" + //
"----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false); "----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false);
} finally {
file.delete();
}
} }
public void testMultipartWithParamByteArrayPart() throws SecurityException, NoSuchMethodException, IOException { public void testMultipartWithParamByteArrayPart() throws SecurityException, NoSuchMethodException, IOException {
@ -1206,24 +1209,27 @@ public class RestAnnotationProcessorTest extends BaseRestApiTest {
Invokable<?, ?> method = method(TestMultipartForm.class, "withParamFileBinaryPart", Invokable<?, ?> method = method(TestMultipartForm.class, "withParamFileBinaryPart",
String.class, File.class); String.class, File.class);
File file = File.createTempFile("foo", "bar"); File file = File.createTempFile("foo", "bar");
Files.write(new byte[] { 17, 26, 39, 40, 50 }, file); try {
file.deleteOnExit(); Files.write(new byte[] { 17, 26, 39, 40, 50 }, file);
GeneratedHttpRequest httpRequest = processor.apply(Invocation.create(method, GeneratedHttpRequest httpRequest = processor.apply(Invocation.create(method,
ImmutableList.<Object> of("name", file))); ImmutableList.<Object> of("name", file)));
assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1"); assertRequestLineEquals(httpRequest, "POST http://localhost:9999 HTTP/1.1");
assertNonPayloadHeadersEqual(httpRequest, ""); assertNonPayloadHeadersEqual(httpRequest, "");
assertPayloadEquals(httpRequest, assertPayloadEquals(httpRequest,
"----JCLOUDS--\r\n" + // "----JCLOUDS--\r\n" + //
"Content-Disposition: form-data; name=\"name\"\r\n" + // "Content-Disposition: form-data; name=\"name\"\r\n" + //
"\r\n" + // "\r\n" + //
"name\r\n" + // / "name\r\n" + // /
"----JCLOUDS--\r\n" + // "----JCLOUDS--\r\n" + //
"Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n" + // "Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"\r\n" + //
"Content-Type: application/octet-stream\r\n" + // "Content-Type: application/octet-stream\r\n" + //
"\r\n" + // "\r\n" + //
"'(2\r\n" + // "'(2\r\n" + //
"----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false); "----JCLOUDS----\r\n", "multipart/form-data; boundary=--JCLOUDS--", false);
} finally {
file.delete();
}
} }
public interface TestPut { public interface TestPut {

View File

@ -160,12 +160,15 @@ public class JschSshClientLiveTest {
@Test @Test
public void testPutAndGet() throws IOException { public void testPutAndGet() throws IOException {
temp = File.createTempFile("foo", "bar"); temp = File.createTempFile("foo", "bar");
temp.deleteOnExit(); try {
SshClient client = setupClient(); SshClient client = setupClient();
client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit")); client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit"));
Payload input = setupClient().get(temp.getAbsolutePath()); Payload input = setupClient().get(temp.getAbsolutePath());
String contents = Strings2.toString(input); String contents = Strings2.toString(input);
assertEquals(contents, "rabbit"); assertEquals(contents, "rabbit");
} finally {
temp.delete();
}
} }
@Test @Test

View File

@ -147,12 +147,15 @@ public class SshjSshClientLiveTest {
public void testPutAndGet() throws IOException { public void testPutAndGet() throws IOException {
temp = File.createTempFile("foo", "bar"); temp = File.createTempFile("foo", "bar");
temp.deleteOnExit(); try {
SshClient client = setupClient(); SshClient client = setupClient();
client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit")); client.put(temp.getAbsolutePath(), Payloads.newStringPayload("rabbit"));
Payload input = client.get(temp.getAbsolutePath()); Payload input = client.get(temp.getAbsolutePath());
String contents = Strings2.toString(input); String contents = Strings2.toString(input);
assertEquals(contents, "rabbit"); assertEquals(contents, "rabbit");
} finally {
temp.delete();
}
} }
public void testGetEtcPassword() throws IOException { public void testGetEtcPassword() throws IOException {