Search in sources :

Example 46 with ServiceReference

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);
}
Also used : ServiceReference(org.osgi.framework.ServiceReference)

Example 47 with ServiceReference

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());
}
Also used : ServiceReference(org.osgi.framework.ServiceReference)

Example 48 with ServiceReference

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);
}
Also used : ServiceReference(org.osgi.framework.ServiceReference)

Example 49 with ServiceReference

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);
}
Also used : ServiceReference(org.osgi.framework.ServiceReference)

Example 50 with ServiceReference

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);
}
Also used : ServiceReference(org.osgi.framework.ServiceReference)

Aggregations

ServiceReference (org.osgi.framework.ServiceReference)1690 Test (org.junit.Test)926 Properties (java.util.Properties)396 Architecture (org.apache.felix.ipojo.architecture.Architecture)263 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)233 BundleContext (org.osgi.framework.BundleContext)231 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)227 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)215 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)183 ArrayList (java.util.ArrayList)167 Bundle (org.osgi.framework.Bundle)144 FooService (org.apache.felix.ipojo.runtime.core.services.FooService)141 Hashtable (java.util.Hashtable)124 IOException (java.io.IOException)107 CheckService (org.apache.felix.ipojo.runtime.core.services.CheckService)92 Dictionary (java.util.Dictionary)82 Configuration (org.osgi.service.cm.Configuration)74 CheckService (org.apache.felix.ipojo.handler.temporal.services.CheckService)70 FooService (org.apache.felix.ipojo.handler.temporal.services.FooService)70 CheckService (org.apache.felix.ipojo.handler.transaction.services.CheckService)65