@Test public void testSaveAsyncAlreadyCancelled() throws Exception { ParseHttpClient restClient = mock(ParseHttpClient.class); ParseFileController controller = new ParseFileController(restClient, null); ParseFile.State state = new ParseFile.State.Builder().build(); Task<Void> cancellationToken = Task.cancelled(); Task<ParseFile.State> task = controller.saveAsync(state, (byte[]) null, null, null, cancellationToken); task.waitForCompletion(); verify(restClient, times(0)).execute(any(ParseHttpRequest.class)); assertTrue(task.isCancelled()); }
public Task<ParseFile.State> saveAsync( final ParseFile.State state, final byte[] data, String sessionToken, ProgressCallback uploadProgressCallback, Task<Void> cancellationToken) { if (state.url() != null) { return Task.forResult(state); } if (cancellationToken != null && cancellationToken.isCancelled()) { return Task.cancelled(); } final ParseRESTCommand command = new ParseRESTFileCommand.Builder() .fileName(state.name()) .data(data) .contentType(state.mimeType()) .sessionToken(sessionToken) .build(); return command.executeAsync( restClient, uploadProgressCallback, null, cancellationToken ).onSuccess(new Continuation<JSONObject, ParseFile.State>() { @Override public ParseFile.State then(Task<JSONObject> task) throws Exception { JSONObject result = task.getResult(); ParseFile.State newState = new ParseFile.State.Builder(state) .name(result.getString("name")) .url(result.getString("url")) .build(); try { ParseFileUtils.writeByteArrayToFile(getCacheFile(newState), data); } catch (IOException e) { } return newState; } }, ParseExecutors.io()); }
ParseFileController { public Task<ParseFile.State> saveAsync( final ParseFile.State state, final byte[] data, String sessionToken, ProgressCallback uploadProgressCallback, Task<Void> cancellationToken) { if (state.url() != null) { return Task.forResult(state); } if (cancellationToken != null && cancellationToken.isCancelled()) { return Task.cancelled(); } final ParseRESTCommand command = new ParseRESTFileCommand.Builder() .fileName(state.name()) .data(data) .contentType(state.mimeType()) .sessionToken(sessionToken) .build(); return command.executeAsync( restClient, uploadProgressCallback, null, cancellationToken ).onSuccess(new Continuation<JSONObject, ParseFile.State>() { @Override public ParseFile.State then(Task<JSONObject> task) throws Exception { JSONObject result = task.getResult(); ParseFile.State newState = new ParseFile.State.Builder(state) .name(result.getString("name")) .url(result.getString("url")) .build(); try { ParseFileUtils.writeByteArrayToFile(getCacheFile(newState), data); } catch (IOException e) { } return newState; } }, ParseExecutors.io()); } }
ParseFileController { public Task<ParseFile.State> saveAsync( final ParseFile.State state, final byte[] data, String sessionToken, ProgressCallback uploadProgressCallback, Task<Void> cancellationToken) { if (state.url() != null) { return Task.forResult(state); } if (cancellationToken != null && cancellationToken.isCancelled()) { return Task.cancelled(); } final ParseRESTCommand command = new ParseRESTFileCommand.Builder() .fileName(state.name()) .data(data) .contentType(state.mimeType()) .sessionToken(sessionToken) .build(); return command.executeAsync( restClient, uploadProgressCallback, null, cancellationToken ).onSuccess(new Continuation<JSONObject, ParseFile.State>() { @Override public ParseFile.State then(Task<JSONObject> task) throws Exception { JSONObject result = task.getResult(); ParseFile.State newState = new ParseFile.State.Builder(state) .name(result.getString("name")) .url(result.getString("url")) .build(); try { ParseFileUtils.writeByteArrayToFile(getCacheFile(newState), data); } catch (IOException e) { } return newState; } }, ParseExecutors.io()); } ParseFileController(ParseHttpClient restClient, File cachePath); }
ParseFileController { public Task<ParseFile.State> saveAsync( final ParseFile.State state, final byte[] data, String sessionToken, ProgressCallback uploadProgressCallback, Task<Void> cancellationToken) { if (state.url() != null) { return Task.forResult(state); } if (cancellationToken != null && cancellationToken.isCancelled()) { return Task.cancelled(); } final ParseRESTCommand command = new ParseRESTFileCommand.Builder() .fileName(state.name()) .data(data) .contentType(state.mimeType()) .sessionToken(sessionToken) .build(); return command.executeAsync( restClient, uploadProgressCallback, null, cancellationToken ).onSuccess(new Continuation<JSONObject, ParseFile.State>() { @Override public ParseFile.State then(Task<JSONObject> task) throws Exception { JSONObject result = task.getResult(); ParseFile.State newState = new ParseFile.State.Builder(state) .name(result.getString("name")) .url(result.getString("url")) .build(); try { ParseFileUtils.writeByteArrayToFile(getCacheFile(newState), data); } catch (IOException e) { } return newState; } }, ParseExecutors.io()); } ParseFileController(ParseHttpClient restClient, File cachePath); File getCacheFile(ParseFile.State state); boolean isDataAvailable(ParseFile.State state); void clearCache(); Task<ParseFile.State> saveAsync(
            final ParseFile.State state,
            final byte[] data,
            String sessionToken,
            ProgressCallback uploadProgressCallback,
            Task<Void> cancellationToken); Task<ParseFile.State> saveAsync(
            final ParseFile.State state,
            final File file,
            String sessionToken,
            ProgressCallback uploadProgressCallback,
            Task<Void> cancellationToken); Task<File> fetchAsync(
            final ParseFile.State state,
            @SuppressWarnings("UnusedParameters") String sessionToken,
            final ProgressCallback downloadProgressCallback,
            final Task<Void> cancellationToken); }
ParseFileController { public Task<ParseFile.State> saveAsync( final ParseFile.State state, final byte[] data, String sessionToken, ProgressCallback uploadProgressCallback, Task<Void> cancellationToken) { if (state.url() != null) { return Task.forResult(state); } if (cancellationToken != null && cancellationToken.isCancelled()) { return Task.cancelled(); } final ParseRESTCommand command = new ParseRESTFileCommand.Builder() .fileName(state.name()) .data(data) .contentType(state.mimeType()) .sessionToken(sessionToken) .build(); return command.executeAsync( restClient, uploadProgressCallback, null, cancellationToken ).onSuccess(new Continuation<JSONObject, ParseFile.State>() { @Override public ParseFile.State then(Task<JSONObject> task) throws Exception { JSONObject result = task.getResult(); ParseFile.State newState = new ParseFile.State.Builder(state) .name(result.getString("name")) .url(result.getString("url")) .build(); try { ParseFileUtils.writeByteArrayToFile(getCacheFile(newState), data); } catch (IOException e) { } return newState; } }, ParseExecutors.io()); } ParseFileController(ParseHttpClient restClient, File cachePath); File getCacheFile(ParseFile.State state); boolean isDataAvailable(ParseFile.State state); void clearCache(); Task<ParseFile.State> saveAsync(
            final ParseFile.State state,
            final byte[] data,
            String sessionToken,
            ProgressCallback uploadProgressCallback,
            Task<Void> cancellationToken); Task<ParseFile.State> saveAsync(
            final ParseFile.State state,
            final File file,
            String sessionToken,
            ProgressCallback uploadProgressCallback,
            Task<Void> cancellationToken); Task<File> fetchAsync(
            final ParseFile.State state,
            @SuppressWarnings("UnusedParameters") String sessionToken,
            final ProgressCallback downloadProgressCallback,
            final Task<Void> cancellationToken);  }