1 package org
.argeo
.slc
.osgi
.test
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
8 import junit
.framework
.TestCase
;
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
;
22 public abstract class AbstractOsgiRuntimeTestCase
extends TestCase
{
23 private final static Log log
= LogFactory
24 .getLog(AbstractOsgiRuntimeTestCase
.class);
26 protected OsgiBoot osgiBoot
= null;
28 protected void installBundles() throws Exception
{
32 public void setUp() throws Exception
{
33 // To avoid xerces from the classpath being detected as the provider
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");
40 BundleContext bundleContext
= startRuntime();
41 osgiBoot
= new OsgiBoot(bundleContext
);
42 log
.info("OSGi runtime started.");
46 List
<String
> bundlesToStart
= getBundlesToStart();
47 osgiBoot
.startBundles(bundlesToStart
);
48 waitAllBundlesOk(bundlesToStart
);
49 if (log
.isTraceEnabled())
50 listInstalledBundles();
53 public void tearDown() throws Exception
{
56 log
.info("OSGi runtime stopped.");
59 protected BundleContext
startRuntime() throws Exception
{
60 String
[] args
= { "-console", "-clean" };
61 BundleContext bundleContext
= EclipseStarter
.startup(args
, null);
65 protected void stopRuntime() throws Exception
{
66 EclipseStarter
.shutdown();
69 protected List
<String
> getBundlesToStart() {
70 return new ArrayList
<String
>();
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());
84 protected Map
<Bundle
, ApplicationContext
> getOsgiApplicationContexts()
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
93 map
.put(sr
.getBundle(), context
);
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();
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
113 if (bundle
.getState() == Bundle
.INSTALLED
) {
114 allBundlesOk
= false;
118 if (bundlesToStart
.contains(bundle
.getSymbolicName())
119 && bundle
.getState() != Bundle
.ACTIVE
) {
120 allBundlesOk
= false;
130 duration
= System
.currentTimeMillis() - begin
;
134 listInstalledBundles();
135 throw new SlcException("Some bundles are in INSTALLED status");
140 * Make sure that the application context of the started bundles starting
141 * with this prefix are properly initialized
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
);
153 * Make sure that the application context of this bundle is properly
156 protected void assertBundleApplicationContext(String bundleSymbolicName
) {
157 String filter
= "(Bundle-SymbolicName=" + bundleSymbolicName
+ ")";
158 // Wait for application context to be ready
160 ServiceReference
[] srs
= getServiceRefSynchronous(
161 ApplicationContext
.class.getName(), filter
);
163 throw new SlcException("No application context for "
164 + bundleSymbolicName
);
165 } catch (InvalidSyntaxException e
) {
166 throw new SlcException(
167 "Unexpected exception when looking for applicaiton context for bundle "
168 + bundleSymbolicName
, e
);
170 log
.info("Application context of bundle " + bundleSymbolicName
171 + " is initalized.");
174 protected ServiceReference
[] getServiceRefSynchronous(String clss
,
175 String filter
) throws InvalidSyntaxException
{
177 if (log
.isTraceEnabled())
178 log
.debug("Filter: '" + filter
+ "'");
179 ServiceReference
[] sfs
= null;
180 boolean waiting
= true;
181 long begin
= System
.currentTimeMillis();
183 sfs
= getBundleContext().getServiceReferences(clss
, filter
);
189 if (System
.currentTimeMillis() - begin
> getDefaultTimeout())
190 throw new SlcException("Search of services " + clss
191 + " with filter " + filter
+ " timed out.");
197 protected BundleContext
getBundleContext() {
198 return osgiBoot
.getBundleContext();
201 /** Default is 120s */
202 protected long getResolvedTimeout() {
206 /** Default is 10s */
207 protected long getDefaultTimeout() {
211 final protected void sleep(long duration
) {
214 } catch (InterruptedException e
) {