package bytecode;

import static org.junit.Assert.*;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;

import org.junit.BeforeClass;
import org.junit.Test;

import de.dhbwstuttgart.bytecode.genericsGeneratorTypes.GenericGenratorResultForSourceFile;
import de.dhbwstuttgart.core.JavaTXCompiler;
import de.dhbwstuttgart.typeinference.result.ResultSet;

public class OLTest {
	private static String path;
	private static File fileToTest;
	private static JavaTXCompiler compiler;
	private static ClassLoader loader;
	private static Class<?> classToTest;
	private static Class<?> classToTest1;
	private static String pathToClassFile;
	private static Object instanceOfClass;
	private static Object instanceOfClass1;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		path = System.getProperty("user.dir")+"/src/test/resources/bytecode/javFiles/OL.jav";
		fileToTest = new File(path);
		compiler = new JavaTXCompiler(fileToTest);
		pathToClassFile = System.getProperty("user.dir")+"/src/test/resources/testBytecode/generatedBC/";
		List<ResultSet> typeinferenceResult = compiler.typeInference();
		List<GenericGenratorResultForSourceFile> simplifyResultsForAllSourceFiles = compiler.getGeneratedGenericResultsForAllSourceFiles(typeinferenceResult);
		compiler.generateBytecode(pathToClassFile,typeinferenceResult,simplifyResultsForAllSourceFiles);
		loader = new URLClassLoader(new URL[] {new URL("file://"+pathToClassFile)});
		classToTest = loader.loadClass("OL");
		instanceOfClass = classToTest.getDeclaredConstructor().newInstance();
		classToTest1 = loader.loadClass("OLMain");
		instanceOfClass1 = classToTest1.getDeclaredConstructor().newInstance();
	}
	
	@Test
	public void testOLClassName() {
		assertEquals("OL", classToTest.getName());
	}
	
	@Test
	public void testmInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Method m = classToTest.getDeclaredMethod("m", Integer.class);
		Integer result = (Integer) m.invoke(instanceOfClass, 5);
		assertEquals(new Integer(10), result);
	}
	
	@Test
	public void testmDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Method m = classToTest.getDeclaredMethod("m", Double.class);
		Double result = (Double) m.invoke(instanceOfClass, 5.0);
		assertEquals(new Double(10.0), result);
	}
	
	@Test
	public void testmString() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Method m = classToTest.getDeclaredMethod("m", String.class);
		String result = (String) m.invoke(instanceOfClass, "xxx");
		assertEquals("xxxxxx", result);
	}
	
	@Test
	public void testOLMainClassName() {
		assertEquals("OLMain", classToTest1.getName());
	}
	
	@Test
	public void testmainInt() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Method main = classToTest1.getDeclaredMethod("main", Integer.class);
		Integer result = (Integer) main.invoke(instanceOfClass1, 5);
		assertEquals(new Integer(10), result);
	}
	
	@Test
	public void testmainDouble() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Method main = classToTest1.getDeclaredMethod("main", Double.class);
		Double result = (Double) main.invoke(instanceOfClass1, 5.0);
		assertEquals(new Double(10.0), result);
	}
	
	@Test
	public void testmainString() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
		Method main = classToTest1.getDeclaredMethod("main", String.class);
		String result = (String) main.invoke(instanceOfClass1, "xxx");
		assertEquals("xxxxxx", result);
	}
}
