Search in sources :

Example 1 with Setter

use of org.apache.felix.ipojo.runtime.core.test.services.Setter 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 2 with Setter

use of org.apache.felix.ipojo.runtime.core.test.services.Setter 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 3 with Setter

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

the class TestCombinationOfInterceptors method testCombination.

@Test
public void testCombination() {
    provider1 = provider(0);
    provider2 = provider(1);
    provider3 = provider(2);
    provider4 = provider(3);
    provider5 = provider(4);
    provider6 = provider(5);
    // Create the interceptor
    Properties configuration = new Properties();
    configuration.put("target", "(dependency.id=foo)");
    ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test.interceptors" + ".TrackerAndRankerInterceptor", 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(5);
    provider6.dispose();
    assertThat(check.getProps().get("grade")).isEqualTo(4);
    // Change range
    Setter setter = osgiHelper.getServiceObject(Setter.class);
    setter.set("LOW REVERSE");
    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 4 with Setter

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

the class TestHidingServices method testHidingAServiceAfterItsBinding.

/**
 * Same as previous but the interceptor arrives after the instance.
 */
@Test
public void testHidingAServiceAfterItsBinding() {
    // Create the interceptor
    Properties configuration = new Properties();
    configuration.put("target", "(dependency.id=foo)");
    // Create the FooConsumer
    ComponentInstance instance = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test" + ".components.FooConsumer");
    assertThat(instance.getState()).isEqualTo(ComponentInstance.VALID);
    ComponentInstance interceptor = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test" + ".interceptors.HidingTrackingInterceptor", configuration);
    // The provider is rejected => Invalid instance
    assertThat(instance.getState()).isEqualTo(ComponentInstance.INVALID);
    Setter setter = osgiHelper.getServiceObject(Setter.class, null);
    setter.set("toto");
    // The provider is now accepted
    assertThat(instance.getState()).isEqualTo(ComponentInstance.VALID);
    setter.set("hidden");
    // The provider is rejected => Invalid instance
    assertThat(instance.getState()).isEqualTo(ComponentInstance.INVALID);
    interceptor.dispose();
    // The provider is now accepted
    assertThat(instance.getState()).isEqualTo(ComponentInstance.VALID);
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Setter(org.apache.felix.ipojo.runtime.core.test.services.Setter) Properties(java.util.Properties) Test(org.junit.Test)

Example 5 with Setter

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

the class TestHidingServices method testHidingServiceAndReconfiguration.

@Test
public void testHidingServiceAndReconfiguration() {
    // Create the interceptor
    Properties configuration = new Properties();
    configuration.put("target", "(dependency.id=foo)");
    ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test.interceptors" + ".HidingTrackingInterceptor", configuration);
    // Create the FooConsumer
    ComponentInstance instance = ipojoHelper.createComponentInstance("org.apache.felix.ipojo.runtime.core.test" + ".components.FooConsumer");
    // The provider is rejected => Invalid instance
    assertThat(instance.getState()).isEqualTo(ComponentInstance.INVALID);
    Setter setter = osgiHelper.getServiceObject(Setter.class, null);
    setter.set("toto");
    // The provider is now accepted
    assertThat(instance.getState()).isEqualTo(ComponentInstance.VALID);
}
Also used : ComponentInstance(org.apache.felix.ipojo.ComponentInstance) Setter(org.apache.felix.ipojo.runtime.core.test.services.Setter) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

Properties (java.util.Properties)5 Setter (org.apache.felix.ipojo.runtime.core.test.services.Setter)5 Test (org.junit.Test)5 ComponentInstance (org.apache.felix.ipojo.ComponentInstance)3 CheckService (org.apache.felix.ipojo.runtime.core.test.services.CheckService)3 Map (java.util.Map)1 ServiceReference (org.osgi.framework.ServiceReference)1