]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.osgi.boot/ext/test/org/argeo/osgi/boot/OsgiBootRuntimeTest.java
Use unstable RPM factory.
[lgpl/argeo-commons.git] / org.argeo.osgi.boot / ext / test / org / argeo / osgi / boot / OsgiBootRuntimeTest.java
1 package org.argeo.osgi.boot;
2
3 import java.util.Iterator;
4 import java.util.Map;
5 import java.util.TreeMap;
6
7 import junit.framework.TestCase;
8
9 import org.eclipse.core.runtime.adaptor.EclipseStarter;
10 import org.osgi.framework.Bundle;
11 import org.osgi.framework.BundleContext;
12
13 /** Starts an Equinox runtime and provision it with OSGi boot. */
14 public class OsgiBootRuntimeTest extends TestCase {
15 protected OsgiBoot osgiBoot = null;
16 private boolean osgiRuntimeAlreadyRunning = false;
17
18 public void testInstallAndStart() throws Exception {
19 if (osgiRuntimeAlreadyRunning) {
20 System.out
21 .println("OSGi runtime already running, skipping test...");
22 return;
23 }
24 osgiBoot.installUrls(osgiBoot.getBundlesUrls(OsgiBoot.DEFAULT_BASE_URL,
25 OsgiBootNoRuntimeTest.BUNDLES));
26 Map<String, Bundle> map = new TreeMap<String, Bundle>(
27 osgiBoot.getBundlesBySymbolicName());
28 for (Iterator<String> keys = map.keySet().iterator(); keys.hasNext();) {
29 String key = keys.next();
30 Bundle bundle = map.get(key);
31 System.out.println(key + " : " + bundle.getLocation());
32 }
33 assertEquals(4, map.size());
34 Iterator<String> keys = map.keySet().iterator();
35 assertEquals("org.argeo.osgi.boot.test.bundle1", keys.next());
36 assertEquals("org.argeo.osgi.boot.test.bundle2", keys.next());
37 assertEquals("org.argeo.osgi.boot.test.bundle3", keys.next());
38 assertEquals("org.eclipse.osgi", keys.next());
39
40 // osgiBoot.startBundles("org.argeo.osgi.boot.test.bundle2");
41 long begin = System.currentTimeMillis();
42 while (System.currentTimeMillis() - begin < 10000) {
43 Map<String, Bundle> mapBundles = osgiBoot
44 .getBundlesBySymbolicName();
45 Bundle bundle = mapBundles.get("org.argeo.osgi.boot.test.bundle2");
46 if (bundle.getState() == Bundle.ACTIVE) {
47 System.out.println("Bundle " + bundle + " started.");
48 return;
49 }
50 }
51 fail("Bundle not started after timeout limit.");
52 }
53
54 protected BundleContext startRuntime() throws Exception {
55 String[] args = { "-console", "-clean" };
56 BundleContext bundleContext = EclipseStarter.startup(args, null);
57
58 // ServiceLoader<FrameworkFactory> ff = ServiceLoader.load(FrameworkFactory.class);
59 // Map<String,String> config = new HashMap<String,String>();
60 // Framework fwk = ff.iterator().next().newFramework(config);
61 // fwk.start();
62 return bundleContext;
63 }
64
65 protected void stopRuntime() throws Exception {
66 EclipseStarter.shutdown();
67 }
68
69 public void setUp() throws Exception {
70 osgiRuntimeAlreadyRunning = EclipseStarter.isRunning();
71 if (osgiRuntimeAlreadyRunning)
72 return;
73 BundleContext bundleContext = startRuntime();
74 osgiBoot = new OsgiBoot(bundleContext);
75 }
76
77 public void tearDown() throws Exception {
78 if (osgiRuntimeAlreadyRunning)
79 return;
80 osgiBoot = null;
81 stopRuntime();
82 }
83
84 }