use of org.osgi.framework.ServiceReference in project sling by apache.
the class AdapterManagerTest method testAdaptExtended.
@org.junit.Test
public void testAdaptExtended() throws Exception {
am.activate(this.createComponentContext());
TestSlingAdaptable2 data = new TestSlingAdaptable2();
assertNull("Expect no adapter", am.getAdapter(data, ITestAdapter.class));
final ServiceReference ref = createServiceReference();
am.bindAdapterFactory(ref);
Object adapter = am.getAdapter(data, ITestAdapter.class);
assertNotNull(adapter);
assertTrue(adapter instanceof ITestAdapter);
}
use of org.osgi.framework.ServiceReference in project sling by apache.
the class AdapterManagerTest method testBindBeforeActivate.
@org.junit.Test
public void testBindBeforeActivate() throws Exception {
final ServiceReference ref = createServiceReference();
am.bindAdapterFactory(ref);
// no cache and no factories yet
assertNotNull("AdapterFactoryDescriptors must not be null", am.getFactories());
assertTrue("AdapterFactoryDescriptors must be empty", am.getFactories().isEmpty());
assertTrue("AdapterFactory cache must be empty", am.getFactoryCache().isEmpty());
am.activate(this.createComponentContext());
// expect the factory, but cache is empty
assertNotNull("AdapterFactoryDescriptors must not be null", am.getFactories());
assertEquals("AdapterFactoryDescriptors must contain one entry", 1, am.getFactories().size());
assertTrue("AdapterFactory cache must be empty", am.getFactoryCache().isEmpty());
}
use of org.osgi.framework.ServiceReference in project sling by apache.
the class AdapterManagerTest method testAdaptMultipleAdapterFactories.
@org.junit.Test
public void testAdaptMultipleAdapterFactories() throws Exception {
final ServiceReference firstAdaptable = new ServiceReferenceImpl(1, new String[] { AdapterObject.class.getName() }, new String[] { ParentInterface.class.getName(), FirstImplementation.class.getName() });
final ServiceReference secondAdaptable = new ServiceReferenceImpl(2, new String[] { AdapterObject.class.getName() }, new String[] { ParentInterface.class.getName(), SecondImplementation.class.getName() });
am.activate(this.createMultipleAdaptersComponentContext(firstAdaptable, secondAdaptable));
AdapterObject first = new AdapterObject(Want.FIRST_IMPL);
assertNull("Expect no adapter", am.getAdapter(first, ParentInterface.class));
AdapterObject second = new AdapterObject(Want.SECOND_IMPL);
assertNull("Expect no adapter", am.getAdapter(second, ParentInterface.class));
am.bindAdapterFactory(firstAdaptable);
am.bindAdapterFactory(secondAdaptable);
Object adapter = am.getAdapter(first, ParentInterface.class);
assertNotNull("Did not get an adapter back for first implementation, service ranking 1", adapter);
assertTrue("Did not get the correct adaptable back for first implementation, service ranking 1, ", adapter instanceof FirstImplementation);
adapter = am.getAdapter(second, ParentInterface.class);
assertNotNull("Did not get an adapter back for second implementation, service ranking 2", adapter);
assertTrue("Did not get the correct adaptable back for second implementation, service ranking 2, ", adapter instanceof SecondImplementation);
adapter = am.getAdapter(first, FirstImplementation.class);
assertNotNull("Did not get an adapter back for first implementation, service ranking 1", adapter);
assertTrue("Did not get the correct adaptable back for first implementation, service ranking 1, ", adapter instanceof FirstImplementation);
adapter = am.getAdapter(second, SecondImplementation.class);
assertNotNull("Did not get an adapter back for second implementation, service ranking 2", adapter);
assertTrue("Did not get the correct adaptable back for second implementation, service ranking 2, ", adapter instanceof SecondImplementation);
}
use of org.osgi.framework.ServiceReference in project sling by apache.
the class AdapterManagerTest method testAdaptBase.
@org.junit.Test
public void testAdaptBase() throws Exception {
am.activate(this.createComponentContext());
TestSlingAdaptable data = new TestSlingAdaptable();
assertNull("Expect no adapter", am.getAdapter(data, ITestAdapter.class));
final ServiceReference ref = createServiceReference();
am.bindAdapterFactory(ref);
Object adapter = am.getAdapter(data, ITestAdapter.class);
assertNotNull(adapter);
assertTrue(adapter instanceof ITestAdapter);
}
use of org.osgi.framework.ServiceReference in project sling by apache.
the class AdapterManagerTest method testAdaptMultipleAdapterFactoriesServiceRankingReverse.
@org.junit.Test
public void testAdaptMultipleAdapterFactoriesServiceRankingReverse() throws Exception {
final ServiceReference firstAdaptable = new ServiceReferenceImpl(1, new String[] { AdapterObject.class.getName() }, new String[] { ParentInterface.class.getName(), FirstImplementation.class.getName() });
final ServiceReference secondAdaptable = new ServiceReferenceImpl(2, new String[] { AdapterObject.class.getName() }, new String[] { ParentInterface.class.getName(), SecondImplementation.class.getName() });
am.activate(this.createMultipleAdaptersComponentContext(firstAdaptable, secondAdaptable));
AdapterObject first = new AdapterObject(Want.INDIFFERENT);
assertNull("Expect no adapter", am.getAdapter(first, ParentInterface.class));
AdapterObject second = new AdapterObject(Want.INDIFFERENT);
assertNull("Expect no adapter", am.getAdapter(second, ParentInterface.class));
// bind these in reverse order from the non-reverse test
am.bindAdapterFactory(secondAdaptable);
am.bindAdapterFactory(firstAdaptable);
Object adapter = am.getAdapter(first, ParentInterface.class);
assertNotNull("Did not get an adapter back for first implementation (from ParentInterface), service ranking 1", adapter);
assertTrue("Did not get the correct adaptable back for first implementation, service ranking 1, ", adapter instanceof FirstImplementation);
adapter = am.getAdapter(first, FirstImplementation.class);
assertNotNull("Did not get an adapter back for first implementation, service ranking 1", adapter);
assertTrue("Did not get the correct adaptable back for first implementation, service ranking 1, ", adapter instanceof FirstImplementation);
adapter = am.getAdapter(second, SecondImplementation.class);
assertNotNull("Did not get an adapter back for second implementation, service ranking 2", adapter);
assertTrue("Did not get the correct adaptable back for second implementation, service ranking 2, ", adapter instanceof SecondImplementation);
}
Aggregations