]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.equinox/src/test/java/org/argeo/slc/osgi/test/AbstractOsgiRuntimeTestCase.java
Fix typo
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.equinox / src / test / java / org / argeo / slc / osgi / test / AbstractOsgiRuntimeTestCase.java
1 package org.argeo.slc.osgi.test;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import junit.framework.TestCase;
9
10 import org.apache.commons.logging.Log;
11 import org.apache.commons.logging.LogFactory;
12 import org.argeo.slc.SlcException;
13 import org.argeo.slc.osgiboot.OsgiBoot;
14 import org.eclipse.core.runtime.adaptor.EclipseStarter;
15 import org.osgi.framework.Bundle;
16 import org.osgi.framework.BundleContext;
17 import org.osgi.framework.InvalidSyntaxException;
18 import org.osgi.framework.ServiceReference;
19 import org.springframework.context.ApplicationContext;
20 import org.springframework.osgi.util.OsgiStringUtils;
21
22 public abstract class AbstractOsgiRuntimeTestCase extends TestCase {
23 private final static Log log = LogFactory
24 .getLog(AbstractOsgiRuntimeTestCase.class);
25
26 protected OsgiBoot osgiBoot = null;
27
28 protected void installBundles() throws Exception {
29
30 }
31
32 public void setUp() throws Exception {
33 // To avoid xerces from the classpath being detected as the provider
34 System
35 .setProperty("javax.xml.parsers.DocumentBuilderFactory",
36 "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
37 System.setProperty("javax.xml.parsers.SAXParserFactory",
38 "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
39
40 BundleContext bundleContext = startRuntime();
41 osgiBoot = new OsgiBoot(bundleContext);
42 log.info("OSGi runtime started.");
43
44 installBundles();
45
46 List<String> bundlesToStart = getBundlesToStart();
47 osgiBoot.startBundles(bundlesToStart);
48 waitAllBundlesOk(bundlesToStart);
49 if (log.isTraceEnabled())
50 listInstalledBundles();
51 }
52
53 public void tearDown() throws Exception {
54 osgiBoot = null;
55 stopRuntime();
56 log.info("OSGi runtime stopped.");
57 }
58
59 protected BundleContext startRuntime() throws Exception {
60 String[] args = { "-console", "-clean" };
61 BundleContext bundleContext = EclipseStarter.startup(args, null);
62 return bundleContext;
63 }
64
65 protected void stopRuntime() throws Exception {
66 EclipseStarter.shutdown();
67 }
68
69 protected List<String> getBundlesToStart() {
70 return new ArrayList<String>();
71 }
72
73 protected void listInstalledBundles() {
74 BundleContext bundleContext = osgiBoot.getBundleContext();
75 Bundle[] bundles = bundleContext.getBundles();
76 for (int i = 0; i < bundles.length; i++) {
77 System.out.println(OsgiStringUtils.nullSafeSymbolicName(bundles[i])
78 + " [" + OsgiStringUtils.bundleStateAsString(bundles[i])
79 + "] " + bundles[i].getLocation());
80 }
81
82 }
83
84 protected Map<Bundle, ApplicationContext> getOsgiApplicationContexts()
85 throws Exception {
86 Map<Bundle, ApplicationContext> map = new HashMap<Bundle, ApplicationContext>();
87 BundleContext bundleContext = osgiBoot.getBundleContext();
88 ServiceReference[] srs = bundleContext.getServiceReferences(
89 ApplicationContext.class.getName(), null);
90 for (ServiceReference sr : srs) {
91 ApplicationContext context = (ApplicationContext) bundleContext
92 .getService(sr);
93 map.put(sr.getBundle(), context);
94 }
95 return map;
96 }
97
98 /** Wait for all bundles to be either RESOLVED or ACTIVE. */
99 protected void waitAllBundlesOk(List<String> bundlesToStart) {
100 BundleContext bundleContext = osgiBoot.getBundleContext();
101 long begin = System.currentTimeMillis();
102 long duration = 0;
103 boolean allBundlesOk = true;
104 while (duration < getResolvedTimeout()) {
105 for (Bundle bundle : bundleContext.getBundles()) {
106 if (bundle.getSymbolicName() != null
107 && bundle.getSymbolicName().startsWith(
108 "org.eclipse.jdt")) {
109 // don't check Eclipse SDK bundles
110 continue;
111 }
112
113 if (bundle.getState() == Bundle.INSTALLED) {
114 allBundlesOk = false;
115 break;// for
116 }
117
118 if (bundlesToStart.contains(bundle.getSymbolicName())
119 && bundle.getState() != Bundle.ACTIVE) {
120 allBundlesOk = false;
121 break;// for
122 }
123 }
124
125 if (allBundlesOk)
126 break;// while
127
128 sleep(1000);
129
130 duration = System.currentTimeMillis() - begin;
131 }
132
133 if (!allBundlesOk) {
134 listInstalledBundles();
135 throw new SlcException("Some bundles are in INSTALLED status");
136 }
137 }
138
139 /**
140 * Make sure that the application context of the started bundles starting
141 * with this prefix are properly initialized
142 */
143 protected void assertStartedBundlesApplicationContext(
144 String bundleSymbolicNamesPrefix) {
145 List<String> bundlesToStart = getBundlesToStart();
146 for (String bundleSName : bundlesToStart) {
147 if (bundleSName.startsWith(bundleSymbolicNamesPrefix))
148 assertBundleApplicationContext(bundleSName);
149 }
150 }
151
152 /**
153 * Make sure that the application context of this bundle is properly
154 * initialized
155 */
156 protected void assertBundleApplicationContext(String bundleSymbolicName) {
157 String filter = "(Bundle-SymbolicName=" + bundleSymbolicName + ")";
158 // Wait for application context to be ready
159 try {
160 ServiceReference[] srs = getServiceRefSynchronous(
161 ApplicationContext.class.getName(), filter);
162 if (srs == null)
163 throw new SlcException("No application context for "
164 + bundleSymbolicName);
165 } catch (InvalidSyntaxException e) {
166 throw new SlcException(
167 "Unexpected exception when looking for application context for bundle "
168 + bundleSymbolicName, e);
169 }
170 log.info("Application context of bundle " + bundleSymbolicName
171 + " is initalized.");
172 }
173
174 protected ServiceReference[] getServiceRefSynchronous(String clss,
175 String filter) throws InvalidSyntaxException {
176 // FIXME: factorize
177 if (log.isTraceEnabled())
178 log.debug("Filter: '" + filter + "'");
179 ServiceReference[] sfs = null;
180 boolean waiting = true;
181 long begin = System.currentTimeMillis();
182 do {
183 sfs = getBundleContext().getServiceReferences(clss, filter);
184
185 if (sfs != null)
186 waiting = false;
187
188 sleep(100);
189 if (System.currentTimeMillis() - begin > getDefaultTimeout())
190 throw new SlcException("Search of services " + clss
191 + " with filter " + filter + " timed out.");
192 } while (waiting);
193
194 return sfs;
195 }
196
197 protected BundleContext getBundleContext() {
198 return osgiBoot.getBundleContext();
199 }
200
201 /** Default is 120s */
202 protected long getResolvedTimeout() {
203 return 120 * 1000l;
204 }
205
206 /** Default is 10s */
207 protected long getDefaultTimeout() {
208 return 10 * 1000l;
209 }
210
211 final protected void sleep(long duration) {
212 try {
213 Thread.sleep(1000);
214 } catch (InterruptedException e) {
215 // silent
216 }
217 }
218 }