package tools.sep;

import java.io.*;
import java.util.*;
import java.lang.*;
import java.lang.reflect.*;
import java.lang.annotation.*;
import org.junit.*;


public class SingleExecutionPreparer {

	private static void usage(){
		System.err.println("Usage: java SingleExecutionPreparer <ExampleTest>");
		System.exit(0);
	}

	private static void writeOutSingleTestExecution(String className){
		PrintWriter writer = null;
		try{
			writer = new PrintWriter(new BufferedWriter(new FileWriter("single_execution.sh")));
			writer.println("#!/bin/bash");
			writer.println("");
			writer.println("echo \"[\" 1>&2 ");
			ClassLoader cl = ClassLoader.getSystemClassLoader();
			Class tc = cl.loadClass(className);
			int counter = 0;
			for(Method method : tc.getMethods()) {
				if(method.isAnnotationPresent(Test.class)){
					if(counter > 0){
						writer.println("echo \",\" 1>&2");
					}
					String methodName = method.getName();
					writer.println("java -XX:+UseConcMarkSweepGC -Xmx1024m -cp lib/json-simple-1.1.1.jar:lib/junit.jar:lib/junitpoints.jar:. -Dmethod=" + methodName + " -Djson=yes org.junit.runner.JUnitCore " + className + " || echo");
					counter++;
				}
			
			}	
		} catch(FileNotFoundException fne) {
			throw new Error("WARNING - Something bad happened while creating the single execution script: " + fne.getMessage());
		} catch(ClassNotFoundException cnfe) {
			throw new Error("WARNING - test class not found: " + className);
		} catch(UnsupportedEncodingException uee ) {
			throw new Error("WARNING - Something bad happened while creating the single execution script" + uee.getMessage());
		} catch (IOException ioe) {
			throw new Error("WARNING - Something bad happened while creating the single execution script" + ioe.getMessage());

		}

		writer.println("echo \"]\" 1>&2");
		writer.close();

	}

	public static void main(String args[]){
		if(args.length != 1) {
			usage();
		}
		
		writeOutSingleTestExecution(args[0]);

	}


}

@@ -191,16 +191,57 @@
 parer %7B%0A
+%09private static boolean isPublic = false;
 %0A%09privat
@@ -320,16 +320,27 @@
 Preparer
+ %5B--public%5D
  %3CExampl
@@ -616,24 +616,43 @@
 rintln(%22%22);%0A
+%09%09%09if(isPublic) %7B%0A%09
 %09%09%09writer.pr
@@ -678,16 +678,21 @@
 %3E&2 %22);%0A
+%09%09%09%7D%0A
 %09%09%09Class
@@ -1840,17 +1840,35 @@
 );%0A%0A%09%09%7D%0A
-%0A
+%09%09if(!isPublic) %7B%0A%09
 %09%09writer
@@ -1896,16 +1896,20 @@
 1%3E&2%22);%0A
+%09%09%7D%0A
 %09%09writer
@@ -1966,19 +1966,62 @@
 s%5B%5D)%7B%0A%09%09
-if(
+String className = null;%0A%09%09if(args == null %7C%7C 
 args.len
@@ -2028,12 +2028,11 @@
 gth 
-!= 1
+%3E 2
 ) %7B%0A
@@ -2042,23 +2042,149 @@
 usage();
+%09
 %0A%09%09%7D%0A
+%0A
 %09%09
+if(args.length == 1) %7B%0A%09%09%09className = args%5B0%5D;%0A%09%09%7D%0A%09%09if(args.length == 2) %7B%0A%09%09%09isPublic = true;%0A%09%09%09className = args%5B1%5D;%0A%09%09%7D%0A
 %0A%09%09write
@@ -2206,23 +2206,25 @@
 ecution(
-args%5B0%5D
+className
 );%0A%0A%09%7D%0A%0A

support for public/secretclass execution, #15