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("");
			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.close();

	}

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

	}


}

@@ -278,16 +278,38 @@
 Preparer
+ %3Cclasspath%3E %5B-Dparam%5D
  %3CExampl
@@ -392,24 +392,50 @@
 tion(String 
+cp, String dparam, String 
 className)%7B%0A
@@ -1001,109 +1001,26 @@
 -cp 
-lib/json-simple-1.1.1.jar:lib/junit.jar:lib/junitpoints.jar:. -Dmethod=%22 + methodN
+%22 +cp+ dpar
 am
-e
  + 
-%22 -Djson=yes
  
+%22
 org.
@@ -1780,16 +1780,60 @@
 = null;%0A
+%09%09String cp = null;%0A%09%09String dparam = null;%0A
 %09%09if(arg
@@ -1861,12 +1861,30 @@
 gth 
-!= 1
+%3C 2 %7C%7C args.length %3E 3
 ) %7B%0A
@@ -1902,16 +1902,169 @@
 %09%0A%09%09%7D%0A%09%09
+cp = args%5B0%5D;%0A%09%09if(args.length == 2) %7B%0A%09%09%09className = args%5B1%5D;%0A%09%09%09dparam = %22 %22;%0A%09%09%7D else %7B%0A%09%09%09dparam  = %22 %22 + args%5B1%5D + %22 %22;%0A%09%09%09className = args%5B2%5D;%0A%09%09%7D%0A
 %0A%09%09write
@@ -2090,19 +2090,32 @@
 ion(
-args%5B0%5D
+cp, dparam, className
 );%0A
-%0A
 %09%7D%0A%0A

make sep more usable, #15