Skip to content

Commit 600c8bc

Browse files
committed
Create cache dir if it does not exist before we create tempFile in ParseAWSRequest
1 parent 799d288 commit 600c8bc

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

Parse/src/main/java/com/parse/ParseAWSRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public Void call() throws Exception {
5656
InputStream responseStream = null;
5757
try {
5858
responseStream = response.getContent();
59-
FileOutputStream tempFileStream = new FileOutputStream(tempFile);
59+
FileOutputStream tempFileStream = ParseFileUtils.openOutputStream(tempFile);
6060

6161
int nRead;
6262
byte[] data = new byte[32 << 10]; // 32KB

Parse/src/test/java/com/parse/ParseFileControllerTest.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,34 @@ public void testFetchAsyncSuccess() throws Exception {
319319
assertFalse(controller.getTempFile(state).exists());
320320
}
321321

322+
@Test
323+
public void testFetchAsyncSuccessWithCacheDirNotExist() throws Exception {
324+
byte[] data = "hello".getBytes();
325+
ParseHttpResponse response = mock(ParseHttpResponse.class);
326+
when(response.getStatusCode()).thenReturn(200);
327+
when(response.getContent()).thenReturn(new ByteArrayInputStream(data));
328+
when(response.getTotalSize()).thenReturn((long) data.length);
329+
330+
ParseHttpClient awsClient = mock(ParseHttpClient.class);
331+
when(awsClient.execute(any(ParseHttpRequest.class))).thenReturn(response);
332+
// Make sure cache dir does not exist
333+
File root = new File(temporaryFolder.getRoot(), "cache");
334+
assertFalse(root.exists());
335+
ParseFileController controller = new ParseFileController(null, root).awsClient(awsClient);
336+
337+
ParseFile.State state = new ParseFile.State.Builder()
338+
.name("file_name")
339+
.url("url")
340+
.build();
341+
Task<File> task = controller.fetchAsync(state, null, null, null);
342+
File result = ParseTaskUtils.wait(task);
343+
344+
verify(awsClient, times(1)).execute(any(ParseHttpRequest.class));
345+
assertTrue(result.exists());
346+
assertEquals("hello", ParseFileUtils.readFileToString(result, "UTF-8"));
347+
assertFalse(controller.getTempFile(state).exists());
348+
}
349+
322350
@Test
323351
public void testFetchAsyncFailure() throws Exception {
324352
// TODO(grantland): Remove once we no longer rely on retry logic.

0 commit comments

Comments
 (0)