use of org.osgi.service.blueprint.container.BlueprintContainer in project aries by apache.
the class TestReferences method testReferencesCallableInDestroy.
@Test
public void testReferencesCallableInDestroy() throws Exception {
bundleContext.registerService(Runnable.class.getName(), new Thread(), null);
BlueprintContainer blueprintContainer = Helper.getBlueprintContainerForBundle(context(), "org.apache.aries.blueprint.sample");
assertNotNull(blueprintContainer);
DestroyTest dt = (DestroyTest) blueprintContainer.getComponentInstance("destroyCallingReference");
Bundle b = findBundle("org.apache.aries.blueprint.sample");
assertNotNull(b);
b.stop();
assertTrue("The destroy method was called", dt.waitForDestruction(1000));
Exception e = dt.getDestroyFailure();
if (e != null)
throw e;
}
use of org.osgi.service.blueprint.container.BlueprintContainer in project aries by apache.
the class TestConfigAdmin method getComponent.
private <T> T getComponent(String componentId) {
BlueprintContainer blueprintContainer = Helper.getBlueprintContainerForBundle(context(), "org.apache.aries.blueprint.sample");
assertNotNull(blueprintContainer);
@SuppressWarnings("unchecked") T component = (T) blueprintContainer.getComponentInstance(componentId);
assertNotNull(component);
return component;
}
use of org.osgi.service.blueprint.container.BlueprintContainer in project aries by apache.
the class TestRegistrationListener method testWithAutoExportEnabled.
@Test
public void testWithAutoExportEnabled() throws Exception {
BlueprintContainer blueprintContainer = Helper.getBlueprintContainerForBundle(context(), "org.apache.aries.blueprint.sample");
assertNotNull(blueprintContainer);
Foo foo = context().getService(Foo.class, "(" + BlueprintConstants.COMPONENT_NAME_PROPERTY + "=foo)");
assertEquals(5, foo.getA());
FooRegistrationListener listener = (FooRegistrationListener) blueprintContainer.getComponentInstance("fooRegistrationListener");
// If registration listener works fine, the registration method should
// have already been called and properties that were passed to this
// method should have been not null
Map<?, ?> props = listener.getProperties();
assertNotNull(props);
assertTrue(props.containsKey(BlueprintConstants.COMPONENT_NAME_PROPERTY));
assertEquals("foo", props.get(BlueprintConstants.COMPONENT_NAME_PROPERTY));
assertTrue(props.containsKey("key"));
assertEquals("value", props.get("key"));
}
use of org.osgi.service.blueprint.container.BlueprintContainer in project aries by apache.
the class FragmentTestBundleBlueprintAttribute method testFragmentProvidesBlueprintFile.
@Test
public void testFragmentProvidesBlueprintFile() throws Exception {
Runnable r = context().getService(Runnable.class);
Assert.assertNotNull("Could not find blueprint registered service", r);
BlueprintContainer bc = Helper.getBlueprintContainerForBundle(context(), "org.apache.aries.test.host");
Assert.assertNotNull("Could not find blueprint container for bundle", bc);
}
use of org.osgi.service.blueprint.container.BlueprintContainer in project aries by apache.
the class ASMMultiBundleTest method multiBundleTest.
// TODO This test seems to fail on some runs. Need to stabilize and reenable
@Test
@Ignore
public void multiBundleTest() throws Exception {
// bundlea provides the ns handlers, bean processors, interceptors etc for this test.
Bundle bundlea = context().getBundleByName("org.apache.aries.blueprint.testbundlea");
assertNotNull(bundlea);
bundlea.start();
// bundleb makes use of the extensions provided by bundlea
Bundle bundleb = context().getBundleByName("org.apache.aries.blueprint.testbundleb");
assertNotNull(bundleb);
bundleb.start();
// bundleb's container will hold the beans we need to query to check the function
// provided by bundlea functioned as expected
BlueprintContainer beanContainer = Helper.getBlueprintContainerForBundle(context(), "org.apache.aries.blueprint.testbundleb");
assertNotNull(beanContainer);
// TestBeanA should have the values below, no interference should be present from other sources.
Object obj1 = beanContainer.getComponentInstance("TestBeanA");
assertTrue(obj1 instanceof TestBean);
TestBean testBeanA = (TestBean) obj1;
org.junit.Assert.assertEquals("RED", testBeanA.getRed());
org.junit.Assert.assertEquals("GREEN", testBeanA.getGreen());
org.junit.Assert.assertEquals("BLUE", testBeanA.getBlue());
// TestBeanB tests that a custom ns handler is able to inject custom components to the blueprint,
// and modify existing components, and use injected components as modifications.
Object obj2 = beanContainer.getComponentInstance("TestBeanB");
assertTrue(obj2 instanceof TestBean);
TestBean testBeanB = (TestBean) obj2;
// value should be set in via the added passthroughmetadata via the nshandler.
org.junit.Assert.assertEquals("ONE_VALUE", testBeanB.getRed());
org.junit.Assert.assertEquals("GREEN", testBeanB.getGreen());
org.junit.Assert.assertEquals("BLUE", testBeanB.getBlue());
// TestBeanC tests that custom ns handlers can add interceptors to beans.
Object obj3 = beanContainer.getComponentInstance("TestBeanC");
assertTrue(obj3 instanceof TestBean);
TestBean testBeanC = (TestBean) obj3;
// handlers are in bundlea, with its own container.
BlueprintContainer handlerContainer = Helper.getBlueprintContainerForBundle(context(), "org.apache.aries.blueprint.testbundlea");
assertNotNull(handlerContainer);
Object ns1 = handlerContainer.getComponentInstance("NSHandlerOne");
assertTrue(ns1 instanceof NSHandlerOne);
Object ns2 = handlerContainer.getComponentInstance("NSHandlerTwo");
assertTrue(ns2 instanceof NSHandlerTwo);
NSHandlerTwo nstwo = (NSHandlerTwo) ns2;
// now we have a handle to the nshandler2, we can query what it 'saw', and ensure
// that the interceptors are functioning as expected.
List<String> log = nstwo.getLog();
// TestBeanC has the interceptor configured, and is injected to OtherBeanA & OtherBeanB
// which then uses the injected bean during their init method call, to invoke a method
checkInterceptorLog(new String[] { "PRECALL:TestBeanC:methodToInvoke:[RED]:", "POSTCALL[true]:TestBeanC:methodToInvoke:[RED]:", "PRECALL:TestBeanC:methodToInvoke:[BLUE]:", "POSTCALL[false]:TestBeanC:methodToInvoke:[BLUE]:" }, log);
// invoking GREEN is hardwired to cause an exception response, we do this
// from here to ensure the exception occurs and is visible as expected
RuntimeException re = null;
try {
testBeanC.methodToInvoke("GREEN");
} catch (RuntimeException e) {
re = e;
}
assertNotNull("invocation of Green did not cause an exception as expected", re);
// Exception responses should be intercepted too, test for the POSTCALLWITHEXCEPTION log entry.
log = nstwo.getLog();
checkInterceptorLog(new String[] { "PRECALL:TestBeanC:methodToInvoke:[RED]:", "POSTCALL[true]:TestBeanC:methodToInvoke:[RED]:", "PRECALL:TestBeanC:methodToInvoke:[BLUE]:", "POSTCALL[false]:TestBeanC:methodToInvoke:[BLUE]:", "PRECALL:TestBeanC:methodToInvoke:[GREEN]:", "POSTCALLEXCEPTION[java.lang.RuntimeException: MATCHED ON GREEN (GREEN)]:TestBeanC:methodToInvoke:[GREEN]:" }, log);
// ProcessedBean is a test to ensure that BeanProcessors are called..
// The test has the BeanProcessor look for ProcessableBeans, and log itself with them
Object obj4 = beanContainer.getComponentInstance("ProcessedBean");
assertTrue(obj4 instanceof ProcessableBean);
ProcessableBean pb = (ProcessableBean) obj4;
// Note, the BeanProcessor exists in the same container as the beans it processes!!
Object bp = beanContainer.getComponentInstance("http://ns.handler.three/BeanProcessor");
assertNotNull(bp);
assertTrue(bp instanceof BeanProcessor);
assertEquals(1, pb.getProcessedBy().size());
// check we were invoked..
assertEquals(pb.getProcessedBy().get(0), bp);
// check invocation for each phase.
assertEquals(pb.getProcessedBy(Phase.BEFORE_INIT).get(0), bp);
assertEquals(pb.getProcessedBy(Phase.AFTER_INIT).get(0), bp);
// destroy invocation will only occur at tear down.. TODO, how to test after teardown.
// assertEquals(pb.getProcessedBy(Phase.BEFORE_DESTROY).get(0),bp);
// assertEquals(pb.getProcessedBy(Phase.AFTER_DESTROY).get(0),bp);
Object objOther = beanContainer.getComponentInstance("PlaceHolderTestBean");
assertTrue(objOther instanceof OtherBean);
assertEquals("test1value", ((OtherBean) objOther).getTestValue());
}
Aggregations