KeyStoreHandler { public static KeyStoreHandler create(final File keyStoreFile, final char... password) throws IOException, KeyStoreException { if (keyStoreFile == null) { throw new FileNotFoundException(null); } else if (keyStoreFile.exists()) { throw new FileAlreadyExistsException(keyStoreFile.getAbsolutePath()); } final KeyStore ks = KeyStore.getInstance(KEYSTORE_TYPE); try { ks.load(null, password); } catch (final Exception ex) { throw new IllegalStateException(ex); } final SecretKeyFactory skf = getSecretKeyFactory(); final KeyStoreHandler ksh = new KeyStoreHandler(ks, password, keyStoreFile, skf); LOGGER.debug("Creating keystore {}.", keyStoreFile); ksh.save(); return ksh; } private  KeyStoreHandler(final KeyStore keyStore, final char[] password, final File keyStoreFile,
            final SecretKeyFactory keyFactory); private  KeyStoreHandler(final KeyStore keyStore, final char[] password, final File keyStoreFile,
            final SecretKeyFactory keyFactory, final boolean isDirty); static KeyStoreHandler create(final File keyStoreFile, final char... password); static KeyStoreHandler open(final File keyStoreFile,
            final char... password); boolean set(final String alias, final char... value); Optional<char[]> get(final String alias); boolean delete(final String alias); boolean isDirty(); void save(); void save(final char... secret);  }
@Test void nullFileOnCreate() { assertThatThrownBy(() -> KeyStoreHandler.create(null)) .isInstanceOf(FileNotFoundException.class); }
@Test void preexistingFileOnCreate() throws IOException { File f = File.createTempFile("robozonky-", ".keystore"); assertThatThrownBy(() -> KeyStoreHandler.create(f)) .isInstanceOf(FileAlreadyExistsException.class); }