WriterHelper { private static <B extends GatewayEntity> void writePropertiesFile(File rootFolder, DocumentFileUtils documentFileUtils, Map<String, B> beans, String fileName) { File configFolder = new File(rootFolder, CONFIG_DIRECTORY); documentFileUtils.createFolder(configFolder.toPath()); Properties properties = new Properties(); properties.putAll(beans .values() .stream() .map(b -> { b.preWrite(configFolder, documentFileUtils); return (PropertiesEntity) b; }) .collect(toMap(PropertiesEntity::getKey, PropertiesEntity::getValue))); writePropertiesFile(rootFolder, documentFileUtils, properties, fileName); } private  WriterHelper();   }
@Test void writePropertiesFile_noMerging() throws IOException { final HashMap<String, String> expectedProperties = new HashMap<>( ImmutableMap.<String, String>builder() .put("key1", "value1") .put("key2", "value2") .build() ); final Properties properties = new Properties(); properties.putAll(ImmutableMap.of("key1", "value1", "key2", "value2")); WriterHelper.writePropertiesFile(testProjectDir, documentFileUtils, properties, TEST_PROPERTIES); assertPropertiesContents(expectedProperties); }
@Test void writePropertiesFile_merging() throws IOException, URISyntaxException { setupCurrentFile(TEST_PROPERTIES, PROPERTIES); final Properties properties = new Properties(); properties.putAll(ImmutableMap.of("key1", "value1", "key2", "value2")); WriterHelper.writePropertiesFile(testProjectDir, documentFileUtils, properties, TEST_PROPERTIES); assertPropertiesContents( new HashMap<>( ImmutableMap.<String, String>builder() .put("key1", "value1") .put("key2", "value2") .put("key3", "value3") .put("key4", "value4") .build() ) ); }
@Test void writePropertiesFile_merging_emptyValues() throws IOException, URISyntaxException { setupCurrentFile(TEST_PROPERTIES, PROPERTIES); final Properties properties = new Properties(); properties.put("key1", ""); properties.put("key4", ""); WriterHelper.writePropertiesFile(testProjectDir, documentFileUtils, properties, TEST_PROPERTIES); assertPropertiesContents( new HashMap<>( ImmutableMap.<String, String>builder() .put("key1", "") .put("key3", "value3") .put("key4", "") .build() ) ); }
@Test void writeProperties_preserve_order() throws IOException, URISyntaxException { setupCurrentFile(TEST_PROPERTIES, PROPERTIES); final Properties properties = new Properties(); properties.putAll(ImmutableMap.of("key1", "value1", "key2", "value2")); WriterHelper.writePropertiesFile(testProjectDir, documentFileUtils, properties, TEST_PROPERTIES); final File propertiesFile = checkFileBasics(WriterHelperTest.TEST_PROPERTIES, PROPERTIES); final List<String> lines = readLines(propertiesFile, StandardCharsets.ISO_8859_1); assertEquals(4, lines.size()); assertTrue(lines.get(0).startsWith("key1")); assertTrue(lines.get(1).startsWith("key3")); assertTrue(lines.get(2).startsWith("key4")); assertTrue(lines.get(3).startsWith("key2")); }