diff --git a/Parse/src/main/java/com/parse/ParseFile.java b/Parse/src/main/java/com/parse/ParseFile.java index 2080fd45c..7393c937b 100644 --- a/Parse/src/main/java/com/parse/ParseFile.java +++ b/Parse/src/main/java/com/parse/ParseFile.java @@ -327,6 +327,7 @@ public Task then(Task task) throws Exception { state = task.getResult(); // Since we have successfully uploaded the file, we do not need to hold the file pointer // anymore. + data = null; file = null; return task.makeVoid(); } @@ -424,31 +425,18 @@ public byte[] getData() throws ParseException { * @return A Task that is resolved when the data has been fetched. */ public Task getDataInBackground(final ProgressCallback progressCallback) { - // If data is already available, just return immediately. - if (data != null) { - // in-memory - return Task.forResult(data); - } - final Task.TaskCompletionSource cts = Task.create(); currentTasks.add(cts); return taskQueue.enqueue(new Continuation>() { @Override public Task then(Task toAwait) throws Exception { - // If data is already available, just return immediately. - if (data != null) { - // in-memory - return Task.forResult(data); - } - return fetchInBackground(progressCallback, toAwait, cts.getTask()).onSuccess(new Continuation() { @Override public byte[] then(Task task) throws Exception { File file = task.getResult(); try { - data = ParseFileUtils.readFileToByteArray(file); - return data; + return ParseFileUtils.readFileToByteArray(file); } catch (IOException e) { // do nothing } @@ -550,7 +538,7 @@ public Task then(Task task) throws Exception { * @return A Task that is resolved when the data has been fetched. */ public Task getFileInBackground() { - return getFileInBackground((ProgressCallback)null); + return getFileInBackground((ProgressCallback) null); } /** diff --git a/Parse/src/test/java/com/parse/ParseFileTest.java b/Parse/src/test/java/com/parse/ParseFileTest.java index 0df277a0f..c685b0d6a 100644 --- a/Parse/src/test/java/com/parse/ParseFileTest.java +++ b/Parse/src/test/java/com/parse/ParseFileTest.java @@ -58,31 +58,29 @@ public void testConstructor() throws Exception { String contentType = "content_type"; File file = temporaryFolder.newFile(name); + // TODO(mengyan): After we have proper staging strategy, we should verify the staging file's + // content is the same with the original file. + ParseFile parseFile = new ParseFile(name, data, contentType); assertEquals("name", parseFile.getName()); - assertEquals("hello", new String(parseFile.getData())); assertEquals("content_type", parseFile.getState().mimeType()); assertTrue(parseFile.isDirty()); parseFile = new ParseFile(data); assertEquals("file", parseFile.getName()); // Default - assertEquals("hello", new String(parseFile.getData())); assertEquals(null, parseFile.getState().mimeType()); assertTrue(parseFile.isDirty()); parseFile = new ParseFile(name, data); assertEquals("name", parseFile.getName()); - assertEquals("hello", new String(parseFile.getData())); assertEquals(null, parseFile.getState().mimeType()); assertTrue(parseFile.isDirty()); parseFile = new ParseFile(data, contentType); assertEquals("file", parseFile.getName()); // Default - assertEquals("hello", new String(parseFile.getData())); assertEquals("content_type", parseFile.getState().mimeType()); assertTrue(parseFile.isDirty()); - // TODO(mengyan): Test file pointer in ParseFile when we have proper stage strategy parseFile = new ParseFile(file); assertEquals(name, parseFile.getName()); // Default assertEquals(null, parseFile.getState().mimeType()); @@ -291,6 +289,22 @@ public void testGetDataAsyncSuccess() throws Exception { assertEquals(url, stateCaptor.getValue().url()); // Verify the data we get is correct assertArrayEquals(content.getBytes(), data); + + // Make sure we always get the data from network + byte[] dataAgain = ParseTaskUtils.wait(parseFile.getDataInBackground()); + + // Verify controller get the correct data + ArgumentCaptor stateCaptorAgain = + ArgumentCaptor.forClass(ParseFile.State.class); + verify(controller, times(2)).fetchAsync( + stateCaptorAgain.capture(), + anyString(), + any(ProgressCallback.class), + Matchers.>any() + ); + assertEquals(url, stateCaptorAgain.getValue().url()); + // Verify the data we get is correct + assertArrayEquals(content.getBytes(), dataAgain); } @Test @@ -325,6 +339,22 @@ public void testGetDataStreamAsyncSuccess() throws Exception { assertEquals(url, stateCaptor.getValue().url()); // Verify the data we get is correct assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(dataStream)); + + // Make sure we always get the data from network + InputStream dataStreamAgain = ParseTaskUtils.wait(parseFile.getDataStreamInBackground()); + + // Verify controller get the correct data + ArgumentCaptor stateCaptorAgain = + ArgumentCaptor.forClass(ParseFile.State.class); + verify(controller, times(2)).fetchAsync( + stateCaptorAgain.capture(), + anyString(), + any(ProgressCallback.class), + Matchers.>any() + ); + assertEquals(url, stateCaptorAgain.getValue().url()); + // Verify the data we get is correct + assertArrayEquals(content.getBytes(), ParseIOUtils.toByteArray(dataStreamAgain)); } @Test @@ -359,6 +389,22 @@ public void testGetFileAsyncSuccess() throws Exception { assertEquals(url, stateCaptor.getValue().url()); // Verify the data we get is correct assertArrayEquals(content.getBytes(), ParseFileUtils.readFileToByteArray(fetchedFile)); + + // Make sure we always get the data from network + File fetchedFileAgain = ParseTaskUtils.wait(parseFile.getFileInBackground()); + + // Verify controller get the correct data + ArgumentCaptor stateCaptorAgain = + ArgumentCaptor.forClass(ParseFile.State.class); + verify(controller, times(2)).fetchAsync( + stateCaptorAgain.capture(), + anyString(), + any(ProgressCallback.class), + Matchers.>any() + ); + assertEquals(url, stateCaptorAgain.getValue().url()); + // Verify the data we get is correct + assertArrayEquals(content.getBytes(), ParseFileUtils.readFileToByteArray(fetchedFileAgain)); } //endregion