UntrustedCertificateException extends CertificateException { @Override public String getMessage() { StringBuilder sb = new StringBuilder(150); sb.append(super.getMessage()); if (x509Certificates != null && x509Certificates.length > 0) { sb.append("\n\nFollow certificates (in PEM format) presented by the peer. Content between being/end certificate (including) can be stored in a file and imported using keytool, e.g. 'keytool -importcert -file cert.cer -alias certAlias -keystore keystore.jks'). Make sure the presented certificates are issued by your trusted CA before adding them to the keystore.\n\n"); for (X509Certificate cert : x509Certificates) { sb.append("Subject: ").append(cert.getSubjectDN()).append("\n"); sb.append("Serial number: ").append(cert.getSerialNumber()).append("\n"); appendThumbPrint(cert, sb); sb.append("\n"); appendCertificate(cert, sb); sb.append("\n"); } } return sb.toString(); } UntrustedCertificateException(String msg, X509Certificate[] x509Certificates); X509Certificate[] getX509Certificates(); @Override String getMessage();  }
@Test public void testExceptionGetMessage() { X509Certificate certificate = keyManager.getCertificate("apollo"); UntrustedCertificateException exception = new UntrustedCertificateException("Error in cert", new X509Certificate[]{certificate}); String message = exception.getMessage(); assertTrue(message.contains("cf:f4:0a:3f:fb:4e:32:a0:4e:65:9d:65:78:d7:45:46:a8:6a:92:32")); }
@Test public void testExceptionNullCert(){ UntrustedCertificateException exception = new UntrustedCertificateException("Error in cert", null); String message = exception.getMessage(); assertNotNull(message); }
@Test public void testExceptionEmptyCert(){ UntrustedCertificateException exception = new UntrustedCertificateException("Error in cert", new X509Certificate[0]); String message = exception.getMessage(); assertNotNull(message); }