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 {
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');

View File

@ -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.<Object> 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.<Object> 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.<Object> 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.<Object> 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 {

View File

@ -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

View File

@ -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 {