Start implementing OSGi integration tests
authorMathieu Baudier <mbaudier@argeo.org>
Thu, 4 Jun 2009 14:33:53 +0000 (14:33 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Thu, 4 Jun 2009 14:33:53 +0000 (14:33 +0000)
git-svn-id: https://svn.argeo.org/slc/trunk@2460 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

12 files changed:
integration-tests/org.argeo.slc.it.webapp/pom.xml
org.argeo.slc/pom.xml
runtime/org.argeo.slc.osgiboot/src/main/java/org/argeo/slc/osgiboot/OsgiBoot.java
runtime/org.argeo.slc.osgiboot/src/test/bundles/jars/test.jar [new file with mode: 0644]
runtime/org.argeo.slc.osgiboot/src/test/java/org/argeo/slc/osgiboot/OsgiBootNoRuntimeTest.java
runtime/org.argeo.slc.osgiboot/src/test/java/org/argeo/slc/osgiboot/OsgiBootRuntimeTest.java
runtime/org.argeo.slc.server/src/main/java/org/argeo/slc/services/impl/runtime/AgentServiceImpl.java
runtime/org.argeo.slc.support.equinox/pom.xml
runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/AbstractOsgiRuntimeTest.java [deleted file]
runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/AbstractOsgiRuntimeTestCase.java [new file with mode: 0644]
runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/HelloTest.java
runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/PlatformStartStopTest.java

index 42aacc2f878c9c71175fef15b5ada4ae657644a4..b2f369655c8743c75bd6a174227753ed22165afd 100644 (file)
@@ -35,7 +35,6 @@
                                                </goals>
                                                <configuration>
                                                        <wait>false</wait>
-                                                       <!--  <pause>30000</pause>-->
                                                        <argsToAppend>
                                                                <arg>-clean</arg>
                                                        </argsToAppend>
@@ -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                                                               
                                                                </slc.osgi.start>
                                                        </systemProperties>
                                                </configuration>
index 1d512ae78fe849a2b8e125ee90e5b3c599040a84..dfa1940ba3d35b4d2bdcbf81e8b06e1dfc10be11 100644 (file)
@@ -20,7 +20,7 @@
                <version.equinox>3.4.2.R34x_v20080826-1230</version.equinox>
                <version.maven-bundle-plugin>2.0.0</version.maven-bundle-plugin>
                <version.activemq>5.2.0.0006</version.activemq>
-               <version.maven-argeo-osgi>0.1.10</version.maven-argeo-osgi>
+               <version.maven-argeo-osgi>0.1.11-SNAPSHOT</version.maven-argeo-osgi>
        </properties>
        <scm>
                <connection>scm:svn:https://www.argeo.org/svn/slc/trunk/org.argeo.slc</connection>
index b6680bbb8208143453ddf67ed771df0ab72656d9..73b817ab2f3014e581152db1d56782e054ca7e00 100644 (file)
@@ -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<String, Bundle> 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<String, Bundle> 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 (file)
index 0000000..e69de29
index aab5799bdf1ddfcb902e5cd79cc084adc1fe9c84..011faa8741d95e95b0b7a16d925801e43e3ee2e7 100644 (file)
@@ -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());
        }
 }
index 401441c42f46c52ac3262724a9931c9f81c53eec..a2ddeb118103e0ff988a4638e156b8416c83fbdd 100644 (file)
@@ -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();
        }
index 021f573645f78f98540ccee7b11d4048cd2df7f3..b08e9e8d10dc8b120e1ad9e813b5b74daa1293ca 100644 (file)
@@ -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<SlcAgentDescriptor> lst = slcAgentDescriptorDao
index bc01f07059bd5991fc9aac3695652b395ccea94b..6bb2762094842423be1aa38658f003904998e282 100644 (file)
                        <groupId>org.springframework.osgi</groupId>
                        <artifactId>org.springframework.osgi.core</artifactId>
                </dependency>
-               
-               <dependency>
-                       <groupId>org.eclipse.osgi</groupId>
-                       <artifactId>org.eclipse.osgi</artifactId>
-               </dependency>
 
                <dependency>
                        <groupId>org.springframework.osgi</groupId>
                        <groupId>org.objectweb.asm</groupId>
                        <artifactId>com.springsource.org.objectweb.asm.attrs</artifactId>
                </dependency>
-               
-               <!-- TEST -->
-               <dependency>
-                       <groupId>org.springframework.osgi</groupId>
-                       <artifactId>org.springframework.osgi.test</artifactId>
-                       <scope>test</scope>
-               </dependency>
+
                <dependency>
                        <groupId>org.argeo.slc.runtime</groupId>
                        <artifactId>org.argeo.slc.osgiboot</artifactId>
-                       <scope>test</scope>
                </dependency>
-               
+
        </dependencies>
 </project>
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 (file)
index fe00799..0000000
+++ /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 (file)
index 0000000..172b3a7
--- /dev/null
@@ -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<String> 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<String> getBundlesToStart() {
+               return new ArrayList<String>();
+       }
+
+       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<Bundle, ApplicationContext> getOsgiApplicationContexts()
+                       throws Exception {
+               Map<Bundle, ApplicationContext> map = new HashMap<Bundle, ApplicationContext>();
+               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<String> 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<String> 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
+               }
+       }
+}
index c534ebe41d0549a97d0fdd4ef7333ed443142db9..02d2ad5478fef8d7f0d2ba017d4ebdccef3a57c8 100644 (file)
@@ -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<String, String> sysProps = new TreeMap(System.getProperties());
+//             for (String key : sysProps.keySet()) {
+//                     System.out.println(key + "=" + sysProps.get(key));
+//             }
+       }
 
+       protected List<String> getBundlesToStart() {
+               List<String> bundlesToStart = new ArrayList<String>();
+               // bundlesToStart.add("org.springframework.osgi.extender");
+               bundlesToStart.add("org.argeo.slc.support.osgi.test.hello");
+               return bundlesToStart;
        }
 
 }
index 2748e8449ee969bfbd0c255e5cf31c4bc30dd19f..8fd2dcacb7a59284611bb2bd4f7676a3be438399 100644 (file)
@@ -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