@Test public void testAuthenticateAsyncFailure() { TwitterController controller = new TwitterController(twitter); Context context = mock(Context.class); Task<Map<String, String>> task = controller.authenticateAsync(context); ArgumentCaptor<AsyncCallback> captor = ArgumentCaptor.forClass(AsyncCallback.class); verify(twitter).authorize(eq(context), captor.capture()); AsyncCallback callback = captor.getValue(); callback.onFailure(new RuntimeException()); assertTrue(task.isFaulted()); }
public Task<Map<String, String>> authenticateAsync(Context context) { final Task<Map<String, String>>.TaskCompletionSource tcs = Task.create(); if (currentTcs != null) { handleCancel(currentTcs); } currentTcs = tcs; if (context == null) { throw new IllegalStateException( "Context must be non-null for Twitter authentication to proceed."); } twitter.authorize(context, new AsyncCallback() { @Override public void onCancel() { handleCancel(tcs); } @Override public void onFailure(Throwable error) { if (currentTcs != tcs) { return; } try { if (error instanceof Exception) { tcs.trySetError((Exception) error); } else { tcs.trySetError(new ParseException(error)); } } finally { currentTcs = null; } } @Override public void onSuccess(Object result) { if (currentTcs != tcs) { return; } try { Map<String, String> authData = getAuthData( twitter.getUserId(), twitter.getScreenName(), twitter.getAuthToken(), twitter.getAuthTokenSecret()); tcs.trySetResult(authData); } finally { currentTcs = null; } } }); return tcs.getTask(); }
TwitterController { public Task<Map<String, String>> authenticateAsync(Context context) { final Task<Map<String, String>>.TaskCompletionSource tcs = Task.create(); if (currentTcs != null) { handleCancel(currentTcs); } currentTcs = tcs; if (context == null) { throw new IllegalStateException( "Context must be non-null for Twitter authentication to proceed."); } twitter.authorize(context, new AsyncCallback() { @Override public void onCancel() { handleCancel(tcs); } @Override public void onFailure(Throwable error) { if (currentTcs != tcs) { return; } try { if (error instanceof Exception) { tcs.trySetError((Exception) error); } else { tcs.trySetError(new ParseException(error)); } } finally { currentTcs = null; } } @Override public void onSuccess(Object result) { if (currentTcs != tcs) { return; } try { Map<String, String> authData = getAuthData( twitter.getUserId(), twitter.getScreenName(), twitter.getAuthToken(), twitter.getAuthTokenSecret()); tcs.trySetResult(authData); } finally { currentTcs = null; } } }); return tcs.getTask(); } }
TwitterController { public Task<Map<String, String>> authenticateAsync(Context context) { final Task<Map<String, String>>.TaskCompletionSource tcs = Task.create(); if (currentTcs != null) { handleCancel(currentTcs); } currentTcs = tcs; if (context == null) { throw new IllegalStateException( "Context must be non-null for Twitter authentication to proceed."); } twitter.authorize(context, new AsyncCallback() { @Override public void onCancel() { handleCancel(tcs); } @Override public void onFailure(Throwable error) { if (currentTcs != tcs) { return; } try { if (error instanceof Exception) { tcs.trySetError((Exception) error); } else { tcs.trySetError(new ParseException(error)); } } finally { currentTcs = null; } } @Override public void onSuccess(Object result) { if (currentTcs != tcs) { return; } try { Map<String, String> authData = getAuthData( twitter.getUserId(), twitter.getScreenName(), twitter.getAuthToken(), twitter.getAuthTokenSecret()); tcs.trySetResult(authData); } finally { currentTcs = null; } } }); return tcs.getTask(); } TwitterController();   TwitterController(Twitter twitter); }
TwitterController { public Task<Map<String, String>> authenticateAsync(Context context) { final Task<Map<String, String>>.TaskCompletionSource tcs = Task.create(); if (currentTcs != null) { handleCancel(currentTcs); } currentTcs = tcs; if (context == null) { throw new IllegalStateException( "Context must be non-null for Twitter authentication to proceed."); } twitter.authorize(context, new AsyncCallback() { @Override public void onCancel() { handleCancel(tcs); } @Override public void onFailure(Throwable error) { if (currentTcs != tcs) { return; } try { if (error instanceof Exception) { tcs.trySetError((Exception) error); } else { tcs.trySetError(new ParseException(error)); } } finally { currentTcs = null; } } @Override public void onSuccess(Object result) { if (currentTcs != tcs) { return; } try { Map<String, String> authData = getAuthData( twitter.getUserId(), twitter.getScreenName(), twitter.getAuthToken(), twitter.getAuthTokenSecret()); tcs.trySetResult(authData); } finally { currentTcs = null; } } }); return tcs.getTask(); } TwitterController();   TwitterController(Twitter twitter); void initialize(String consumerKey, String consumerSecret); Twitter getTwitter(); Task<Map<String, String>> authenticateAsync(Context context); Map<String, String> getAuthData(
            String userId,
            String screenName,
            String authToken,
            String authTokenSecret); void setAuthData(Map<String, String> authData); }
TwitterController { public Task<Map<String, String>> authenticateAsync(Context context) { final Task<Map<String, String>>.TaskCompletionSource tcs = Task.create(); if (currentTcs != null) { handleCancel(currentTcs); } currentTcs = tcs; if (context == null) { throw new IllegalStateException( "Context must be non-null for Twitter authentication to proceed."); } twitter.authorize(context, new AsyncCallback() { @Override public void onCancel() { handleCancel(tcs); } @Override public void onFailure(Throwable error) { if (currentTcs != tcs) { return; } try { if (error instanceof Exception) { tcs.trySetError((Exception) error); } else { tcs.trySetError(new ParseException(error)); } } finally { currentTcs = null; } } @Override public void onSuccess(Object result) { if (currentTcs != tcs) { return; } try { Map<String, String> authData = getAuthData( twitter.getUserId(), twitter.getScreenName(), twitter.getAuthToken(), twitter.getAuthTokenSecret()); tcs.trySetResult(authData); } finally { currentTcs = null; } } }); return tcs.getTask(); } TwitterController();   TwitterController(Twitter twitter); void initialize(String consumerKey, String consumerSecret); Twitter getTwitter(); Task<Map<String, String>> authenticateAsync(Context context); Map<String, String> getAuthData(
            String userId,
            String screenName,
            String authToken,
            String authTokenSecret); void setAuthData(Map<String, String> authData);  }