package boa.test.datagen;

import org.apache.commons.io.FileUtils;
import org.junit.Test;

import boa.datagen.util.FileIO;
import boa.evaluator.BoaEvaluator;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertThat;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;


public class TestQueries {

	@Test
	public void testBugFix() {
		try {
			Process p = Runtime.getRuntime().exec("./boa.sh -e "
					+ "-i /Users/roberts/git/compiler/test/known-good/bug-fix.boa "
					+ "-d /Users/roberts/git/compiler/test/datagen/test_datagen "
					+ "-o /Users/roberts/git/compiler/test/datagen/Bug-fix_output");
			BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String s = null;
			 while ((s = stdInput.readLine()) != null) {
	                System.out.println(s);
	            }
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		String expected = "AddedNullCheck[] = 1\n";
		File outputDir = new File("/Users/roberts/git/compiler/test/datagen/Bug-fix_output");
		String actual = getResults(outputDir);// evaluator.getResults();
		try {
			FileUtils.deleteDirectory(outputDir);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		assertEquals(expected, actual);
	}
	
	@Test
	public void testq20() {
		try {
			Process p = Runtime.getRuntime().exec("./boa.sh -e "
					+ "-i /Users/roberts/git/compiler/test/known-good/q20.boa "
					+ "-d /Users/roberts/git/compiler/test/datagen/test_datagen "
					+ "-o /Users/roberts/git/compiler/test/datagen/q20_output");
			BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String s = null;
			 while ((s = stdInput.readLine()) != null) {
	                System.out.println(s);
	            }
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String expected = "TransientMax[] = 140492550, 3.0\n"
				+ "TransientMean[] = 0.0410958904109589\n"
				+ "TransientMin[] = 140492550, 0.0\n"
				+ "TransientTotal[] = 3\n"
				+ "VolatileMax[] = 140492550, 1.0\n"
				+ "VolatileMean[] = 0.0136986301369863\n"
				+ "VolatileMin[] = 140492550, 0.0\n"
				+ "VolatileTotal[] = 1\n";
		File outputDir = new File("/Users/roberts/git/compiler/test/datagen/q20_output");
		String actual = getResults(outputDir);// evaluator.getResults();
		try {
			FileUtils.deleteDirectory(outputDir);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		assertEquals(expected, actual);
	}

	@Test
	public void testAnnot_names() {
		try {
			Process p = Runtime.getRuntime().exec("./boa.sh -e "
					+ "-i /Users/roberts/git/compiler/test/known-good/annot-names.boa "
					+ "-d /Users/roberts/git/compiler/test/datagen/test_datagen "
					+ "-o /Users/roberts/git/compiler/test/datagen/annot_names_output");
			BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
			String s = null;
			 while ((s = stdInput.readLine()) != null) {
	                System.out.println(s);
	            }
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String expected = "AnnotUse[FunctionalInterface] = 3\n"
				+ "AnnotUse[Override] = 11\n"
				+ "AnnotUse[Retention] = 1\n"
				+ "AnnotUse[SafeVarargs] = 1\n"
				+ "AnnotUse[SuppressWarnings] = 1\n"
				+ "AnnotUse[Target] = 2\n"
				+ "AnnotUse[ThreadSafe] = 1\n"
				+ "AnnotUse[ToDo] = 17\n";
		File outputDir = new File("/Users/roberts/git/compiler/test/datagen/annot_names_output");
		String actual = getResults(outputDir);// evaluator.getResults();
		try {
			FileUtils.deleteDirectory(outputDir);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		assertEquals(expected, actual);
	}
	
	public String getResults(File outputDir) {
		for (final File f : outputDir.listFiles()) {
			if (f.getName().startsWith("part")) {
				return FileIO.readFileContents(f);
			}
		}
		return "";
	}
}

@@ -549,36 +549,8 @@
 %22-i 
-/Users/roberts/git/compiler/
 test
@@ -590,36 +590,8 @@
 %22-d 
-/Users/roberts/git/compiler/
 test
@@ -629,36 +629,8 @@
 %22-o 
-/Users/roberts/git/compiler/
 test
@@ -1008,36 +1008,8 @@
 le(%22
-/Users/roberts/git/compiler/
 test
@@ -1183,46 +1183,8 @@
 ) %7B%0A
-%09%09%09// TODO Auto-generated catch block%0A
 %09%09%09e
@@ -1357,36 +1357,8 @@
 %22-i 
-/Users/roberts/git/compiler/
 test
@@ -1394,36 +1394,8 @@
 %22-d 
-/Users/roberts/git/compiler/
 test
@@ -1433,36 +1433,8 @@
 %22-o 
-/Users/roberts/git/compiler/
 test
@@ -2135,36 +2135,8 @@
 le(%22
-/Users/roberts/git/compiler/
 test
@@ -2525,36 +2525,8 @@
 %22-i 
-/Users/roberts/git/compiler/
 test
@@ -2570,36 +2570,8 @@
 %22-d 
-/Users/roberts/git/compiler/
 test
@@ -2609,36 +2609,8 @@
 %22-o 
-/Users/roberts/git/compiler/
 test
@@ -3247,24 +3247,24 @@
 o%5D = 17%5Cn%22;%0A
+
 %09%09File outpu
@@ -3284,36 +3284,8 @@
 le(%22
-/Users/roberts/git/compiler/
 test

Use relative path.