KeystoreFactory { @SneakyThrows public X509Certificate loadCert(String certLocation) { CertificateFactory cf = CertificateFactory.getInstance("X509"); Resource certRes = resourceLoader.getResource(certLocation); X509Certificate cert = (X509Certificate) cf.generateCertificate(certRes.getInputStream()); return cert; } KeystoreFactory();  KeystoreFactory(ResourceLoader resourceLoader); @SneakyThrows KeyStore loadKeystore(String certResourceLocation, String privateKeyResourceLocation, String alias, String keyPassword); @SneakyThrows void addKeyToKeystore(KeyStore keyStore, X509Certificate cert, RSAPrivateKey privateKey, String alias, String password); @SneakyThrows KeyStore createEmptyKeystore(); @SneakyThrows X509Certificate loadCert(String certLocation); @SneakyThrows RSAPrivateKey loadPrivateKey(String privateKeyLocation); void setResourceLoader(DefaultResourceLoader resourceLoader);  }
@Test public void loadCert() throws Exception { KeystoreFactory keystoreFactory = new KeystoreFactory(new DefaultResourceLoader()); Certificate cert = keystoreFactory.loadCert("classpath:/localhost.cert"); assertThat(cert.getType()).isEqualTo("X.509"); cert.verify(cert.getPublicKey()); }
@Test(expected = FileNotFoundException.class) public void loadCert_notFound() throws Exception { KeystoreFactory keystoreFactory = new KeystoreFactory(new DefaultResourceLoader()); Certificate cert = keystoreFactory.loadCert("classpath:/not_found.cert"); }
@Test(expected = CertificateParsingException.class) public void loadCert_invalid() throws Exception { KeystoreFactory keystoreFactory = new KeystoreFactory(new DefaultResourceLoader()); Certificate cert = keystoreFactory.loadCert("classpath:/localhost.key.der"); }