Search in sources :

Example 91 with CheckService

use of org.apache.felix.ipojo.runtime.core.test.services.CheckService in project felix by apache.

the class TestInterceptorsOnSeveralDependencies method testBeingBoundToSeveralDependencies.

@Test
public void testBeingBoundToSeveralDependencies() {
    // Create the interceptor
    Properties configuration = new Properties();
    configuration.put("target", "(dependency.id=foo)");
    ComponentInstance interceptor = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test" + ".interceptors.PropertyTrackingInterceptor", configuration);
    // Create the FooConsumer
    ComponentInstance instance1 = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test" + ".components.FooConsumer");
    ComponentInstance instance2 = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test" + ".components.FooConsumer");
    assertThat(instance1.getState()).isEqualTo(ComponentInstance.VALID);
    assertThat(instance2.getState()).isEqualTo(ComponentInstance.VALID);
    final ServiceReference ref1 = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance1.getInstanceName());
    final ServiceReference ref2 = ipojoHelper.getServiceReferenceByName(CheckService.class.getName(), instance2.getInstanceName());
    CheckService cs1 = (CheckService) osgiHelper.getRawServiceObject(ref1);
    CheckService cs2 = (CheckService) osgiHelper.getRawServiceObject(ref2);
    @SuppressWarnings("unchecked") Map<String, ?> props1 = (Map<String, ?>) cs1.getProps().get("props");
    @SuppressWarnings("unchecked") Map<String, ?> props2 = (Map<String, ?>) cs2.getProps().get("props");
    assertThat(props1.get("location")).isEqualTo("kitchen");
    assertThat(props2.get("location")).isEqualTo("kitchen");
    Setter setter = osgiHelper.getServiceObject(Setter.class, null);
    setter.set("bedroom");
    props1 = (Map<String, ?>) cs1.getProps().get("props");
    props2 = (Map<String, ?>) cs2.getProps().get("props");
    assertThat(props1.get("location")).isEqualTo("bedroom");
    assertThat(props2.get("location")).isEqualTo("bedroom");
    interceptor.dispose();
    props1 = (Map<String, ?>) cs1.getProps().get("props");
    props2 = (Map<String, ?>) cs2.getProps().get("props");
    assertThat(props1.get("location")).isNull();
    assertThat(props2.get("location")).isNull();
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Setter(org.apache.felix.ipojo.runtime.core.test.services.Setter) Properties(java.util.Properties) CheckService(org.apache.felix.ipojo.runtime.core.test.services.CheckService) Map(java.util.Map) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 92 with CheckService

use of org.apache.felix.ipojo.runtime.core.test.services.CheckService in project felix by apache.

the class TestRankingServices method testRankingWhenInterceptorIsComingAfterTheBattle.

@Test
public void testRankingWhenInterceptorIsComingAfterTheBattle() {
    // Provider with grade 0 first
    provider1 = provider(0);
    provider2 = provider(1);
    // Create the FooConsumer
    ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test.components.FooConsumer");
    osgiHelper.waitForService(CheckService.class.getName(), null, 1000, true);
    CheckService check = osgiHelper.getServiceObject(CheckService.class);
    assertThat(check.getProps().get("grade")).isEqualTo(0);
    // Create the interceptor
    Properties configuration = new Properties();
    configuration.put("target", "(dependency.id=foo)");
    ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test.interceptors" + ".FilterRankingInterceptor", configuration);
    // Check we are using provider 2
    assertThat(check.getProps().get("grade")).isEqualTo(1);
    provider2.dispose();
    assertThat(check.getProps().get("grade")).isEqualTo(0);
}
Also used : CheckService(org.apache.felix.ipojo.runtime.core.test.services.CheckService) Properties(java.util.Properties) Test(org.junit.Test)

Example 93 with CheckService

use of org.apache.felix.ipojo.runtime.core.test.services.CheckService in project felix by apache.

the class TestRankingServices method testRankingChanges.

@Test
public void testRankingChanges() {
    // Provider with grade 0 first
    provider1 = provider(0);
    provider2 = provider(1);
    // Create the interceptor
    Properties configuration = new Properties();
    configuration.put("target", "(dependency.id=foo)");
    ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test.interceptors" + ".FilterRankingInterceptor", configuration);
    // Create the FooConsumer
    ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test.components.FooConsumer");
    // Check we are using provider 2
    osgiHelper.waitForService(CheckService.class.getName(), null, 1000, true);
    CheckService check = osgiHelper.getServiceObject(CheckService.class);
    assertThat(check.getProps().get("grade")).isEqualTo(1);
    Setter setter = osgiHelper.getServiceObject(Setter.class);
    setter.set("true");
    assertThat(check.getProps().get("grade")).isEqualTo(0);
}
Also used : Setter(org.apache.felix.ipojo.runtime.core.test.services.Setter) Properties(java.util.Properties) CheckService(org.apache.felix.ipojo.runtime.core.test.services.CheckService) Test(org.junit.Test)

Example 94 with CheckService

use of org.apache.felix.ipojo.runtime.core.test.services.CheckService in project felix by apache.

the class TestRankingServices method testRanking.

@Test
public void testRanking() {
    // Provider with grade 0 first
    provider1 = provider(0);
    provider2 = provider(1);
    // Create the interceptor
    Properties configuration = new Properties();
    configuration.put("target", "(dependency.id=foo)");
    ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test.interceptors" + ".FilterRankingInterceptor", configuration);
    // Create the FooConsumer
    ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test.components.FooConsumer");
    // Check we are using provider 2
    osgiHelper.waitForService(CheckService.class.getName(), null, 1000, true);
    CheckService check = osgiHelper.getServiceObject(CheckService.class);
    assertThat(check.getProps().get("grade")).isEqualTo(1);
    provider2.dispose();
    assertThat(check.getProps().get("grade")).isEqualTo(0);
}
Also used : Properties(java.util.Properties) CheckService(org.apache.felix.ipojo.runtime.core.test.services.CheckService) Test(org.junit.Test)

Example 95 with CheckService

use of org.apache.felix.ipojo.runtime.core.test.services.CheckService in project felix by apache.

the class TestTransformingServices method testTransformationMakingFilterMatch.

/**
 * The interceptor makes the instance valid.
 */
@Test
public void testTransformationMakingFilterMatch() {
    // Create the FooConsumer
    Properties configuration = new Properties();
    Properties filters = new Properties();
    filters.put("foo", "(location=kitchen)");
    configuration.put("requires.filters", filters);
    ComponentInstance consumer = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test" + ".components.FooConsumer", configuration);
    // Invalid instance
    assertThat(consumer.getInstanceDescription().getState()).isEqualTo(ComponentInstance.INVALID);
    // Create the interceptor
    Properties config = new Properties();
    config.put("target", "(dependency.id=foo)");
    ComponentInstance interceptor = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test" + ".interceptors.AddLocationTrackingInterceptor", config);
    assertThat(consumer.getInstanceDescription().getState()).isEqualTo(ComponentInstance.VALID);
    CheckService check = osgiHelper.getServiceObject(CheckService.class);
    assertThat(check.check());
    Map<String, ?> props = (Map<String, ?>) check.getProps().get("props");
    assertThat(props.get("location")).isEqualTo("kitchen");
    assertThat(props.get("hidden")).isNull();
    // Removing the interceptor should revert to the base set.
    interceptor.dispose();
    System.out.println(consumer.getInstanceDescription().getDescription());
    assertThat(consumer.getInstanceDescription().getState()).isEqualTo(ComponentInstance.INVALID);
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Properties(java.util.Properties) CheckService(org.apache.felix.ipojo.runtime.core.test.services.CheckService) Map(java.util.Map) Test(org.junit.Test)

Aggregations

CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)245 Test (org.junit.Test)243 ServiceReference (org.osgi.framework.ServiceReference)233 Architecture (org.apache.felix.ipojo.architecture.Architecture)218 InstanceDescription (org.apache.felix.ipojo.architecture.InstanceDescription)213 Properties (java.util.Properties)208 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)20 FooService (org.apache.felix.ipojo.runtime.core.test.services.FooService)20 Map (java.util.Map)5 PrimitiveInstanceDescription (org.apache.felix.ipojo.PrimitiveInstanceDescription)5 DependencyHandlerDescription (org.apache.felix.ipojo.handlers.dependency.DependencyHandlerDescription)5 ProvidedServiceHandlerDescription (org.apache.felix.ipojo.handlers.providedservice.ProvidedServiceHandlerDescription)5 Setter (org.apache.felix.ipojo.runtime.core.test.services.Setter)3 Dictionary (java.util.Dictionary)1 Hashtable (java.util.Hashtable)1 DependencyDescription (org.apache.felix.ipojo.handlers.dependency.DependencyDescription)1