package com.stratio.cucumber.testng;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.reflections.Reflections;

import cucumber.api.CucumberOptions;
import cucumber.runtime.ClassFinder;
import cucumber.runtime.RuntimeOptions;
import cucumber.runtime.RuntimeOptionsFactory;
import cucumber.runtime.io.MultiLoader;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.io.ResourceLoaderClassFinder;


public class CucumberRunner {

    private final cucumber.runtime.Runtime runtime;

    /**
     * Default constructor for cucumber Runner.
     * 
     * @param clazz
     * @param feature
     * @throws IOException
     * @throws ClassNotFoundException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws NoSuchMethodException
     * @throws InvocationTargetException
     */
    @SuppressWarnings("unused")
    public CucumberRunner(Class<?> clazz, String... feature) throws IOException, ClassNotFoundException,
            InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        ClassLoader classLoader = clazz.getClassLoader();
        ResourceLoader resourceLoader = new MultiLoader(classLoader);

        RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(clazz,
                new Class[] { CucumberOptions.class });
        RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();

        boolean aux = new File("target/executions/").mkdirs();
        CucumberReporter reporterTestNG;

        if ((feature.length == 0)) {
            reporterTestNG = new CucumberReporter("target/executions/", clazz.getCanonicalName(), "");
        } else {
            List<String> features = new ArrayList<String>();
            String fPath = "src/test/resources/features/" + feature[0] + ".feature";
            features.add(fPath);
            runtimeOptions.getFeaturePaths().addAll(features);
            reporterTestNG = new CucumberReporter("target/executions/", clazz.getCanonicalName(), feature[0]);
        }

        List<String> uniqueGlue = new ArrayList<String>();
        uniqueGlue.add("classpath:com/stratio/specs");
	uniqueGlue.add("classpath:com/stratio/sparta/testsAT/specs");
	uniqueGlue.add("classpath:com/stratio/gosecsso/testsAT/specs");
        uniqueGlue.add("classpath:com/stratio/crossdata/testsAT/specs");
        uniqueGlue.add("classpath:com/stratio/streaming/testsAT/specs");
        uniqueGlue.add("classpath:com/stratio/ingestion/testsAT/specs");
        uniqueGlue.add("classpath:com/stratio/datavis/testsAT/specs");
        uniqueGlue.add("classpath:com/stratio/connectors/testsAT/specs");
        uniqueGlue.add("classpath:com/stratio/admin/testsAT/specs");
        uniqueGlue.add("classpath:com/stratio/explorer/testsAT/specs");
        uniqueGlue.add("classpath:com/stratio/manager/testsAT/specs");
        uniqueGlue.add("classpath:com/stratio/viewer/testsAT/specs");
        uniqueGlue.add("classpath:com/stratio/decision/testsAT/specs");
        uniqueGlue.add("classpath:com/stratio/cassandra/lucene/testsAT/specs");                                  
        runtimeOptions.getGlue().clear();
        runtimeOptions.getGlue().addAll(uniqueGlue);

        runtimeOptions.addFormatter(reporterTestNG);
        Set<Class<? extends ICucumberFormatter>> implementers = new Reflections("com.stratio.tests.utils")
                .getSubTypesOf(ICucumberFormatter.class);

        for (Class<? extends ICucumberFormatter> implementerClazz : implementers) {
            Constructor<?> ctor = implementerClazz.getConstructor();
            ctor.setAccessible(true);
            runtimeOptions.addFormatter((ICucumberFormatter) ctor.newInstance());
        }

        ClassFinder classFinder = new ResourceLoaderClassFinder(resourceLoader, classLoader);
        runtime = new cucumber.runtime.Runtime(resourceLoader, classFinder, classLoader, runtimeOptions);
    }

    /**
     * Run the testclases(Features).
     * 
     * @throws IOException
     */
    public void runCukes() throws IOException {
        runtime.run();
    }
}

@@ -44,259 +44,75 @@
 ort 
-java.io.File;%0D%0Aimport java.io.IOException;%0D%0Aimport java.lang.reflect.Constructor;%0D%0Aimport java.lang.reflect.InvocationTargetException;%0D%0Aimport java.util.ArrayList;%0D%0Aimport java.util.List;%0D%0Aimport java.util.Set;%0D%0A%0D%0Aimport org.reflections.Reflections;%0D%0A
+cucumber.api.CucumberOptions;%0D%0Aimport cucumber.runtime.ClassFinder;
 %0D%0Aim
@@ -117,35 +117,39 @@
 import cucumber.
-api
+runtime
 .CucumberOptions
@@ -145,53 +145,17 @@
 mber
-Options;%0D%0Aimport cucumber.runtime.ClassFinder
+Exception
 ;%0D%0Ai
@@ -382,16 +382,274 @@
 inder;%0D%0A
+import org.reflections.Reflections;%0D%0A%0D%0Aimport java.io.File;%0D%0Aimport java.io.IOException;%0D%0Aimport java.lang.reflect.Constructor;%0D%0Aimport java.lang.reflect.InvocationTargetException;%0D%0Aimport java.util.ArrayList;%0D%0Aimport java.util.List;%0D%0Aimport java.util.Set;%0D%0A
 %0D%0A%0D%0Apubl
@@ -4392,23 +4392,153 @@
 time.run();%0D
+%0A%0D%0A        if (!runtime.getErrors().isEmpty()) %7B%0D%0A            throw new CucumberException(runtime.getErrors().get(0));%0D%0A        %7D%0D
 %0A    %7D%0D%0A%7D%0D%0A

throw failed tests exceptions up