@VisibleForTesting
   static <T extends Exception> T wrapExceptionWithMessage(
      T exception, String msg) {
    Class<? extends Throwable> exceptionClass = exception.getClass();
    try {
      Constructor<? extends Throwable> ctor = exceptionClass
          .getConstructor(String.class);
      Throwable t = ctor.newInstance(msg);
      return (T) (t.initCause(exception));
    } catch (Throwable e) {
      LOG.debug("Unable to wrap exception of type {}, it has "
          + "no (String) constructor.", exceptionClass, e);
      return exception;
    }
  }
@Test(timeout = 60000)
  public void testWrapExceptionWithMessage() {
    IOException ex;
    ex = new IOException("Induced exception");
    ex = KerberosAuthenticator.wrapExceptionWithMessage(ex, "Error while "
        + "authenticating with endpoint: localhost");
    Assert.assertEquals("Induced exception", ex.getCause().getMessage());
    Assert.assertEquals("Error while authenticating with endpoint: localhost",
        ex.getMessage());

    ex = new AuthenticationException("Auth exception");
    ex = KerberosAuthenticator.wrapExceptionWithMessage(ex, "Error while "
        + "authenticating with endpoint: localhost");
    Assert.assertEquals("Auth exception", ex.getCause().getMessage());
    Assert.assertEquals("Error while authenticating with endpoint: localhost",
        ex.getMessage());

    // Test for Exception with  no (String) constructor
    // redirect the LOG to and check log message
    ex = new CharacterCodingException();
    Exception ex2 = KerberosAuthenticator.wrapExceptionWithMessage(ex,
        "Error while authenticating with endpoint: localhost");
    Assert.assertTrue(ex instanceof CharacterCodingException);
    Assert.assertTrue(ex.equals(ex2));
  }