PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/samples/blueprint/helloworld/helloworld-itests/src/test/java/org/apache/aries/samples/blueprint/helloworld/itests/AbstractIntegrationTest.java

https://github.com/bosschaert/aries
Java | 282 lines | 218 code | 35 blank | 29 comment | 23 complexity | a9d64768a63f56aeb21bbb8b14567783 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.apache.aries.samples.blueprint.helloworld.itests;
  20. import static org.ops4j.pax.exam.CoreOptions.options;
  21. import static org.ops4j.pax.exam.CoreOptions.wrappedBundle;
  22. import static org.ops4j.pax.exam.OptionUtils.combine;
  23. import java.io.BufferedReader;
  24. import java.io.FileNotFoundException;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.io.InputStreamReader;
  28. import java.net.HttpURLConnection;
  29. import java.net.MalformedURLException;
  30. import java.net.URL;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33. import java.util.Properties;
  34. import org.junit.After;
  35. import org.junit.Before;
  36. import org.ops4j.pax.exam.CoreOptions;
  37. import org.ops4j.pax.exam.Inject;
  38. import org.ops4j.pax.exam.Option;
  39. import org.ops4j.pax.exam.options.MavenArtifactProvisionOption;
  40. import org.ops4j.pax.url.mvn.Handler;
  41. import org.ops4j.pax.url.mvn.ServiceConstants;
  42. import org.osgi.framework.Bundle;
  43. import org.osgi.framework.BundleContext;
  44. import org.osgi.framework.Constants;
  45. import org.osgi.framework.Filter;
  46. import org.osgi.framework.FrameworkUtil;
  47. import org.osgi.framework.InvalidSyntaxException;
  48. import org.osgi.framework.ServiceReference;
  49. import org.osgi.util.tracker.ServiceTracker;
  50. public abstract class AbstractIntegrationTest {
  51. private static final int CONNECTION_TIMEOUT = 30000;
  52. public static final long DEFAULT_TIMEOUT = 60000;
  53. @Inject
  54. protected BundleContext bundleContext;
  55. private List<ServiceTracker> srs;
  56. @Before
  57. public void setUp() {
  58. srs = new ArrayList<ServiceTracker>();
  59. }
  60. @After
  61. public void tearDown() throws Exception{
  62. for (ServiceTracker st : srs) {
  63. if (st != null) {
  64. st.close();
  65. }
  66. }
  67. }
  68. public static MavenArtifactProvisionOption mavenBundle(String groupId, String artifactId) {
  69. return CoreOptions.mavenBundle().groupId(groupId).artifactId(artifactId).versionAsInProject();
  70. }
  71. public static MavenArtifactProvisionOption mavenBundleInTest(String groupId, String artifactId) {
  72. return CoreOptions.mavenBundle().groupId(groupId).artifactId(artifactId).version(getArtifactVersion(groupId, artifactId));
  73. }
  74. public static String getArtifactVersion( final String groupId,
  75. final String artifactId )
  76. {
  77. final Properties dependencies = new Properties();
  78. try
  79. {
  80. InputStream in = getFileFromClasspath("META-INF/maven/dependencies.properties");
  81. try {
  82. dependencies.load(in);
  83. } finally {
  84. in.close();
  85. }
  86. final String version = dependencies.getProperty( groupId + "/" + artifactId + "/version" );
  87. if( version == null )
  88. {
  89. throw new RuntimeException(
  90. "Could not resolve version. Do you have a dependency for " + groupId + "/" + artifactId
  91. + " in your maven project?"
  92. );
  93. }
  94. return version;
  95. }
  96. catch( IOException e )
  97. {
  98. // TODO throw a better exception
  99. throw new RuntimeException(
  100. "Could not resolve version. Did you configured the plugin in your maven project?"
  101. + "Or maybe you did not run the maven build and you are using an IDE?"
  102. );
  103. }
  104. }
  105. protected Bundle getInstalledBundle(String symbolicName) {
  106. for (Bundle b : bundleContext.getBundles()) {
  107. if (b.getSymbolicName().equals(symbolicName)) {
  108. return b;
  109. }
  110. }
  111. return null;
  112. }
  113. private static InputStream getFileFromClasspath( final String filePath )
  114. throws FileNotFoundException
  115. {
  116. try
  117. {
  118. URL fileURL = AbstractIntegrationTest.class.getClassLoader().getResource( filePath );
  119. if( fileURL == null )
  120. {
  121. throw new FileNotFoundException( "File [" + filePath + "] could not be found in classpath" );
  122. }
  123. return fileURL.openStream();
  124. }
  125. catch (IOException e)
  126. {
  127. throw new FileNotFoundException( "File [" + filePath + "] could not be found: " + e.getMessage() );
  128. }
  129. }
  130. protected void listBundleServices(Bundle b) {
  131. ServiceReference []srb = b.getRegisteredServices();
  132. for(ServiceReference sr:srb){
  133. System.out.println(b.getSymbolicName() + " SERVICE: "+sr);
  134. }
  135. }
  136. protected Boolean isServiceRegistered(Bundle b) {
  137. ServiceReference []srb = b.getRegisteredServices();
  138. if(srb == null) {
  139. return false;
  140. }
  141. return true;
  142. }
  143. protected void waitForServices(Bundle b, String sclass) {
  144. try {
  145. BundleContext bc = b.getBundleContext();
  146. String bsn = b.getSymbolicName();
  147. ServiceTracker st = new ServiceTracker(bc, sclass, null);
  148. st.open();
  149. Object bac = st.waitForService(DEFAULT_TIMEOUT);
  150. /* Uncomment for debug */
  151. /*
  152. if(bac == null) {
  153. System.out.println("SERVICE NOTFOUND " + bsn);
  154. } else {
  155. System.out.println("SERVICE FOUND " + bsn);
  156. }
  157. */
  158. st.close();
  159. return;
  160. }
  161. catch (Exception e) {
  162. System.out.println("Failed to register services for " + b.getSymbolicName() + e.getMessage());
  163. }
  164. }
  165. protected static Option[] updateOptions(Option[] options) {
  166. if ("IBM Corporation".equals(System.getProperty("java.vendor"))) {
  167. Option[] ibmOptions = options(
  168. wrappedBundle(mavenBundle("org.ops4j.pax.exam", "pax-exam-junit"))
  169. );
  170. options = combine(ibmOptions, options);
  171. }
  172. return options;
  173. }
  174. public static String getHTTPResponse(HttpURLConnection conn) throws IOException
  175. {
  176. StringBuilder response = new StringBuilder();
  177. BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(),
  178. "ISO-8859-1"));
  179. try {
  180. for (String s = reader.readLine(); s != null; s = reader.readLine()) {
  181. response.append(s).append("\r\n");
  182. }
  183. } finally {
  184. reader.close();
  185. }
  186. return response.toString();
  187. }
  188. public static HttpURLConnection makeConnection(String contextPath) throws IOException
  189. {
  190. URL url = new URL(contextPath);
  191. HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  192. conn.setConnectTimeout(CONNECTION_TIMEOUT);
  193. conn.connect();
  194. return conn;
  195. }
  196. protected <T> T getOsgiService(Class<T> type, long timeout) {
  197. return getOsgiService(type, null, timeout);
  198. }
  199. protected <T> T getOsgiService(Class<T> type) {
  200. return getOsgiService(type, null, DEFAULT_TIMEOUT);
  201. }
  202. protected <T> T getOsgiService(Class<T> type, String filter, long timeout) {
  203. return getOsgiService(null, type, filter, timeout);
  204. }
  205. protected <T> T getOsgiService(BundleContext bc, Class<T> type,
  206. String filter, long timeout) {
  207. ServiceTracker tracker = null;
  208. try {
  209. String flt;
  210. if (filter != null) {
  211. if (filter.startsWith("(")) {
  212. flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")"
  213. + filter + ")";
  214. } else {
  215. flt = "(&(" + Constants.OBJECTCLASS + "=" + type.getName() + ")("
  216. + filter + "))";
  217. }
  218. } else {
  219. flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
  220. }
  221. Filter osgiFilter = FrameworkUtil.createFilter(flt);
  222. tracker = new ServiceTracker(bc == null ? bundleContext : bc, osgiFilter,
  223. null);
  224. tracker.open();
  225. // add tracker to the list of trackers we close at tear down
  226. srs.add(tracker);
  227. Object x = tracker.waitForService(timeout);
  228. Object svc = type.cast(x);
  229. if (svc == null) {
  230. throw new RuntimeException("Gave up waiting for service " + flt);
  231. }
  232. return type.cast(svc);
  233. } catch (InvalidSyntaxException e) {
  234. throw new IllegalArgumentException("Invalid filter", e);
  235. } catch (InterruptedException e) {
  236. throw new RuntimeException(e);
  237. }
  238. }
  239. public static URL getUrlToEba(String groupId, String artifactId) throws MalformedURLException {
  240. String artifactVersion = getArtifactVersion(groupId, artifactId);
  241. // Need to use handler from org.ops4j.pax.url.mvn
  242. URL urlToEba = new URL(null,
  243. ServiceConstants.PROTOCOL + ":" + groupId + "/" +artifactId + "/"
  244. + artifactVersion + "/eba", new Handler());
  245. return urlToEba;
  246. }
  247. }