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();
}
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);
}
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);
}
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);
}
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);
}
Aggregations