PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/plugin/src/test/java/com/atlassian/upm/test/osgi/OsgiTestBase.java

https://bitbucket.org/atlassian/atlassian-universal-plugin-manager
Java | 297 lines | 242 code | 52 blank | 3 comment | 9 complexity | 1ca23ecbba7ce954fca5336d3255a498 MD5 | raw file
  1. package com.atlassian.upm.test.osgi;
  2. import java.net.URI;
  3. import java.util.Collections;
  4. import java.util.Hashtable;
  5. import java.util.List;
  6. import java.util.Map;
  7. import com.atlassian.upm.osgi.Bundle;
  8. import com.atlassian.upm.osgi.Bundle.HeaderClause;
  9. import com.atlassian.upm.osgi.Bundle.State;
  10. import com.atlassian.upm.osgi.BundleAccessor;
  11. import com.atlassian.upm.osgi.Package;
  12. import com.atlassian.upm.osgi.PackageAccessor;
  13. import com.atlassian.upm.osgi.Service;
  14. import com.atlassian.upm.osgi.ServiceAccessor;
  15. import com.atlassian.upm.osgi.impl.BundleAccessorImpl;
  16. import com.atlassian.upm.osgi.impl.PackageAccessorImpl;
  17. import com.atlassian.upm.osgi.impl.ServiceAccessorImpl;
  18. import org.junit.Before;
  19. import org.junit.runner.RunWith;
  20. import org.mockito.Mock;
  21. import org.mockito.runners.MockitoJUnitRunner;
  22. import org.osgi.framework.BundleContext;
  23. import org.osgi.framework.ServiceReference;
  24. import org.osgi.service.packageadmin.ExportedPackage;
  25. import org.osgi.service.packageadmin.PackageAdmin;
  26. import static com.atlassian.upm.osgi.impl.VersionImpl.fromString;
  27. import static com.atlassian.upm.test.osgi.Mockery.arrayOf;
  28. import static com.atlassian.upm.test.osgi.Mockery.listOf;
  29. import static com.google.common.collect.ImmutableMap.of;
  30. import static java.lang.String.format;
  31. import static java.util.Arrays.asList;
  32. import static org.mockito.Mockito.when;
  33. import static org.osgi.framework.Constants.BUNDLE_NAME;
  34. import static org.osgi.framework.Constants.BUNDLE_SYMBOLICNAME;
  35. import static org.osgi.framework.Constants.BUNDLE_VERSION;
  36. import static org.osgi.framework.Constants.EXPORT_PACKAGE;
  37. import static org.osgi.framework.Constants.IMPORT_PACKAGE;
  38. import static org.osgi.framework.Constants.OBJECTCLASS;
  39. import static org.osgi.framework.Constants.SERVICE_DESCRIPTION;
  40. import static org.osgi.framework.Constants.SERVICE_ID;
  41. import static org.osgi.framework.Constants.SERVICE_PID;
  42. import static org.osgi.framework.Constants.SERVICE_RANKING;
  43. import static org.osgi.framework.Constants.SERVICE_VENDOR;
  44. import static org.osgi.framework.Version.emptyVersion;
  45. @RunWith(MockitoJUnitRunner.class)
  46. public class OsgiTestBase
  47. {
  48. @Mock BundleContext bundleContext;
  49. @Mock ServiceReference packageAdminServiceReference;
  50. @Mock PackageAdmin packageAdmin;
  51. // bundles
  52. private BundleAccessorImpl bundleAccessor;
  53. final org.osgi.framework.Bundle[] OSGI_BUNDLES = arrayOf(org.osgi.framework.Bundle.class, 3);
  54. final List<Bundle> UPM_BUNDLES = listOf(Bundle.class, 3);
  55. // services
  56. private ServiceAccessorImpl serviceAccessor;
  57. final ServiceReference[] OSGI_SERVICES = arrayOf(ServiceReference.class, 9);
  58. final List<Service> UPM_SERVICES = listOf(Service.class, 9);
  59. // packages
  60. private PackageAccessorImpl packageAccessor;
  61. final ExportedPackage[] OSGI_PACKAGES = arrayOf(ExportedPackage.class, 9);
  62. final List<Package> UPM_PACKAGES = listOf(Package.class, 9);
  63. private void populateBundles()
  64. {
  65. for (int id = 0; id < OSGI_BUNDLES.length; ++id)
  66. {
  67. org.osgi.framework.Bundle osgiBundle = OSGI_BUNDLES[id];
  68. Bundle upmBundle = UPM_BUNDLES.get(id);
  69. String name = format("test.bundle%d", id);
  70. ServiceReference[] osgiRegisteredServices = { OSGI_SERVICES[id], OSGI_SERVICES[id + 3], OSGI_SERVICES[id + 6] };
  71. Iterable<Service> upmRegisteredServices = asList(UPM_SERVICES.get(id), UPM_SERVICES.get(id + 3), UPM_SERVICES.get(id + 6));
  72. when(osgiBundle.getState()).thenReturn(org.osgi.framework.Bundle.ACTIVE);
  73. when(upmBundle.getState()).thenReturn(State.ACTIVE);
  74. when(osgiBundle.getHeaders()).thenReturn(new Hashtable<String, String>(of(
  75. BUNDLE_NAME, format("Test Bundle %d", id),
  76. BUNDLE_SYMBOLICNAME, name,
  77. BUNDLE_VERSION, "1.0",
  78. EXPORT_PACKAGE, buildOsgiExportPackage(id),
  79. IMPORT_PACKAGE, buildOsgiImportPackage())));
  80. when(upmBundle.getUnparsedHeaders()).thenReturn(of(
  81. BUNDLE_NAME, format("Test Bundle %d", id),
  82. BUNDLE_SYMBOLICNAME, name,
  83. BUNDLE_VERSION, "1.0"));
  84. Map<String, Iterable<HeaderClause>> parsedHeaders = of(
  85. EXPORT_PACKAGE, buildUpmExportPackage(id),
  86. IMPORT_PACKAGE, buildUpmImportPackage());
  87. when(upmBundle.getParsedHeaders()).thenReturn(parsedHeaders);
  88. when(osgiBundle.getBundleId()).thenReturn((long) id);
  89. when(upmBundle.getId()).thenReturn((long) id);
  90. when(osgiBundle.getLocation()).thenReturn(format("file:///bundle/%d", id));
  91. when(upmBundle.getLocation()).thenReturn(URI.create(format("file:///bundle/%d", id)));
  92. when(osgiBundle.getRegisteredServices()).thenReturn(osgiRegisteredServices);
  93. when(upmBundle.getRegisteredServices()).thenReturn(upmRegisteredServices);
  94. when(osgiBundle.getServicesInUse()).thenReturn(OSGI_SERVICES);
  95. when(upmBundle.getServicesInUse()).thenReturn(UPM_SERVICES);
  96. when(upmBundle.getName()).thenReturn(format("Test Bundle %d", id));
  97. when(osgiBundle.getSymbolicName()).thenReturn(name);
  98. when(upmBundle.getSymbolicName()).thenReturn(name);
  99. when(osgiBundle.getVersion()).thenReturn(emptyVersion);
  100. when(upmBundle.getVersion()).thenReturn(fromString("0"));
  101. when(bundleContext.getBundle((long) id)).thenReturn(osgiBundle);
  102. }
  103. }
  104. private String buildOsgiExportPackage(int bundleId)
  105. {
  106. return format("test.package%d;test.package%d;test.package%d;version=\"0\"", bundleId, bundleId + 3, bundleId + 6);
  107. }
  108. private Iterable<HeaderClause> buildUpmExportPackage(int bundleId)
  109. {
  110. List<HeaderClause> clauses = listOf(HeaderClause.class, 3);
  111. for (int i = 0; i < 3; ++i)
  112. {
  113. HeaderClause clause = clauses.get(i);
  114. int packageId = bundleId + i * 3;
  115. when(clause.getPath()).thenReturn(format("test.package%d", packageId));
  116. when(clause.getParameters()).thenReturn(of("version", "0"));
  117. when(clause.getReferencedPackage()).thenReturn(UPM_PACKAGES.get(packageId));
  118. }
  119. return clauses;
  120. }
  121. private static String buildOsgiImportPackage()
  122. {
  123. StringBuilder header = new StringBuilder();
  124. for (int packageId = 0; packageId < 9; ++packageId)
  125. {
  126. if (packageId != 0)
  127. {
  128. header.append(',');
  129. }
  130. header.append(format("test.package%d", packageId));
  131. }
  132. return header.toString();
  133. }
  134. private Iterable<HeaderClause> buildUpmImportPackage()
  135. {
  136. List<HeaderClause> clauses = listOf(HeaderClause.class, 9);
  137. for (int packageId = 0; packageId < 9; ++packageId)
  138. {
  139. HeaderClause clause = clauses.get(packageId);
  140. when(clause.getPath()).thenReturn(format("test.package%d", packageId));
  141. when(clause.getParameters()).thenReturn(Collections.<String, String> emptyMap());
  142. when(clause.getReferencedPackage()).thenReturn(UPM_PACKAGES.get(packageId));
  143. }
  144. return clauses;
  145. }
  146. private void populateServices() throws Exception
  147. {
  148. for (int serviceId = 0; serviceId < 9; ++serviceId)
  149. {
  150. ServiceReference osgiService = OSGI_SERVICES[serviceId];
  151. Service upmService = UPM_SERVICES.get(serviceId);
  152. String name = format("test.service%d", serviceId);
  153. int bundleId = serviceId % 3;
  154. when(osgiService.getProperty(OBJECTCLASS)).thenReturn(new String[] { name + ".class0", name + "class1" });
  155. when(upmService.getObjectClasses()).thenReturn(asList(name + ".class0", name + "class1"));
  156. when(osgiService.getProperty(SERVICE_DESCRIPTION)).thenReturn(name);
  157. when(upmService.getDescription()).thenReturn(name);
  158. when(osgiService.getProperty(SERVICE_ID)).thenReturn((long) serviceId);
  159. when(upmService.getId()).thenReturn((long) serviceId);
  160. when(osgiService.getProperty(SERVICE_PID)).thenReturn(new String[] { name + ".pid0", name + ".pid1" });
  161. when(upmService.getPid()).thenReturn(asList(name + ".pid0", name + ".pid1"));
  162. when(osgiService.getProperty(SERVICE_RANKING)).thenReturn(0);
  163. when(upmService.getRanking()).thenReturn(0);
  164. when(osgiService.getProperty(SERVICE_VENDOR)).thenReturn("vendor." + name);
  165. when(upmService.getVendor()).thenReturn("vendor." + name);
  166. when(osgiService.getBundle()).thenReturn(OSGI_BUNDLES[bundleId]);
  167. when(upmService.getBundle()).thenReturn(UPM_BUNDLES.get(bundleId));
  168. when(osgiService.getUsingBundles()).thenReturn(OSGI_BUNDLES);
  169. when(upmService.getUsingBundles()).thenReturn(UPM_BUNDLES);
  170. when(bundleContext.getAllServiceReferences(null, format("(%s=%d)", SERVICE_ID, serviceId)))
  171. .thenReturn(new ServiceReference[] {osgiService});
  172. }
  173. }
  174. private void populatePackages()
  175. {
  176. for (int packageId = 0; packageId < 9; ++packageId)
  177. {
  178. ExportedPackage osgiPackage = OSGI_PACKAGES[packageId];
  179. Package upmPackage = UPM_PACKAGES.get(packageId);
  180. String name = format("test.package%d", packageId);
  181. int exportingBundleId = packageId % 3;
  182. when(osgiPackage.getName()).thenReturn(name);
  183. when(upmPackage.getName()).thenReturn(name);
  184. when(osgiPackage.getExportingBundle()).thenReturn(OSGI_BUNDLES[exportingBundleId]);
  185. when(upmPackage.getExportingBundle()).thenReturn(UPM_BUNDLES.get(exportingBundleId));
  186. when(osgiPackage.getImportingBundles()).thenReturn(buildOsgiImportingBundles(exportingBundleId));
  187. when(upmPackage.getImportingBundles()).thenReturn(UPM_BUNDLES);
  188. when(osgiPackage.getVersion()).thenReturn(emptyVersion);
  189. when(upmPackage.getVersion()).thenReturn(fromString("0"));
  190. when(packageAdmin.getExportedPackages(name)).thenReturn(new ExportedPackage[] { osgiPackage });
  191. }
  192. }
  193. private org.osgi.framework.Bundle[] buildOsgiImportingBundles(int bundleId)
  194. {
  195. switch (bundleId)
  196. {
  197. case 0: return new org.osgi.framework.Bundle[] { OSGI_BUNDLES[1], OSGI_BUNDLES[2] };
  198. case 1: return new org.osgi.framework.Bundle[] { OSGI_BUNDLES[0], OSGI_BUNDLES[2] };
  199. case 2: return new org.osgi.framework.Bundle[] { OSGI_BUNDLES[0], OSGI_BUNDLES[1] };
  200. }
  201. return null;
  202. }
  203. @Before
  204. public void populateAccessors() throws Exception
  205. {
  206. when(bundleContext.getBundles()).thenReturn(OSGI_BUNDLES);
  207. when(bundleContext.getAllServiceReferences(null, null)).thenReturn(OSGI_SERVICES);
  208. when(bundleContext.getServiceReference(PackageAdmin.class.getName())).thenReturn(packageAdminServiceReference);
  209. when(bundleContext.getService(packageAdminServiceReference)).thenReturn(packageAdmin);
  210. when(packageAdmin.getExportedPackages((org.osgi.framework.Bundle) null)).thenReturn(OSGI_PACKAGES);
  211. packageAccessor = new PackageAccessorImpl();
  212. packageAccessor.setBundleContext(bundleContext);
  213. populatePackages();
  214. bundleAccessor = new BundleAccessorImpl(packageAccessor);
  215. bundleAccessor.setBundleContext(bundleContext);
  216. populateBundles();
  217. serviceAccessor = new ServiceAccessorImpl(packageAccessor);
  218. serviceAccessor.setBundleContext(bundleContext);
  219. populateServices();
  220. }
  221. protected BundleAccessor getBundleAccessor()
  222. {
  223. return bundleAccessor;
  224. }
  225. protected Bundle getExpectedBundle(int id)
  226. {
  227. return UPM_BUNDLES.get(id);
  228. }
  229. protected ServiceAccessor getServiceAccessor()
  230. {
  231. return serviceAccessor;
  232. }
  233. protected Service getExpectedService(int id)
  234. {
  235. return UPM_SERVICES.get(id);
  236. }
  237. protected PackageAccessor getPackageAccessor()
  238. {
  239. return packageAccessor;
  240. }
  241. protected Package getExpectedPackage(int id)
  242. {
  243. return UPM_PACKAGES.get(id);
  244. }
  245. }