package com.adviser.maven.kar.itests.greeter.hello;

import static org.ops4j.pax.exam.CoreOptions.maven;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.features;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.karafDistributionConfiguration;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.keepRuntimeFolder;
import static org.ops4j.pax.exam.karaf.options.KarafDistributionOption.logLevel;

import java.io.File;
import java.util.logging.Logger;

import javax.inject.Inject;

import org.apache.karaf.features.Feature;
import org.apache.karaf.features.FeaturesService;
import org.junit.Assert;
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.karaf.options.LogLevelOption.LogLevel;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;

import com.adviser.maven.kar.greeter.hello.HelloGreeter;
import com.adviser.maven.kar.greeter.service.GreeterService;

/**
 * @author h.nunner
 */
@RunWith(PaxExam.class)
public class HelloGreeterTest {

    /** logger */
    private static final Logger LOG = Logger.getLogger(HelloGreeterTest.class.getName());
    /** feature name */
    private static final String FEATURE_NAME = "maven-kar-archive-greeter-hello";
    /** service class name */
    private static final String SERVICE_CLAZZ_NAME = GreeterService.class.getName();

    /** features service */
    @Inject
    private FeaturesService featuresService;

    /** bundle context */
    @Inject
    private BundleContext context;

    /**
     * General test configuration setting up configurations.
     *
     * @return the configurations
     * @throws Exception
     *          on fail
     */
    @Configuration
    public static Option[] configure() throws Exception {
        return new Option[] {
                // provision and launch a container based on a Apache Karaf distribution
                karafDistributionConfiguration()
                        .frameworkUrl(
                                maven().groupId("org.apache.karaf").artifactId("apache-karaf").type("zip").versionAsInProject())
                        .useDeployFolder(false).karafVersion("2.3.3").unpackDirectory(new File("target/pax/")),
                // adds the key-value pairs to the defined configuration files
//                editConfigurationFilePut("etc/system.properties", "firstProp", "value"),
                // keeps the runtime folder after finishing the tests
                keepRuntimeFolder(),
                // don't bother with local console output as it just ends up cluttering the logs
                // configureConsole().ignoreLocalConsole(),
                // default is WARN
                logLevel(LogLevel.INFO),
                // feature required by the following tests
                features(
//                        new RawUrlReference("target/classes/features.xml"),
                        maven().groupId("com.adviser.maven.kar").artifactId("maven-kar-archive-greeter-hello").type("xml")
                                .versionAsInProject(),
                                FEATURE_NAME)
                // test executes in another process, so Pax Exam must be launched with debugging enabled
                // debugConfiguration("5000", true),
        };
    }

    /**
     * Tests if the feature has been installed correctly.
     *
     * @throws Exception
     *          on fail
     */
    @Test
    public void featureTest() throws Exception {
        Feature greeterFeature = featuresService.getFeature(FEATURE_NAME);
        Assert.assertNotNull("Feature not found: " + FEATURE_NAME, greeterFeature);
        Assert.assertTrue("Feature not installed: " + FEATURE_NAME, featuresService.isInstalled(greeterFeature));

        LOG.info("\n#######################\nFeature successfully installed: " + FEATURE_NAME + "\n#######################");
    }

    /**
     * Tests if the service can be retrieved correctly.
     *
     * @throws Exception
     *          on fail
     */
    @Test
    public void serviceTest() throws Exception {
        ServiceTracker serviceTracker = new ServiceTracker(context, SERVICE_CLAZZ_NAME, null);

        serviceTracker.open();
        HelloGreeter greeterService = (HelloGreeter) serviceTracker.waitForService(30 * 1000);
        serviceTracker.close();

        Assert.assertNotNull("No service registered: " + SERVICE_CLAZZ_NAME , greeterService);
        Assert.assertEquals("Hello test", greeterService.greet("test"));

        LOG.info("\n#######################\nService successfully installed: " + SERVICE_CLAZZ_NAME + "\n#######################");
    }

}

@@ -892,16 +892,125 @@
 gLevel;%0A
+import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;%0Aimport org.ops4j.pax.exam.spi.reactors.PerSuite;%0A
 import o
@@ -1042,16 +1042,16 @@
 ontext;%0A
-
 import o
@@ -1259,16 +1259,59 @@
 .class)%0A
+@ExamReactorStrategy(value=PerSuite.class)%0A
 public c
@@ -2540,49 +2540,80 @@
  // 
-adds the key-value pairs to the defined c
+if local maven repository is not found in ~/.m2%0A                // editC
 onfi
@@ -2624,17 +2624,114 @@
 tion
- files%0A//
+FilePut(%22org.ops4j.pax.url.mvn.cfg%22, %22org.ops4j.pax.url.mvn.settings%22, %22/home/hnunner/.m3/settings.xml%22),%0A
     
@@ -2741,16 +2741,19 @@
         
+ //
  editCon
@@ -2775,51 +2775,106 @@
 ut(%22
-etc/system.properties%22, %22firstProp%22, %22value
+org.ops4j.pax.url.mvn.cfg%22, %22org.ops4j.pax.url.mvn.localRepository%22, %22/home/hnunner/.m3/repository
 %22),%0A
@@ -3302,86 +3302,8 @@
 es(%0A
-//                        new RawUrlReference(%22target/classes/features.xml%22),%0A
     
@@ -4827,24 +4827,24 @@
 erService);%0A
-
         Asse
@@ -4859,16 +4859,114 @@
 tEquals(
+%22Unexpected message: %22 + greeterService.greet(%22test%22) + %22, expected: Hello test%22,%0A                
 %22Hello t

use of same karaf instance for all tests, minor cleanups