### Question:
FileConfigPersister implements ConfigPersister { @Override public void saveConfiguration(final ComplexConfigDTO configDTO) { Assert.paramNotNull(configDTO, "configuration"); if (configDTO.getVersion() < 1) { throw new StoreConfigException( "Illegal config version. Must be 1 or higher. Occures at scope Path '" + configDTO.getDefiningScopePath() + "'!"); } final File configFile = new File(rootDirectory + File.separator + gen.getPathFromScopePath(configDTO.getDefiningScopePath()) + File.separator + gen.createName(configDTO.getDefiningScopePath()) + fileExtension); if (configFile.exists()) { throw new StaleConfigException( configDTO.getDefiningScopePath(), "Can't save new configuration. There exists already a file for the scope path '" + configDTO.getDefiningScopePath() + "'!"); } try { configFile.getParentFile().mkdirs(); configFile.createNewFile(); } catch (final IOException e) { throw new StoreConfigException( "Error while creating new file. Occures at scope path '" + configDTO.getDefiningScopePath() + "'!", e); } FileOutputStream out = null; try { out = FileUtils.openOutputStream(configFile); marshaller.marshal(out, configDTO); } catch (final FileNotFoundException e) { throw new StoreConfigException( "Error while saving configuration. Occures at scope path '" + configDTO.getDefiningScopePath() + "'!", e); } catch (final IOException e) { throw new StoreConfigException("Error while saving configuration." + configDTO.getDefiningScopePath() + "'!", e); } finally { if (out != null) { try { out.close(); } catch (final IOException e) { LOG.warn( "Error while closing outputStream for configuration with scopepath '" + configDTO.getDefiningScopePath() + "'!", e); } } } } FileConfigPersister(final ConfigMarshaller marshaller, final String rootDirectory, final String fileExtension); @Override String getId(); @Override ComplexConfigDTO loadConfiguration(final ScopePath scopePath); @Override void updateConfiguration(final ComplexConfigDTO configDTO); @Override void saveConfiguration(final ComplexConfigDTO configDTO); @Override void delete(final ScopePath scopePath, final boolean deleteChildren); @Override void deleteAllOccurences(final String scopeName, final Map<String, String> properties); @Override Collection<ScopePath> listScopes(final String scopeName, final Map<String, String> properties); static final String ID; }### Answer:
@Test public void testSaveConfiguration() { final ScopePathBuilderFactory factory = getConfigService().getScopePathBuilderFactory(TestConfiguration.class); final ComplexConfigDTO configuration = createTestConfigDTO(factory.annotatedPath().create()); persister.saveConfiguration(configuration); final File file = new File(rootDirectory + File.separator + "default" + File.separator + "global" + File.separator + "user" + File.separator + "userName-hugo" + File.separator + gen.createName(factory.annotatedPath().create()) + fileExtension); Assert.assertTrue(file.exists()); }
@Test(expected = StaleConfigException.class) public void testSaveConfigurationAlreadyExists() { final ScopePathBuilderFactory factory = getConfigService().getScopePathBuilderFactory(TestConfiguration.class); final ComplexConfigDTO configuration = createTestConfigDTO(factory.annotatedPath().create()); persister.saveConfiguration(configuration); persister.saveConfiguration(configuration); }
@Test(expected = StoreConfigException.class) public void testSaveConfigurationWithIllegalVersion() { final ScopePathBuilderFactory factory = getConfigService().getScopePathBuilderFactory(TestConfiguration.class); final ComplexConfigDTO configuration = new ComplexConfigDTO(); configuration.setPolymorph(false); configuration.setDefiningScopePath(factory.annotatedPath().create()); configuration.setVersion(-1); configuration.setNulled(false); persister.saveConfiguration(configuration); }