JsonFileKeyStorage implements KeyStorage { @Override public void store(KeyEntry keyEntry) { File dir = new File(keysPath); if (dir.exists()) { if (!dir.isDirectory()) { throw new InvalidPathException(keysPath, "Is not a directory"); } } else { dir.mkdirs(); } String name = keyEntry.getName(); if (exists(name)) { throw new KeyEntryAlreadyExistsException(); } KeyEntry entry; if (keyEntry instanceof JsonKeyEntry) { entry = keyEntry; } else { entry = new JsonKeyEntry(keyEntry.getName(), keyEntry.getValue()); entry.setMeta(keyEntry.getMeta()); } String json = getGson().toJson(entry); File file = new File(dir, name.toLowerCase()); try (FileOutputStream os = new FileOutputStream(file)) { os.write(json.getBytes(Charset.forName("UTF-8"))); } catch (Exception e) { throw new KeyStorageException(e); } } JsonFileKeyStorage();  JsonFileKeyStorage(String keysPath); @Override void delete(String keyName); @Override boolean exists(String keyName); @Override KeyEntry load(String keyName); @Override Set<String> names(); @Override void store(KeyEntry keyEntry); @Override KeyEntry createEntry(String name, byte[] value); @Override void update(KeyEntry keyEntry);  }
@Test public void store() { storage.store(entry); assertTrue(storage.exists(alias)); }
@Test public void store_duplicated() { storage.store(entry); assertThrows(KeyEntryAlreadyExistsException.class, () -> { storage.store(entry); }); }