/** * Create an iPojo instance for a given class and a given instance name using the given * properties. * * @param instanceClass class of the instance to be instantiated * @param instanceName instance name * @param props properties to be used to instantiate the object * @returns {@link InstanceHolder} object holding the instance just created and the IPOJO * description object of class {@link ComponentInstance} */ public <I, T> InstanceHolder<I> create( final Class<I> interfaceClass, final String instanceClassName, final String instanceName, final Dictionary<String, Object> props) { if (instanceName != null) { props.put("instance.name", instanceName); } // Create, register and start an instance of class instanceClass ComponentInstance ci = this.ipojoHelper.createComponentInstance(instanceClassName, props); // get the just created instance object from the OSGI registry and return it final I instance = this.osgiHelper.getServiceObject( interfaceClass, "(instance.name=" + ci.getInstanceName() + ")"); if (instance != null && interfaceClass.isInstance(instance)) { LOG.debug( "Created instance of class '{}' with instance name '{}'successfully.", instanceClassName, ci.getInstanceName()); } else { LOG.warn("Creation of instance of class '{}' failed.", instanceClassName); } return new InstanceHolder<I>(instance, ci); }
public void setUp() { tataFactory = (ComponentFactory) Utils.getFactoryByName(getContext(), "tata"); totoFactory = (ComponentFactory) Utils.getFactoryByName(getContext(), "toto"); tataFactory2 = (ComponentFactory) Utils.getFactoryByName(getContext(), "comp-6"); tataFactory2.stop(); tataFactory.stop(); Properties props = new Properties(); props.put("instance.name", "toto provider"); try { totoProv = totoFactory.createComponentInstance(props); } catch (Exception e) { e.printStackTrace(); } Properties props3 = new Properties(); props3.put("instance.name", "toto provider 2"); try { totoProv2 = totoFactory.createComponentInstance(props3); } catch (Exception e) { e.printStackTrace(); } totoProv.stop(); totoProv2.stop(); Factory factory = Utils.getFactoryByName(getContext(), "comp-2"); Properties props2 = new Properties(); props2.put("instance.name", "ff"); try { under = factory.createComponentInstance(props2); } catch (Exception e) { e.printStackTrace(); } }
@Test public void testItfsExposition() { ServiceReference refFoo = ipojoHelper.getServiceReferenceByName( FooService.class.getName(), fooBarProvider.getInstanceName()); assertNotNull( "Check the availability of the FS from " + fooBarProvider.getInstanceName(), refFoo); ServiceReference refBar = ipojoHelper.getServiceReferenceByName( BarService.class.getName(), fooBarProvider.getInstanceName()); assertNotNull( "Check the availability of the BS from " + fooBarProvider.getInstanceName(), refBar); assertSame("Check service reference equality", refFoo, refBar); FooService fs = (FooService) osgiHelper.getServiceObject(refFoo); assertTrue("Check fs invocation", fs.foo()); fs = null; BarService bs = (BarService) osgiHelper.getServiceObject(refBar); assertTrue("Check bs invocation", bs.bar()); bs = null; fooBarProvider.stop(); refFoo = ipojoHelper.getServiceReferenceByName( FooService.class.getName(), fooBarProvider.getInstanceName()); refBar = ipojoHelper.getServiceReferenceByName( BarService.class.getName(), fooBarProvider.getInstanceName()); assertNull("Check the absence of the FS from " + fooBarProvider.getInstanceName(), refFoo); assertNull("Check the absence of the BS from " + fooBarProvider.getInstanceName(), refBar); }
public void tearDown() { tataFactory.start(); totoProv.dispose(); totoProv = null; totoProv2.dispose(); totoProv2 = null; tataFactory2.start(); // Reset counters TotoProvider.toto = 0; TotoProvider.toto_2 = 0; TotoProvider.toto_3 = 0; TotoProvider.toto_4 = 0; TotoProvider.toto1 = 0; }
@Test public void testItfExposition() { ServiceReference ref = ipojoHelper.getServiceReferenceByName( FooService.class.getName(), fooProviderItf.getInstanceName()); assertNotNull("Check the availability of the FS from " + fooProviderItf.getInstanceName(), ref); FooService fs = (FooService) osgiHelper.getServiceObject(ref); assertTrue("Check fs invocation", fs.foo()); fs = null; fooProviderItf.stop(); ref = ipojoHelper.getServiceReferenceByName( FooService.class.getName(), fooProviderItf.getInstanceName()); assertNull("Check the absence of the FS from " + fooProviderItf.getInstanceName(), ref); }
/** Check if a pojo can correctly be cast in POJO. Check the getComponentInstance method. */ @Test public void testGetComponentInstance() { String factName = "Manipulation-FooProviderType-1"; String compName = "FooProvider-1"; ServiceReference ref = null; // Get the factory to create a component instance Factory fact = ipojoHelper.getFactory(factName); assertNotNull("Cannot find the factory FooProvider-1", fact); Properties props = new Properties(); props.put("instance.name", compName); ComponentInstance ci = null; try { ci = fact.createComponentInstance(props); } catch (Exception e1) { fail(e1.getMessage()); } assertEquals("Check instance name", compName, ci.getInstanceName()); // Get a FooService provider ref = ipojoHelper.getServiceReferenceByName(FooService.class.getName(), compName); assertNotNull("FS not available", ref); // Get foo object FooService fs = (FooService) osgiHelper.getServiceObject(ref); // Cast to POJO Pojo pojo = (Pojo) fs; // GetComponentInstance ComponentInstance instance = pojo.getComponentInstance(); assertEquals("Check component instance name", instance.getInstanceName(), compName); assertEquals("Check component factory name", instance.getFactory().getName(), factName); assertNotNull("Instance description not null", instance.getInstanceDescription()); PrimitiveInstanceDescription id = (PrimitiveInstanceDescription) instance.getInstanceDescription(); assertTrue("Check instance state", id.getState() == ComponentInstance.VALID); assertEquals("Check created pojo count", id.getCreatedObjects().length, 1); assertEquals("Check instance description name", id.getName(), compName); ci.dispose(); // Check that there is no more FooService ref = osgiHelper.getServiceReference(FooService.class.getName()); assertNull("FS available, but component instance stopped", ref); }
@Before public void setUp() { tataFactory = (ComponentFactory) ipojoHelper.getFactory("tata"); totoFactory = (ComponentFactory) ipojoHelper.getFactory("toto"); tataFactory2 = (ComponentFactory) ipojoHelper.getFactory("comp-6"); tataFactory2.stop(); Properties props = new Properties(); props.put("instance.name", "toto provider"); try { totoProv = totoFactory.createComponentInstance(props); } catch (Exception e) { e.printStackTrace(); } Properties props3 = new Properties(); props3.put("instance.name", "toto provider 2"); try { totoProv2 = totoFactory.createComponentInstance(props3); } catch (Exception e) { e.printStackTrace(); } totoProv.stop(); totoProv2.stop(); Factory factory = ipojoHelper.getFactory("comp-7"); Properties props2 = new Properties(); props2.put("instance.name", "ff"); try { under = factory.createComponentInstance(props2); } catch (Exception e) { e.printStackTrace(); } tataFactory.stop(); }
@Test public void testItfsExposition3() { ServiceReference refFoo = ipojoHelper.getServiceReferenceByName( FooService.class.getName(), fooBarProvider3.getInstanceName()); assertNotNull( "Check the availability of the FS from " + fooBarProvider3.getInstanceName(), refFoo); ServiceReference refBar = ipojoHelper.getServiceReferenceByName( BarService.class.getName(), fooBarProvider3.getInstanceName()); assertNotNull( "Check the availability of the BS from " + fooBarProvider3.getInstanceName(), refBar); assertNotSame("Check service reference inequality", refFoo, refBar); FooService fs = (FooService) osgiHelper.getServiceObject(refFoo); assertTrue("Check fs invocation", fs.foo()); fs = null; BarService bs = (BarService) osgiHelper.getServiceObject(refBar); assertTrue("Check bs invocation", bs.bar()); bs = null; // Check properties String baz1 = (String) refFoo.getProperty("baz"); String baz2 = (String) refBar.getProperty("baz"); assertEquals("Check Baz Property 1", baz1, "foo"); assertEquals("Check Baz Property 2", baz2, "bar"); fooBarProvider3.stop(); refFoo = ipojoHelper.getServiceReferenceByName( FooService.class.getName(), fooBarProvider3.getInstanceName()); refBar = ipojoHelper.getServiceReferenceByName( BarService.class.getName(), fooBarProvider3.getInstanceName()); assertNull("Check the absence of the FS from " + fooBarProvider.getInstanceName(), refFoo); assertNull("Check the absence of the BS from " + fooBarProvider.getInstanceName(), refBar); }
/** * Creates a logger. * * @param context the bundle context * @param instance the instance * @param level the trace level */ public Logger(BundleContext context, ComponentInstance instance, int level) { this(context, instance.getInstanceName(), level); m_instance = instance; }
@Test public void testSimple() { // Neither factory nor instance assertTrue("Assert under state - 1", under.getState() == ComponentInstance.INVALID); assertNull( "Assert no tota service - 1", getContext().getServiceReference(Tota.class.getName())); // Start the importer totoProv.start(); assertTrue("Assert under state - 2", under.getState() == ComponentInstance.INVALID); assertNull( "Assert no tota service - 2", getContext().getServiceReference(Tota.class.getName())); // Start the factory tataFactory.start(); assertTrue("Assert under state - 3", under.getState() == ComponentInstance.VALID); assertNotNull( "Assert tota service - 3", getContext().getServiceReference(Tota.class.getName())); ServiceReference ref = getContext().getServiceReference(Tota.class.getName()); Tota tota = (Tota) getContext().getService(ref); invokeAll(tota); // Check toto Properties props = tota.getProps(); Integer toto = (Integer) props.get("toto"); Integer toto_2 = (Integer) props.get("toto_2"); Integer toto_3 = (Integer) props.get("toto_3"); Integer toto_4 = (Integer) props.get("toto_4"); assertEquals("Assert toto - 3 (" + toto.intValue() + ")", toto.intValue(), 1); assertEquals("Assert toto_2 - 3", toto_2.intValue(), 1); assertEquals("Assert toto_3 - 3", toto_3.intValue(), 1); assertEquals("Assert toto_4 - 3", toto_4.intValue(), 0); // Check tata props = tota.getPropsTata(); Integer tata = (Integer) props.get("tata"); Integer tataStr = (Integer) props.get("tataStr"); Integer tataStrs = (Integer) props.get("tataStrs"); Integer tata_2 = (Integer) props.get("tata_2"); Integer tata_3 = (Integer) props.get("tata_3"); Integer tata1 = (Integer) props.get("tata1"); Integer tata1_1 = (Integer) props.get("tata1_1"); Integer tata5 = (Integer) props.get("tata5"); Integer tata5_1 = (Integer) props.get("tata5_1"); Integer tata5_2 = (Integer) props.get("tata5_2"); assertEquals("Assert tata - 3", tata.intValue(), 1); assertEquals("Assert tataStr - 3", tataStr.intValue(), 1); assertEquals("Assert tataStrs - 3", tataStrs.intValue(), 0); assertEquals("Assert tata_2 - 3", tata_2.intValue(), 1); assertEquals("Assert tata_3 - 3", tata_3.intValue(), 1); assertEquals("Assert tata1 - 3", tata1.intValue(), 1); assertEquals("Assert tata1_1 - 3", tata1_1.intValue(), 1); assertEquals("Assert tata5 - 3", tata5.intValue(), 1); assertEquals("Assert tata5_1 - 3", tata5_1.intValue(), 1); assertEquals("Assert tata5_2 - 3", tata5_2.intValue(), 1); getContext().ungetService(ref); tota = null; // Start a second import totoProv2.start(); assertTrue("Assert under state - 4", under.getState() == ComponentInstance.VALID); assertNotNull( "Assert tota service - 4", getContext().getServiceReference(Tota.class.getName())); ref = getContext().getServiceReference(Tota.class.getName()); tota = (Tota) getContext().getService(ref); invokeAll(tota); // Check toto props = tota.getProps(); toto = (Integer) props.get("toto"); toto_2 = (Integer) props.get("toto_2"); toto_3 = (Integer) props.get("toto_3"); toto_4 = (Integer) props.get("toto_4"); assertEquals("Assert toto - 4", toto.intValue(), 2); assertEquals("Assert toto_2 - 4", toto_2.intValue(), 2); assertEquals("Assert toto_3 - 4", toto_3.intValue(), 2); assertEquals("Assert toto_4 - 4", toto_4.intValue(), 0); // Check tata props = tota.getPropsTata(); tata = (Integer) props.get("tata"); tataStr = (Integer) props.get("tataStr"); tataStrs = (Integer) props.get("tataStrs"); tata_2 = (Integer) props.get("tata_2"); tata_3 = (Integer) props.get("tata_3"); tata1 = (Integer) props.get("tata1"); tata1_1 = (Integer) props.get("tata1_1"); tata5 = (Integer) props.get("tata5"); tata5_1 = (Integer) props.get("tata5_1"); tata5_2 = (Integer) props.get("tata5_2"); assertEquals("Assert tata - 4", tata.intValue(), 2); assertEquals("Assert tataStr - 4", tataStr.intValue(), 2); assertEquals("Assert tataStrs - 4", tataStrs.intValue(), 0); assertEquals("Assert tata_2 - 4", tata_2.intValue(), 2); assertEquals("Assert tata_3 - 4", tata_3.intValue(), 2); assertEquals("Assert tata1 - 4", tata1.intValue(), 2); assertEquals("Assert tata1_1 - 4", tata1_1.intValue(), 2); assertEquals("Assert tata5 - 4", tata5.intValue(), 2); assertEquals("Assert tata5_1 - 4", tata5_1.intValue(), 2); assertEquals("Assert tata5_2 - 4", tata5_2.intValue(), 2); getContext().ungetService(ref); tota = null; tataFactory.stop(); assertTrue("Assert under state - 5", under.getState() == ComponentInstance.INVALID); assertNull( "Assert no tota service - 5", getContext().getServiceReference(Tota.class.getName())); totoProv2.stop(); tataFactory.start(); assertTrue("Assert under state - 6", under.getState() == ComponentInstance.VALID); assertNotNull( "Assert tota service - 6", getContext().getServiceReference(Tota.class.getName())); ref = getContext().getServiceReference(Tota.class.getName()); tota = (Tota) getContext().getService(ref); invokeAll(tota); // Check toto props = tota.getProps(); toto = (Integer) props.get("toto"); toto_2 = (Integer) props.get("toto_2"); toto_3 = (Integer) props.get("toto_3"); toto_4 = (Integer) props.get("toto_4"); assertEquals("Assert toto - 6", toto.intValue(), 3); assertEquals("Assert toto_2 - 6", toto_2.intValue(), 3); assertEquals("Assert toto_3 - 6", toto_3.intValue(), 3); assertEquals("Assert toto_4 - 6", toto_4.intValue(), 0); // Check tata props = tota.getPropsTata(); tata = (Integer) props.get("tata"); tataStr = (Integer) props.get("tataStr"); tataStrs = (Integer) props.get("tataStrs"); tata_2 = (Integer) props.get("tata_2"); tata_3 = (Integer) props.get("tata_3"); tata1 = (Integer) props.get("tata1"); tata1_1 = (Integer) props.get("tata1_1"); tata5 = (Integer) props.get("tata5"); tata5_1 = (Integer) props.get("tata5_1"); tata5_2 = (Integer) props.get("tata5_2"); assertEquals("Assert tata - 6", 1, tata.intValue()); assertEquals("Assert tataStr - 6", tataStr.intValue(), 1); assertEquals("Assert tataStrs - 6", tataStrs.intValue(), 0); assertEquals("Assert tata_2 - 6", tata_2.intValue(), 1); assertEquals("Assert tata_3 - 6", tata_3.intValue(), 1); assertEquals("Assert tata1 - 6", tata1.intValue(), 1); assertEquals("Assert tata1_1 - 6", tata1_1.intValue(), 1); assertEquals("Assert tata5 - 6", tata5.intValue(), 1); assertEquals("Assert tata5_1 - 6", tata5_1.intValue(), 1); assertEquals("Assert tata5_2 - 6", tata5_2.intValue(), 1); getContext().ungetService(ref); tota = null; // Is arch exposed assertNotNull( "Test arch", ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "ff")); totoProv.stop(); assertTrue("Assert under state - 7", under.getState() == ComponentInstance.INVALID); assertNotNull( "Test arch-2", ipojoHelper.getServiceReferenceByName(Architecture.class.getName(), "ff")); assertNull( "Assert no tota service - 7", getContext().getServiceReference(Tota.class.getName())); under.dispose(); under = null; }