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