1 package org
.argeo
.slc
.spring
.unit
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
8 import org
.apache
.commons
.logging
.Log
;
9 import org
.apache
.commons
.logging
.LogFactory
;
10 import org
.argeo
.osgi
.boot
.OsgiBoot
;
11 import org
.argeo
.slc
.SlcException
;
12 import org
.eclipse
.core
.runtime
.adaptor
.EclipseStarter
;
13 import org
.eclipse
.gemini
.blueprint
.util
.OsgiStringUtils
;
14 import org
.osgi
.framework
.Bundle
;
15 import org
.osgi
.framework
.BundleContext
;
16 import org
.osgi
.framework
.InvalidSyntaxException
;
17 import org
.osgi
.framework
.ServiceReference
;
18 import org
.springframework
.context
.ApplicationContext
;
20 import junit
.framework
.TestCase
;
22 @SuppressWarnings("restriction")
23 public abstract class AbstractOsgiRuntimeTestCase
extends TestCase
{
24 private final static Log log
= LogFactory
25 .getLog(AbstractOsgiRuntimeTestCase
.class);
27 protected OsgiBoot osgiBoot
= null;
29 protected void installBundles() throws Exception
{
33 public void setUp() throws Exception
{
34 // To avoid xerces from the classpath being detected as the provider
36 .setProperty("javax.xml.parsers.DocumentBuilderFactory",
37 "com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
38 System
.setProperty("javax.xml.parsers.SAXParserFactory",
39 "com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl");
41 BundleContext bundleContext
= startRuntime();
42 osgiBoot
= new OsgiBoot(bundleContext
);
43 log
.info("OSGi runtime started.");
47 List
<String
> bundlesToStart
= getBundlesToStart();
48 osgiBoot
.startBundles(bundlesToStart
);
49 waitAllBundlesOk(bundlesToStart
);
50 if (log
.isTraceEnabled())
51 listInstalledBundles();
54 public void tearDown() throws Exception
{
57 log
.info("OSGi runtime stopped.");
60 protected BundleContext
startRuntime() throws Exception
{
61 String
[] args
= { "-console", "-clean" };
62 BundleContext bundleContext
= EclipseStarter
.startup(args
, null);
66 protected void stopRuntime() throws Exception
{
67 EclipseStarter
.shutdown();
70 protected List
<String
> getBundlesToStart() {
71 return new ArrayList
<String
>();
74 protected void listInstalledBundles() {
75 BundleContext bundleContext
= osgiBoot
.getBundleContext();
76 Bundle
[] bundles
= bundleContext
.getBundles();
77 for (int i
= 0; i
< bundles
.length
; i
++) {
78 System
.out
.println(OsgiStringUtils
.nullSafeSymbolicName(bundles
[i
])
79 + " [" + OsgiStringUtils
.bundleStateAsString(bundles
[i
])
80 + "] " + bundles
[i
].getLocation());
85 protected Map
<Bundle
, ApplicationContext
> getOsgiApplicationContexts()
87 Map
<Bundle
, ApplicationContext
> map
= new HashMap
<Bundle
, ApplicationContext
>();
88 BundleContext bundleContext
= osgiBoot
.getBundleContext();
89 ServiceReference
[] srs
= bundleContext
.getServiceReferences(
90 ApplicationContext
.class.getName(), null);
91 for (ServiceReference sr
: srs
) {
92 ApplicationContext context
= (ApplicationContext
) bundleContext
94 map
.put(sr
.getBundle(), context
);
99 /** Wait for all bundles to be either RESOLVED or ACTIVE. */
100 protected void waitAllBundlesOk(List
<String
> bundlesToStart
) {
101 BundleContext bundleContext
= osgiBoot
.getBundleContext();
102 long begin
= System
.currentTimeMillis();
104 boolean allBundlesOk
= true;
105 StringBuffer badBundles
= null;
106 while (duration
< getResolvedTimeout()) {
107 badBundles
= new StringBuffer();
108 for (Bundle bundle
: bundleContext
.getBundles()) {
109 if (bundle
.getSymbolicName() != null
110 && bundle
.getSymbolicName().startsWith(
111 "org.eclipse.jdt")) {
112 // don't check Eclipse SDK bundles
116 if (bundle
.getState() == Bundle
.INSTALLED
) {
117 allBundlesOk
= false;
119 .append(OsgiStringUtils
120 .nullSafeSymbolicName(bundle
)
123 .bundleStateAsString(bundle
) + "]");
126 if (bundlesToStart
.contains(bundle
.getSymbolicName())
127 && bundle
.getState() != Bundle
.ACTIVE
) {
128 allBundlesOk
= false;
129 badBundles
.append(OsgiStringUtils
130 .nullSafeSymbolicName(bundle
)
132 + OsgiStringUtils
.bundleStateAsString(bundle
)
142 duration
= System
.currentTimeMillis() - begin
;
146 listInstalledBundles();
147 throw new SlcException(
148 "Some bundles are not at the proper status:\n" + badBundles
);
153 * Make sure that the application context of the started bundles starting
154 * with this prefix are properly initialized
156 protected void assertStartedBundlesApplicationContext(
157 String bundleSymbolicNamesPrefix
) {
158 List
<String
> bundlesToStart
= getBundlesToStart();
159 for (String bundleSName
: bundlesToStart
) {
160 if (bundleSName
.startsWith(bundleSymbolicNamesPrefix
))
161 assertBundleApplicationContext(bundleSName
);
166 * Make sure that the application context of this bundle is properly
169 protected void assertBundleApplicationContext(String bundleSymbolicName
) {
170 String filter
= "(Bundle-SymbolicName=" + bundleSymbolicName
+ ")";
171 // Wait for application context to be ready
173 ServiceReference
[] srs
= getServiceRefSynchronous(
174 ApplicationContext
.class.getName(), filter
);
176 throw new SlcException("No application context for "
177 + bundleSymbolicName
);
178 } catch (InvalidSyntaxException e
) {
179 throw new SlcException(
180 "Unexpected exception when looking for application context for bundle "
181 + bundleSymbolicName
, e
);
183 log
.info("Application context of bundle " + bundleSymbolicName
184 + " is initalized.");
187 protected ServiceReference
[] getServiceRefSynchronous(String clss
,
188 String filter
) throws InvalidSyntaxException
{
190 if (log
.isTraceEnabled())
191 log
.debug("Filter: '" + filter
+ "'");
192 ServiceReference
[] sfs
= null;
193 boolean waiting
= true;
194 long begin
= System
.currentTimeMillis();
196 sfs
= getBundleContext().getServiceReferences(clss
, filter
);
202 if (System
.currentTimeMillis() - begin
> getDefaultTimeout())
203 throw new SlcException("Search of services " + clss
204 + " with filter " + filter
+ " timed out.");
210 protected BundleContext
getBundleContext() {
211 return osgiBoot
.getBundleContext();
214 /** Default is 30s */
215 protected long getResolvedTimeout() {
219 /** Default is 10s */
220 protected long getDefaultTimeout() {
224 final protected void sleep(long duration
) {
227 } catch (InterruptedException e
) {