@Test void writeFile_preserve_order() throws IOException { Random random = new Random(); for (int i = 0; i < 10; i++) { String testFile = TEST_FILE + i; Map<String, TestBean> originalBeans = new LinkedHashMap<>(); for (int j = 0; j < 20; j++) { int num = random.nextInt(20) + 10; originalBeans.put("bean_" + num, new TestBean("bean_" + num)); } this.jsonTools.setOutputType(YML_FILE); WriterHelper.writeFile(testProjectDir, documentFileUtils, jsonTools, originalBeans, testFile, TestBean.class); final File propertiesFile = checkFileBasics(testFile, YML_FILE); final LinkedHashMap<String, TestBean> originalReloaded = jsonTools.getObjectMapper().readValue(propertiesFile, TestBean.MAPPING); Iterator<Map.Entry<String, TestBean>> reloadedIterator = originalReloaded.entrySet().iterator(); originalBeans.forEach((b, v) -> assertEquals(reloadedIterator.next().getKey(), b)); Map<String, TestBean> newBeans = new LinkedHashMap<>(); for (int j = 0; j < 20; j++) { int num = random.nextInt(40); newBeans.put("bean_" + num, new TestBean("bean_" + num)); } WriterHelper.writeFile(testProjectDir, documentFileUtils, jsonTools, newBeans, testFile, TestBean.class); final LinkedHashMap<String, TestBean> mergedBeans = jsonTools.getObjectMapper().readValue(propertiesFile, TestBean.MAPPING); Iterator<Map.Entry<String, TestBean>> mergedBeanIterator = mergedBeans.entrySet().iterator(); originalBeans.forEach((b, v) -> assertEquals(mergedBeanIterator.next().getKey(), b)); newBeans.entrySet().stream().filter(e -> !originalBeans.containsKey(e.getKey())).forEach(e -> assertEquals(mergedBeanIterator.next().getKey(), e.getKey())); } }
static <B extends GatewayEntity> void writeFile(File rootFolder, DocumentFileUtils documentFileUtils, JsonTools jsonTools, Map<String, B> beans, String fileName, Class<B> beanClass) { if (beans.isEmpty()) { return; } File configFolder = new File(rootFolder, CONFIG_DIRECTORY); documentFileUtils.createFolder(configFolder.toPath()); LinkedHashMap<String, B> beansByName = new LinkedHashMap<>(); beans.forEach((k, v) -> { v.preWrite(configFolder, documentFileUtils); beansByName.put(v.getMappingValue(), v); }); beans = beansByName; ObjectWriter objectWriter = jsonTools.getObjectWriter(); File configFile = new File(configFolder, fileName + jsonTools.getFileExtension()); if (configFile.exists()) { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); try { objectWriter.writeValue(byteStream, beans); } catch (IOException e) { throw new WriteException(format(ERROR_WRITE, fileName), e); } try { final ObjectMapper objectMapper = jsonTools.getObjectMapper(); final MapType type = objectMapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class, beanClass); final Map<String, B> contents = objectMapper.readValue(configFile, type); beans = objectMapper.readerForUpdating(contents).readValue(new ByteArrayInputStream(byteStream.toByteArray())); } catch (IOException e) { throw new WriteException("Exception reading existing contents from " + fileName + " config file", e); } } try (OutputStream fileStream = Files.newOutputStream(configFile.toPath())) { objectWriter.writeValue(fileStream, beans); } catch (IOException e) { throw new WriteException(format(ERROR_WRITE, fileName), e); } }
WriterHelper { static <B extends GatewayEntity> void writeFile(File rootFolder, DocumentFileUtils documentFileUtils, JsonTools jsonTools, Map<String, B> beans, String fileName, Class<B> beanClass) { if (beans.isEmpty()) { return; } File configFolder = new File(rootFolder, CONFIG_DIRECTORY); documentFileUtils.createFolder(configFolder.toPath()); LinkedHashMap<String, B> beansByName = new LinkedHashMap<>(); beans.forEach((k, v) -> { v.preWrite(configFolder, documentFileUtils); beansByName.put(v.getMappingValue(), v); }); beans = beansByName; ObjectWriter objectWriter = jsonTools.getObjectWriter(); File configFile = new File(configFolder, fileName + jsonTools.getFileExtension()); if (configFile.exists()) { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); try { objectWriter.writeValue(byteStream, beans); } catch (IOException e) { throw new WriteException(format(ERROR_WRITE, fileName), e); } try { final ObjectMapper objectMapper = jsonTools.getObjectMapper(); final MapType type = objectMapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class, beanClass); final Map<String, B> contents = objectMapper.readValue(configFile, type); beans = objectMapper.readerForUpdating(contents).readValue(new ByteArrayInputStream(byteStream.toByteArray())); } catch (IOException e) { throw new WriteException("Exception reading existing contents from " + fileName + " config file", e); } } try (OutputStream fileStream = Files.newOutputStream(configFile.toPath())) { objectWriter.writeValue(fileStream, beans); } catch (IOException e) { throw new WriteException(format(ERROR_WRITE, fileName), e); } } }
WriterHelper { static <B extends GatewayEntity> void writeFile(File rootFolder, DocumentFileUtils documentFileUtils, JsonTools jsonTools, Map<String, B> beans, String fileName, Class<B> beanClass) { if (beans.isEmpty()) { return; } File configFolder = new File(rootFolder, CONFIG_DIRECTORY); documentFileUtils.createFolder(configFolder.toPath()); LinkedHashMap<String, B> beansByName = new LinkedHashMap<>(); beans.forEach((k, v) -> { v.preWrite(configFolder, documentFileUtils); beansByName.put(v.getMappingValue(), v); }); beans = beansByName; ObjectWriter objectWriter = jsonTools.getObjectWriter(); File configFile = new File(configFolder, fileName + jsonTools.getFileExtension()); if (configFile.exists()) { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); try { objectWriter.writeValue(byteStream, beans); } catch (IOException e) { throw new WriteException(format(ERROR_WRITE, fileName), e); } try { final ObjectMapper objectMapper = jsonTools.getObjectMapper(); final MapType type = objectMapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class, beanClass); final Map<String, B> contents = objectMapper.readValue(configFile, type); beans = objectMapper.readerForUpdating(contents).readValue(new ByteArrayInputStream(byteStream.toByteArray())); } catch (IOException e) { throw new WriteException("Exception reading existing contents from " + fileName + " config file", e); } } try (OutputStream fileStream = Files.newOutputStream(configFile.toPath())) { objectWriter.writeValue(fileStream, beans); } catch (IOException e) { throw new WriteException(format(ERROR_WRITE, fileName), e); } } private  WriterHelper(); }
WriterHelper { static <B extends GatewayEntity> void writeFile(File rootFolder, DocumentFileUtils documentFileUtils, JsonTools jsonTools, Map<String, B> beans, String fileName, Class<B> beanClass) { if (beans.isEmpty()) { return; } File configFolder = new File(rootFolder, CONFIG_DIRECTORY); documentFileUtils.createFolder(configFolder.toPath()); LinkedHashMap<String, B> beansByName = new LinkedHashMap<>(); beans.forEach((k, v) -> { v.preWrite(configFolder, documentFileUtils); beansByName.put(v.getMappingValue(), v); }); beans = beansByName; ObjectWriter objectWriter = jsonTools.getObjectWriter(); File configFile = new File(configFolder, fileName + jsonTools.getFileExtension()); if (configFile.exists()) { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); try { objectWriter.writeValue(byteStream, beans); } catch (IOException e) { throw new WriteException(format(ERROR_WRITE, fileName), e); } try { final ObjectMapper objectMapper = jsonTools.getObjectMapper(); final MapType type = objectMapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class, beanClass); final Map<String, B> contents = objectMapper.readValue(configFile, type); beans = objectMapper.readerForUpdating(contents).readValue(new ByteArrayInputStream(byteStream.toByteArray())); } catch (IOException e) { throw new WriteException("Exception reading existing contents from " + fileName + " config file", e); } } try (OutputStream fileStream = Files.newOutputStream(configFile.toPath())) { objectWriter.writeValue(fileStream, beans); } catch (IOException e) { throw new WriteException(format(ERROR_WRITE, fileName), e); } } private  WriterHelper();  }
WriterHelper { static <B extends GatewayEntity> void writeFile(File rootFolder, DocumentFileUtils documentFileUtils, JsonTools jsonTools, Map<String, B> beans, String fileName, Class<B> beanClass) { if (beans.isEmpty()) { return; } File configFolder = new File(rootFolder, CONFIG_DIRECTORY); documentFileUtils.createFolder(configFolder.toPath()); LinkedHashMap<String, B> beansByName = new LinkedHashMap<>(); beans.forEach((k, v) -> { v.preWrite(configFolder, documentFileUtils); beansByName.put(v.getMappingValue(), v); }); beans = beansByName; ObjectWriter objectWriter = jsonTools.getObjectWriter(); File configFile = new File(configFolder, fileName + jsonTools.getFileExtension()); if (configFile.exists()) { ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); try { objectWriter.writeValue(byteStream, beans); } catch (IOException e) { throw new WriteException(format(ERROR_WRITE, fileName), e); } try { final ObjectMapper objectMapper = jsonTools.getObjectMapper(); final MapType type = objectMapper.getTypeFactory().constructMapType(LinkedHashMap.class, String.class, beanClass); final Map<String, B> contents = objectMapper.readValue(configFile, type); beans = objectMapper.readerForUpdating(contents).readValue(new ByteArrayInputStream(byteStream.toByteArray())); } catch (IOException e) { throw new WriteException("Exception reading existing contents from " + fileName + " config file", e); } } try (OutputStream fileStream = Files.newOutputStream(configFile.toPath())) { objectWriter.writeValue(fileStream, beans); } catch (IOException e) { throw new WriteException(format(ERROR_WRITE, fileName), e); } } private  WriterHelper();   }