1 package org
.argeo
.osgi
.boot
;
3 import java
.util
.Iterator
;
5 import java
.util
.TreeMap
;
7 import junit
.framework
.TestCase
;
9 import org
.eclipse
.core
.runtime
.adaptor
.EclipseStarter
;
10 import org
.osgi
.framework
.Bundle
;
11 import org
.osgi
.framework
.BundleContext
;
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;
18 public void testInstallAndStart() throws Exception
{
19 if (osgiRuntimeAlreadyRunning
) {
21 .println("OSGi runtime already running, skipping test...");
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());
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());
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.");
51 fail("Bundle not started after timeout limit.");
54 protected BundleContext
startRuntime() throws Exception
{
55 String
[] args
= { "-console", "-clean" };
56 BundleContext bundleContext
= EclipseStarter
.startup(args
, null);
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);
65 protected void stopRuntime() throws Exception
{
66 EclipseStarter
.shutdown();
69 public void setUp() throws Exception
{
70 osgiRuntimeAlreadyRunning
= EclipseStarter
.isRunning();
71 if (osgiRuntimeAlreadyRunning
)
73 BundleContext bundleContext
= startRuntime();
74 osgiBoot
= new OsgiBoot(bundleContext
);
77 public void tearDown() throws Exception
{
78 if (osgiRuntimeAlreadyRunning
)