FileKeyStoreStorage implements KeyStoreStorage { @Override public void save(final KeyStore keyStore, final char[] password) throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException { keyStoreFile.getParentFile().mkdirs(); try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(keyStoreFile))) { keyStore.store(bos, password); } lastRead = System.currentTimeMillis(); } FileKeyStoreStorage(final File keyStoreFile); @VisibleForTesting File getKeyStoreFile(); @Override boolean exists(); @Override boolean modified(); @Override void load(final KeyStore keyStore, final char[] password); @Override void save(final KeyStore keyStore, final char[] password); @Override String toString();  }
@Test public void testSave() throws Exception { KeyStore keyStore = newKeyStoreWithData(); storage.save(keyStore, STORE_PASSWORD); assertThat(storage.modified(), is(false)); keyStore = newKeyStore(); try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(keyStoreFile))) { keyStore.load(bis, STORE_PASSWORD); } assertThat(keyStore.containsAlias(CERT_ALIAS), is(true)); }
@Test public void testSave_CreateParentDirectories() throws Exception { keyStoreFile = new File(basedir, "sub/dir/test.ks"); storage = new FileKeyStoreStorage(keyStoreFile); storage.save(newKeyStore(), STORE_PASSWORD); assertThat(keyStoreFile.isFile(), is(true)); }