now distinguishing between provided and required service and added
[utils] / system / general / src / test / java / org / wamblee / system / SystemAssemblerTest.java
index 60d1b587007387dc710ef1e56cdfe0b32b8f04b7..2dba056e63197879c35e4e9e30a96c70b0516843 100644 (file)
@@ -1,36 +1,92 @@
 package org.wamblee.system;
 
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import org.wamblee.test.AssertionUtils;
+
 import junit.framework.TestCase;
 
 public class SystemAssemblerTest extends TestCase {
-       
-       private ServiceRegistry _registry; 
-       
+
+       private ServiceRegistry _registry;
+
        @Override
        protected void setUp() throws Exception {
                super.setUp();
-               _registry = new DefaultServiceRegistry(); 
+               _registry = new DefaultServiceRegistry();
+       }
+
+       private static class MyMultiple implements Serializable, Runnable {
+               @Override
+               public void run() {
+                       // Empty
+               }
+       }
+
+       public void testFilterProvided() {
+               RequiredServiceDescriptor req1 = new DefaultRequiredServiceDescriptor(
+                               "name", Runnable.class);
+               RequiredServiceDescriptor req2 = new DefaultRequiredServiceDescriptor(
+                               "name", Serializable.class);
+               ProvidedServiceDescriptor prov1 = new DefaultProvidedServiceDescriptor(
+                               "name", Runnable.class);
+               ProvidedServiceDescriptor prov2 = new DefaultProvidedServiceDescriptor(
+                               "name", Serializable.class);
+               ProvidedServiceDescriptor prov3 = new DefaultProvidedServiceDescriptor(
+                               "name", MyMultiple.class);
+
+               AssertionUtils.assertEquals(new RequiredServiceDescriptor[] { req1 },
+                               SystemAssembler.filterRequiredServices(prov1, Arrays
+                                               .asList(new RequiredServiceDescriptor[] { req1 })));
+               AssertionUtils.assertEquals(new RequiredServiceDescriptor[] { req1 },
+                               SystemAssembler
+                                               .filterRequiredServices(prov1, Arrays
+                                                               .asList(new RequiredServiceDescriptor[] { req1,
+                                                                               req2 })));
+               AssertionUtils.assertEquals(new RequiredServiceDescriptor[] { req1,
+                               req2 }, SystemAssembler.filterRequiredServices(prov3, Arrays
+                               .asList(new RequiredServiceDescriptor[] { req1, req2 })));
+
+               AssertionUtils.assertEquals(new ProvidedServiceDescriptor[] { prov1 },
+                               SystemAssembler.filterProvidedServices(req1, Arrays
+                                               .asList(new ProvidedServiceDescriptor[] { prov1 })));
+               AssertionUtils.assertEquals(new ProvidedServiceDescriptor[] { prov1 },
+                               SystemAssembler.filterProvidedServices(req1,
+                                               Arrays.asList(new ProvidedServiceDescriptor[] { prov1,
+                                                               prov2 })));
+               AssertionUtils.assertEquals(new ProvidedServiceDescriptor[] { prov1,
+                               prov3 }, SystemAssembler.filterProvidedServices(req1, Arrays
+                               .asList(new ProvidedServiceDescriptor[] { prov1, prov3 })));
        }
 
        public void testEnvironmentApplication() {
-               SubSystem environment = new Environment();
-               SubSystem application = new Application();
+               SubSystem environment = new Environment(_registry);
+               SubSystem application = new Application(_registry);
                SystemAssembler assembler = new SystemAssembler(new SubSystem[] {
-                               environment, application }, new ServiceDescriptor[0]);
+                               environment, application }, new ProvidedServiceDescriptor[0]);
                assembler.start(_registry, new Service[0]);
                Service[] envServices = environment.getRunningServices();
                assertEquals(2, envServices.length);
                Service[] appServices = environment.getRunningServices();
                assertEquals(2, appServices.length);
                assertEquals(2, _registry.listAllServices().length);
+
+               environment.stop();
+               assertEquals(0, _registry.listAllServices().length);
+
+               application.stop();
+               assertEquals(0, _registry.listAllServices().length);
        }
 
        public void testApplicationEnvironment() {
                try {
-                       SubSystem environment = new Environment();
-                       SubSystem application = new Application();
+                       SubSystem environment = new Environment(_registry);
+                       SubSystem application = new Application(_registry);
                        SystemAssembler assembler = new SystemAssembler(new SubSystem[] {
-                                       application, environment }, new ServiceDescriptor[0]);
+                                       application, environment },
+                                       new ProvidedServiceDescriptor[0]);
                        assembler.start(_registry, new Service[0]);
                } catch (SystemAssemblyException e) {
                        // e.printStackTrace();
@@ -40,12 +96,12 @@ public class SystemAssemblerTest extends TestCase {
        }
 
        public void testComposite() {
-               SubSystem environment = new Environment();
-               SubSystem application = new Application();
-               CompositeSystem system = new CompositeSystem("all", new SubSystem[] {
-                               environment, application }, new ServiceDescriptor[0],
-                               new ServiceDescriptor[0]);
-               system.start("root", _registry, new Service[0]);
+               SubSystem environment = new Environment(_registry);
+               SubSystem application = new Application(_registry);
+               CompositeSystem system = new CompositeSystem("all", _registry,
+                               new SubSystem[] { environment, application },
+                               new ServiceDescriptor[0], new ServiceDescriptor[0]);
+               system.start("root", new Service[0]);
                ServiceDescriptor[] required = system.getRequiredServices();
                assertEquals(0, required.length);
                ServiceDescriptor[] provided = system.getProvidedServices();
@@ -54,12 +110,15 @@ public class SystemAssemblerTest extends TestCase {
 
        public void testCompositeWithWrongProvidedInfo() {
                try {
-                       SubSystem environment = new Environment();
-                       SubSystem application = new Application();
-                       CompositeSystem system = new CompositeSystem("all",
+                       SubSystem environment = new Environment(_registry);
+                       SubSystem application = new Application(_registry);
+                       CompositeSystem system = new CompositeSystem(
+                                       "all",
+                                       _registry,
                                        new SubSystem[] { environment, application },
-                                       new ServiceDescriptor[] { new DefaultServiceDescriptor(
-                                                       String.class) }, new ServiceDescriptor[0]);
+                                       new ServiceDescriptor[] { new DefaultProvidedServiceDescriptor(
+                                                       "string", String.class) },
+                                       new DefaultRequiredServiceDescriptor[0]);
                } catch (SystemAssemblyException e) {
                        return;
                }
@@ -67,13 +126,14 @@ public class SystemAssemblerTest extends TestCase {
        }
 
        public void testCompositeWithSuperfluousRequiredInfo() {
-               SubSystem environment = new Environment();
-               SubSystem application = new Application();
-               CompositeSystem system = new CompositeSystem("all", new SubSystem[] {
-                               environment, application }, new ServiceDescriptor[0],
-                               new ServiceDescriptor[] { new DefaultServiceDescriptor(
-                                               String.class) });
-               system.start("root", _registry, new Service[0]);
+               SubSystem environment = new Environment(_registry);
+               SubSystem application = new Application(_registry);
+               CompositeSystem system = new CompositeSystem("all", _registry,
+                               new SubSystem[] { environment, application },
+                               new ServiceDescriptor[0],
+                               new ServiceDescriptor[] { new DefaultRequiredServiceDescriptor(
+                                               "string", String.class) });
+               system.start("root", new Service[0]);
                ServiceDescriptor[] required = system.getRequiredServices();
                assertEquals(1, required.length);
                ServiceDescriptor[] provided = system.getProvidedServices();
@@ -82,12 +142,12 @@ public class SystemAssemblerTest extends TestCase {
 
        public void testCompositeWithExternalDependencesNotProvided() {
                try {
-                       SubSystem environment = new Environment();
-                       SubSystem application = new Application();
-                       CompositeSystem system = new CompositeSystem("all",
+                       SubSystem environment = new Environment(_registry);
+                       SubSystem application = new Application(_registry);
+                       CompositeSystem system = new CompositeSystem("all", _registry,
                                        new SubSystem[] { application }, new ServiceDescriptor[0],
                                        application.getRequiredServices());
-                       system.start("root", _registry, new Service[0]);
+                       system.start("root", new Service[0]);
                } catch (SystemAssemblyException e) {
                        return;
                }
@@ -97,13 +157,13 @@ public class SystemAssemblerTest extends TestCase {
 
        public void testCompositeWithExternalDependencesProvided() {
 
-               SubSystem environment = new Environment();
-               SubSystem application = new Application();
-               CompositeSystem system = new CompositeSystem("all",
+               SubSystem environment = new Environment(_registry);
+               SubSystem application = new Application(_registry);
+               CompositeSystem system = new CompositeSystem("all", _registry,
                                new SubSystem[] { application }, new ServiceDescriptor[0],
                                application.getRequiredServices());
-               Service[] envServices = environment.start("env", _registry,new Service[0]);
-               system.start("root", _registry, envServices);
+               Service[] envServices = environment.start("env", new Service[0]);
+               system.start("root", envServices);
                ServiceDescriptor[] required = system.getRequiredServices();
                assertEquals(2, required.length);
                ServiceDescriptor[] provided = system.getProvidedServices();
@@ -111,4 +171,19 @@ public class SystemAssemblerTest extends TestCase {
 
        }
 
+       public void testAmbiguousInterfaces() {
+               try {
+                       SubSystem environment1 = new Environment(_registry);
+                       SubSystem environment2 = new Environment(_registry);
+                       SubSystem application = new Application(_registry);
+                       SystemAssembler assembler = new SystemAssembler(new SubSystem[] {
+                                       environment1, environment2, application },
+                                       new ProvidedServiceDescriptor[0]);
+                       assembler.start(_registry, new Service[0]);
+               } catch (SystemAssemblyException e) {
+                       return;
+               }
+               fail();
+       }
+
 }