package conf; import static org.junit.Assert.*; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.net.URL; import org.junit.Before; import org.junit.Test; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; public class SSSyncConfParserTest { private File currentFolder; @Before public void setup() { URL main = SSSyncConfParserTest.class.getResource("SSSyncConfParserTest.class"); if (!"file".equalsIgnoreCase(main.getProtocol())) throw new IllegalStateException("This class is not stored in a file"); currentFolder = new File(main.getPath()).getParentFile(); } @Test public void loadConfigTest() throws Exception { String expectedMain = readEntireFile(new File(currentFolder, "testExpectedMain.yaml")); String expectedConn = readEntireFile(new File(currentFolder, "testExpectedConn.yaml")); String mainConfigFile = new File(currentFolder, "testMain.yaml").getAbsolutePath(); String connConfigFile = new File(currentFolder, "testConn.yaml").getAbsolutePath(); // Loading (config => beans) ConfigRootBean confMain = SSSyncConfParser.loadMainConfig(mainConfigFile); ConfigConnectionsBean confConn = SSSyncConfParser.loadConnConfig(connConfigFile); System.out.println(confMain); System.out.println(confConn); // Dumping (beans => config) DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yamlDump = new Yaml(options); String dumpMain = yamlDump.dump(confMain); String dumpConn = yamlDump.dump(confConn); // Checking that everything is kept assertEquals(expectedMain, dumpMain); assertEquals(expectedConn, dumpConn); } private static String readEntireFile(File file) throws IOException { FileReader in = new FileReader(file); StringBuilder contents = new StringBuilder((int) file.length()); char[] buffer = new char[4096]; int read = 0; do { contents.append(buffer, 0, read); read = in.read(buffer); } while (read >= 0); in.close(); return contents.toString(); } }