]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.unit/src/main/java/org/argeo/slc/equinox/unit/AbstractOsgiRuntimeTestCase.java
Introduction of annotation to handle MVC flows // Class cleaning
[gpl/argeo-slc.git] / runtime / org.argeo.slc.unit / src / main / java / org / argeo / slc / equinox / unit / AbstractOsgiRuntimeTestCase.java
1 package org.argeo.slc.equinox.unit;
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 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
112 continue;
113 }
114
115 if (bundle.getState() == Bundle.INSTALLED) {
116 allBundlesOk = false;
117 badBundles
118 .append(OsgiStringUtils
119 .nullSafeSymbolicName(bundle)
120 + " ["
121 + OsgiStringUtils
122 .bundleStateAsString(bundle) + "]");
123 }
124
125 if (bundlesToStart.contains(bundle.getSymbolicName())
126 && bundle.getState() != Bundle.ACTIVE) {
127 allBundlesOk = false;
128 badBundles.append(OsgiStringUtils
129 .nullSafeSymbolicName(bundle)
130 + " ["
131 + OsgiStringUtils.bundleStateAsString(bundle)
132 + "]\n");
133 }
134 }
135
136 if (allBundlesOk)
137 break;// while
138
139 sleep(1000);
140
141 duration = System.currentTimeMillis() - begin;
142 }
143
144 if (!allBundlesOk) {
145 listInstalledBundles();
146 throw new SlcException(
147 "Some bundles are not at the proper status:\n" + badBundles);
148 }
149 }
150
151 /**
152 * Make sure that the application context of the started bundles starting
153 * with this prefix are properly initialized
154 */
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);
161 }
162 }
163
164 /**
165 * Make sure that the application context of this bundle is properly
166 * initialized
167 */
168 protected void assertBundleApplicationContext(String bundleSymbolicName) {
169 String filter = "(Bundle-SymbolicName=" + bundleSymbolicName + ")";
170 // Wait for application context to be ready
171 try {
172 ServiceReference[] srs = getServiceRefSynchronous(
173 ApplicationContext.class.getName(), filter);
174 if (srs == null)
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);
181 }
182 log.info("Application context of bundle " + bundleSymbolicName
183 + " is initalized.");
184 }
185
186 protected ServiceReference[] getServiceRefSynchronous(String clss,
187 String filter) throws InvalidSyntaxException {
188 // FIXME: factorize
189 if (log.isTraceEnabled())
190 log.debug("Filter: '" + filter + "'");
191 ServiceReference[] sfs = null;
192 boolean waiting = true;
193 long begin = System.currentTimeMillis();
194 do {
195 sfs = getBundleContext().getServiceReferences(clss, filter);
196
197 if (sfs != null)
198 waiting = false;
199
200 sleep(100);
201 if (System.currentTimeMillis() - begin > getDefaultTimeout())
202 throw new SlcException("Search of services " + clss
203 + " with filter " + filter + " timed out.");
204 } while (waiting);
205
206 return sfs;
207 }
208
209 protected BundleContext getBundleContext() {
210 return osgiBoot.getBundleContext();
211 }
212
213 /** Default is 30s */
214 protected long getResolvedTimeout() {
215 return 30 * 1000l;
216 }
217
218 /** Default is 10s */
219 protected long getDefaultTimeout() {
220 return 10 * 1000l;
221 }
222
223 final protected void sleep(long duration) {
224 try {
225 Thread.sleep(1000);
226 } catch (InterruptedException e) {
227 // silent
228 }
229 }
230 }