package test;

import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.Map;
import com.electronwill.toml.Toml;
import com.electronwill.toml.TomlException;

/**
 * Tests the TOML library.
 * 
 * @author TheElectronWill
 * 		
 */
public class Tester {
	
	private static final File outputFolder = new File("./test-output");// where to write TOML data
	private static final File validFolder = new File("./test-valid");// contains valid TOML files
	private static final File invalidFolder = new File("./test-invalid");// contains invalid TOML files
	
	public static void main(String[] args) throws IOException, TomlException, URISyntaxException {
		System.out.println("---------------- Testing with valid files ----------------");
		for (File file : validFolder.listFiles()) {
			if (file.isFile() && file.getName().endsWith(".toml"))
				readAndRewriteTest(file, false);
		}
		
		System.out.println("---------------- Testing with invalid files ----------------");
		for (File file : invalidFolder.listFiles()) {
			if (file.isFile() && file.getName().endsWith(".toml")) {
				try {
					readTest(file, false);
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			}
		}
	}
	
	private static void readWrittenFiles() throws IOException {
		File validFolder = new File(".");
		for (File file : validFolder.listFiles()) {
			if (file.isFile() && file.getName().endsWith(".toml"))
				readTest(file, false);
		}
	}
	
	private static void writeTest(File file, Map<String, Object> data) throws IOException {
		System.out.println("Writing \"" + file + "\"...");
		
		long t0 = System.nanoTime();
		Toml.write(data, file);
		double time = (System.nanoTime() - t0) / (1000_000.0);
		System.out.println("Written in " + time + " milliseconds");
	}
	
	private static Map<String, Object> readTest(File file, boolean print) throws IOException, TomlException {
		System.out.println("Reading \"" + file.getName() + "\"...");
		
		long t0 = System.nanoTime();
		Map<String, Object> read = Toml.read(file);
		double time = (System.nanoTime() - t0) / (1000_000.0);
		System.out.println("Read in " + time + " milliseconds");
		
		if (print) {
			System.out.println("====== Data output =======");
			printMap(read);
			System.out.println("====== End of data =======");
		}
		return read;
	}
	
	private static void readAndRewriteTest(File file, boolean print) throws IOException, TomlException {
		String[] pathParts = file.getPath().split("/");
		System.out.println("========== " + pathParts[pathParts.length - 2] + "/" + pathParts[pathParts.length - 1] + " ==========");
		Map<String, Object> data = readTest(file, print);
		File out = new File(outputFolder, file.getName());
		writeTest(out, data);
		System.out.println();
	}
	
	private static void printMap(Map<String, Object> map) {
		System.out.println("{{");
		for (Map.Entry<String, Object> entry : map.entrySet()) {
			String name = entry.getKey();
			Object value = entry.getValue();
			System.out.print(name);
			System.out.print(" = ");
			if (value instanceof Map) {
				printMap((Map) value);
			} else if (value instanceof Collection) {
				printCollection((Collection) value);
			} else {
				System.out.println(value);
			}
		}
		System.out.println("}}");
	}
	
	private static void printCollection(Collection c) {
		System.out.print("[");
		for (Object o : c) {
			System.out.print(o);
			System.out.print(", ");
		}
		System.out.println("]");
	}
	
}

@@ -118,24 +118,76 @@
 Collection;%0A
+import java.util.LinkedList;%0Aimport java.util.List;%0A
 import java.
@@ -754,32 +754,37 @@
 ntaxException %7B%0A
+%09%09/*%0A
 %09%09System.out.pri
@@ -996,16 +996,21 @@
 e);%0A%09%09%7D%0A
+%09%09*/%0A
 %09%09%0A%09%09Sys
@@ -1082,32 +1082,81 @@
 ------------%22);%0A
+%09%09List%3CString%3E noException = new LinkedList%3C%3E();%0A
 %09%09for (File file
@@ -1266,97 +1266,447 @@
 %09%09%09%09
-readTest(file, false);%0A%09%09%09%09%7D catch (Exception ex) %7B%0A%09%09%09%09%09ex.printStackTrace();%0A%09%09%09%09%7D%0A%09%09%09%7D
+Map%3CString, Object%3E read = readTest(file, false);%0A%09%09%09%09%09printMap(read);%0A%09%09%09%09%09noException.add(file.getName());%0A%09%09%09%09%09System.err.println(%22/!%5C%5C No exception thrown when reading an invalid file!%22);%0A%09%09%09%09%09System.err.println();%0A%09%09%09%09%7D catch (Exception ex) %7B%0A%09%09%09%09%09System.out.println(%22--%3E %22 + ex.toString());%0A%09%09%09%09%09System.out.println();%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%7D%0A%09%09for (String s : noException) %7B%0A%09%09%09System.err.println(%22%5B!%5D No exception thrown when reading %22 + s);
 %0A%09%09%7D

Update Tester.java