コード例 #1
0
    public void TestShouldRetry() {
      DfaErrorHandler errorHandler = null;

      // Should not retry if RetryCount == 0
      errorHandler = new DfaErrorHandler(adRemoteService);
      user.Config.RetryCount = 0;
      Assert.IsFalse(errorHandler.ShouldRetry(new DfaCredentialsExpiredException(USER_TOKEN)));

      // Should not retry if exception is not DfaCredentialsExpiredException.
      errorHandler = new DfaErrorHandler(adRemoteService);
      user.Config.RetryCount = 1;
      Assert.IsFalse(errorHandler.ShouldRetry(new ArgumentNullException()));

      // Should retry if exception is DfaCredentialsExpiredException and
      // RetryCount > 0
      errorHandler = new DfaErrorHandler(adRemoteService);
      user.Config.RetryCount = 1;
      Assert.IsTrue(errorHandler.ShouldRetry(new DfaCredentialsExpiredException(USER_TOKEN)));
    }
コード例 #2
0
        /// <summary>
        /// Gets a custom exception that wraps the SOAP exception thrown
        /// by the server.
        /// </summary>
        /// <param name="exception">SOAPException that was thrown by the server.</param>
        /// <returns>A custom exception object that wraps the SOAP exception.
        /// </returns>
        protected override Exception GetCustomException(SoapException exception)
        {
            string defaultNs = GetDefaultNamespace();

            object apiException = Activator.CreateInstance(Type.GetType(
                                                               this.GetType().Namespace + ".ApiException"));
            XmlNode   faultNode = GetDetailsNode(exception);
            ErrorCode errorCode = null;

            if (faultNode != null)
            {
                errorCode = new ErrorCode();
                foreach (XmlElement xNode in faultNode.SelectNodes("*"))
                {
                    switch (xNode.Name)
                    {
                    case "errorCode":
                        errorCode.Code = int.Parse(xNode.InnerText);
                        break;

                    case "errorMessage":
                        errorCode.Description = xNode.InnerText;
                        break;
                    }
                }
            }
            DfaApiException dfaApiException = new DfaApiException(errorCode, exception.Message,
                                                                  exception);

            if (DfaErrorHandler.IsTokenExpiredError(dfaApiException))
            {
                return(new DfaCredentialsExpiredException(this.Token));
            }
            else
            {
                return(dfaApiException);
            }
        }
コード例 #3
0
    public void TestPrepareForRetry() {
      LoginUtil.Cache.AddToken(USER_NAME, USER_TOKEN);
      (user.Config as DfaAppConfig).DfaAuthToken = PASSWORD;
      adRemoteService.Token = USER_TOKEN;
      user.Config.RetryCount = 1;

      // PrepareForRetry should reset all relevant values when presented with
      // correct values.
      DfaErrorHandler errorHandler = new DfaErrorHandler(adRemoteService);
      errorHandler.PrepareForRetry(new DfaCredentialsExpiredException(USER_TOKEN));
      Assert.IsNull(LoginUtil.Cache.GetToken(USER_NAME));
      Assert.IsNull(adRemoteService.Token);
      Assert.IsNull((user.Config as DfaAppConfig).DfaAuthToken);

      // Indirectly test retryCount. This should become 1 after
      // PrepareForRetry() and another attempt to retry should fail.
      Assert.IsFalse(errorHandler.ShouldRetry(new DfaCredentialsExpiredException(USER_TOKEN)));

      // PrepareRetry should rethrow any exception that is not
      // DfaCredentialsExpiredException
      errorHandler = new DfaErrorHandler(adRemoteService);

      Exception ex = new Exception();
      try {
        errorHandler.PrepareForRetry(ex);
        Assert.Fail("Should not prepare for retry if exception is not " +
            "DfaCredentialsExpiredException.");
      } catch (Exception e) {
        Assert.AreSame(ex, e);
      }
    }