package java2typescript.jackson.module.util;

import java.io.IOException;
import java.io.Writer;
import java.net.URL;
import java.util.Collections;
import java.util.List;

import org.junit.Assert;

import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
import com.google.common.io.Resources;

public class ExpectedOutputChecker {

	public static void checkOutputFromFile(Writer out) {
		compareFileContent(out, getCaller());
	}

	public static void checkOutputFromFileEquals(Writer out) {
		Assert.assertEquals(getExpectedOutput(getCaller()), out.toString());
	}
	private static StackTraceElement getCaller() {
		return new Throwable().getStackTrace()[2];
	}
	private static void compareFileContent(Writer out, StackTraceElement testMethodStackTraceElem) {
		// Can't rely on specific order of classes/fields/methods, so file content equality can't be used.
		// Using naive approach to check that actual output contains exactly the same lines as expected output
		Assert.assertEquals(getLinesAlphabetically(getExpectedOutput(testMethodStackTraceElem)), getLinesAlphabetically(out.toString()));
	}

	private static List<String> getLinesAlphabetically(String s) {
		s = s.replace("\r", "");
		List<String> lines = Lists.newArrayList(s.split("\\n"));
		Collections.sort(lines);
		return lines;
	}

	private static String getExpectedOutput(StackTraceElement testMethodStackTraceElem) {
		String testMethodName = testMethodStackTraceElem.getMethodName();
		String className = testMethodStackTraceElem.getClassName();
		return getFileContent(className.replace('.', '/') + "." + testMethodName + ".d.ts");
	}

	private static String getFileContent(String resourceName) {
		URL url = Resources.getResource(resourceName);
		try {
			return Resources.toString(url, Charsets.UTF_8);
		}
		catch (IOException e) {
			throw new RuntimeException("failed to read content of " + url, e);
		}
	}
}

@@ -1185,35 +1185,8 @@
 ) %7B%0A
-%09%09s = s.replace(%22%5Cr%22, %22%22);%0A
 %09%09Li
@@ -1506,22 +1506,47 @@
 me();%0A%09%09
-return
+String expectedOutputFromFile =
  getFile
@@ -1616,16 +1616,118 @@
 d.ts%22);%0A
+%09%09expectedOutputFromFile = expectedOutputFromFile.replace(%22%5Cr%22, %22%22);%0A%09%09return expectedOutputFromFile;%0A
 %09%7D%0A%0A%09pri

fix #65 last test failure (also caused by "\n\r" vs "\n" line ending diferences when comparing expected and actual output on windows)