]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.osgi.boot/ext/test/org/argeo/osgi/boot/OsgiBootRuntimeTest.java
Recognize explicit locations
[lgpl/argeo-commons.git] / org.argeo.osgi.boot / ext / test / org / argeo / osgi / boot / OsgiBootRuntimeTest.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.osgi.boot;
17
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.Map;
21 import java.util.ServiceLoader;
22 import java.util.TreeMap;
23
24 import junit.framework.TestCase;
25
26 import org.eclipse.core.runtime.adaptor.EclipseStarter;
27 import org.osgi.framework.Bundle;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.launch.Framework;
30 import org.osgi.framework.launch.FrameworkFactory;
31
32 /** Starts an Equinox runtime and provision it with OSGi boot. */
33 public class OsgiBootRuntimeTest extends TestCase {
34 protected OsgiBoot osgiBoot = null;
35 private boolean osgiRuntimeAlreadyRunning = false;
36
37 public void testInstallAndStart() throws Exception {
38 if (osgiRuntimeAlreadyRunning) {
39 System.out
40 .println("OSGi runtime already running, skipping test...");
41 return;
42 }
43 osgiBoot.installUrls(osgiBoot.getBundlesUrls(OsgiBoot.DEFAULT_BASE_URL,
44 OsgiBootNoRuntimeTest.BUNDLES));
45 Map<String, Bundle> map = new TreeMap<String, Bundle>(
46 osgiBoot.getBundlesBySymbolicName());
47 for (Iterator<String> keys = map.keySet().iterator(); keys.hasNext();) {
48 String key = keys.next();
49 Bundle bundle = map.get(key);
50 System.out.println(key + " : " + bundle.getLocation());
51 }
52 assertEquals(4, map.size());
53 Iterator<String> keys = map.keySet().iterator();
54 assertEquals("org.argeo.osgi.boot.test.bundle1", keys.next());
55 assertEquals("org.argeo.osgi.boot.test.bundle2", keys.next());
56 assertEquals("org.argeo.osgi.boot.test.bundle3", keys.next());
57 assertEquals("org.eclipse.osgi", keys.next());
58
59 // osgiBoot.startBundles("org.argeo.osgi.boot.test.bundle2");
60 long begin = System.currentTimeMillis();
61 while (System.currentTimeMillis() - begin < 10000) {
62 Map<String, Bundle> mapBundles = osgiBoot
63 .getBundlesBySymbolicName();
64 Bundle bundle = mapBundles.get("org.argeo.osgi.boot.test.bundle2");
65 if (bundle.getState() == Bundle.ACTIVE) {
66 System.out.println("Bundle " + bundle + " started.");
67 return;
68 }
69 }
70 fail("Bundle not started after timeout limit.");
71 }
72
73 protected BundleContext startRuntime() throws Exception {
74 String[] args = { "-console", "-clean" };
75 BundleContext bundleContext = EclipseStarter.startup(args, null);
76
77 // ServiceLoader<FrameworkFactory> ff = ServiceLoader.load(FrameworkFactory.class);
78 // Map<String,String> config = new HashMap<String,String>();
79 // Framework fwk = ff.iterator().next().newFramework(config);
80 // fwk.start();
81 return bundleContext;
82 }
83
84 protected void stopRuntime() throws Exception {
85 EclipseStarter.shutdown();
86 }
87
88 public void setUp() throws Exception {
89 osgiRuntimeAlreadyRunning = EclipseStarter.isRunning();
90 if (osgiRuntimeAlreadyRunning)
91 return;
92 BundleContext bundleContext = startRuntime();
93 osgiBoot = new OsgiBoot(bundleContext);
94 }
95
96 public void tearDown() throws Exception {
97 if (osgiRuntimeAlreadyRunning)
98 return;
99 osgiBoot = null;
100 stopRuntime();
101 }
102
103 }