private Exception convertErrorToException(int code, String message) { if (code == ERROR_CODE_NETWORK_ERROR) { return new IOException(message); } if (code == ERROR_CODE_UNSUPPORTED_OPERATION) { return new UnsupportedOperationException(message); } if (code == ERROR_CODE_INVALID_RESPONSE) { return new AuthenticatorException(message); } if (code == ERROR_CODE_BAD_ARGUMENTS) { return new IllegalArgumentException(message); } return new AuthenticatorException(message); }
@Override public Bundle doWork() throws OperationCanceledException, IOException, AuthenticatorException { if (!authenticators.containsKey(accountType)) { throw new AuthenticatorException("No authenticator specified for " + accountType); } resultBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType); if (activity == null) { Intent resultIntent = new Intent(); resultBundle.putParcelable(AccountManager.KEY_INTENT, resultIntent); } else if (callback == null) { resultBundle.putString(AccountManager.KEY_ACCOUNT_NAME, "some_user@gmail.com"); } return resultBundle; } }
private Bundle getAuthToken(Account account, String authTokenType) throws OperationCanceledException, IOException, AuthenticatorException { Bundle result = new Bundle(); String authToken = blockingGetAuthToken(account, authTokenType, false); result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name); result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type); result.putString(AccountManager.KEY_AUTHTOKEN, authToken); if (authToken != null) { return result; } if (!authenticators.containsKey(account.type)) { throw new AuthenticatorException("No authenticator specified for " + account.type); } Intent resultIntent = new Intent(); result.putParcelable(AccountManager.KEY_INTENT, resultIntent); return result; }
@Override public Account[] doWork() throws OperationCanceledException, IOException, AuthenticatorException { if (authenticationErrorOnNextResponse) { setAuthenticationErrorOnNextResponse(false); throw new AuthenticatorException(); } List<Account> result = new ArrayList<>(); Account[] accountsByType = getAccountsByType(type); for (Account account : accountsByType) { Set<String> featureSet = accountFeatures.get(account); if (features == null || featureSet.containsAll(Arrays.asList(features))) { result.add(account); } } return result.toArray(new Account[result.size()]); } });
@Override public T getResult() throws OperationCanceledException, IOException, AuthenticatorException { start(); if (exception instanceof OperationCanceledException) { throw new OperationCanceledException(exception); } else if (exception instanceof IOException) { throw new IOException(exception); } else if (exception instanceof AuthenticatorException) { throw new AuthenticatorException(exception); } return result; }
@Implementation protected AccountManagerFuture<Account[]> getAccountsByTypeAndFeatures( final String type, final String[] features, AccountManagerCallback<Account[]> callback, Handler handler) { return start( new BaseRoboAccountManagerFuture<Account[]>(callback, handler) { @Override public Account[] doWork() throws OperationCanceledException, IOException, AuthenticatorException { if (authenticationErrorOnNextResponse) { setAuthenticationErrorOnNextResponse(false); throw new AuthenticatorException(); } List<Account> result = new ArrayList<>(); Account[] accountsByType = getAccountsByType(type); for (Account account : accountsByType) { Set<String> featureSet = accountFeatures.get(account); if (features == null || featureSet.containsAll(Arrays.asList(features))) { result.add(account); } } return result.toArray(new Account[result.size()]); } }); }
throw (IOException) cause; } else if (cause instanceof UnsupportedOperationException) { throw new AuthenticatorException(cause); } else if (cause instanceof AuthenticatorException) { throw (AuthenticatorException) cause;
@Test(expected = AuthFailureError.class) public void failedGetAuthToken() throws Exception { when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture); when(mFuture.getResult()).thenThrow(new AuthenticatorException("sadness!")); mAuthenticator.getAuthToken(); }
@Test(expected = AuthFailureError.class) public void failedGetAuthToken() throws Exception { when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture); when(mFuture.getResult()).thenThrow(new AuthenticatorException("sadness!")); mAuthenticator.getAuthToken(); }
@Test public void shouldHandleExceptionsThrownByFuture() throws Exception { when(authPreferences.getOauth2Token()).thenReturn("token"); when(authPreferences.getOauth2Username()).thenReturn("username"); AccountManagerFuture<Bundle> future = mock(AccountManagerFuture.class); when(accountManager.getAuthToken(notNull(Account.class), anyString(), isNull(Bundle.class), anyBoolean(), any(AccountManagerCallback.class), any(Handler.class))).thenReturn(future); AuthenticatorException exception = new AuthenticatorException(); when(future.getResult()).thenThrow(exception); try { refresher.refreshOAuth2Token(); fail("expected exception"); } catch (TokenRefreshException e) { assertThat(e.getCause()).isSameAs(exception); } verify(accountManager).invalidateAuthToken(GOOGLE_TYPE, "token"); }
@Override public Bundle getResult(long l, TimeUnit timeUnit) throws OperationCanceledException, IOException, AuthenticatorException { if (!authenticators.containsKey(accountType)) { throw new AuthenticatorException("No authenticator specified for " + accountType); } return resultBundle; } }
@Override public Bundle getResult() throws OperationCanceledException, IOException, AuthenticatorException { if (!authenticators.containsKey(accountType)) { throw new AuthenticatorException("No authenticator specified for " + accountType); } resultBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType); return resultBundle; }
@Override public Bundle doWork() throws OperationCanceledException, IOException, AuthenticatorException { if (!authenticators.containsKey(accountType)) { throw new AuthenticatorException("No authenticator specified for " + accountType); } resultBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType); if (activity == null) { Intent resultIntent = new Intent(); resultBundle.putParcelable(AccountManager.KEY_INTENT, resultIntent); } else if (callback == null) { resultBundle.putString(AccountManager.KEY_ACCOUNT_NAME, "some_user@gmail.com"); } return resultBundle; } }
@Override public Bundle doWork() throws OperationCanceledException, IOException, AuthenticatorException { if (!authenticators.containsKey(accountType)) { throw new AuthenticatorException("No authenticator specified for " + accountType); } resultBundle.putString(AccountManager.KEY_ACCOUNT_TYPE, accountType); if (activity == null) { Intent resultIntent = new Intent(); resultBundle.putParcelable(AccountManager.KEY_INTENT, resultIntent); } else if (callback == null) { resultBundle.putString(AccountManager.KEY_ACCOUNT_NAME, "some_user@gmail.com"); } return resultBundle; } }
@Override public T getResult() throws OperationCanceledException, IOException, AuthenticatorException { start(); if (exception instanceof OperationCanceledException) { throw new OperationCanceledException(exception); } else if (exception instanceof IOException) { throw new IOException(exception); } else if (exception instanceof AuthenticatorException) { throw new AuthenticatorException(exception); } return result; }
@Override public T getResult() throws OperationCanceledException, IOException, AuthenticatorException { start(); if (exception instanceof OperationCanceledException) { throw new OperationCanceledException(exception); } else if (exception instanceof IOException) { throw new IOException(exception); } else if (exception instanceof AuthenticatorException) { throw new AuthenticatorException(exception); } return result; }
@Override public T getResult() throws OperationCanceledException, IOException, AuthenticatorException { start(); if (exception instanceof OperationCanceledException) { throw new OperationCanceledException(exception); } else if (exception instanceof IOException) { throw new IOException(exception); } else if (exception instanceof AuthenticatorException) { throw new AuthenticatorException(exception); } return result; }
@Test(expected = AuthFailureError.class) public void failedGetAuthToken() throws Exception { when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture); when(mFuture.getResult()).thenThrow(new AuthenticatorException("sadness!")); mAuthenticator.getAuthToken(); }
@Test(expected = AuthFailureError.class) public void failedGetAuthToken() throws Exception { when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture); when(mFuture.getResult()).thenThrow(new AuthenticatorException("sadness!")); mAuthenticator.getAuthToken(); }
@Test(expected = AuthFailureError.class) public void failedGetAuthToken() throws Exception { when(mAccountManager.getAuthToken(mAccount, "cooltype", false, null, null)).thenReturn(mFuture); when(mFuture.getResult()).thenThrow(new AuthenticatorException("sadness!")); mAuthenticator.getAuthToken(); }