]> git.argeo.org Git - lgpl/argeo-commons.git/blob - osgiboot/OsgiBootRuntimeTest.java
Prepare next development cycle
[lgpl/argeo-commons.git] / osgiboot / OsgiBootRuntimeTest.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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
17 package org.argeo.slc.osgiboot;
18
19 import java.util.Iterator;
20 import java.util.Map;
21 import java.util.TreeMap;
22
23 import junit.framework.TestCase;
24
25 import org.argeo.osgi.boot.OsgiBoot;
26 import org.eclipse.core.runtime.adaptor.EclipseStarter;
27 import org.osgi.framework.Bundle;
28 import org.osgi.framework.BundleContext;
29
30 public class OsgiBootRuntimeTest extends TestCase {
31 protected OsgiBoot osgiBoot = null;
32
33 public void testInstallAndStart() throws Exception {
34 osgiBoot.installUrls(osgiBoot.getBundlesUrls(OsgiBoot.DEFAULT_BASE_URL,
35 OsgiBootNoRuntimeTest.BUNDLES));
36 Map map = new TreeMap(osgiBoot.getBundles());
37 for (Iterator keys = map.keySet().iterator(); keys.hasNext();) {
38 Object key = keys.next();
39 Bundle bundle = (Bundle) map.get(key);
40 System.out.println(key + " : " + bundle.getLocation());
41 }
42 assertEquals(4, map.size());
43 Iterator keys = map.keySet().iterator();
44 assertEquals("org.argeo.slc.osgiboot.test.bundle1", keys.next());
45 assertEquals("org.argeo.slc.osgiboot.test.bundle2", keys.next());
46 assertEquals("org.argeo.slc.osgiboot.test.bundle3", keys.next());
47 assertEquals("org.eclipse.osgi", keys.next());
48
49 osgiBoot.startBundles("org.argeo.slc.osgiboot.test.bundle2");
50 long begin = System.currentTimeMillis();
51 while (System.currentTimeMillis() - begin < 10000) {
52 Map mapBundles = osgiBoot.getBundles();
53 Bundle bundle = (Bundle) mapBundles
54 .get("org.argeo.slc.osgiboot.test.bundle2");
55 if (bundle.getState() == Bundle.ACTIVE) {
56 System.out.println("Bundle " + bundle + " started.");
57 return;
58 }
59 }
60 fail("Bundle not started after timeout limit.");
61 }
62
63 protected BundleContext startRuntime() throws Exception {
64 String[] args = { "-console", "-clean" };
65 BundleContext bundleContext = EclipseStarter.startup(args, null);
66 return bundleContext;
67 }
68
69 protected void stopRuntime() throws Exception {
70 EclipseStarter.shutdown();
71 }
72
73 public void setUp() throws Exception {
74 BundleContext bundleContext = startRuntime();
75 osgiBoot = new OsgiBoot(bundleContext);
76 }
77
78 public void tearDown() throws Exception {
79 osgiBoot = null;
80 stopRuntime();
81 }
82
83 }