ConfigWriter { public void write(ConfigNode writing) throws IOException { Verify.nullCheck(outputPath, () -> new IllegalStateException("Output path was null.")); ConfigNode storedParent = writing.parent; writing.updateParent(null); Path directory = outputPath.getParent(); if (!directory.toRealPath().toFile().isDirectory()) { Files.createDirectories(directory); } Files.write(outputPath, createLines(writing), StandardCharsets.UTF_8, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE); writing.updateParent(storedParent); } ConfigWriter();  ConfigWriter(Path outputPath); void write(ConfigNode writing); List<String> createLines(ConfigNode writing);  }
@Test void defaultConfigIsWrittenCorrectly() throws IOException { File original = tempFolder.resolve("loaded.yml").toFile(); File written = tempFolder.resolve("written.yml").toFile(); TestResources.copyResourceIntoFile(original, "/assets/plan/config.yml"); try (ConfigReader reader = new ConfigReader(Files.newInputStream(original.toPath()))) { new ConfigWriter(written.toPath()).write(reader.read()); } List<String> originalLines = FileResource.lines(original); List<String> writtenLines = FileResource.lines(written); assertFalse(originalLines.isEmpty()); assertFalse(writtenLines.isEmpty()); StringBuilder differing = new StringBuilder(); for (int i = 0; i < originalLines.size(); i++) { String origLine = originalLines.get(i); String testLine = writtenLines.get(i).replace(" ", " "); if (!origLine.equals(testLine)) { differing.append(i + 1).append("! ").append(origLine).append("\n"); differing.append(i + 1).append("! ").append(testLine).append("\n\n"); } } assertEquals(0, differing.length(), differing::toString); }
@Test void indentedConfigIsWrittenCorrectly() throws IOException { File original = tempFolder.resolve("loaded.yml").toFile(); File indented = tempFolder.resolve("indented.yml").toFile(); File written = tempFolder.resolve("written.yml").toFile(); TestResources.copyResourceIntoFile(original, "/assets/plan/config.yml"); try (ConfigReader reader = new ConfigReader(Files.newInputStream(original.toPath()))) { new ConfigWriter(indented.toPath()).write(reader.read()); } try (ConfigReader reader = new ConfigReader(Files.newInputStream(indented.toPath()))) { new ConfigWriter(written.toPath()).write(reader.read()); } List<String> originalLines = FileResource.lines(indented); List<String> writtenLines = FileResource.lines(written); assertFalse(originalLines.isEmpty()); assertFalse(writtenLines.isEmpty()); StringBuilder differing = new StringBuilder(); for (int i = 0; i < originalLines.size(); i++) { String origLine = originalLines.get(i); String testLine = writtenLines.get(i); if (!origLine.equals(testLine)) { differing.append(i + 1).append("! ").append(origLine).append("\n"); differing.append(i + 1).append("! ").append(testLine).append("\n\n"); } } assertEquals(0, differing.length(), differing::toString); }
@Test void listIndent() throws IOException { ConfigNode root = new ConfigNode(null, null, null); root.addNode("Test").set(Arrays.asList("First", "Second", "Third")); Path out = tempFolder.resolve("listIndent.yml"); new ConfigWriter(out).write(root); List<String> writtenLines = FileResource.lines(out.toFile()); List<String> expected = Arrays.asList( "Test:", " - First", " - Second", " - Third" ); assertEquals(expected, writtenLines); }
@Test void listIndentSecondLevel() throws IOException { ConfigNode root = new ConfigNode(null, null, null); root.addNode("Test").addNode("List").set(Arrays.asList("First", "Second", "Third")); Path out = tempFolder.resolve("listIndent.yml"); new ConfigWriter(out).write(root); List<String> writtenLines = FileResource.lines(out.toFile()); List<String> expected = Arrays.asList( "Test:", " List:", " - First", " - Second", " - Third" ); assertEquals(expected, writtenLines); }
@Test void valueAfterAList() throws IOException { ConfigNode root = new ConfigNode(null, null, null); ConfigNode test = root.addNode("Test"); test.addNode("List").set(Arrays.asList("First", "Second", "Third")); test.addNode("Value").set("Example"); root.addNode("Second").set(2); Path out = tempFolder.resolve("listIndent.yml"); new ConfigWriter(out).write(root); List<String> writtenLines = FileResource.lines(out.toFile()); List<String> expected = Arrays.asList( "Test:", " List:", " - First", " - Second", " - Third", " Value: Example", "Second: 2" ); assertEquals(expected, writtenLines); }