From: Mathieu Baudier Date: Thu, 4 Jun 2009 14:33:53 +0000 (+0000) Subject: Start implementing OSGi integration tests X-Git-Tag: argeo-slc-2.1.7~1866 X-Git-Url: http://git.argeo.org/?a=commitdiff_plain;h=747ed8fc0f215e55f8aa7921f6fc7c46edd53237;hp=a6912571bad762e9f4289e9c3ba570de225a884c;p=gpl%2Fargeo-slc.git Start implementing OSGi integration tests git-svn-id: https://svn.argeo.org/slc/trunk@2460 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- diff --git a/integration-tests/org.argeo.slc.it.webapp/pom.xml b/integration-tests/org.argeo.slc.it.webapp/pom.xml index 42aacc2f8..b2f369655 100644 --- a/integration-tests/org.argeo.slc.it.webapp/pom.xml +++ b/integration-tests/org.argeo.slc.it.webapp/pom.xml @@ -35,7 +35,6 @@ false - -clean @@ -52,7 +51,7 @@ org.argeo.slc.server.services, org.argeo.slc.server.jms, org.argeo.slc.webapp, - org.argeo.slc.ria + org.argeo.slc.ria diff --git a/org.argeo.slc/pom.xml b/org.argeo.slc/pom.xml index 1d512ae78..dfa1940ba 100644 --- a/org.argeo.slc/pom.xml +++ b/org.argeo.slc/pom.xml @@ -20,7 +20,7 @@ 3.4.2.R34x_v20080826-1230 2.0.0 5.2.0.0006 - 0.1.10 + 0.1.11-SNAPSHOT scm:svn:https://www.argeo.org/svn/slc/trunk/org.argeo.slc diff --git a/runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/OsgiBoot.java b/runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/OsgiBoot.java index b6680bbb8..73b817ab2 100644 --- a/runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/OsgiBoot.java +++ b/runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/OsgiBoot.java @@ -65,14 +65,26 @@ public class OsgiBoot { startBundles(bundlesToStart); } - public void startBundles(String bundlesToStart) throws Exception { - if (bundlesToStart == null) + public void startBundles(String bundlesToStartStr) throws Exception { + if (bundlesToStartStr == null) return; - StringTokenizer st = new StringTokenizer(bundlesToStart, ","); - Map bundles = getBundles(); + StringTokenizer st = new StringTokenizer(bundlesToStartStr, ","); + List bundlesToStart = new ArrayList(); while (st.hasMoreTokens()) { String name = st.nextToken().trim(); + bundlesToStart.add(name); + } + startBundles(bundlesToStart); + } + + public void startBundles(List bundlesToStart) throws Exception { + if (bundlesToStart.size() == 0) + return; + + Map bundles = getBundles(); + for (int i = 0; i < bundlesToStart.size(); i++) { + String name = bundlesToStart.get(i).toString(); Bundle bundle = bundles.get(name); if (bundle != null) try { @@ -221,7 +233,7 @@ public class OsgiBoot { } } } else { - warn("Not a directory: " + newFullPath); + //warn("Not a directory: " + newFullPath); } } } diff --git a/runtime/org.argeo.slc.osgiboot/src/test/bundles/jars/test.jar b/runtime/org.argeo.slc.osgiboot/src/test/bundles/jars/test.jar new file mode 100644 index 000000000..e69de29bb diff --git a/runtime/org.argeo.slc.osgiboot/src/test/java/org/argeo/slc/osgiboot/OsgiBootNoRuntimeTest.java b/runtime/org.argeo.slc.osgiboot/src/test/java/org/argeo/slc/osgiboot/OsgiBootNoRuntimeTest.java index aab5799bd..011faa874 100644 --- a/runtime/org.argeo.slc.osgiboot/src/test/java/org/argeo/slc/osgiboot/OsgiBootNoRuntimeTest.java +++ b/runtime/org.argeo.slc.osgiboot/src/test/java/org/argeo/slc/osgiboot/OsgiBootNoRuntimeTest.java @@ -31,5 +31,11 @@ public class OsgiBootNoRuntimeTest extends TestCase { for (int i = 0; i < urls.size(); i++) System.out.println(urls.get(i)); assertEquals(3, urls.size()); + + List jarUrls = osgiBoot.getBundlesUrls(baseUrl, + "src/test/bundles/jars;in=*.jar"); + for (int i = 0; i < jarUrls.size(); i++) + System.out.println(jarUrls.get(i)); + assertEquals(1, jarUrls.size()); } } diff --git a/runtime/org.argeo.slc.osgiboot/src/test/java/org/argeo/slc/osgiboot/OsgiBootRuntimeTest.java b/runtime/org.argeo.slc.osgiboot/src/test/java/org/argeo/slc/osgiboot/OsgiBootRuntimeTest.java index 401441c42..a2ddeb118 100644 --- a/runtime/org.argeo.slc.osgiboot/src/test/java/org/argeo/slc/osgiboot/OsgiBootRuntimeTest.java +++ b/runtime/org.argeo.slc.osgiboot/src/test/java/org/argeo/slc/osgiboot/OsgiBootRuntimeTest.java @@ -44,7 +44,7 @@ public class OsgiBootRuntimeTest extends TestCase { } protected BundleContext startRuntime() throws Exception { - String[] args = { "-console" }; + String[] args = { "-console", "-clean" }; BundleContext bundleContext = EclipseStarter.startup(args, null); return bundleContext; } @@ -53,12 +53,12 @@ public class OsgiBootRuntimeTest extends TestCase { EclipseStarter.shutdown(); } - public void setUp() throws Exception{ + public void setUp() throws Exception { BundleContext bundleContext = startRuntime(); osgiBoot = new OsgiBoot(bundleContext); } - - public void tearDown() throws Exception{ + + public void tearDown() throws Exception { osgiBoot = null; stopRuntime(); } diff --git a/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/services/impl/runtime/AgentServiceImpl.java b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/services/impl/runtime/AgentServiceImpl.java index 021f57364..b08e9e8d1 100644 --- a/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/services/impl/runtime/AgentServiceImpl.java +++ b/runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/services/impl/runtime/AgentServiceImpl.java @@ -56,6 +56,15 @@ public class AgentServiceImpl implements AgentService, InitializingBean, protected class PingThread extends Thread { public void run() { + + // FIXME: temporary hack so that the ping starts after the server + // has been properly started. + try { + Thread.sleep(5 * 1000); + } catch (InterruptedException e1) { + // silent + } + log.info("Start pinging agents."); while (pingThreadActive) { List lst = slcAgentDescriptorDao diff --git a/runtime/org.argeo.slc.support.equinox/pom.xml b/runtime/org.argeo.slc.support.equinox/pom.xml index bc01f0705..6bb276209 100644 --- a/runtime/org.argeo.slc.support.equinox/pom.xml +++ b/runtime/org.argeo.slc.support.equinox/pom.xml @@ -54,11 +54,6 @@ org.springframework.osgi org.springframework.osgi.core - - - org.eclipse.osgi - org.eclipse.osgi - org.springframework.osgi @@ -73,18 +68,11 @@ org.objectweb.asm com.springsource.org.objectweb.asm.attrs - - - - org.springframework.osgi - org.springframework.osgi.test - test - + org.argeo.slc.runtime org.argeo.slc.osgiboot - test - + diff --git a/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/AbstractOsgiRuntimeTest.java b/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/AbstractOsgiRuntimeTest.java deleted file mode 100644 index fe007997d..000000000 --- a/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/AbstractOsgiRuntimeTest.java +++ /dev/null @@ -1,69 +0,0 @@ -package org.argeo.slc.osgi.test; - -import java.util.Properties; - -import junit.framework.TestCase; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.argeo.slc.osgiboot.OsgiBoot; -import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; -import org.springframework.osgi.test.platform.EquinoxPlatform; -import org.springframework.osgi.test.platform.OsgiPlatform; -import org.springframework.osgi.util.OsgiStringUtils; - -public abstract class AbstractOsgiRuntimeTest extends TestCase { - private final static Log log = LogFactory - .getLog(AbstractOsgiRuntimeTest.class); - - protected OsgiBoot osgiBoot = null; - protected OsgiPlatform osgiPlatform = null; - - protected OsgiPlatform createOsgiPlatform() { - return new EquinoxPlatform() { - - public Properties getConfigurationProperties() { - Properties props = super.getConfigurationProperties(); - props.setProperty("osgi.configuration.area", "target/slc/conf"); - return props; - } - }; - } - - protected void postStart() throws Exception { - - } - - public void setUp() throws Exception { - // To avoid xerces from the classpath being detected as the provider - System - .setProperty("javax.xml.parsers.DocumentBuilderFactory", - "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"); - System.setProperty("javax.xml.parsers.SAXParserFactory", - "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"); - - osgiPlatform = createOsgiPlatform(); - osgiPlatform.start(); - osgiBoot = new OsgiBoot(osgiPlatform.getBundleContext()); - log.info("OSGi platform " + osgiPlatform + " started."); - postStart(); - } - - public void tearDown() throws Exception { - osgiBoot = null; - osgiPlatform.stop(); - osgiPlatform = null; - log.info("OSGi platform " + osgiPlatform + " stopped."); - } - - protected void listInstalledBundles() { - BundleContext bundleContext = osgiPlatform.getBundleContext(); - Bundle[] bundles = bundleContext.getBundles(); - for (int i = 0; i < bundles.length; i++) { - System.out - .println(OsgiStringUtils.nullSafeSymbolicName(bundles[i])); - } - - } -} diff --git a/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/AbstractOsgiRuntimeTestCase.java b/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/AbstractOsgiRuntimeTestCase.java new file mode 100644 index 000000000..172b3a784 --- /dev/null +++ b/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/AbstractOsgiRuntimeTestCase.java @@ -0,0 +1,218 @@ +package org.argeo.slc.osgi.test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import junit.framework.TestCase; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.argeo.slc.SlcException; +import org.argeo.slc.osgiboot.OsgiBoot; +import org.eclipse.core.runtime.adaptor.EclipseStarter; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.framework.ServiceReference; +import org.springframework.context.ApplicationContext; +import org.springframework.osgi.util.OsgiStringUtils; + +public abstract class AbstractOsgiRuntimeTestCase extends TestCase { + private final static Log log = LogFactory + .getLog(AbstractOsgiRuntimeTestCase.class); + + protected OsgiBoot osgiBoot = null; + + protected void installBundles() throws Exception { + + } + + public void setUp() throws Exception { + // To avoid xerces from the classpath being detected as the provider + System + .setProperty("javax.xml.parsers.DocumentBuilderFactory", + "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"); + System.setProperty("javax.xml.parsers.SAXParserFactory", + "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl"); + + BundleContext bundleContext = startRuntime(); + osgiBoot = new OsgiBoot(bundleContext); + log.info("OSGi runtime started."); + + installBundles(); + + List bundlesToStart = getBundlesToStart(); + osgiBoot.startBundles(bundlesToStart); + waitAllBundlesOk(bundlesToStart); + if (log.isTraceEnabled()) + listInstalledBundles(); + } + + public void tearDown() throws Exception { + osgiBoot = null; + stopRuntime(); + log.info("OSGi runtime stopped."); + } + + protected BundleContext startRuntime() throws Exception { + String[] args = { "-console", "-clean" }; + BundleContext bundleContext = EclipseStarter.startup(args, null); + return bundleContext; + } + + protected void stopRuntime() throws Exception { + EclipseStarter.shutdown(); + } + + protected List getBundlesToStart() { + return new ArrayList(); + } + + protected void listInstalledBundles() { + BundleContext bundleContext = osgiBoot.getBundleContext(); + Bundle[] bundles = bundleContext.getBundles(); + for (int i = 0; i < bundles.length; i++) { + System.out.println(OsgiStringUtils.nullSafeSymbolicName(bundles[i]) + + " [" + OsgiStringUtils.bundleStateAsString(bundles[i]) + + "] " + bundles[i].getLocation()); + } + + } + + protected Map getOsgiApplicationContexts() + throws Exception { + Map map = new HashMap(); + BundleContext bundleContext = osgiBoot.getBundleContext(); + ServiceReference[] srs = bundleContext.getServiceReferences( + ApplicationContext.class.getName(), null); + for (ServiceReference sr : srs) { + ApplicationContext context = (ApplicationContext) bundleContext + .getService(sr); + map.put(sr.getBundle(), context); + } + return map; + } + + /** Wait for all bundles to be either RESOLVED or ACTIVE. */ + protected void waitAllBundlesOk(List bundlesToStart) { + BundleContext bundleContext = osgiBoot.getBundleContext(); + long begin = System.currentTimeMillis(); + long duration = 0; + boolean allBundlesOk = true; + while (duration < getResolvedTimeout()) { + for (Bundle bundle : bundleContext.getBundles()) { + if (bundle.getSymbolicName() != null + && bundle.getSymbolicName().startsWith( + "org.eclipse.jdt")) { + // don't check Eclipse SDK bundles + continue; + } + + if (bundle.getState() == Bundle.INSTALLED) { + allBundlesOk = false; + break;// for + } + + if (bundlesToStart.contains(bundle.getSymbolicName()) + && bundle.getState() != Bundle.ACTIVE) { + allBundlesOk = false; + break;// for + } + } + + if (allBundlesOk) + break;// while + + sleep(1000); + + duration = System.currentTimeMillis() - begin; + } + + if (!allBundlesOk) { + listInstalledBundles(); + throw new SlcException("Some bundles are in INSTALLED status"); + } + } + + /** + * Make sure that the application context of the started bundles starting + * with this prefix are properly initialized + */ + protected void assertStartedBundlesApplicationContext( + String bundleSymbolicNamesPrefix) { + List bundlesToStart = getBundlesToStart(); + for (String bundleSName : bundlesToStart) { + if (bundleSName.startsWith(bundleSymbolicNamesPrefix)) + assertBundleApplicationContext(bundleSName); + } + } + + /** + * Make sure that the application context of this bundle is properly + * initialized + */ + protected void assertBundleApplicationContext(String bundleSymbolicName) { + String filter = "(Bundle-SymbolicName=" + bundleSymbolicName + ")"; + // Wait for application context to be ready + try { + ServiceReference[] srs = getServiceRefSynchronous( + ApplicationContext.class.getName(), filter); + if (srs == null) + throw new SlcException("No application context for " + + bundleSymbolicName); + } catch (InvalidSyntaxException e) { + throw new SlcException( + "Unexpected exception when looking for applicaiton context for bundle " + + bundleSymbolicName, e); + } + log.info("Application context of bundle " + bundleSymbolicName + + " is initalized."); + } + + protected ServiceReference[] getServiceRefSynchronous(String clss, + String filter) throws InvalidSyntaxException { + // FIXME: factorize + if (log.isTraceEnabled()) + log.debug("Filter: '" + filter + "'"); + ServiceReference[] sfs = null; + boolean waiting = true; + long begin = System.currentTimeMillis(); + do { + sfs = getBundleContext().getServiceReferences(clss, filter); + + if (sfs != null) + waiting = false; + + sleep(100); + if (System.currentTimeMillis() - begin > getDefaultTimeout()) + throw new SlcException("Search of services " + clss + + " with filter " + filter + " timed out."); + } while (waiting); + + return sfs; + } + + protected BundleContext getBundleContext() { + return osgiBoot.getBundleContext(); + } + + /** Default is 120s */ + protected long getResolvedTimeout() { + return 120 * 1000l; + } + + /** Default is 10s */ + protected long getDefaultTimeout() { + return 10 * 1000l; + } + + final protected void sleep(long duration) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + // silent + } + } +} diff --git a/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/HelloTest.java b/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/HelloTest.java index c534ebe41..02d2ad547 100644 --- a/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/HelloTest.java +++ b/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/HelloTest.java @@ -1,23 +1,35 @@ package org.argeo.slc.osgi.test; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + import org.argeo.slc.osgiboot.OsgiBoot; -public class HelloTest extends AbstractOsgiRuntimeTest { +public class HelloTest extends AbstractOsgiRuntimeTestCase { public void testHello() throws Exception { Thread.sleep(2000); } - protected void postStart() throws Exception { + protected void installBundles() throws Exception { osgiBoot.installUrls(osgiBoot.getLocationsUrls( OsgiBoot.DEFAULT_BASE_URL, System .getProperty("java.class.path"))); osgiBoot.installUrls(osgiBoot.getBundlesUrls(OsgiBoot.DEFAULT_BASE_URL, "src/test/bundles;in=*")); - listInstalledBundles(); - String bundlesToStart = "org.springframework.osgi.extender,org.argeo.slc.support.osgi.test.hello"; - osgiBoot.startBundles(bundlesToStart); +// Map sysProps = new TreeMap(System.getProperties()); +// for (String key : sysProps.keySet()) { +// System.out.println(key + "=" + sysProps.get(key)); +// } + } + protected List getBundlesToStart() { + List bundlesToStart = new ArrayList(); + // bundlesToStart.add("org.springframework.osgi.extender"); + bundlesToStart.add("org.argeo.slc.support.osgi.test.hello"); + return bundlesToStart; } } diff --git a/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/PlatformStartStopTest.java b/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/PlatformStartStopTest.java index 2748e8449..8fd2dcacb 100644 --- a/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/PlatformStartStopTest.java +++ b/runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/PlatformStartStopTest.java @@ -3,10 +3,10 @@ package org.argeo.slc.osgi.test; import org.osgi.framework.BundleContext; import org.osgi.framework.Constants; -public class PlatformStartStopTest extends AbstractOsgiRuntimeTest { +public class PlatformStartStopTest extends AbstractOsgiRuntimeTestCase { public void testStartStop() { - BundleContext bundleContext = osgiPlatform.getBundleContext(); + BundleContext bundleContext = osgiBoot.getBundleContext(); System.out.println(bundleContext .getProperty(Constants.FRAMEWORK_VENDOR)); System.out.println(bundleContext