Added missing test case.
[utils] / system / general / src / test / java / org / wamblee / system / SystemAssemblerTest.java
index 70558250b547d1b4bfdc92b7c56c0e61cbdbada2..df08998e4b7351b2b1cd82963da44efa32b9b4e5 100644 (file)
@@ -1,42 +1,94 @@
 package org.wamblee.system;
 
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import org.wamblee.system.Component.Status;
+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() {
+               RequiredInterfaceDescriptor req1 = new DefaultRequiredInterfaceDescriptor(
+                               "name", Runnable.class);
+               RequiredInterfaceDescriptor req2 = new DefaultRequiredInterfaceDescriptor(
+                               "name", Serializable.class);
+               ProvidedInterfaceDescriptor prov1 = new DefaultProvidedInterfaceDescriptor(
+                               "name", Runnable.class);
+               ProvidedInterfaceDescriptor prov2 = new DefaultProvidedInterfaceDescriptor(
+                               "name", Serializable.class);
+               ProvidedInterfaceDescriptor prov3 = new DefaultProvidedInterfaceDescriptor(
+                               "name", MyMultiple.class);
+
+               AssertionUtils.assertEquals(new RequiredInterfaceDescriptor[] { req1 },
+                               SystemAssembler.filterRequiredServices(prov1, Arrays
+                                               .asList(new RequiredInterfaceDescriptor[] { req1 })));
+               AssertionUtils.assertEquals(new RequiredInterfaceDescriptor[] { req1 },
+                               SystemAssembler.filterRequiredServices(prov1,
+                                               Arrays.asList(new RequiredInterfaceDescriptor[] { req1,
+                                                               req2 })));
+               AssertionUtils.assertEquals(new RequiredInterfaceDescriptor[] { req1,
+                               req2 }, SystemAssembler.filterRequiredServices(prov3, Arrays
+                               .asList(new RequiredInterfaceDescriptor[] { req1, req2 })));
+
+               AssertionUtils.assertEquals(
+                               new ProvidedInterfaceDescriptor[] { prov1 },
+                               SystemAssembler.filterProvidedServices(req1, Arrays
+                                               .asList(new ProvidedInterfaceDescriptor[] { prov1 })));
+               AssertionUtils.assertEquals(
+                               new ProvidedInterfaceDescriptor[] { prov1 }, SystemAssembler
+                                               .filterProvidedServices(req1, Arrays
+                                                               .asList(new ProvidedInterfaceDescriptor[] {
+                                                                               prov1, prov2 })));
+               AssertionUtils.assertEquals(new ProvidedInterfaceDescriptor[] { prov1,
+                               prov3 }, SystemAssembler.filterProvidedServices(req1, Arrays
+                               .asList(new ProvidedInterfaceDescriptor[] { prov1, prov3 })));
        }
 
        public void testEnvironmentApplication() {
-               SubSystem environment = new Environment();
-               SubSystem application = new Application();
-               SystemAssembler assembler = new SystemAssembler(new SubSystem[] {
-                               environment, application }, new ServiceDescriptor[0]);
+               Component environment = new Environment(_registry);
+               Component application = new Application(_registry);
+               SystemAssembler assembler = new SystemAssembler(new Component[] {
+                               environment, application }, new ProvidedInterfaceDescriptor[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("", _registry);
+
+               environment.stop();
                assertEquals(0, _registry.listAllServices().length);
-               
-               application.stop("", _registry); 
+
+               application.stop();
                assertEquals(0, _registry.listAllServices().length);
        }
 
        public void testApplicationEnvironment() {
                try {
-                       SubSystem environment = new Environment();
-                       SubSystem application = new Application();
-                       SystemAssembler assembler = new SystemAssembler(new SubSystem[] {
-                                       application, environment }, new ServiceDescriptor[0]);
+                       Component environment = new Environment(_registry);
+                       Component application = new Application(_registry);
+                       SystemAssembler assembler = new SystemAssembler(new Component[] {
+                                       application, environment },
+                                       new ProvidedInterfaceDescriptor[0]);
                        assembler.start(_registry, new Service[0]);
                } catch (SystemAssemblyException e) {
                        // e.printStackTrace();
@@ -46,26 +98,40 @@ 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]);
-               ServiceDescriptor[] required = system.getRequiredServices();
+               Component environment = new Environment(_registry);
+               Component application = new Application(_registry);
+               assertEquals(Status.NOT_STARTED, environment.getStatus());
+               assertEquals(Status.NOT_STARTED, application.getStatus());
+               Container system = new Container("all", _registry, new Component[] {
+                               environment, application }, new ProvidedInterfaceDescriptor[0],
+                               new RequiredInterfaceDescriptor[0]);
+               assertEquals(Status.NOT_STARTED, system.getStatus());
+               system.start("root", new Service[0]);
+               InterfaceDescriptor[] required = system.getRequiredServices();
                assertEquals(0, required.length);
-               ServiceDescriptor[] provided = system.getProvidedServices();
+               InterfaceDescriptor[] provided = system.getProvidedServices();
                assertEquals(0, provided.length);
+               assertEquals(Status.RUNNING, environment.getStatus());
+               assertEquals(Status.RUNNING, application.getStatus());
+               assertEquals(Status.RUNNING, system.getStatus());
+
+               system.stop();
+               assertEquals(Status.STOPPED, environment.getStatus());
+               assertEquals(Status.STOPPED, application.getStatus());
+               assertEquals(Status.STOPPED, system.getStatus());
        }
 
        public void testCompositeWithWrongProvidedInfo() {
                try {
-                       SubSystem environment = new Environment();
-                       SubSystem application = new Application();
-                       CompositeSystem system = new CompositeSystem("all",
-                                       new SubSystem[] { environment, application },
-                                       new ServiceDescriptor[] { new DefaultServiceDescriptor(
-                                                       String.class) }, new ServiceDescriptor[0]);
+                       Component environment = new Environment(_registry);
+                       Component application = new Application(_registry);
+                       Container system = new Container(
+                                       "all",
+                                       _registry,
+                                       new Component[] { environment, application },
+                                       new ProvidedInterfaceDescriptor[] { new DefaultProvidedInterfaceDescriptor(
+                                                       "string", String.class) },
+                                       new DefaultRequiredInterfaceDescriptor[0]);
                } catch (SystemAssemblyException e) {
                        return;
                }
@@ -73,27 +139,31 @@ 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]);
-               ServiceDescriptor[] required = system.getRequiredServices();
+               Component environment = new Environment(_registry);
+               Component application = new Application(_registry);
+               Container system = new Container(
+                               "all",
+                               _registry,
+                               new Component[] { environment, application },
+                               new ProvidedInterfaceDescriptor[0],
+                               new RequiredInterfaceDescriptor[] { new DefaultRequiredInterfaceDescriptor(
+                                               "string", String.class) });
+               system.start("root", new Service[0]);
+               RequiredInterfaceDescriptor[] required = system.getRequiredServices();
                assertEquals(1, required.length);
-               ServiceDescriptor[] provided = system.getProvidedServices();
+               ProvidedInterfaceDescriptor[] provided = system.getProvidedServices();
                assertEquals(0, provided.length);
        }
 
        public void testCompositeWithExternalDependencesNotProvided() {
                try {
-                       SubSystem environment = new Environment();
-                       SubSystem application = new Application();
-                       CompositeSystem system = new CompositeSystem("all",
-                                       new SubSystem[] { application }, new ServiceDescriptor[0],
-                                       application.getRequiredServices());
-                       system.start("root", _registry, new Service[0]);
+                       Component environment = new Environment(_registry);
+                       Component application = new Application(_registry);
+                       Container system = new Container("all", _registry,
+                                       new Component[] { application },
+                                       new ProvidedInterfaceDescriptor[0], application
+                                                       .getRequiredServices());
+                       system.start("root", new Service[0]);
                } catch (SystemAssemblyException e) {
                        return;
                }
@@ -103,18 +173,49 @@ public class SystemAssemblerTest extends TestCase {
 
        public void testCompositeWithExternalDependencesProvided() {
 
-               SubSystem environment = new Environment();
-               SubSystem application = new Application();
-               CompositeSystem system = new CompositeSystem("all",
-                               new SubSystem[] { application }, new ServiceDescriptor[0],
-                               application.getRequiredServices());
-               Service[] envServices = environment.start("env", _registry,new Service[0]);
-               system.start("root", _registry, envServices);
-               ServiceDescriptor[] required = system.getRequiredServices();
+               Component environment = new Environment(_registry);
+               Component application = new Application(_registry);
+               Container system = new Container("all", _registry,
+                               new Component[] { application },
+                               new ProvidedInterfaceDescriptor[0], application
+                                               .getRequiredServices());
+               Service[] envServices = environment.start("env", new Service[0]);
+               system.start("root", envServices);
+               RequiredInterfaceDescriptor[] required = system.getRequiredServices();
                assertEquals(2, required.length);
-               ServiceDescriptor[] provided = system.getProvidedServices();
+               ProvidedInterfaceDescriptor[] provided = system.getProvidedServices();
                assertEquals(0, provided.length);
 
        }
 
+       public void testAmbiguousInterfaces() {
+               try {
+                       Component environment1 = new Environment(_registry);
+                       Component environment2 = new Environment(_registry);
+                       Component application = new Application(_registry);
+                       SystemAssembler assembler = new SystemAssembler(new Component[] {
+                                       environment1, environment2, application },
+                                       new ProvidedInterfaceDescriptor[0]);
+                       assembler.start(_registry, new Service[0]);
+
+               } catch (SystemAssemblyException e) {
+                       return;
+               }
+               fail();
+       }
+
+       public void testIncompleteRequirements() {
+               try {
+                       Component application = new Application(_registry);
+                       Container system = new Container("all", _registry,
+                                       new Component[] { application },
+                                       new ProvidedInterfaceDescriptor[0],
+                                       new RequiredInterfaceDescriptor[0]);
+                       system.start("root", new Service[0]);
+               } catch (SystemAssemblyException e) {
+                       return;
+               }
+               fail();
+       }
+
 }