1 package org
.argeo
.slc
.osgi
.test
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
8 import javax
.management
.BadBinaryOpValueExpException
;
10 import junit
.framework
.TestCase
;
12 import org
.apache
.commons
.logging
.Log
;
13 import org
.apache
.commons
.logging
.LogFactory
;
14 import org
.argeo
.slc
.SlcException
;
15 import org
.argeo
.slc
.osgiboot
.OsgiBoot
;
16 import org
.eclipse
.core
.runtime
.adaptor
.EclipseStarter
;
17 import org
.osgi
.framework
.Bundle
;
18 import org
.osgi
.framework
.BundleContext
;
19 import org
.osgi
.framework
.InvalidSyntaxException
;
20 import org
.osgi
.framework
.ServiceReference
;
21 import org
.springframework
.context
.ApplicationContext
;
22 import org
.springframework
.osgi
.util
.OsgiStringUtils
;
24 public abstract class AbstractOsgiRuntimeTestCase
extends TestCase
{
25 private final static Log log
= LogFactory
26 .getLog(AbstractOsgiRuntimeTestCase
.class);
28 protected OsgiBoot osgiBoot
= null;
30 protected void installBundles() throws Exception
{
34 public void setUp() throws Exception
{
35 // To avoid xerces from the classpath being detected as the provider
37 .setProperty("javax.xml.parsers.DocumentBuilderFactory",
38 "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
39 System
.setProperty("javax.xml.parsers.SAXParserFactory",
40 "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
42 BundleContext bundleContext
= startRuntime();
43 osgiBoot
= new OsgiBoot(bundleContext
);
44 log
.info("OSGi runtime started.");
48 List
<String
> bundlesToStart
= getBundlesToStart();
49 osgiBoot
.startBundles(bundlesToStart
);
50 waitAllBundlesOk(bundlesToStart
);
51 if (log
.isTraceEnabled())
52 listInstalledBundles();
55 public void tearDown() throws Exception
{
58 log
.info("OSGi runtime stopped.");
61 protected BundleContext
startRuntime() throws Exception
{
62 String
[] args
= { "-console", "-clean" };
63 BundleContext bundleContext
= EclipseStarter
.startup(args
, null);
67 protected void stopRuntime() throws Exception
{
68 EclipseStarter
.shutdown();
71 protected List
<String
> getBundlesToStart() {
72 return new ArrayList
<String
>();
75 protected void listInstalledBundles() {
76 BundleContext bundleContext
= osgiBoot
.getBundleContext();
77 Bundle
[] bundles
= bundleContext
.getBundles();
78 for (int i
= 0; i
< bundles
.length
; i
++) {
79 System
.out
.println(OsgiStringUtils
.nullSafeSymbolicName(bundles
[i
])
80 + " [" + OsgiStringUtils
.bundleStateAsString(bundles
[i
])
81 + "] " + bundles
[i
].getLocation());
86 protected Map
<Bundle
, ApplicationContext
> getOsgiApplicationContexts()
88 Map
<Bundle
, ApplicationContext
> map
= new HashMap
<Bundle
, ApplicationContext
>();
89 BundleContext bundleContext
= osgiBoot
.getBundleContext();
90 ServiceReference
[] srs
= bundleContext
.getServiceReferences(
91 ApplicationContext
.class.getName(), null);
92 for (ServiceReference sr
: srs
) {
93 ApplicationContext context
= (ApplicationContext
) bundleContext
95 map
.put(sr
.getBundle(), context
);
100 /** Wait for all bundles to be either RESOLVED or ACTIVE. */
101 protected void waitAllBundlesOk(List
<String
> bundlesToStart
) {
102 BundleContext bundleContext
= osgiBoot
.getBundleContext();
103 long begin
= System
.currentTimeMillis();
105 boolean allBundlesOk
= true;
106 StringBuffer badBundles
= null;
107 while (duration
< getResolvedTimeout()) {
108 badBundles
= new StringBuffer();
109 for (Bundle bundle
: bundleContext
.getBundles()) {
110 if (bundle
.getSymbolicName() != null
111 && bundle
.getSymbolicName().startsWith(
112 "org.eclipse.jdt")) {
113 // don't check Eclipse SDK bundles
117 if (bundle
.getState() == Bundle
.INSTALLED
) {
118 allBundlesOk
= false;
120 .append(OsgiStringUtils
121 .nullSafeSymbolicName(bundle
)
124 .bundleStateAsString(bundle
) + "]");
127 if (bundlesToStart
.contains(bundle
.getSymbolicName())
128 && bundle
.getState() != Bundle
.ACTIVE
) {
129 allBundlesOk
= false;
130 badBundles
.append(OsgiStringUtils
131 .nullSafeSymbolicName(bundle
)
133 + OsgiStringUtils
.bundleStateAsString(bundle
)
143 duration
= System
.currentTimeMillis() - begin
;
147 listInstalledBundles();
148 throw new SlcException(
149 "Some bundles are not at the proper status:\n" + badBundles
);
154 * Make sure that the application context of the started bundles starting
155 * with this prefix are properly initialized
157 protected void assertStartedBundlesApplicationContext(
158 String bundleSymbolicNamesPrefix
) {
159 List
<String
> bundlesToStart
= getBundlesToStart();
160 for (String bundleSName
: bundlesToStart
) {
161 if (bundleSName
.startsWith(bundleSymbolicNamesPrefix
))
162 assertBundleApplicationContext(bundleSName
);
167 * Make sure that the application context of this bundle is properly
170 protected void assertBundleApplicationContext(String bundleSymbolicName
) {
171 String filter
= "(Bundle-SymbolicName=" + bundleSymbolicName
+ ")";
172 // Wait for application context to be ready
174 ServiceReference
[] srs
= getServiceRefSynchronous(
175 ApplicationContext
.class.getName(), filter
);
177 throw new SlcException("No application context for "
178 + bundleSymbolicName
);
179 } catch (InvalidSyntaxException e
) {
180 throw new SlcException(
181 "Unexpected exception when looking for application context for bundle "
182 + bundleSymbolicName
, e
);
184 log
.info("Application context of bundle " + bundleSymbolicName
185 + " is initalized.");
188 protected ServiceReference
[] getServiceRefSynchronous(String clss
,
189 String filter
) throws InvalidSyntaxException
{
191 if (log
.isTraceEnabled())
192 log
.debug("Filter: '" + filter
+ "'");
193 ServiceReference
[] sfs
= null;
194 boolean waiting
= true;
195 long begin
= System
.currentTimeMillis();
197 sfs
= getBundleContext().getServiceReferences(clss
, filter
);
203 if (System
.currentTimeMillis() - begin
> getDefaultTimeout())
204 throw new SlcException("Search of services " + clss
205 + " with filter " + filter
+ " timed out.");
211 protected BundleContext
getBundleContext() {
212 return osgiBoot
.getBundleContext();
215 /** Default is 30s */
216 protected long getResolvedTimeout() {
220 /** Default is 10s */
221 protected long getDefaultTimeout() {
225 final protected void sleep(long duration
) {
228 } catch (InterruptedException e
) {