You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cl...@apache.org on 2008/01/28 00:21:37 UTC
svn commit: r615675 [4/21] - in /felix/sandbox/clement/ipojo: ./
annotations/ ant/ arch/ core/
core/src/main/java/org/apache/felix/ipojo/handlers/configuration/
core/src/main/java/org/apache/felix/ipojo/handlers/dependency/
core/src/main/java/org/apach...
Added: felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/infrastructure/ServiceRangeTest.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/infrastructure/ServiceRangeTest.java?rev=615675&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/infrastructure/ServiceRangeTest.java (added)
+++ felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/infrastructure/ServiceRangeTest.java Sun Jan 27 15:21:24 2008
@@ -0,0 +1,464 @@
+package org.apache.felix.ipojo.test.composite.infrastructure;
+
+import java.util.Properties;
+
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.ServiceContext;
+import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
+import org.apache.felix.ipojo.test.composite.util.Utils;
+import org.apache.felix.ipojo.test.scenarios.service.CheckService;
+import org.apache.felix.ipojo.test.scenarios.service.FooService;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceReference;
+
+public class ServiceRangeTest extends OSGiTestCase {
+
+ private Factory emptyFactory;
+ private ComponentInstance empty;
+
+
+ public void setUp() {
+ emptyFactory = Utils.getFactoryByName(context, "composite.empty");
+ Properties props = new Properties();
+ props.put("name", "empty-1");
+ try {
+ empty = emptyFactory.createComponentInstance(props);
+ } catch(Exception e) { fail("Cannot create empty instance " + e.getMessage()); }
+ }
+
+ public void tearDown() {
+ empty.dispose();
+ empty = null;
+ }
+
+ public void testLevelOne1() {
+ ServiceContext sc2 = Utils.getServiceContext(empty);
+
+ Factory fact1 = Utils.getFactoryByName(sc2, "SimpleCheckServiceProvider");
+ Properties props = new Properties();
+ props.put("name", "client");
+ ComponentInstance client = null;
+ try {
+ client = fact1.createComponentInstance(props);
+ } catch(Exception e) { fail("Cannot instantiate the client : " + e.getMessage()); }
+
+ Factory fact2 = Utils.getFactoryByName(sc2, "FooProviderType-1");
+ Properties props2 = new Properties();
+ props2.put("name", "provider");
+ ComponentInstance provider = null;
+ try {
+ provider = fact2.createComponentInstance(props2);
+ } catch(Exception e) {
+ fail("Cannot instantiate the provider : " + e.getMessage());
+ }
+
+ ServiceReference ref = sc2.getServiceReference(CheckService.class.getName());
+ CheckService check = (CheckService) sc2.getService(ref);
+
+ assertTrue("Check invocation", check.check());
+
+ sc2.ungetService(ref);
+
+ // Check visibility
+ assertNotNull("Check foo service visible inside the composite", sc2.getServiceReference(FooService.class.getName()));
+ assertNotNull("Check check service visible inside the composite", sc2.getServiceReference(CheckService.class.getName()));
+ // Check invisibilty
+ assertNull("Check foo service invisible inside the context", context.getServiceReference(FooService.class.getName()));
+ try {
+ assertNull("Check check service invisible inside the context", context.getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
+ } catch (InvalidSyntaxException e) {
+ fail("Invalid filter : " + e);
+ }
+
+ provider.dispose();
+ client.dispose();
+
+ assertNull("Check foo service invisible inside the composite", sc2.getServiceReference(FooService.class.getName()));
+ assertNull("Check check service invisible inside the composite", sc2.getServiceReference(CheckService.class.getName()));
+ assertNull("Check foo service invisible from the context", context.getServiceReference(FooService.class.getName()));
+ try {
+ assertNull("Check check service invisible inside the context", context.getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
+ } catch (InvalidSyntaxException e) {
+ fail("Invalid filter : " + e);
+ }
+ }
+
+ public void testLevelOne2() {
+ ServiceContext sc2 = Utils.getServiceContext(empty);
+
+ Factory fact1 = Utils.getFactoryByName(sc2, "SimpleCheckServiceProvider");
+ Properties props = new Properties();
+ props.put("name", "client");
+ ComponentInstance client = null;
+ try {
+ client = fact1.createComponentInstance(props, sc2);
+ } catch(Exception e) { fail("Cannot instantiate the client : " + e.getMessage()); }
+
+ Factory fact2 = Utils.getFactoryByName(sc2, "FooProviderType-1");
+ Properties props2 = new Properties();
+ props2.put("name", "provider");
+ ComponentInstance provider = null;
+ try {
+ provider = fact2.createComponentInstance(props2, sc2);
+ } catch(Exception e) {
+ fail("Cannot instantiate the provider : " + e.getMessage());
+ }
+
+ ServiceReference ref = sc2.getServiceReference(CheckService.class.getName());
+ CheckService check = (CheckService) sc2.getService(ref);
+
+ assertTrue("Check invocation", check.check());
+
+ sc2.ungetService(ref);
+
+ // Check visibility
+ assertNotNull("Check foo service visible inside the composite", sc2.getServiceReference(FooService.class.getName()));
+ assertNotNull("Check check service visible inside the composite", sc2.getServiceReference(CheckService.class.getName()));
+ // Check invisibilty
+ assertNull("Check foo service invisible inside the context", context.getServiceReference(FooService.class.getName()));
+ try {
+ assertNull("Check check service invisible inside the context", context.getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
+ } catch (InvalidSyntaxException e) {
+ fail("Invalid filter : " + e);
+ }
+
+ client.dispose();
+ provider.dispose();
+
+ assertNull("Check foo service visible inside the composite 2", sc2.getServiceReference(FooService.class.getName()));
+ assertNull("Check check service visible inside the composite 2", sc2.getServiceReference(CheckService.class.getName()));
+ assertNull("Check foo service invisible inside the global", context.getServiceReference(FooService.class.getName()));
+ try {
+ assertNull("Check check service invisible inside the context", context.getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
+ } catch (InvalidSyntaxException e) {
+ fail("Invalid filter : " + e);
+ }
+ }
+
+ public void testLevelOne3() {
+ ServiceContext sc2 = Utils.getServiceContext(empty);
+
+ Factory fact1 = Utils.getFactoryByName(context, "SimpleCheckServiceProvider");
+ Properties props = new Properties();
+ props.put("name", "client");
+ ComponentInstance client = null;
+ try {
+ client = fact1.createComponentInstance(props, sc2);
+ } catch(Exception e) { fail("Cannot instantiate the client : " + e.getMessage()); }
+
+ Factory fact2 = Utils.getFactoryByName(context, "FooProviderType-1");
+ Properties props2 = new Properties();
+ props2.put("name", "provider");
+ ComponentInstance provider = null;
+ try {
+ provider = fact2.createComponentInstance(props2, sc2);
+ } catch(Exception e) {
+ fail("Cannot instantiate the provider : " + e.getMessage());
+ }
+
+ ServiceReference ref = sc2.getServiceReference(CheckService.class.getName());
+ CheckService check = (CheckService) sc2.getService(ref);
+
+ assertTrue("Check invocation", check.check());
+
+ sc2.ungetService(ref);
+
+ // Check visibility
+ assertNotNull("Check foo service visible inside the composite", sc2.getServiceReference(FooService.class.getName()));
+ assertNotNull("Check check service visible inside the composite", sc2.getServiceReference(CheckService.class.getName()));
+ // Check invisibilty
+ assertNull("Check foo service invisible inside the context", context.getServiceReference(FooService.class.getName()));
+ try {
+ assertNull("Check check service invisible inside the context", context.getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
+ } catch (InvalidSyntaxException e) {
+ fail("Invalid filter : " + e);
+ }
+
+ client.dispose();
+ provider.dispose();
+
+ assertNull("Check foo service visible inside the composite 2", sc2.getServiceReference(FooService.class.getName()));
+ assertNull("Check check service visible inside the composite 2", sc2.getServiceReference(CheckService.class.getName()));
+ assertNull("Check foo service invisible inside the global", context.getServiceReference(FooService.class.getName()));
+ try {
+ assertNull("Check check service invisible inside the context", context.getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
+ } catch (InvalidSyntaxException e) {
+ fail("Invalid filter : " + e);
+ }
+ }
+
+ public void testLevelTwo1() {
+ ServiceContext sc1 = Utils.getServiceContext(empty);
+ Factory fact = Utils.getFactoryByName(sc1, "composite.empty");
+ Properties p = new Properties();
+ p.put("name", "empty2");
+ ComponentInstance empty2 = null;
+ try {
+ empty2 = fact.createComponentInstance(p);
+ } catch(Exception e) {
+ fail("Cannot instantiate empty2 instance : " + e.getMessage());
+ }
+
+ ServiceContext sc2 = Utils.getServiceContext(empty2);
+
+ Factory fact1 = Utils.getFactoryByName(sc2, "SimpleCheckServiceProvider");
+ Properties props = new Properties();
+ props.put("name", "client");
+ ComponentInstance client = null;
+ try {
+ client = fact1.createComponentInstance(props);
+ } catch(Exception e) { fail("Cannot instantiate the client : " + e.getMessage()); }
+
+ Factory fact2 = Utils.getFactoryByName(sc2, "FooProviderType-1");
+ Properties props2 = new Properties();
+ props2.put("name", "provider");
+ ComponentInstance provider = null;
+ try {
+ provider = fact2.createComponentInstance(props2);
+ } catch(Exception e) {
+ fail("Cannot instantiate the provider : " + e.getMessage());
+ }
+
+ ServiceReference ref = sc2.getServiceReference(CheckService.class.getName());
+ CheckService check = (CheckService) sc2.getService(ref);
+
+ assertTrue("Check invocation", check.check());
+
+ sc2.ungetService(ref);
+
+ // Check visibility
+ assertNotNull("Check foo service visible inside the composite 2", sc2.getServiceReference(FooService.class.getName()));
+ assertNotNull("Check check service visible inside the composite 2", sc2.getServiceReference(CheckService.class.getName()));
+ // Check invisibilty
+ assertNull("Check foo service invisible inside the composite 1", sc1.getServiceReference(FooService.class.getName()));
+ assertNull("Check check service invisible inside the composite 1", sc1.getServiceReference(CheckService.class.getName()));
+ assertNull("Check foo service invisible inside the global", context.getServiceReference(FooService.class.getName()));
+ try {
+ assertNull("Check check service invisible inside the context", context.getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
+ } catch (InvalidSyntaxException e) {
+ fail("Invalid filter : " + e);
+ }
+
+ client.dispose();
+ provider.dispose();
+
+ assertNull("Check foo service visible inside the composite 2", sc2.getServiceReference(FooService.class.getName()));
+ assertNull("Check check service visible inside the composite 2", sc2.getServiceReference(CheckService.class.getName()));
+ assertNull("Check foo service invisible inside the composite 1", sc1.getServiceReference(FooService.class.getName()));
+ assertNull("Check check service invisible inside the composite 1", sc1.getServiceReference(CheckService.class.getName()));
+ assertNull("Check foo service invisible inside the global", context.getServiceReference(FooService.class.getName()));
+ try {
+ assertNull("Check check service invisible inside the context", context.getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
+ } catch (InvalidSyntaxException e) {
+ fail("Invalid filter : " + e);
+ }
+ empty2.dispose();
+ }
+
+ public void testLevelTwo2() {
+ ServiceContext sc1 = Utils.getServiceContext(empty);
+ Factory fact = Utils.getFactoryByName(sc1, "composite.empty");
+ Properties p = new Properties();
+ p.put("name", "empty2");
+ ComponentInstance empty2 = null;
+ try {
+ empty2 = fact.createComponentInstance(p);
+ } catch(Exception e) {
+ fail("Cannot instantiate empty2 instance : " + e.getMessage());
+ }
+
+ ServiceContext sc2 = Utils.getServiceContext(empty2);
+
+ Factory fact1 = Utils.getFactoryByName(sc1, "SimpleCheckServiceProvider");
+ Properties props = new Properties();
+ props.put("name", "client");
+ ComponentInstance client = null;
+ try {
+ client = fact1.createComponentInstance(props, sc2);
+ } catch(Exception e) { fail("Cannot instantiate the client : " + e.getMessage()); }
+
+ Factory fact2 = Utils.getFactoryByName(sc1, "FooProviderType-1");
+ Properties props2 = new Properties();
+ props2.put("name", "provider");
+ ComponentInstance provider = null;
+ try {
+ provider = fact2.createComponentInstance(props2, sc2);
+ } catch(Exception e) {
+ fail("Cannot instantiate the provider : " + e.getMessage());
+ }
+
+ ServiceReference ref = sc2.getServiceReference(CheckService.class.getName());
+ CheckService check = (CheckService) sc2.getService(ref);
+
+ assertTrue("Check invocation", check.check());
+
+ sc2.ungetService(ref);
+
+ // Check visibility
+ assertNotNull("Check foo service visible inside the composite 2", sc2.getServiceReference(FooService.class.getName()));
+ assertNotNull("Check check service visible inside the composite 2", sc2.getServiceReference(CheckService.class.getName()));
+ // Check invisibilty
+ assertNull("Check foo service invisible inside the composite 1", sc1.getServiceReference(FooService.class.getName()));
+ assertNull("Check check service invisible inside the composite 1", sc1.getServiceReference(CheckService.class.getName()));
+ assertNull("Check foo service invisible inside the global", context.getServiceReference(FooService.class.getName()));
+ try {
+ assertNull("Check check service invisible inside the context", context.getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
+ } catch (InvalidSyntaxException e) {
+ fail("Invalid filter : " + e);
+ }
+
+ client.dispose();
+ provider.dispose();
+
+ assertNull("Check foo service visible inside the composite 2", sc2.getServiceReference(FooService.class.getName()));
+ assertNull("Check check service visible inside the composite 2", sc2.getServiceReference(CheckService.class.getName()));
+ assertNull("Check foo service invisible inside the composite 1", sc1.getServiceReference(FooService.class.getName()));
+ assertNull("Check check service invisible inside the composite 1", sc1.getServiceReference(CheckService.class.getName()));
+ assertNull("Check foo service invisible inside the global", context.getServiceReference(FooService.class.getName()));
+ try {
+ assertNull("Check check service invisible inside the context", context.getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
+ } catch (InvalidSyntaxException e) {
+ fail("Invalid filter : " + e);
+ }
+ empty2.dispose();
+ }
+
+ public void testLevelTwo3() {
+ ServiceContext sc1 = Utils.getServiceContext(empty);
+ Factory fact = Utils.getFactoryByName(sc1, "composite.empty");
+ Properties p = new Properties();
+ p.put("name", "empty2");
+ ComponentInstance empty2 = null;
+ try {
+ empty2 = fact.createComponentInstance(p);
+ } catch(Exception e) {
+ fail("Cannot instantiate empty2 instance : " + e.getMessage());
+ }
+
+ ServiceContext sc2 = Utils.getServiceContext(empty2);
+
+ Factory fact1 = Utils.getFactoryByName(context, "SimpleCheckServiceProvider");
+ Properties props = new Properties();
+ props.put("name", "client");
+ ComponentInstance client = null;
+ try {
+ client = fact1.createComponentInstance(props, sc2);
+ } catch(Exception e) { fail("Cannot instantiate the client : " + e.getMessage()); }
+
+ Factory fact2 = Utils.getFactoryByName(context, "FooProviderType-1");
+ Properties props2 = new Properties();
+ props2.put("name", "provider");
+ ComponentInstance provider = null;
+ try {
+ provider = fact2.createComponentInstance(props2, sc2);
+ } catch(Exception e) {
+ fail("Cannot instantiate the provider : " + e.getMessage());
+ }
+
+ ServiceReference ref = sc2.getServiceReference(CheckService.class.getName());
+ CheckService check = (CheckService) sc2.getService(ref);
+
+ assertTrue("Check invocation", check.check());
+
+ sc2.ungetService(ref);
+
+ // Check visibility
+ assertNotNull("Check foo service visible inside the composite 2", sc2.getServiceReference(FooService.class.getName()));
+ assertNotNull("Check check service visible inside the composite 2", sc2.getServiceReference(CheckService.class.getName()));
+ // Check invisibilty
+ assertNull("Check foo service invisible inside the composite 1", sc1.getServiceReference(FooService.class.getName()));
+ assertNull("Check check service invisible inside the composite 1", sc1.getServiceReference(CheckService.class.getName()));
+ assertNull("Check foo service invisible inside the global", context.getServiceReference(FooService.class.getName()));
+ try {
+ assertNull("Check check service invisible inside the context", context.getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
+ } catch (InvalidSyntaxException e) {
+ fail("Invalid filter : " + e);
+ }
+
+ client.dispose();
+ provider.dispose();
+
+ assertNull("Check foo service visible inside the composite 2", sc2.getServiceReference(FooService.class.getName()));
+ assertNull("Check check service visible inside the composite 2", sc2.getServiceReference(CheckService.class.getName()));
+ assertNull("Check foo service invisible inside the composite 1", sc1.getServiceReference(FooService.class.getName()));
+ assertNull("Check check service invisible inside the composite 1", sc1.getServiceReference(CheckService.class.getName()));
+ assertNull("Check foo service invisible inside the global", context.getServiceReference(FooService.class.getName()));
+ try {
+ assertNull("Check check service invisible inside the context", context.getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
+ } catch (InvalidSyntaxException e) {
+ fail("Invalid filter : " + e);
+ }
+ empty2.dispose();
+ }
+
+ public void testLevelTwo4() {
+ ServiceContext sc1 = Utils.getServiceContext(empty);
+ Factory fact = Utils.getFactoryByName(sc1, "composite.empty");
+ Properties p = new Properties();
+ p.put("name", "empty2");
+ ComponentInstance empty2 = null;
+ try {
+ empty2 = fact.createComponentInstance(p);
+ } catch(Exception e) {
+ fail("Cannot instantiate empty2 instance : " + e.getMessage());
+ }
+
+ ServiceContext sc2 = Utils.getServiceContext(empty2);
+
+ Factory fact1 = Utils.getFactoryByName(sc2, "SimpleCheckServiceProvider");
+ Properties props = new Properties();
+ props.put("name", "client");
+ ComponentInstance client = null;
+ try {
+ client = fact1.createComponentInstance(props, sc2);
+ } catch(Exception e) { fail("Cannot instantiate the client : " + e.getMessage()); }
+
+ Factory fact2 = Utils.getFactoryByName(sc2, "FooProviderType-1");
+ Properties props2 = new Properties();
+ props2.put("name", "provider");
+ ComponentInstance provider = null;
+ try {
+ provider = fact2.createComponentInstance(props2, sc2);
+ } catch(Exception e) {
+ fail("Cannot instantiate the provider : " + e.getMessage());
+ }
+
+ ServiceReference ref = sc2.getServiceReference(CheckService.class.getName());
+ CheckService check = (CheckService) sc2.getService(ref);
+
+ assertTrue("Check invocation", check.check());
+
+ sc2.ungetService(ref);
+
+ // Check visibility
+ assertNotNull("Check foo service visible inside the composite 2", sc2.getServiceReference(FooService.class.getName()));
+ assertNotNull("Check check service visible inside the composite 2", sc2.getServiceReference(CheckService.class.getName()));
+ // Check invisibilty
+ assertNull("Check foo service invisible inside the composite 1", sc1.getServiceReference(FooService.class.getName()));
+ assertNull("Check check service invisible inside the composite 1", sc1.getServiceReference(CheckService.class.getName()));
+ assertNull("Check foo service invisible inside the global", context.getServiceReference(FooService.class.getName()));
+ try {
+ assertNull("Check check service invisible inside the context", context.getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
+ } catch (InvalidSyntaxException e) {
+ fail("Invalid filter : " + e);
+ }
+
+ client.dispose();
+ provider.dispose();
+
+ assertNull("Check foo service visible inside the composite 2", sc2.getServiceReference(FooService.class.getName()));
+ assertNull("Check check service visible inside the composite 2", sc2.getServiceReference(CheckService.class.getName()));
+ assertNull("Check foo service invisible inside the composite 1", sc1.getServiceReference(FooService.class.getName()));
+ assertNull("Check check service invisible inside the composite 1", sc1.getServiceReference(CheckService.class.getName()));
+ assertNull("Check foo service invisible inside the global", context.getServiceReference(FooService.class.getName()));
+ try {
+ assertNull("Check check service invisible inside the context", context.getServiceReferences(CheckService.class.getName(), "(instance.name=client)"));
+ } catch (InvalidSyntaxException e) {
+ fail("Invalid filter : " + e);
+ }
+ empty2.dispose();
+ }
+
+
+}
Added: felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/infrastructure/ServiceRegistryTest.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/infrastructure/ServiceRegistryTest.java?rev=615675&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/infrastructure/ServiceRegistryTest.java (added)
+++ felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/infrastructure/ServiceRegistryTest.java Sun Jan 27 15:21:24 2008
@@ -0,0 +1,227 @@
+package org.apache.felix.ipojo.test.composite.infrastructure;
+
+import java.util.Dictionary;
+import java.util.Properties;
+
+import org.apache.felix.ipojo.ComponentFactory;
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.InstanceStateListener;
+import org.apache.felix.ipojo.architecture.ComponentTypeDescription;
+import org.apache.felix.ipojo.architecture.InstanceDescription;
+import org.apache.felix.ipojo.context.ServiceRegistry;
+import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
+import org.apache.felix.ipojo.test.scenarios.service.BarService;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.ServiceEvent;
+import org.osgi.framework.ServiceListener;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+
+public class ServiceRegistryTest extends OSGiTestCase {
+
+ private class svcListener implements ServiceListener {
+ public int registration = 0;
+ public int unregistration = 0;
+ public int modification = 0;
+ public void serviceChanged(ServiceEvent ev) {
+ if(ev.getType() == ServiceEvent.REGISTERED) { registration++; }
+ if(ev.getType() == ServiceEvent.UNREGISTERING) { unregistration++; }
+ if(ev.getType() == ServiceEvent.MODIFIED) { modification++; }
+ }
+ }
+
+ private class barProvider implements BarService {
+
+ public boolean bar() { return true; }
+
+ public Properties getProps() { return null; }
+
+ }
+
+ private class FakeComponent implements ComponentInstance {
+
+ public ComponentTypeDescription getComponentDescription() {
+ return null;
+ }
+
+ public BundleContext getContext() {
+ return null;
+ }
+
+ public ComponentFactory getFactory() {
+ return null;
+ }
+
+ public InstanceDescription getInstanceDescription() {
+ return null;
+ }
+
+ public String getInstanceName() {
+ return null;
+ }
+
+ public int getState() {
+ return 0;
+ }
+
+ public boolean isStarted() {
+ return false;
+ }
+
+ public void reconfigure(Dictionary dictionary) { }
+
+ public void start() { }
+
+ public void stop() { }
+
+ public void dispose() { }
+
+ public void addInstanceStateListener(InstanceStateListener arg0) { }
+
+ public void removeInstanceStateListener(InstanceStateListener arg0) { }
+
+ }
+
+ public void testRegistrationAndListener() {
+ ComponentInstance im = new FakeComponent();
+ ComponentInstance im2 = new FakeComponent();
+ ServiceRegistry registry = new ServiceRegistry(context);
+ assertNotNull("Assert registry not null", registry);
+ svcListener all = new svcListener();
+ try {
+ assertNull("Check that there is no available service", registry.getServiceReferences(null, null));
+ } catch (InvalidSyntaxException e) { fail("Cannot query the registry : " + e.getMessage()); }
+ registry.addServiceListener(all);
+
+ ServiceRegistration reg1 = registry.registerService(im, BarService.class.getName(), new barProvider(), null);
+
+ try {
+ assertEquals("Check number of registred service", 1, registry.getServiceReferences(null, null).length);
+ } catch (InvalidSyntaxException e) { fail("Cannot query the registry : " + e.getMessage());
+ }
+
+ ServiceRegistration reg2 = registry.registerService(im2, BarService.class.getName(), new barProvider(), null);
+
+ try {
+ assertEquals("Check number of registred service", 2, registry.getServiceReferences(null, null).length);
+ } catch (InvalidSyntaxException e) { fail("Cannot query the registry : " + e.getMessage()); }
+
+ assertEquals("Check the number of registration", 2, all.registration);
+
+ Properties props = new Properties();
+ props.put("foo", "bar");
+ reg1.setProperties(props);
+ assertEquals("Check the number of modification", 1, all.modification);
+
+ reg1.unregister();
+ assertEquals("Check the number of unregistration", 1, all.unregistration);
+
+ reg2.setProperties(props);
+ assertEquals("Check the number of modification", 2, all.modification);
+
+ reg2.unregister();
+ assertEquals("Check the number of unregistration", 2, all.unregistration);
+
+ registry.removeServiceListener(all);
+ }
+
+ public void testRegistrationAndFilter() {
+ ComponentInstance im = new FakeComponent();
+ ComponentInstance im2 = new FakeComponent();
+ ServiceRegistry registry = new ServiceRegistry(context);
+ svcListener filtered = new svcListener();
+
+ try {
+ assertNull("Check that there is no available service", registry.getServiceReferences(null, null));
+ } catch (InvalidSyntaxException e) { fail("Cannot query the registry : " + e.getMessage()); }
+
+ registry.addServiceListener(filtered, "(foo=bar)");
+
+ Properties props = new Properties();
+ props.put("foo", "bar");
+ ServiceRegistration reg1 = registry.registerService(im, BarService.class.getName(), new barProvider(), props);
+
+ try {
+ assertEquals("Check number of registred service", 1, registry.getServiceReferences(null, null).length);
+ } catch (InvalidSyntaxException e) { fail("Cannot query the registry : " + e.getMessage()); }
+
+ ServiceRegistration reg2 = registry.registerService(im2, BarService.class.getName(), new barProvider(), null);
+
+ try {
+ assertEquals("Check number of registred service", 2, registry.getServiceReferences(null, null).length);
+ } catch (InvalidSyntaxException e) { fail("Cannot query the registry : " + e.getMessage()); }
+
+ assertEquals("Check the number of registration", 1, filtered.registration);
+
+ reg2.setProperties(props);
+ assertEquals("Check the number of modification", 1, filtered.modification);
+ // Follow the OSGi semantics of filters
+
+ reg1.unregister();
+ reg2.unregister();
+ assertEquals("Check the number of unregistration", 2, filtered.unregistration);
+ registry.removeServiceListener(filtered);
+ }
+
+ public void testGetService() {
+ ComponentInstance im = new FakeComponent();
+ ComponentInstance im2 = new FakeComponent();
+ ServiceRegistry registry = new ServiceRegistry(context);
+
+ try {
+ assertNull("Check that there is no available service", registry.getServiceReferences(null, null));
+ } catch (InvalidSyntaxException e) { fail("Cannot query the registry : " + e.getMessage()); }
+
+ Properties props = new Properties();
+ props.put("foo", "bar");
+ ServiceRegistration reg1 = registry.registerService(im, BarService.class.getName(), new barProvider(), props);
+
+ ServiceReference ref = registry.getServiceReference(BarService.class.getName());
+ assertNotNull("Check ref not null", ref);
+ assertEquals("Test property", ref.getProperty("foo"), "bar");
+ BarService bar = (BarService) registry.getService(im2, ref);
+ assertTrue("Test invocation", bar.bar());
+
+ reg1.unregister();
+ ref = registry.getServiceReference(BarService.class.getName());
+ assertNull("Check ref null", ref);
+ }
+
+ public void testGetFilteredService() {
+ ComponentInstance im = new FakeComponent();
+ ComponentInstance im2 = new FakeComponent();
+ ServiceRegistry registry = new ServiceRegistry(context);
+
+ try {
+ assertNull("Check that there is no available service", registry.getServiceReferences(null, null));
+ } catch (InvalidSyntaxException e) { fail("Cannot query the registry : " + e.getMessage()); }
+
+ Properties props = new Properties();
+ props.put("foo", "bar");
+ ServiceRegistration reg1 = registry.registerService(im, BarService.class.getName(), new barProvider(), props);
+ ServiceRegistration reg2 = registry.registerService(im2, BarService.class.getName(), new barProvider(), null);
+
+ ServiceReference[] ref = null;
+ try {
+ ref = registry.getServiceReferences(BarService.class.getName(), "(foo=bar)");
+ } catch (InvalidSyntaxException e) { fail("Registry query fail : " + e.getMessage()); }
+ assertNotNull("Check ref not null", ref);
+ assertEquals("Check ref count", ref.length, 1);
+ assertEquals("Test property", ref[0].getProperty("foo"), "bar");
+ BarService bar = (BarService) registry.getService(im2, ref[0]);
+ assertTrue("Test invocation", bar.bar());
+
+ ref = null;
+ reg1.unregister();
+ try {
+ ref = registry.getServiceReferences(BarService.class.getName(), "(bar=foo)");
+ } catch (InvalidSyntaxException e) { fail("Registry query fail : " + e.getMessage()); }
+ assertNull("Check ref null", ref);
+
+ reg2.unregister();
+ }
+
+
+
+}
Propchange: felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instance/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Sun Jan 27 15:21:24 2008
@@ -0,0 +1,6 @@
+target*
+bin*
+.settings*
+.classpath
+.project
+.checkstyle
Added: felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instance/SimpleInstance.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instance/SimpleInstance.java?rev=615675&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instance/SimpleInstance.java (added)
+++ felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instance/SimpleInstance.java Sun Jan 27 15:21:24 2008
@@ -0,0 +1,249 @@
+package org.apache.felix.ipojo.test.composite.instance;
+
+import java.util.Properties;
+
+import org.apache.felix.ipojo.ComponentFactory;
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.ServiceContext;
+import org.apache.felix.ipojo.architecture.Architecture;
+import org.apache.felix.ipojo.architecture.InstanceDescription;
+import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
+import org.apache.felix.ipojo.test.composite.util.Utils;
+import org.apache.felix.ipojo.test.scenarios.service.FooService;
+import org.osgi.framework.ServiceReference;
+
+public class SimpleInstance extends OSGiTestCase {
+
+ private ComponentFactory fooFactory1, fooFactory2;
+ private ComponentFactory compoFactory;
+ private ComponentInstance empty;
+
+
+ public void setUp() {
+ fooFactory1 = (ComponentFactory) Utils.getFactoryByName(context, "FooProviderType-1");
+ fooFactory2 = (ComponentFactory) Utils.getFactoryByName(context, "FooProviderType-Dyn2");
+ compoFactory = (ComponentFactory) Utils.getFactoryByName(context, "composite.inst.1");
+ Factory fact = Utils.getFactoryByName(context, "composite.empty");
+ Properties props = new Properties();
+ props.put("name", "empty-X");
+ try {
+ empty = fact.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot create the empty composite : " + e.getMessage());
+ }
+ }
+
+ public void tearDown() {
+ empty.dispose();
+ empty = null;
+ }
+
+ public void testCreation() {
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = compoFactory.createComponentInstance(props);
+ } catch(Exception e) {
+ e.printStackTrace();
+ fail("Cannot instantiate under from " + compoFactory + " -> " + e.getMessage());
+ }
+ assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
+ under.dispose();
+ }
+
+ public void testServiceAvailability() {
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = compoFactory.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
+ ServiceContext sc = Utils.getServiceContext(under);
+
+ assertNotNull("Check service availability", sc.getServiceReference(FooService.class.getName()));
+ assertEquals("Check service provider", Utils.getServiceReferences(sc, FooService.class.getName(), null).length, 2);
+
+ under.dispose();
+ }
+
+ public void testCreationLevel2() {
+ ServiceContext sc = Utils.getServiceContext(empty);
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = compoFactory.createComponentInstance(props, sc);
+ } catch(Exception e) {
+ e.printStackTrace();
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
+ under.dispose();
+ }
+
+ public void testServiceAvailabilityLevel2() {
+ ServiceContext sc = Utils.getServiceContext(empty);
+ Properties props = new Properties();
+ props.put("name", "under-X");
+ ComponentInstance under = null;
+ try {
+ under = compoFactory.createComponentInstance(props, sc);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
+ ServiceContext sc2 = Utils.getServiceContext(under);
+
+ assertNotNull("Check service availability", sc2.getServiceReference(FooService.class.getName()));
+ assertEquals("Check service providers", Utils.getServiceReferences(sc2, FooService.class.getName(), null).length, 2);
+
+ under.dispose();
+ }
+
+ public void testFactoryManagement() {
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = compoFactory.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity - 1", under.getState() == ComponentInstance.VALID);
+
+ fooFactory1.stop();
+ assertTrue("Check instance invalidity - 2", under.getState() == ComponentInstance.INVALID);
+
+ fooFactory1.start();
+ assertTrue("Check instance validity - 3", under.getState() == ComponentInstance.VALID);
+
+ fooFactory2.stop();
+ assertTrue("Check instance invalidity", under.getState() == ComponentInstance.INVALID);
+
+ fooFactory2.start();
+ assertTrue("Check instance validity - 4", under.getState() == ComponentInstance.VALID);
+
+ under.dispose();
+ fooFactory1.start();
+ fooFactory2.start();
+ }
+
+ public void testFactoryManagementLevel2() {
+ ServiceContext sc = Utils.getServiceContext(empty);
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = compoFactory.createComponentInstance(props, sc);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity - 1", under.getState() == ComponentInstance.VALID);
+
+ assertTrue("Check instance validity - 1", under.getState() == ComponentInstance.VALID);
+
+ fooFactory1.stop();
+ assertTrue("Check instance invalidity - 2", under.getState() == ComponentInstance.INVALID);
+
+ fooFactory1.start();
+ assertTrue("Check instance validity - 3", under.getState() == ComponentInstance.VALID);
+
+ fooFactory2.stop();
+ assertTrue("Check instance invalidity", under.getState() == ComponentInstance.INVALID);
+
+ fooFactory2.start();
+ assertTrue("Check instance validity - 4", under.getState() == ComponentInstance.VALID);
+
+ under.dispose();
+ fooFactory1.start();
+ fooFactory2.start();
+ }
+
+ public void atestArchitecture() { //TODO : to reactive
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = compoFactory.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ ServiceReference ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), "under");
+ assertNotNull("Check architecture availability", ref);
+ Architecture arch = (Architecture) context.getService(ref);
+ InstanceDescription id = arch.getInstanceDescription();
+
+ assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
+ InstanceDescription[] contained = id.getContainedInstances();
+ assertEquals("Check contained instances count (" + contained.length + ")", contained.length, 1);
+ assertEquals("Check that no object are created" , id.getCreatedObjects().length, 0);
+ assertEquals("Check instance name" , id.getName(), "under");
+ assertEquals("Check component type name" , id.getComponentDescription().getName(), "composite.bar.1");
+
+ ComponentFactory fact1 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-1");
+ ComponentFactory fact2 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-2");
+ ComponentFactory fact3 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-3");
+
+ fact1.stop();
+ assertTrue("Check instance validity - 2", under.getState() == ComponentInstance.VALID);
+ ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), "under");
+ assertNotNull("Check architecture availability", ref);
+ arch = (Architecture) context.getService(ref);
+ id = arch.getInstanceDescription();
+ assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
+ contained = id.getContainedInstances();
+ assertEquals("Check contained instances count", contained.length, 1);
+ assertEquals("Check that no object are created" , id.getCreatedObjects().length, 0);
+ assertEquals("Check instance name" , id.getName(), "under");
+ assertEquals("Check component type name" , id.getComponentDescription().getName(), "composite.bar.1");
+
+ fact2.stop();
+ assertTrue("Check instance validity - 3", under.getState() == ComponentInstance.VALID);
+ ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), "under");
+ assertNotNull("Check architecture availability", ref);
+ arch = (Architecture) context.getService(ref);
+ id = arch.getInstanceDescription();
+ assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
+ contained = id.getContainedInstances();
+ assertEquals("Check contained instances count", contained.length, 1);
+ assertEquals("Check that no object are created" , id.getCreatedObjects().length, 0);
+ assertEquals("Check instance name" , id.getName(), "under");
+ assertEquals("Check component type name" , id.getComponentDescription().getName(), "composite.bar.1");
+
+ fact3.stop();
+ assertTrue("Check instance invalidity", under.getState() == ComponentInstance.INVALID);
+ ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), "under");
+ assertNotNull("Check architecture availability", ref);
+ arch = (Architecture) context.getService(ref);
+ id = arch.getInstanceDescription();
+ assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.INVALID);
+ contained = id.getContainedInstances();
+ assertEquals("Check contained instances count", contained.length, 0);
+ assertEquals("Check that no object are created" , id.getCreatedObjects().length, 0);
+ assertEquals("Check instance name" , id.getName(), "under");
+ assertEquals("Check component type name" , id.getComponentDescription().getName(), "composite.bar.1");
+
+ fact1.start();
+ assertTrue("Check instance validity - 4", under.getState() == ComponentInstance.VALID);
+ ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), "under");
+ assertNotNull("Check architecture availability", ref);
+ arch = (Architecture) context.getService(ref);
+ id = arch.getInstanceDescription();
+ assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
+ contained = id.getContainedInstances();
+ assertEquals("Check contained instances count", contained.length, 1);
+ assertEquals("Check that no object are created" , id.getCreatedObjects().length, 0);
+ assertEquals("Check instance name" , id.getName(), "under");
+ assertEquals("Check component type name" , id.getComponentDescription().getName(), "composite.bar.1");
+
+ under.dispose();
+ fact2.start();
+ fact3.start();
+ }
+
+}
Propchange: felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Sun Jan 27 15:21:24 2008
@@ -0,0 +1,6 @@
+target*
+bin*
+.settings*
+.classpath
+.project
+.checkstyle
Added: felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/ConfigurableInstantiation.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/ConfigurableInstantiation.java?rev=615675&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/ConfigurableInstantiation.java (added)
+++ felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/ConfigurableInstantiation.java Sun Jan 27 15:21:24 2008
@@ -0,0 +1,83 @@
+package org.apache.felix.ipojo.test.composite.instantiator;
+
+import java.util.Properties;
+
+import org.apache.felix.ipojo.ComponentFactory;
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.ServiceContext;
+import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
+import org.apache.felix.ipojo.test.composite.util.Utils;
+import org.apache.felix.ipojo.test.scenarios.service.FooService;
+import org.osgi.framework.ServiceReference;
+
+public class ConfigurableInstantiation extends OSGiTestCase {
+
+ private ComponentFactory acceptF;
+ private ComponentFactory refuse1F;
+ private ComponentFactory refuse2F;
+
+ public void setUp() {
+ acceptF = (ComponentFactory) Utils.getFactoryByName(context, "composite.bar.5-accept");
+ refuse1F = (ComponentFactory) Utils.getFactoryByName(context, "composite.bar.5-refuse1");
+ refuse2F = (ComponentFactory) Utils.getFactoryByName(context, "composite.bar.5-refuse2");
+
+ }
+
+ public void tearDown() { }
+
+ public void testAccept() {
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = acceptF.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
+ ServiceContext sc = Utils.getServiceContext(under);
+ ServiceReference ref = sc.getServiceReference(FooService.class.getName());
+ assertNotNull("Check refs not null", ref);
+ FooService foo = (FooService) sc.getService(ref);
+ Properties p = foo.fooProps();
+ boolean b = ((Boolean) p.get("boolProp")).booleanValue();
+ String s = (String) p.get("strProp");
+ int i = ( (Integer) p.get("intProp")).intValue();
+ assertTrue("Test boolean", b);
+ assertEquals("Test string", s, "foo");
+
+ assertEquals("Test int", i, 5); // The code fix to 5.
+ under.dispose();
+ }
+
+ public void testRefuse1() {
+ Properties props = new Properties();
+ props.put("name", "under-ref1");
+ ComponentInstance under = null;
+ try {
+ under = refuse1F.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+
+ assertTrue("Check that under is not valid", under.getState() == ComponentInstance.INVALID);
+
+ under.dispose();
+ }
+
+ public void testRefuse2() {
+ Properties props = new Properties();
+ props.put("name", "under-ref2");
+ ComponentInstance under = null;
+ try {
+ under = refuse2F.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+
+ assertTrue("Check that under is not valid", under.getState() == ComponentInstance.INVALID);
+
+ under.dispose();
+ }
+
+}
Added: felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/InstantiatorTestSuite.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/InstantiatorTestSuite.java?rev=615675&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/InstantiatorTestSuite.java (added)
+++ felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/InstantiatorTestSuite.java Sun Jan 27 15:21:24 2008
@@ -0,0 +1,20 @@
+package org.apache.felix.ipojo.test.composite.instantiator;
+
+import junit.framework.Test;
+
+import org.apache.felix.ipojo.junit4osgi.OSGiTestSuite;
+import org.osgi.framework.BundleContext;
+
+public class InstantiatorTestSuite {
+
+ public static Test suite(BundleContext bc) {
+ OSGiTestSuite ots = new OSGiTestSuite("Composite Service Instantiation Test Suite", bc);
+ ots.addTestSuite(SimpleInstantiation.class);
+ ots.addTestSuite(OptionalInstantiation.class);
+ ots.addTestSuite(MultipleInstantiation.class);
+ ots.addTestSuite(OptionalMultipleInstantiation.class);
+ ots.addTestSuite(ConfigurableInstantiation.class);
+ return ots;
+ }
+
+}
Added: felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/MultipleInstantiation.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/MultipleInstantiation.java?rev=615675&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/MultipleInstantiation.java (added)
+++ felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/MultipleInstantiation.java Sun Jan 27 15:21:24 2008
@@ -0,0 +1,270 @@
+package org.apache.felix.ipojo.test.composite.instantiator;
+
+import java.util.Properties;
+
+import org.apache.felix.ipojo.ComponentFactory;
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.ServiceContext;
+import org.apache.felix.ipojo.architecture.Architecture;
+import org.apache.felix.ipojo.architecture.InstanceDescription;
+import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
+import org.apache.felix.ipojo.test.composite.util.Utils;
+import org.apache.felix.ipojo.test.scenarios.service.BarService;
+import org.osgi.framework.ServiceReference;
+
+public class MultipleInstantiation extends OSGiTestCase {
+
+ private ComponentFactory bar2Factory;
+ private ComponentInstance empty;
+
+ public void setUp() {
+ bar2Factory = (ComponentFactory) Utils.getFactoryByName(context, "composite.bar.2");
+ Factory fact = Utils.getFactoryByName(context, "composite.empty");
+ Properties props = new Properties();
+ props.put("name", "empty");
+ try {
+ empty = fact.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot create the empty composite : " + e.getMessage());
+ }
+ }
+
+ public void tearDown() {
+ empty.dispose();
+ empty = null;
+ }
+
+ public void testCreation() {
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar2Factory.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
+ under.dispose();
+ }
+
+ public void testServiceAvailability() {
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar2Factory.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
+ ServiceContext sc = Utils.getServiceContext(under);
+ assertNotNull("Check service availability", sc.getServiceReference(BarService.class.getName()));
+ int count = Utils.getServiceReferences(sc, BarService.class.getName(), null).length;
+ assertEquals("Check service provider number : " + count, count, 3);
+
+ under.dispose();
+ }
+
+ public void testCreationLevel2() {
+ ServiceContext sc = Utils.getServiceContext(empty);
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar2Factory.createComponentInstance(props, sc);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
+ under.dispose();
+ }
+
+ public void testServiceAvailabilityLevel2() {
+ ServiceContext sc = Utils.getServiceContext(empty);
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar2Factory.createComponentInstance(props, sc);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
+ ServiceContext sc2 = Utils.getServiceContext(under);
+ assertNotNull("Check service availability", sc2.getServiceReference(BarService.class.getName()));
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc2, BarService.class.getName(), null).length, 3);
+
+ under.dispose();
+ }
+
+ public void testFactoryManagement() {
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar2Factory.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ ServiceContext sc = Utils.getServiceContext(under);
+ assertTrue("Check instance validity - 1", under.getState() == ComponentInstance.VALID);
+
+ ComponentFactory fact1 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-1");
+ ComponentFactory fact2 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-2");
+ ComponentFactory fact3 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-3");
+
+ fact1.stop();
+ assertTrue("Check instance validity - 2", under.getState() == ComponentInstance.VALID);
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc, BarService.class.getName(), null).length, 2);
+
+ fact2.stop();
+ assertTrue("Check instance validity - 3", under.getState() == ComponentInstance.VALID);
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc, BarService.class.getName(), null).length, 1);
+
+ fact3.stop();
+ assertTrue("Check instance invalidity", under.getState() == ComponentInstance.INVALID);
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc, BarService.class.getName(), null).length, 0);
+
+ fact1.start();
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc, BarService.class.getName(), null).length, 1);
+ assertTrue("Check instance validity - 4", under.getState() == ComponentInstance.VALID);
+
+ under.dispose();
+ fact2.start();
+ fact3.start();
+ }
+
+ public void testFactoryManagementLevel2() {
+ ServiceContext sc = Utils.getServiceContext(empty);
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar2Factory.createComponentInstance(props, sc);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity - 1", under.getState() == ComponentInstance.VALID);
+ ServiceContext sc2 = Utils.getServiceContext(under);
+
+ ComponentFactory fact1 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-1");
+ ComponentFactory fact2 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-2");
+ ComponentFactory fact3 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-3");
+
+ fact1.stop();
+ assertTrue("Check instance validity - 2", under.getState() == ComponentInstance.VALID);
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc2, BarService.class.getName(), null).length, 2);
+
+ fact2.stop();
+ assertTrue("Check instance validity - 3", under.getState() == ComponentInstance.VALID);
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc2, BarService.class.getName(), null).length, 1);
+
+ fact3.stop();
+ assertTrue("Check instance invalidity", under.getState() == ComponentInstance.INVALID);
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc2, BarService.class.getName(), null).length, 0);
+
+ fact1.start();
+ assertTrue("Check instance validity - 4", under.getState() == ComponentInstance.VALID);
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc2, BarService.class.getName(), null).length, 1);
+
+ under.dispose();
+ fact2.start();
+ fact3.start();
+ }
+ public void testArchitecture() {
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar2Factory.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ ServiceReference ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), "under");
+ assertNotNull("Check architecture availability", ref);
+ Architecture arch = (Architecture) context.getService(ref);
+ InstanceDescription id = arch.getInstanceDescription();
+
+ assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
+ InstanceDescription[] contained = id.getContainedInstances();
+ assertEquals("Check contained instances count", contained.length, 3);
+ assertEquals("Check that no object are created" , id.getCreatedObjects().length, 0);
+ assertEquals("Check instance name" , id.getName(), "under");
+ assertEquals("Check component type name" , id.getComponentDescription().getName(), "composite.bar.2");
+
+ ComponentFactory fact1 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-1");
+ ComponentFactory fact2 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-2");
+ ComponentFactory fact3 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-3");
+
+ fact1.stop();
+ assertTrue("Check instance validity - 2", under.getState() == ComponentInstance.VALID);
+ ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), "under");
+ assertNotNull("Check architecture availability", ref);
+ arch = (Architecture) context.getService(ref);
+ id = arch.getInstanceDescription();
+ assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
+ contained = id.getContainedInstances();
+ assertEquals("Check contained instances count", contained.length, 2);
+ assertEquals("Check that no object are created" , id.getCreatedObjects().length, 0);
+ assertEquals("Check instance name" , id.getName(), "under");
+ assertEquals("Check component type name" , id.getComponentDescription().getName(), "composite.bar.2");
+
+ fact2.stop();
+ assertTrue("Check instance validity - 3", under.getState() == ComponentInstance.VALID);
+ ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), "under");
+ assertNotNull("Check architecture availability", ref);
+ arch = (Architecture) context.getService(ref);
+ id = arch.getInstanceDescription();
+ assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
+ contained = id.getContainedInstances();
+ assertEquals("Check contained instances count", contained.length, 1);
+ assertEquals("Check that no object are created" , id.getCreatedObjects().length, 0);
+ assertEquals("Check instance name" , id.getName(), "under");
+ assertEquals("Check component type name" , id.getComponentDescription().getName(), "composite.bar.2");
+
+ fact3.stop();
+ assertTrue("Check instance invalidity", under.getState() == ComponentInstance.INVALID);
+ ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), "under");
+ assertNotNull("Check architecture availability", ref);
+ arch = (Architecture) context.getService(ref);
+ id = arch.getInstanceDescription();
+ assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.INVALID);
+ contained = id.getContainedInstances();
+ assertEquals("Check contained instances count", contained.length, 0);
+ assertEquals("Check that no object are created" , id.getCreatedObjects().length, 0);
+ assertEquals("Check instance name" , id.getName(), "under");
+ assertEquals("Check component type name" , id.getComponentDescription().getName(), "composite.bar.2");
+
+ fact1.start();
+ assertTrue("Check instance validity - 4", under.getState() == ComponentInstance.VALID);
+ ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), "under");
+ assertNotNull("Check architecture availability", ref);
+ arch = (Architecture) context.getService(ref);
+ id = arch.getInstanceDescription();
+ assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
+ contained = id.getContainedInstances();
+ assertEquals("Check contained instances count", contained.length, 1);
+ assertEquals("Check that no object are created" , id.getCreatedObjects().length, 0);
+ assertEquals("Check instance name" , id.getName(), "under");
+ assertEquals("Check component type name" , id.getComponentDescription().getName(), "composite.bar.2");
+
+ context.ungetService(ref);
+ under.dispose();
+ fact2.start();
+ fact3.start();
+ }
+
+
+
+
+
+
+
+
+
+
+
+
+
+}
Added: felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/OptionalInstantiation.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/OptionalInstantiation.java?rev=615675&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/OptionalInstantiation.java (added)
+++ felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/OptionalInstantiation.java Sun Jan 27 15:21:24 2008
@@ -0,0 +1,261 @@
+package org.apache.felix.ipojo.test.composite.instantiator;
+
+import java.util.Properties;
+
+import org.apache.felix.ipojo.ComponentFactory;
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.ServiceContext;
+import org.apache.felix.ipojo.architecture.Architecture;
+import org.apache.felix.ipojo.architecture.InstanceDescription;
+import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
+import org.apache.felix.ipojo.test.composite.util.Utils;
+import org.apache.felix.ipojo.test.scenarios.service.BarService;
+import org.osgi.framework.ServiceReference;
+
+public class OptionalInstantiation extends OSGiTestCase {
+
+ private ComponentFactory bar1Factory;
+ private ComponentInstance empty;
+
+ public void setUp() {
+ bar1Factory = (ComponentFactory) Utils.getFactoryByName(context, "composite.bar.3");
+ Factory fact = Utils.getFactoryByName(context, "composite.empty");
+ Properties props = new Properties();
+ props.put("name", "empty");
+ try {
+ empty = fact.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot create the empty composite : " + e.getMessage());
+ }
+ }
+
+ public void tearDown() {
+ empty.dispose();
+ empty = null;
+ }
+
+ public void testCreation() {
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar1Factory.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
+ under.dispose();
+ }
+
+ public void testServiceAvailability() {
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar1Factory.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
+ ServiceContext sc = Utils.getServiceContext(under);
+ assertNotNull("Check service availability", sc.getServiceReference(BarService.class.getName()));
+
+ under.dispose();
+ }
+
+ public void testCreationLevel2() {
+ ServiceContext sc = Utils.getServiceContext(empty);
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar1Factory.createComponentInstance(props, sc);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
+ under.dispose();
+ }
+
+ public void testServiceAvailabilityLevel2() {
+ ServiceContext sc = Utils.getServiceContext(empty);
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar1Factory.createComponentInstance(props, sc);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
+ ServiceContext sc2 = Utils.getServiceContext(under);
+ assertNotNull("Check service availability", sc2.getServiceReference(BarService.class.getName()));
+
+ under.dispose();
+ }
+
+ public void testFactoryManagement() {
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar1Factory.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity - 1", under.getState() == ComponentInstance.VALID);
+
+ ComponentFactory fact1 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-1");
+ ComponentFactory fact2 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-2");
+ ComponentFactory fact3 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-3");
+
+ fact1.stop();
+ assertTrue("Check instance validity - 2", under.getState() == ComponentInstance.VALID);
+
+ fact2.stop();
+ assertTrue("Check instance validity - 3", under.getState() == ComponentInstance.VALID);
+
+ fact3.stop();
+ assertTrue("Check instance validity - 4", under.getState() == ComponentInstance.VALID);
+ ServiceContext sc = Utils.getServiceContext(under);
+ assertNull("Check that no Bar Service is available", sc.getServiceReference(BarService.class.getName()));
+
+ fact1.start();
+ assertTrue("Check instance validity - 5", under.getState() == ComponentInstance.VALID);
+
+ under.dispose();
+ fact2.start();
+ fact3.start();
+ }
+
+ public void testFactoryManagementLevel2() {
+ ServiceContext sc = Utils.getServiceContext(empty);
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar1Factory.createComponentInstance(props, sc);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity - 1", under.getState() == ComponentInstance.VALID);
+
+ ComponentFactory fact1 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-1");
+ ComponentFactory fact2 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-2");
+ ComponentFactory fact3 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-3");
+
+ fact1.stop();
+ assertTrue("Check instance validity - 2", under.getState() == ComponentInstance.VALID);
+
+ fact2.stop();
+ assertTrue("Check instance validity - 3", under.getState() == ComponentInstance.VALID);
+
+ fact3.stop();
+ assertTrue("Check instance validity - 4", under.getState() == ComponentInstance.VALID);
+
+ fact1.start();
+ assertTrue("Check instance validity - 5", under.getState() == ComponentInstance.VALID);
+
+ under.dispose();
+ fact2.start();
+ fact3.start();
+ }
+
+ public void testArchitecture() {
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar1Factory.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+
+ ServiceReference ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), "under");
+ assertNotNull("Check architecture availability", ref);
+ Architecture arch = (Architecture) context.getService(ref);
+ assertNotNull("Check architecture", arch);
+ InstanceDescription id = arch.getInstanceDescription();
+ assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
+ InstanceDescription[] contained = id.getContainedInstances();
+ assertNotNull("Check contained not null", contained);
+ assertEquals("Check contained instances count ("+contained.length+") - 1", contained.length, 1);
+ assertEquals("Check that no object are created" , id.getCreatedObjects().length, 0);
+ assertEquals("Check instance name" , id.getName(), "under");
+ assertEquals("Check component type name" , id.getComponentDescription().getName(), "composite.bar.3");
+
+ ComponentFactory fact1 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-1");
+ ComponentFactory fact2 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-2");
+ ComponentFactory fact3 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-3");
+
+
+ fact1.stop();
+ assertTrue("Check instance validity - 2", under.getState() == ComponentInstance.VALID);
+ ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), "under");
+ assertNotNull("Check architecture availability", ref);
+ arch = (Architecture) context.getService(ref);
+ id = arch.getInstanceDescription();
+ assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
+ contained = id.getContainedInstances();
+ assertEquals("Check contained instances count", contained.length, 1);
+ assertEquals("Check that no object are created" , id.getCreatedObjects().length, 0);
+ assertEquals("Check instance name" , id.getName(), "under");
+ assertEquals("Check component type name" , id.getComponentDescription().getName(), "composite.bar.3");
+
+ fact2.stop();
+ assertTrue("Check instance validity - 3", under.getState() == ComponentInstance.VALID);
+ ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), "under");
+ assertNotNull("Check architecture availability", ref);
+ arch = (Architecture) context.getService(ref);
+ id = arch.getInstanceDescription();
+ assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
+ contained = id.getContainedInstances();
+ assertEquals("Check contained instances count", contained.length, 1);
+ assertEquals("Check that no object are created" , id.getCreatedObjects().length, 0);
+ assertEquals("Check instance name" , id.getName(), "under");
+ assertEquals("Check component type name" , id.getComponentDescription().getName(), "composite.bar.3");
+
+ fact3.stop();
+ assertTrue("Check instance invalidity", under.getState() == ComponentInstance.VALID);
+ ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), "under");
+ assertNotNull("Check architecture availability", ref);
+ arch = (Architecture) context.getService(ref);
+ id = arch.getInstanceDescription();
+ assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
+ contained = id.getContainedInstances();
+ assertEquals("Check contained instances count", contained.length, 0);
+ assertEquals("Check that no object are created" , id.getCreatedObjects().length, 0);
+ assertEquals("Check instance name" , id.getName(), "under");
+ assertEquals("Check component type name" , id.getComponentDescription().getName(), "composite.bar.3");
+
+ fact1.start();
+ assertTrue("Check instance validity - 4", under.getState() == ComponentInstance.VALID);
+ ref = Utils.getServiceReferenceByName(context, Architecture.class.getName(), "under");
+ assertNotNull("Check architecture availability", ref);
+ arch = (Architecture) context.getService(ref);
+ id = arch.getInstanceDescription();
+ assertTrue("Check instance validity - 1", id.getState() == ComponentInstance.VALID);
+ contained = id.getContainedInstances();
+ assertEquals("Check contained instances count", contained.length, 1);
+ assertEquals("Check that no object are created" , id.getCreatedObjects().length, 0);
+ assertEquals("Check instance name" , id.getName(), "under");
+ assertEquals("Check component type name" , id.getComponentDescription().getName(), "composite.bar.3");
+
+ context.ungetService(ref);
+ under.dispose();
+ fact2.start();
+ fact3.start();
+ }
+
+
+
+
+
+
+
+
+
+
+
+}
Added: felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/OptionalMultipleInstantiation.java
URL: http://svn.apache.org/viewvc/felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/OptionalMultipleInstantiation.java?rev=615675&view=auto
==============================================================================
--- felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/OptionalMultipleInstantiation.java (added)
+++ felix/sandbox/clement/ipojo/tests/tests.composite/src/main/java/org/apache/felix/ipojo/test/composite/instantiator/OptionalMultipleInstantiation.java Sun Jan 27 15:21:24 2008
@@ -0,0 +1,185 @@
+package org.apache.felix.ipojo.test.composite.instantiator;
+
+import java.util.Properties;
+
+import org.apache.felix.ipojo.ComponentFactory;
+import org.apache.felix.ipojo.ComponentInstance;
+import org.apache.felix.ipojo.Factory;
+import org.apache.felix.ipojo.ServiceContext;
+import org.apache.felix.ipojo.junit4osgi.OSGiTestCase;
+import org.apache.felix.ipojo.test.composite.util.Utils;
+import org.apache.felix.ipojo.test.scenarios.service.BarService;
+
+public class OptionalMultipleInstantiation extends OSGiTestCase {
+
+ private ComponentFactory bar2Factory;
+ private ComponentInstance empty;
+
+ public void setUp() {
+ bar2Factory = (ComponentFactory) Utils.getFactoryByName(context, "composite.bar.4");
+ assertNotNull("Check bar2factory availability", bar2Factory);
+
+ Factory fact = Utils.getFactoryByName(context, "composite.empty");
+ Properties props = new Properties();
+ props.put("name", "empty");
+ try {
+ empty = fact.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot create the empty composite : " + e.getMessage());
+ }
+ }
+
+ public void tearDown() {
+ empty.dispose();
+ empty = null;
+ }
+
+ public void testCreation() {
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar2Factory.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
+ under.dispose();
+ }
+
+ public void testServiceAvailability() {
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar2Factory.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
+ ServiceContext sc = Utils.getServiceContext(under);
+ assertNotNull("Check service availability", sc.getServiceReference(BarService.class.getName()));
+ int count = Utils.getServiceReferences(sc, BarService.class.getName(), null).length;
+ assertEquals("Check service provider number : " + count, count, 3);
+
+ under.dispose();
+ }
+
+ public void testCreationLevel2() {
+ ServiceContext sc = Utils.getServiceContext(empty);
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar2Factory.createComponentInstance(props, sc);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
+ under.dispose();
+ }
+
+ public void testServiceAvailabilityLevel2() {
+ ServiceContext sc = Utils.getServiceContext(empty);
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar2Factory.createComponentInstance(props, sc);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity", under.getState() == ComponentInstance.VALID);
+ ServiceContext sc2 = Utils.getServiceContext(under);
+ assertNotNull("Check service availability", sc2.getServiceReference(BarService.class.getName()));
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc2, BarService.class.getName(), null).length, 3);
+
+ under.dispose();
+ }
+
+ public void testFactoryManagement() {
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar2Factory.createComponentInstance(props);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ ServiceContext sc = Utils.getServiceContext(under);
+ assertTrue("Check instance validity - 1", under.getState() == ComponentInstance.VALID);
+
+ ComponentFactory fact1 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-1");
+ ComponentFactory fact2 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-2");
+ ComponentFactory fact3 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-3");
+
+ fact1.stop();
+ assertTrue("Check instance validity - 2", under.getState() == ComponentInstance.VALID);
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc, BarService.class.getName(), null).length, 2);
+
+ fact2.stop();
+ assertTrue("Check instance validity - 3", under.getState() == ComponentInstance.VALID);
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc, BarService.class.getName(), null).length, 1);
+
+ fact3.stop();
+ assertTrue("Check instance validity - 4", under.getState() == ComponentInstance.VALID);
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc, BarService.class.getName(), null).length, 0);
+
+ fact1.start();
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc, BarService.class.getName(), null).length, 1);
+ assertTrue("Check instance validity - 5", under.getState() == ComponentInstance.VALID);
+
+ under.dispose();
+ fact2.start();
+ fact3.start();
+ }
+
+ public void testFactoryManagementLevel2() {
+ ServiceContext sc = Utils.getServiceContext(empty);
+ Properties props = new Properties();
+ props.put("name", "under");
+ ComponentInstance under = null;
+ try {
+ under = bar2Factory.createComponentInstance(props, sc);
+ } catch(Exception e) {
+ fail("Cannot instantiate under : " + e.getMessage());
+ }
+ assertTrue("Check instance validity - 1", under.getState() == ComponentInstance.VALID);
+ ServiceContext sc2 = Utils.getServiceContext(under);
+
+ ComponentFactory fact1 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-1");
+ ComponentFactory fact2 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-2");
+ ComponentFactory fact3 = (ComponentFactory) Utils.getFactoryByName(context, "FooBarProviderType-3");
+
+ fact1.stop();
+ assertTrue("Check instance validity - 2", under.getState() == ComponentInstance.VALID);
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc2, BarService.class.getName(), null).length, 2);
+
+ fact2.stop();
+ assertTrue("Check instance validity - 3", under.getState() == ComponentInstance.VALID);
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc2, BarService.class.getName(), null).length, 1);
+
+ fact3.stop();
+ assertTrue("Check instance validity - 4", under.getState() == ComponentInstance.VALID);
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc2, BarService.class.getName(), null).length, 0);
+
+ fact1.start();
+ assertTrue("Check instance validity - 5", under.getState() == ComponentInstance.VALID);
+ assertEquals("Check service provider number", Utils.getServiceReferences(sc2, BarService.class.getName(), null).length, 1);
+
+ under.dispose();
+ fact2.start();
+ fact3.start();
+ }
+
+
+
+
+
+
+
+
+
+
+
+}