Author: clement
Date: Thu Mar 7 09:51:14 2013
New Revision: 1453750
URL: http://svn.apache.org/r1453750
Log:
Make tests a bit more reliable on KF.
Modified:
felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-annotations-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/annotations/Common.java
felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/Common.java
felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/VersionConflictTest.java
felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-lifecycle-callback-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java
Modified:
felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-annotations-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/annotations/Common.java
URL:
http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-annotations-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/annotations/Common.java?rev=1453750&r1=1453749&r2=1453750&view=diff
==============================================================================
---
felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-annotations-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/annotations/Common.java
(original)
+++
felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-annotations-test/src/test/java/org/apache/felix/ipojo/runtime/core/test/annotations/Common.java
Thu Mar 7 09:51:14 2013
@@ -98,7 +98,7 @@ public class Common {
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 @@ public class Common {
}
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");
}
Modified:
felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/Common.java
URL:
http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/Common.java?rev=1453750&r1=1453749&r2=1453750&view=diff
==============================================================================
---
felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/Common.java
(original)
+++
felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/Common.java
Thu Mar 7 09:51:14 2013
@@ -20,6 +20,7 @@ import org.ops4j.pax.tinybundles.core.Ti
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 @@ public class Common {
root.setLevel(Level.INFO);
return options(
+ cleanCaches(),
ipojoBundles(),
junitBundles(),
//testedBundle(),
@@ -127,6 +129,8 @@ public class Common {
}
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 @@ public class Common {
}
}
+ /**
+ * 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;
+ }
+
}
Modified:
felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/VersionConflictTest.java
URL:
http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/VersionConflictTest.java?rev=1453750&r1=1453749&r2=1453750&view=diff
==============================================================================
---
felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/VersionConflictTest.java
(original)
+++
felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-factory-version-test/src/test/java/org/apache/felix/ipojo/tests/core/VersionConflictTest.java
Thu Mar 7 09:51:14 2013
@@ -18,8 +18,8 @@ import org.ops4j.pax.exam.spi.reactors.E
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 @@ public class VersionConflictTest extends
@Inject
private BundleContext context;
-
@Configuration
public Option[] config() throws IOException {
@@ -74,7 +73,7 @@ public class VersionConflictTest extends
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 @@ public class VersionConflictTest extends
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 @@ public class VersionConflictTest extends
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 @@ public class VersionConflictTest extends
.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 @@ public class VersionConflictTest extends
// 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 @@ public class VersionConflictTest extends
Bundle b5 = context.installBundle(context.getProperty("cons"));
b5.start();
+ waitForStability(bc);
Bundle[] bundles = context.getBundles();
for (Bundle bundle : bundles) {
@@ -162,23 +168,11 @@ public class VersionConflictTest extends
//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 @@ public class VersionConflictTest extends
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.
Modified:
felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-lifecycle-callback-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java
URL:
http://svn.apache.org/viewvc/felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-lifecycle-callback-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java?rev=1453750&r1=1453749&r2=1453750&view=diff
==============================================================================
---
felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-lifecycle-callback-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java
(original)
+++
felix/trunk/ipojo/runtime/core-it/src/it/ipojo-core-lifecycle-callback-test/src/test/java/org/apache/felix/ipojo/runtime/core/Common.java
Thu Mar 7 09:51:14 2013
@@ -17,8 +17,10 @@ import org.ops4j.pax.exam.spi.reactors.E
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 @@ public class Common {
}
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 @@ public class Common {
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;
+ }
+
}