CallbackHandlerImpl implements CallbackHandler { @Override public void handle(Callback[] callbacks) throws UnsupportedCallbackException, IOException { for (Callback callBack : callbacks) { if (callBack instanceof NameCallback) { NameCallback nameCallback = (NameCallback) callBack; nameCallback.setName(name); } else if (callBack instanceof PasswordCallback) { PasswordCallback passwordCallback = (PasswordCallback) callBack; passwordCallback.setPassword(password.toCharArray()); } else { throw new UnsupportedCallbackException(callBack, "Callback not supported"); } } } CallbackHandlerImpl(String name, String password); @Override void handle(Callback[] callbacks);  }
@Test public void test() throws Exception { NameCallback nameCallback = new NameCallback("username"); PasswordCallback passwordCallback = new PasswordCallback("password", false); new CallbackHandlerImpl("tester", "secret").handle(new Callback[] {nameCallback, passwordCallback}); assertThat(nameCallback.getName()).isEqualTo("tester"); assertThat(passwordCallback.getPassword()).isEqualTo("secret".toCharArray()); }
@Test(expected = UnsupportedCallbackException.class) public void unsupportedCallback() throws Exception { new CallbackHandlerImpl("tester", "secret").handle(new Callback[] {mock(Callback.class)}); }