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 { @Override protected void setUp() throws Exception { super.setUp(); } private static class MyMultiple implements Serializable, Runnable { @Override public void run() { // Empty } } public void testFilterProvided() { RequiredInterface req1 = new DefaultRequiredInterfaceDescriptor("name", Runnable.class); RequiredInterface req2 = new DefaultRequiredInterfaceDescriptor("name", Serializable.class); ProvidedInterface prov1 = new DefaultProvidedInterfaceDescriptor( "name", Runnable.class); ProvidedInterface prov2 = new DefaultProvidedInterfaceDescriptor( "name", Serializable.class); ProvidedInterface prov3 = new DefaultProvidedInterfaceDescriptor( "name", MyMultiple.class); AssertionUtils.assertEquals(new RequiredInterface[] { req1 }, SystemAssembler.filterRequiredServices(prov1, Arrays .asList(new RequiredInterface[] { req1 }))); AssertionUtils.assertEquals(new RequiredInterface[] { req1 }, SystemAssembler.filterRequiredServices(prov1, Arrays .asList(new RequiredInterface[] { req1, req2 }))); AssertionUtils.assertEquals(new RequiredInterface[] { req1, req2 }, SystemAssembler.filterRequiredServices(prov3, Arrays .asList(new RequiredInterface[] { req1, req2 }))); AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 }, SystemAssembler.filterProvidedServices(req1, Arrays .asList(new ProvidedInterface[] { prov1 }))); AssertionUtils.assertEquals(new ProvidedInterface[] { prov1 }, SystemAssembler.filterProvidedServices(req1, Arrays .asList(new ProvidedInterface[] { prov1, prov2 }))); AssertionUtils.assertEquals(new ProvidedInterface[] { prov1, prov3 }, SystemAssembler.filterProvidedServices(req1, Arrays .asList(new ProvidedInterface[] { prov1, prov3 }))); } public void testEnvironmentApplication() { Component environment = new Environment(); Component application = new Application(); SystemAssembler assembler = new SystemAssembler(new Component[] { environment, application }, new ProvidedInterface[0]); assembler.start(); ProvidedInterface[] envServices = environment.getRunningServices(); assertEquals(2, envServices.length); ProvidedInterface[] appServices = environment.getRunningServices(); assertEquals(2, appServices.length); // TODO test stopping!!!!!! environment.stop(); application.stop(); } public void testApplicationEnvironment() { try { Component environment = new Environment(); Component application = new Application(); SystemAssembler assembler = new SystemAssembler(new Component[] { application, environment }, new ProvidedInterface[0]); assembler.start(); } catch (SystemAssemblyException e) { // e.printStackTrace(); return; } fail(); } public void testComposite() { Component environment = new Environment(); Component application = new Application(); assertEquals(Status.NOT_STARTED, environment.getStatus()); assertEquals(Status.NOT_STARTED, application.getStatus()); Container system = new Container("all", new Component[] { environment, application }, new ProvidedInterface[0], new RequiredInterface[0]); assertEquals(Status.NOT_STARTED, system.getStatus()); system.start("root"); RequiredInterface[] required = system.getRequiredServices(); assertEquals(0, required.length); ProvidedInterface[] 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 { Component environment = new Environment(); Component application = new Application(); Container system = new Container( "all", new Component[] { environment, application }, new ProvidedInterface[] { new DefaultProvidedInterfaceDescriptor( "string", String.class) }, new DefaultRequiredInterfaceDescriptor[0]); } catch (SystemAssemblyException e) { return; } fail(); } public void testCompositeRequiredInterfaceNotProvided() { try { Component environment = new Environment(); Component application = new Application(); Container system = new Container( "all", new Component[] { environment, application }, new ProvidedInterface[0], new RequiredInterface[] { new DefaultRequiredInterfaceDescriptor( "string", String.class) }); system.start("root"); } catch (SystemAssemblyException e) { return; } fail(); } public void testCompositeWithSuperfluousRequiredInfo() { Component environment = new Environment(); Component application = new Application(); Container system = new Container( "all", new Component[] { environment, application }, new ProvidedInterface[0], new RequiredInterface[] { new DefaultRequiredInterfaceDescriptor( "string", String.class) }); system.getRequiredServices()[0].setProvider( new DefaultProvidedInterfaceDescriptor("hallo", String.class)); system.start("root"); RequiredInterface[] required = system.getRequiredServices(); assertEquals(1, required.length); ProvidedInterface[] provided = system.getProvidedServices(); assertEquals(0, provided.length); } public void testCompositeWithExternalDependencesNotProvided() { try { Component environment = new Environment(); Component application = new Application(); Container system = new Container("all", new Component[] { application }, new ProvidedInterface[0], application.getRequiredServices()); system.start("root"); } catch (SystemAssemblyException e) { return; } fail(); } public void testCompositeWithExternalDependencesProvided() { Component environment = new Environment(); Component application = new Application(); Container system = new Container("all", new Component[] { application }, new ProvidedInterface[0], application.getRequiredServices()); environment.start("env"); system.getRequiredServices()[0].setProvider(environment.getProvidedServices()[0]); system.getRequiredServices()[1].setProvider(environment.getProvidedServices()[1]); system.start("root"); RequiredInterface[] required = system.getRequiredServices(); assertEquals(2, required.length); ProvidedInterface[] provided = system.getProvidedServices(); assertEquals(0, provided.length); } public void testAmbiguousInterfaces() { try { Component environment1 = new Environment(); Component environment2 = new Environment(); Component application = new Application(); SystemAssembler assembler = new SystemAssembler(new Component[] { environment1, environment2, application }, new ProvidedInterface[0]); assembler.start(); } catch (SystemAssemblyException e) { return; } fail(); } public void testIncompleteRequirements() { try { Component application = new Application(); Container system = new Container("all", new Component[] { application }, new ProvidedInterface[0], new RequiredInterface[0]); system.start("root"); } catch (SystemAssemblyException e) { return; } fail(); } }