package cucumber.examples.java.paxexam.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.ops4j.pax.exam.CoreOptions.junitBundles;
import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
import static org.ops4j.pax.exam.CoreOptions.options;

import java.util.Collections;

import javax.inject.Inject;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.ops4j.pax.exam.Configuration;
import org.ops4j.pax.exam.Option;
import org.ops4j.pax.exam.junit.PaxExam;
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
import org.ops4j.pax.exam.spi.reactors.PerMethod;
import org.ops4j.pax.exam.util.Injector;
import org.osgi.framework.BundleContext;

import cucumber.api.CucumberOptions;
import cucumber.java.runtime.osgi.OsgiClassFinder;
import cucumber.java.runtime.osgi.PaxExamObjectFactory;
import cucumber.runtime.Backend;
import cucumber.runtime.ClassFinder;
import cucumber.runtime.CucumberException;
import cucumber.runtime.Runtime;
import cucumber.runtime.RuntimeOptions;
import cucumber.runtime.RuntimeOptionsFactory;
import cucumber.runtime.io.FileResourceLoader;
import cucumber.runtime.io.ResourceLoader;
import cucumber.runtime.java.JavaBackend;
import cucumber.runtime.java.ObjectFactory;

@RunWith(PaxExam.class)
@ExamReactorStrategy(PerMethod.class)
@CucumberOptions(features = "features")
public class CalculatorTest {

    @Inject
    private Injector injector;

    @Inject
    private BundleContext bundleContext;

    @Configuration
    public Option[] config() {

        return options(
            mavenBundle("info.cukes", "pax-exam-calculator-api"),
            mavenBundle("info.cukes", "pax-exam-calculator-service"),

            mavenBundle("info.cukes", "gherkin"),
            mavenBundle("info.cukes", "cucumber-jvm-deps", "1.0.4-SNAPSHOT"),
            mavenBundle("info.cukes", "cucumber-core"),
            mavenBundle("info.cukes", "cucumber-java"),
            mavenBundle("info.cukes", "cucumber-osgi"),

            mavenBundle("org.slf4j", "slf4j-api"),
            mavenBundle("ch.qos.logback", "logback-core"),
            mavenBundle("ch.qos.logback", "logback-classic"),

            junitBundles()
        );
    }

    @Test
    public void cucumber() throws Exception {
        assertNotNull(injector);
        assertNotNull(bundleContext);
        final ResourceLoader resourceLoader = new FileResourceLoader();
        final ClassLoader classLoader = Runtime.class.getClassLoader();
        final ObjectFactory objectFactory = new PaxExamObjectFactory(injector);
        final ClassFinder classFinder = new OsgiClassFinder(bundleContext);
        final Backend backend = new JavaBackend(objectFactory, classFinder);

        final RuntimeOptionsFactory runtimeOptionsFactory = new RuntimeOptionsFactory(getClass());
        final RuntimeOptions runtimeOptions = runtimeOptionsFactory.create();

        final Runtime runtime = new Runtime(resourceLoader, classLoader, Collections.singleton(backend), runtimeOptions);

        runtime.run();

        if (!runtime.getErrors().isEmpty()) {
            throw new CucumberException(runtime.getErrors().get(0));
        } else if (runtime.exitStatus() != 0x00) {
            throw new CucumberException("There are pending or undefined steps.");
        }
        assertEquals(runtime.getErrors().size(), 0);
    }
}

@@ -358,24 +358,64 @@
 ct.Inject;%0A%0A
+import cucumber.api.java.ObjectFactory;%0A
 import org.j
@@ -1279,52 +1279,8 @@
 end;
-%0Aimport cucumber.runtime.java.ObjectFactory;
 %0A%0A@R

Fix compilation error