1 package org
.argeo
.slc
.spring
.unit
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
8 import org
.argeo
.api
.cms
.CmsLog
;
9 import org
.argeo
.init
.osgi
.OsgiBoot
;
10 import org
.argeo
.slc
.SlcException
;
11 import org
.eclipse
.core
.runtime
.adaptor
.EclipseStarter
;
12 import org
.eclipse
.gemini
.blueprint
.util
.OsgiStringUtils
;
13 import org
.osgi
.framework
.Bundle
;
14 import org
.osgi
.framework
.BundleContext
;
15 import org
.osgi
.framework
.InvalidSyntaxException
;
16 import org
.osgi
.framework
.ServiceReference
;
17 import org
.springframework
.context
.ApplicationContext
;
19 import junit
.framework
.TestCase
;
21 @SuppressWarnings("restriction")
22 public abstract class AbstractOsgiRuntimeTestCase
extends TestCase
{
23 private final static CmsLog log
= CmsLog
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 StringBuffer badBundles
= null;
105 while (duration
< getResolvedTimeout()) {
106 badBundles
= new StringBuffer();
107 for (Bundle bundle
: bundleContext
.getBundles()) {
108 if (bundle
.getSymbolicName() != null
109 && bundle
.getSymbolicName().startsWith(
110 "org.eclipse.jdt")) {
111 // don't check Eclipse SDK bundles
115 if (bundle
.getState() == Bundle
.INSTALLED
) {
116 allBundlesOk
= false;
118 .append(OsgiStringUtils
119 .nullSafeSymbolicName(bundle
)
122 .bundleStateAsString(bundle
) + "]");
125 if (bundlesToStart
.contains(bundle
.getSymbolicName())
126 && bundle
.getState() != Bundle
.ACTIVE
) {
127 allBundlesOk
= false;
128 badBundles
.append(OsgiStringUtils
129 .nullSafeSymbolicName(bundle
)
131 + OsgiStringUtils
.bundleStateAsString(bundle
)
141 duration
= System
.currentTimeMillis() - begin
;
145 listInstalledBundles();
146 throw new SlcException(
147 "Some bundles are not at the proper status:\n" + badBundles
);
152 * Make sure that the application context of the started bundles starting
153 * with this prefix are properly initialized
155 protected void assertStartedBundlesApplicationContext(
156 String bundleSymbolicNamesPrefix
) {
157 List
<String
> bundlesToStart
= getBundlesToStart();
158 for (String bundleSName
: bundlesToStart
) {
159 if (bundleSName
.startsWith(bundleSymbolicNamesPrefix
))
160 assertBundleApplicationContext(bundleSName
);
165 * Make sure that the application context of this bundle is properly
168 protected void assertBundleApplicationContext(String bundleSymbolicName
) {
169 String filter
= "(Bundle-SymbolicName=" + bundleSymbolicName
+ ")";
170 // Wait for application context to be ready
172 ServiceReference
[] srs
= getServiceRefSynchronous(
173 ApplicationContext
.class.getName(), filter
);
175 throw new SlcException("No application context for "
176 + bundleSymbolicName
);
177 } catch (InvalidSyntaxException e
) {
178 throw new SlcException(
179 "Unexpected exception when looking for application context for bundle "
180 + bundleSymbolicName
, e
);
182 log
.info("Application context of bundle " + bundleSymbolicName
183 + " is initalized.");
186 protected ServiceReference
[] getServiceRefSynchronous(String clss
,
187 String filter
) throws InvalidSyntaxException
{
189 if (log
.isTraceEnabled())
190 log
.debug("Filter: '" + filter
+ "'");
191 ServiceReference
[] sfs
= null;
192 boolean waiting
= true;
193 long begin
= System
.currentTimeMillis();
195 sfs
= getBundleContext().getServiceReferences(clss
, filter
);
201 if (System
.currentTimeMillis() - begin
> getDefaultTimeout())
202 throw new SlcException("Search of services " + clss
203 + " with filter " + filter
+ " timed out.");
209 protected BundleContext
getBundleContext() {
210 return osgiBoot
.getBundleContext();
213 /** Default is 30s */
214 protected long getResolvedTimeout() {
218 /** Default is 10s */
219 protected long getDefaultTimeout() {
223 final protected void sleep(long duration
) {
226 } catch (InterruptedException e
) {