KerberosAuthenticator implements Authenticator { @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; } }  @Override void setConnectionConfigurator(ConnectionConfigurator configurator); @Override void authenticate(URL url, AuthenticatedURL.Token token); static final String WWW_AUTHENTICATE; static final String AUTHORIZATION; static final String NEGOTIATE; }
@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()); ex = new CharacterCodingException(); Exception ex2 = KerberosAuthenticator.wrapExceptionWithMessage(ex, "Error while authenticating with endpoint: localhost"); Assert.assertTrue(ex instanceof CharacterCodingException); Assert.assertTrue(ex.equals(ex2)); }