package com.binarytweed.libgdx.test;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import org.junit.runner.Description;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.model.InitializationError;

public class LibGdxTestRunner extends Runner
{
	private final Object innerRunner;
	private final Class<?> innerRunnerClass;
	
	
	public LibGdxTestRunner(Class<?> klass) throws InitializationError
	{
		try(ExcludingTestClassLoader classLoader = new ExcludingTestClassLoader("com.badlogic", "com.binarytweed.libgdx.test.InnerLibGdxTestRunner", "com.binarytweed.libgdx.test.LibGdxTest"))
		{
			innerRunnerClass = classLoader.loadClass(InnerLibGdxTestRunner.class.getName());
			Class<?> testClass = classLoader.loadClass(klass.getName());
			innerRunner = innerRunnerClass.cast(innerRunnerClass.getConstructor(Class.class).newInstance(testClass));
		}
		catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
				| NoSuchMethodException | SecurityException | ClassNotFoundException | IOException e)
		{
			throw new InitializationError(e);
		}	
	}


	@Override
	public Description getDescription()
	{
		try
		{
			return (Description) innerRunnerClass.getMethod("getDescription").invoke(innerRunner);
		}
		catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
				| SecurityException e)
		{
			throw new RuntimeException("Could not get description");
		}
	}

	@Override
	public void run(RunNotifier notifier)
	{
		try
		{
			innerRunnerClass.getMethod("run", RunNotifier.class).invoke(innerRunner, notifier);
		}
		catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException
				| SecurityException e)
		{
			throw new RuntimeException("Could not invoke inner runner");
		}
	}

}

@@ -446,17 +446,25 @@
 lass%3C?%3E 
-k
+testFileC
 lass) th
@@ -565,16 +565,41 @@
 sLoader(
+testFileClass.getName(), 
 %22com.bad
@@ -661,50 +661,8 @@
 ner%22
-, %22com.binarytweed.libgdx.test.LibGdxTest%22
 ))%0A%09
@@ -794,17 +794,25 @@
 adClass(
-k
+testFileC
 lass.get

remove some hardcoding