Less duplication of validation and start. Validation should be a dry-run
[utils] / system / general / src / test / java / org / wamblee / system / core / ContainerTest.java
index 74eb43101d0fa461b30e8bd4b31dcf25fd68f479..90723ed1c93c4f6271448cf19622ed0eb8166dbe 100644 (file)
@@ -15,6 +15,7 @@
  */
 package org.wamblee.system.core;
 
+import java.io.IOException;
 import java.io.Serializable;
 import java.util.Arrays;
 
@@ -83,7 +84,24 @@ public class ContainerTest extends TestCase {
                Container container = new Container("root", new Component[] {
                                environment, application }, new ProvidedInterface[0],
                                new RequiredInterface[0]);
+               Scope scope = container.start();
+               assertTrue(container.isSealed());
+               AssertionUtils.assertEquals(new String[] { "start.environment",
+                               "start.application" }, _tracker.getEvents(
+                               Thread.currentThread()).toArray(new String[0]));
+               assertEquals(0, scope.getProvidedInterfaces().length);
+               
+               assertEquals(environment.getString(), application.getString());
+               assertEquals(environment.getInteger(), application.getInteger());
 
+       }
+       
+       public void testEnvironmentApplicationSimpleConstructor() {
+               Environment environment = new Environment(_tracker);
+               Application application = new Application(_tracker);
+               Container container = new Container("root").
+                 addComponent(environment).addComponent(application);
+               
                Scope scope = container.start();
                AssertionUtils.assertEquals(new String[] { "start.environment",
                                "start.application" }, _tracker.getEvents(
@@ -145,6 +163,7 @@ public class ContainerTest extends TestCase {
                                        new ProvidedInterface[] { new DefaultProvidedInterface(
                                                        "float", Float.class) },
                                        new DefaultRequiredInterface[0]);
+                       system.validate(); 
                } catch (SystemAssemblyException e) {
                        return;
                }
@@ -196,6 +215,30 @@ public class ContainerTest extends TestCase {
                fail();
 
        }
+       
+       public void testDuplicateComponent() { 
+           try { 
+               Component comp1 = new Application(); 
+               Component comp2 = new Application();
+               Container system = new Container("top");
+               system.addComponent(comp1).addComponent(comp2);
+           } catch (SystemAssemblyException e) { 
+               return; 
+           }
+           fail();
+       }
+       
+
+       public void testInconsistentHierarchy() { 
+           try {
+               Component comp = new Application(); 
+               Container system = new Container("top").addComponent(comp);
+               Container system2 = new Container("top2").addComponent(comp);
+           } catch (SystemAssemblyException e) {
+               return;
+           }
+           fail();
+       }
 
        public void testCompositeWithExternalDependencesProvided() {
 
@@ -290,7 +333,7 @@ public class ContainerTest extends TestCase {
                
                // application 2 will throw an exception while starting
                Application application2 = control.createMock(Application.class,
-                               new ConstructorArgs(Application.class.getConstructor()),
+                               new ConstructorArgs(Application.class.getConstructor(String.class), "application2"),
                                Application.class.getDeclaredMethod("doStart", Scope.class));
 
                application2.doStart(EasyMockMatchers.anyObject(Scope.class));
@@ -363,4 +406,32 @@ public class ContainerTest extends TestCase {
            assertSame(env.getProvidedInterfaces()[0], application.getRequiredInterfaces()[0].getProvider());
            assertSame(env.getProvidedInterfaces()[1], application.getRequiredInterfaces()[1].getProvider());   
        }
+       
+       public void testSealed() { 
+               final Container container = new Container("xx"); 
+               assertFalse(container.isSealed()); 
+               container.start();
+               assertTrue(container.isSealed());
+               
+               AssertionUtils.assertException(new AssertionUtils.ErroneousCode() { 
+                       @Override
+                       public void run() throws Exception {
+                               container.addComponent(new Application());      
+                       }
+               }, SystemAssemblyException.class); 
+               
+               AssertionUtils.assertException(new AssertionUtils.ErroneousCode() { 
+                       @Override
+                       public void run() throws Exception {
+                               container.addProvidedInterface(new DefaultProvidedInterface("xx", String.class));       
+                       }
+               }, SystemAssemblyException.class); 
+               
+               AssertionUtils.assertException(new AssertionUtils.ErroneousCode() { 
+                       @Override
+                       public void run() throws Exception {
+                               container.addRequiredInterface(new DefaultRequiredInterface("xx", String.class));       
+                       }
+               }, SystemAssemblyException.class); 
+       }
 }