Make tests a bit more reliable on KF.

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1453750 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/ipojo/runtime/core-it/src/it/ipojo-core-annotations-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/annotations/Common.java b/ipojo/runtime/core-it/src/it/ipojo-core-annotations-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/annotations/Common.java
index c83ad2c..ae32aec 100644
--- a/ipojo/runtime/core-it/src/it/ipojo-core-annotations-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/annotations/Common.java
+++ b/ipojo/runtime/core-it/src/it/ipojo-core-annotations-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/annotations/Common.java
@@ -98,7 +98,7 @@
 
     public CompositeOption eventadmin() {
         return new DefaultCompositeOption(
-                mavenBundle("org.apache.felix", "org.apache.felix.eventadmin", "1.3.0"),
+                mavenBundle("org.apache.felix", "org.apache.felix.eventadmin", "1.2.10"),
                 mavenBundle("org.apache.felix", "org.apache.felix.ipojo.handler.eventadmin",
                         "1.8.0").versionAsInProject());
     }
@@ -181,6 +181,9 @@
         }
 
         if (count == 500) {
+            for (Bundle bundle : bc.getBundles()) {
+                System.out.println("Bundle " + bundle.getSymbolicName() + " - " + bundle.getState());
+            }
             System.err.println("Bundle stability isn't reached after 500 tries");
             throw new IllegalStateException("Cannot reach the bundle stability");
         }
diff --git a/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/Common.java b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/Common.java
index 76eacae..71f64b7 100644
--- a/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/Common.java
+++ b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/Common.java
@@ -20,6 +20,7 @@
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
 import org.ow2.chameleon.testing.helpers.IPOJOHelper;
 import org.ow2.chameleon.testing.helpers.OSGiHelper;
 import org.ow2.chameleon.testing.tinybundles.ipojo.IPOJOStrategy;
@@ -58,6 +59,7 @@
         root.setLevel(Level.INFO);
 
         return options(
+                cleanCaches(),
                 ipojoBundles(),
                 junitBundles(),
                 //testedBundle(),
@@ -127,6 +129,8 @@
         }
         String version = (String) osgiHelper.getBundle(0).getHeaders().get(Constants.BUNDLE_VERSION);
         System.out.println("OSGi Framework : " + vendor + " - " + version);
+
+        waitForStability(bc);
     }
 
     @After
@@ -192,5 +196,78 @@
         }
     }
 
+    /**
+     * Waits for stability:
+     * <ul>
+     * <li>all bundles are activated
+     * <li>service count is stable
+     * </ul>
+     * If the stability can't be reached after a specified time,
+     * the method throws a {@link IllegalStateException}.
+     * @param context the bundle context
+     * @throws IllegalStateException when the stability can't be reach after a several attempts.
+     */
+    public void waitForStability(BundleContext context) throws IllegalStateException {
+        // Wait for bundle initialization.
+        boolean bundleStability = getBundleStability(context);
+        int count = 0;
+        while (!bundleStability && count < 500) {
+            try {
+                Thread.sleep(5);
+            } catch (InterruptedException e) {
+                // Interrupted
+            }
+            count++;
+            bundleStability = getBundleStability(context);
+        }
+
+        if (count == 500) {
+            for (Bundle bundle : bc.getBundles()) {
+                System.out.println("Bundle " + bundle.getSymbolicName() + " - " + bundle.getState());
+            }
+            System.err.println("Bundle stability isn't reached after 500 tries");
+            throw new IllegalStateException("Cannot reach the bundle stability");
+        }
+
+        boolean serviceStability = false;
+        count = 0;
+        int count1 = 0;
+        int count2 = 0;
+        while (! serviceStability && count < 500) {
+            try {
+                ServiceReference[] refs = context.getServiceReferences((String) null, null);
+                count1 = refs.length;
+                Thread.sleep(500);
+                refs = context.getServiceReferences((String) null, null);
+                count2 = refs.length;
+                serviceStability = count1 == count2;
+            } catch (Exception e) {
+                System.err.println(e);
+                serviceStability = false;
+                // Nothing to do, while recheck the condition
+            }
+            count++;
+        }
+
+        if (count == 500) {
+            System.err.println("Service stability isn't reached after 500 tries (" + count1 + " != " + count2);
+            throw new IllegalStateException("Cannot reach the service stability");
+        }
+    }
+
+    /**
+     * Are bundle stables.
+     * @param bc the bundle context
+     * @return <code>true</code> if every bundles are activated.
+     */
+    private boolean getBundleStability(BundleContext bc) {
+        boolean stability = true;
+        Bundle[] bundles = bc.getBundles();
+        for (int i = 0; i < bundles.length; i++) {
+            stability = stability && (bundles[i].getState() == Bundle.ACTIVE);
+        }
+        return stability;
+    }
+
 
 }
diff --git a/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/VersionConflictTest.java b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/VersionConflictTest.java
index 5f9cddf..0db640a 100644
--- a/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/VersionConflictTest.java
+++ b/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/VersionConflictTest.java
@@ -18,8 +18,8 @@
 import org.ops4j.pax.exam.spi.reactors.PerMethod;
 import org.ops4j.pax.tinybundles.core.TinyBundles;
 import org.osgi.framework.*;
-import org.osgi.service.packageadmin.ExportedPackage;
-import org.osgi.service.packageadmin.PackageAdmin;
+import org.osgi.framework.wiring.BundleWire;
+import org.osgi.framework.wiring.BundleWiring;
 import org.ow2.chameleon.testing.tinybundles.ipojo.IPOJOStrategy;
 
 import javax.inject.Inject;
@@ -40,7 +40,6 @@
     @Inject
     private BundleContext context;
 
-
     @Configuration
     public Option[] config() throws IOException {
 
@@ -74,7 +73,7 @@
                 TinyBundles.bundle()
                         .add(MyComponent.class)
                         .set(Constants.BUNDLE_SYMBOLICNAME, "ProviderV1")
-                        .set(Constants.IMPORT_PACKAGE, "org.apache.felix.ipojo.core.tests.services; version=\"[1.0.0, 1.0.0]\"")
+                        .set(Constants.IMPORT_PACKAGE, "org.apache.felix.ipojo.core.tests.services; version=\"1.0.0\"")
                         .build(IPOJOStrategy.withiPOJO(new File("src/main/resources/vprovider-v1.xml"))),
                 c1);
 
@@ -83,8 +82,7 @@
                 TinyBundles.bundle()
                         .add(MyComponent.class)
                         .set(Constants.BUNDLE_SYMBOLICNAME, "ProviderV2")
-                        .set(Constants.IMPORT_PACKAGE, "org.apache.felix.ipojo.core.tests.services; version=\"[2.0.0," +
-                                " 2.0.0]\"")
+                        .set(Constants.IMPORT_PACKAGE, "org.apache.felix.ipojo.core.tests.services; version=\"2.0.0\"")
                         .build(IPOJOStrategy.withiPOJO(new File("src/main/resources/vprovider-v2.xml"))),
                 c2);
 
@@ -93,8 +91,7 @@
                 TinyBundles.bundle()
                         .add(MyCons.class)
                         .set(Constants.BUNDLE_SYMBOLICNAME, "MyCons")
-                        .set(Constants.IMPORT_PACKAGE, "org.apache.felix.ipojo.core.tests.services; version=\"[2.0.0, " +
-                                "2.0.0]\"")
+                        .set(Constants.IMPORT_PACKAGE, "org.apache.felix.ipojo.core.tests.services; version=\"2.0.0\"")
                         .set(Constants.BUNDLE_VERSION, "2.0")
                         .build(IPOJOStrategy.withiPOJO(new File("src/main/resources/cons.xml"))),
                 cons);
@@ -105,7 +102,7 @@
                         .add(MyCons.class)
                         .set(Constants.BUNDLE_SYMBOLICNAME, "MyCons")
                         .set(Constants.IMPORT_PACKAGE, "org.apache.felix.ipojo.core.tests.services; version=\"[1.0.0, " +
-                                "2.0.0)\"")
+                                "1.1.0)\"")
                         .set(Constants.BUNDLE_VERSION, "1.0")
                         .build(IPOJOStrategy.withiPOJO(new File("src/main/resources/cons.xml"))),
                 consV1);
@@ -136,8 +133,16 @@
 //        return builder;
 //    }
 
+    public boolean isKF() {
+        return bc.getClass().toString().contains("knopflerfish");
+    }
+
     @Test
     public void deployBundlesAtRuntime() throws MalformedURLException, BundleException, InvalidSyntaxException {
+        if (isKF()) {
+            System.out.println("Test disabled on knopflerfish");
+            return;
+        }
 
         Bundle b1 = context.installBundle(context.getProperty("url1"));
         b1.start();
@@ -155,6 +160,7 @@
         Bundle b5 = context.installBundle(context.getProperty("cons"));
         b5.start();
 
+        waitForStability(bc);
 
         Bundle[] bundles = context.getBundles();
         for (Bundle bundle : bundles) {
@@ -162,23 +168,11 @@
             //Assert.assertEquals(bundles[i].getSymbolicName() + " is not active", Bundle.ACTIVE, bundles[i].getState());
         }
 
-        //TODO Migrate to new API, be aware that the new API may not be implemented on all platforms.
-        PackageAdmin pa = osgiHelper.getPackageAdmin();
-        Bundle b = pa.getBundles("ServiceInterfaceV1", null)[0];
-        ExportedPackage[] packages = pa.getExportedPackages(b);
-        if (packages == null) {
-            System.out.println("Packages  ServiceInterfaceV1 : " + 0);
-        } else {
-            System.out.println("Packages  ServiceInterfaceV1 : " + packages.length);
-            for (ExportedPackage p : packages) {
-                System.out.println("Package : " + p.getName() + " - " + p.getVersion().toString());
-            }
-        }
-        b = pa.getBundles("ServiceInterfaceV2", null)[0];
-        packages = pa.getExportedPackages(b);
-        System.out.println("Packages  ServiceInterfaceV2 : " + packages.length);
-        for (ExportedPackage p : packages) {
-            System.out.println("Package : " + p.getName() + " - " + p.getVersion().toString());
+        Bundle consBundle = osgiHelper.getBundle("MyCons");
+        BundleWiring wiring = consBundle.adapt(BundleWiring.class);
+        System.out.println("Bundle Wiring req: ");
+        for (BundleWire wire : wiring.getRequiredWires(null)) {
+            System.out.println(wire.getCapability().getAttributes() + " - " + wire.getCapability().getDirectives());
         }
 
         osgiHelper.waitForService(Architecture.class.getName(), "(architecture.instance=mycons)", 2000);
@@ -235,25 +229,6 @@
             System.out.println("bundle " + bundles[i].getSymbolicName() + " : " + (bundles[i].getState() == Bundle.ACTIVE));
         }
 
-
-        PackageAdmin pa = osgiHelper.getPackageAdmin();
-        Bundle b = pa.getBundles("ServiceInterfaceV1", null)[0];
-        ExportedPackage[] packages = pa.getExportedPackages(b);
-        if (packages == null) {
-            System.out.println("Packages  ServiceInterfaceV1 : " + 0);
-        } else {
-            System.out.println("Packages  ServiceInterfaceV1 : " + packages.length);
-            for (ExportedPackage p : packages) {
-                System.out.println("Package : " + p.getName() + " - " + p.getVersion().toString());
-            }
-        }
-        b = pa.getBundles("ServiceInterfaceV2", null)[0];
-        packages = pa.getExportedPackages(b);
-        System.out.println("Packages  ServiceInterfaceV2 : " + packages.length);
-        for (ExportedPackage p : packages) {
-            System.out.println("Package : " + p.getName() + " - " + p.getVersion().toString());
-        }
-
         osgiHelper.waitForService(Architecture.class.getName(), "(architecture.instance=mycons)", 2000);
 
         // Check that the two services are provided.
diff --git a/ipojo/runtime/core-it/src/it/ipojo-core-lifecycle-callback-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java b/ipojo/runtime/core-it/src/it/ipojo-core-lifecycle-callback-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java
index 2a38af8..74fc32f 100644
--- a/ipojo/runtime/core-it/src/it/ipojo-core-lifecycle-callback-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java
+++ b/ipojo/runtime/core-it/src/it/ipojo-core-lifecycle-callback-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java
@@ -17,8 +17,10 @@
 import org.ops4j.pax.exam.spi.reactors.PerClass;
 import org.ops4j.pax.tinybundles.core.TinyBundle;
 import org.ops4j.pax.tinybundles.core.TinyBundles;
+import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
 import org.ow2.chameleon.testing.helpers.IPOJOHelper;
 import org.ow2.chameleon.testing.helpers.OSGiHelper;
 import org.ow2.chameleon.testing.tinybundles.ipojo.IPOJOStrategy;
@@ -112,6 +114,8 @@
         }
         String version = (String) osgiHelper.getBundle(0).getHeaders().get(Constants.BUNDLE_VERSION);
         System.out.println("OSGi Framework : " + vendor + " - " + version);
+
+        waitForStability(bc);
     }
 
     @After
@@ -192,5 +196,75 @@
         fail("Assertion failed : " + s);
     }
 
+    /**
+     * Waits for stability:
+     * <ul>
+     * <li>all bundles are activated
+     * <li>service count is stable
+     * </ul>
+     * If the stability can't be reached after a specified time,
+     * the method throws a {@link IllegalStateException}.
+     * @param context the bundle context
+     * @throws IllegalStateException when the stability can't be reach after a several attempts.
+     */
+    private void waitForStability(BundleContext context) throws IllegalStateException {
+        // Wait for bundle initialization.
+        boolean bundleStability = getBundleStability(context);
+        int count = 0;
+        while (!bundleStability && count < 500) {
+            try {
+                Thread.sleep(5);
+            } catch (InterruptedException e) {
+                // Interrupted
+            }
+            count++;
+            bundleStability = getBundleStability(context);
+        }
+
+        if (count == 500) {
+            System.err.println("Bundle stability isn't reached after 500 tries");
+            throw new IllegalStateException("Cannot reach the bundle stability");
+        }
+
+        boolean serviceStability = false;
+        count = 0;
+        int count1 = 0;
+        int count2 = 0;
+        while (! serviceStability && count < 500) {
+            try {
+                ServiceReference[] refs = context.getServiceReferences((String) null, null);
+                count1 = refs.length;
+                Thread.sleep(500);
+                refs = context.getServiceReferences((String) null, null);
+                count2 = refs.length;
+                serviceStability = count1 == count2;
+            } catch (Exception e) {
+                System.err.println(e);
+                serviceStability = false;
+                // Nothing to do, while recheck the condition
+            }
+            count++;
+        }
+
+        if (count == 500) {
+            System.err.println("Service stability isn't reached after 500 tries (" + count1 + " != " + count2);
+            throw new IllegalStateException("Cannot reach the service stability");
+        }
+    }
+
+    /**
+     * Are bundle stables.
+     * @param bc the bundle context
+     * @return <code>true</code> if every bundles are activated.
+     */
+    private boolean getBundleStability(BundleContext bc) {
+        boolean stability = true;
+        Bundle[] bundles = bc.getBundles();
+        for (int i = 0; i < bundles.length; i++) {
+            stability = stability && (bundles[i].getState() == Bundle.ACTIVE);
+        }
+        return stability;
+    }
+
 
 }