use of org.osgi.framework.ServiceRegistration in project aries by apache.
the class ProviderBundleTrackerCustomizerGenericCapabilityTest method testMultipleServiceInstancesAndTypes.
@Test
public void testMultipleServiceInstancesAndTypes() throws Exception {
Bundle mediatorBundle = EasyMock.createMock(Bundle.class);
EasyMock.expect(mediatorBundle.getBundleId()).andReturn(42l).anyTimes();
EasyMock.replay(mediatorBundle);
BaseActivator activator = new BaseActivator() {
@Override
public void start(BundleContext context) throws Exception {
}
};
ProviderBundleTrackerCustomizer customizer = new ProviderBundleTrackerCustomizer(activator, mediatorBundle);
BundleContext implBC = mockSPIBundleContext4();
Dictionary<String, String> headers = new Hashtable<String, String>();
headers.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.PROVIDER_REQUIREMENT);
headers.put(SpiFlyConstants.PROVIDE_CAPABILITY, SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "; " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "=org.apache.aries.mytest.MySPI," + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "; " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "=org.apache.aries.mytest.MySPI2");
Bundle implBundle = mockSPIBundle4(implBC, headers);
List<ServiceRegistration> registrations = customizer.addingBundle(implBundle, null);
assertEquals(3, registrations.size());
boolean foundA = false, foundB = false, foundC = false;
for (ServiceRegistration sreg : registrations) {
ServiceReference sref = sreg.getReference();
String objectClassName = ((String[]) sref.getProperty(Constants.OBJECTCLASS))[0];
String serviceImplClassName = (String) sref.getProperty(SpiFlyConstants.PROVIDER_IMPLCLASS_PROPERTY);
if (MySPIImpl4a.class.getName().equals(serviceImplClassName)) {
assertEquals("org.apache.aries.mytest.MySPI", objectClassName);
MySPI svc = (MySPI) implBC.getService(sreg.getReference());
assertEquals("impl4a", svc.someMethod(""));
foundA = true;
} else if (MySPIImpl4b.class.getName().equals(serviceImplClassName)) {
assertEquals("org.apache.aries.mytest.MySPI2", objectClassName);
MySPI2 svc = (MySPI2) implBC.getService(sreg.getReference());
assertEquals("impl4b", svc.someMethod(""));
foundB = true;
} else if (MySPIImpl4c.class.getName().equals(serviceImplClassName)) {
assertEquals("org.apache.aries.mytest.MySPI2", objectClassName);
MySPI2 svc = (MySPI2) implBC.getService(sreg.getReference());
assertEquals("impl4c", svc.someMethod(""));
foundC = true;
}
}
assertTrue(foundA);
assertTrue(foundB);
assertTrue(foundC);
}
use of org.osgi.framework.ServiceRegistration in project aries by apache.
the class ProviderBundleTrackerCustomizerGenericCapabilityTest method testNonServiceRegistryBundle.
@Test
public void testNonServiceRegistryBundle() throws Exception {
Bundle mediatorBundle = EasyMock.createMock(Bundle.class);
EasyMock.expect(mediatorBundle.getBundleId()).andReturn(42l).anyTimes();
EasyMock.replay(mediatorBundle);
BaseActivator activator = new BaseActivator() {
@Override
public void start(BundleContext context) throws Exception {
}
};
ProviderBundleTrackerCustomizer customizer = new ProviderBundleTrackerCustomizer(activator, mediatorBundle);
ServiceRegistration sreg = EasyMock.createMock(ServiceRegistration.class);
EasyMock.replay(sreg);
BundleContext implBC = mockSPIBundleContext(sreg);
Dictionary<String, String> headers = new Hashtable<String, String>();
headers.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.PROVIDER_REQUIREMENT);
headers.put(SpiFlyConstants.PROVIDE_CAPABILITY, SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "; " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "=org.apache.aries.mytest.MySPI; approval=yeah;" + SpiFlyConstants.REGISTER_DIRECTIVE + ":=\"\"");
Bundle implBundle = mockSPIBundle(implBC, headers);
List<ServiceRegistration> registrations = customizer.addingBundle(implBundle, null);
assertEquals(0, registrations.size());
Collection<Bundle> bundles = activator.findProviderBundles("org.apache.aries.mytest.MySPI");
assertEquals(1, bundles.size());
assertSame(implBundle, bundles.iterator().next());
Map<String, Object> attrs = activator.getCustomBundleAttributes("org.apache.aries.mytest.MySPI", implBundle);
assertEquals(1, attrs.size());
assertEquals("yeah", attrs.get("approval"));
}
use of org.osgi.framework.ServiceRegistration in project aries by apache.
the class ProviderBundleTrackerCustomizerGenericCapabilityTest method testServiceSubsetSelectionAndRegistrationProperties.
@Test
public void testServiceSubsetSelectionAndRegistrationProperties() throws Exception {
Bundle mediatorBundle = EasyMock.createMock(Bundle.class);
EasyMock.expect(mediatorBundle.getBundleId()).andReturn(42l).anyTimes();
EasyMock.replay(mediatorBundle);
BaseActivator activator = new BaseActivator() {
@Override
public void start(BundleContext context) throws Exception {
}
};
ProviderBundleTrackerCustomizer customizer = new ProviderBundleTrackerCustomizer(activator, mediatorBundle);
BundleContext implBC = mockSPIBundleContext4();
Dictionary<String, String> headers = new Hashtable<String, String>();
headers.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.PROVIDER_REQUIREMENT);
headers.put(SpiFlyConstants.PROVIDE_CAPABILITY, SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "; " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "=org.apache.aries.mytest.MySPI2; approval=yeah; " + SpiFlyConstants.REGISTER_DIRECTIVE + ":=\"org.apache.aries.spifly.impl4.MySPIImpl4b\"");
Bundle implBundle = mockSPIBundle4(implBC, headers);
List<ServiceRegistration> registrations = customizer.addingBundle(implBundle, null);
assertEquals(1, registrations.size());
String[] objectClassProp = (String[]) registrations.iterator().next().getReference().getProperty(Constants.OBJECTCLASS);
assertEquals(1, objectClassProp.length);
assertEquals("org.apache.aries.mytest.MySPI2", objectClassProp[0]);
assertNotNull(registrations.iterator().next().getReference().getProperty(SpiFlyConstants.SERVICELOADER_MEDIATOR_PROPERTY));
assertEquals("yeah", registrations.iterator().next().getReference().getProperty("approval"));
}
use of org.osgi.framework.ServiceRegistration in project aries by apache.
the class ProviderBundleTrackerCustomizerGenericCapabilityTest method testNoServiceRegistration.
@Test
public void testNoServiceRegistration() throws Exception {
Bundle mediatorBundle = EasyMock.createMock(Bundle.class);
EasyMock.expect(mediatorBundle.getBundleId()).andReturn(42l).anyTimes();
EasyMock.replay(mediatorBundle);
BaseActivator activator = new BaseActivator() {
@Override
public void start(BundleContext context) throws Exception {
}
};
ProviderBundleTrackerCustomizer customizer = new ProviderBundleTrackerCustomizer(activator, mediatorBundle);
ServiceRegistration sreg = EasyMock.createMock(ServiceRegistration.class);
EasyMock.replay(sreg);
BundleContext implBC = mockSPIBundleContext(sreg);
Bundle implBundle = mockSPIBundle(implBC, SpiFlyConstants.PROVIDER_REQUIREMENT);
List<ServiceRegistration> registrations = customizer.addingBundle(implBundle, null);
assertEquals(0, registrations.size());
Collection<Bundle> bundles = activator.findProviderBundles("org.apache.aries.mytest.MySPI");
assertEquals(1, bundles.size());
assertSame(implBundle, bundles.iterator().next());
}
use of org.osgi.framework.ServiceRegistration in project aries by apache.
the class ProviderBundleTrackerCustomizerGenericCapabilityTest method testProvidedSPIDirective.
@Test
public void testProvidedSPIDirective() throws Exception {
Bundle mediatorBundle = EasyMock.createMock(Bundle.class);
EasyMock.expect(mediatorBundle.getBundleId()).andReturn(42l).anyTimes();
EasyMock.replay(mediatorBundle);
BaseActivator activator = new BaseActivator() {
@Override
public void start(BundleContext context) throws Exception {
}
};
ProviderBundleTrackerCustomizer customizer = new ProviderBundleTrackerCustomizer(activator, mediatorBundle);
BundleContext implBC = mockSPIBundleContext4();
Dictionary<String, String> headers = new Hashtable<String, String>();
headers.put(SpiFlyConstants.REQUIRE_CAPABILITY, SpiFlyConstants.PROVIDER_REQUIREMENT);
headers.put(SpiFlyConstants.PROVIDE_CAPABILITY, SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "; " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "=org.apache.aries.mytest.MySPI; approval=yeah, " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "; " + SpiFlyConstants.SERVICELOADER_CAPABILITY_NAMESPACE + "=org.apache.aries.mytest.MySPI2");
Bundle implBundle = mockSPIBundle4(implBC, headers);
List<ServiceRegistration> registrations = customizer.addingBundle(implBundle, null);
assertEquals("Expected 3 registrations, one for MySPI and 2 for MySPI2", 3, registrations.size());
Set<String> expectedObjectClasses = new HashSet<String>(Arrays.asList("org.apache.aries.mytest.MySPI", "org.apache.aries.mytest.MySPI2"));
Set<String> actualObjectClasses = new HashSet<String>();
boolean foundMySPI = false;
boolean foundMySPI2 = false;
for (ServiceRegistration sr : registrations) {
List<String> objectClasses = Arrays.asList((String[]) sr.getReference().getProperty(Constants.OBJECTCLASS));
actualObjectClasses.addAll(objectClasses);
assertNotNull(sr.getReference().getProperty(SpiFlyConstants.SERVICELOADER_MEDIATOR_PROPERTY));
if (objectClasses.contains("org.apache.aries.mytest.MySPI")) {
assertEquals("yeah", sr.getReference().getProperty("approval"));
foundMySPI = true;
} else if (objectClasses.contains("org.apache.aries.mytest.MySPI2")) {
assertNull(sr.getReference().getProperty("approval"));
foundMySPI2 = true;
}
}
assertTrue(foundMySPI);
assertTrue(foundMySPI2);
assertEquals(expectedObjectClasses, actualObjectClasses);
}
Aggregations