From: erik Date: Mon, 24 Mar 2008 15:19:19 +0000 (+0000) Subject: added spring subsystem implementation. X-Git-Tag: wamblee-utils-0.2@603~224 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=9d6684855a0198c1af8bffcccdcee8be019a0582;p=utils added spring subsystem implementation. --- diff --git a/trunk/.classpath b/trunk/.classpath index 216ffd34..709e35be 100644 --- a/trunk/.classpath +++ b/trunk/.classpath @@ -4,8 +4,11 @@ - - + + + + + diff --git a/trunk/pom.xml b/trunk/pom.xml index dd95286d..092ca43e 100644 --- a/trunk/pom.xml +++ b/trunk/pom.xml @@ -334,9 +334,22 @@ + + + org.codehaus.mojo + cobertura-maven-plugin + + + + clean + + + + + - + diff --git a/trunk/support/src/test/java/org/wamblee/test/SpringTestCase.java b/trunk/support/src/test/java/org/wamblee/test/SpringTestCase.java index 29334e00..556bac2a 100644 --- a/trunk/support/src/test/java/org/wamblee/test/SpringTestCase.java +++ b/trunk/support/src/test/java/org/wamblee/test/SpringTestCase.java @@ -140,7 +140,6 @@ public class SpringTestCase extends MockObjectTestCase { if (_context == null) { _context = new ClassPathXmlApplicationContext( (String[]) _configLocations, _parentContext); - assertNotNull(_context); } return _context; } diff --git a/trunk/system/general/src/main/java/org/wamblee/system/AbstractSubSystem.java b/trunk/system/general/src/main/java/org/wamblee/system/AbstractSubSystem.java index 7a6724f3..6ae6f3cc 100644 --- a/trunk/system/general/src/main/java/org/wamblee/system/AbstractSubSystem.java +++ b/trunk/system/general/src/main/java/org/wamblee/system/AbstractSubSystem.java @@ -17,6 +17,7 @@ public abstract class AbstractSubSystem implements SubSystem { private static final Log LOG = LogFactory.getLog(AbstractSubSystem.class); private String _name; + private ServiceRegistry _registry; private List _provided; private List _required; private Map _running; @@ -33,9 +34,10 @@ public abstract class AbstractSubSystem implements SubSystem { * @param aRequired * Required services. */ - protected AbstractSubSystem(String aName, ServiceDescriptor[] aProvided, + protected AbstractSubSystem(String aName, ServiceRegistry aRegistry, ServiceDescriptor[] aProvided, ServiceDescriptor[] aRequired) { _name = aName; + _registry = aRegistry; _provided = new ArrayList(); _provided.addAll(Arrays.asList(aProvided)); _required = new ArrayList(); @@ -47,6 +49,10 @@ public abstract class AbstractSubSystem implements SubSystem { public final String getName() { return _name; } + + public ServiceRegistry getRegistry() { + return _registry; + } @Override public final ServiceDescriptor[] getProvidedServices() { @@ -59,25 +65,23 @@ public abstract class AbstractSubSystem implements SubSystem { } @Override - public final Service[] start(String aContext, ServiceRegistry aRegistry, + public final Service[] start(String aContext, Service[] aRequiredServices) { LOG.info("Initializing '" + aContext + "." + _name + "' with " + Arrays.asList(aRequiredServices)); - doStart(aContext + "." + getName(), aRegistry, aRequiredServices); + doStart(aContext + "." + getName(), aRequiredServices); return _running.values().toArray(new Service[0]); } /** * Must be implemented for initializing the subsystem. The implementation * must call {@link #addService(Service)} for each service that is started. - * - * @param aRegistry Service registry. + * * @param aRequiredServices * Services that are already running from other subsystems that * may be used. */ protected abstract void doStart(String aContext, - ServiceRegistry aRegistry, Service[] aRequiredServices); /** @@ -88,10 +92,9 @@ public abstract class AbstractSubSystem implements SubSystem { * Service. */ protected final void addService(String aContext, - ServiceRegistry aRegistry, ServiceDescriptor aDescriptor, Object aService) { LOG.info(aContext + ": service '" + aService + "' started."); - Service svc = aRegistry.register(aDescriptor, aService); + Service svc = getRegistry().register(aDescriptor, aService); _running.put(svc.getDescriptor(), svc); } @@ -101,14 +104,14 @@ public abstract class AbstractSubSystem implements SubSystem { } @Override - public void stop(String aContext, ServiceRegistry aRegistry) { - doStop(aContext); + public void stop() { + doStop(); for (Service svc: _running.values()) { - aRegistry.remove(svc); + getRegistry().remove(svc); } } - protected abstract void doStop(String aContext); + protected abstract void doStop(); @Override public String toString() { diff --git a/trunk/system/general/src/main/java/org/wamblee/system/CompositeSystem.java b/trunk/system/general/src/main/java/org/wamblee/system/CompositeSystem.java index 32551498..155d5603 100644 --- a/trunk/system/general/src/main/java/org/wamblee/system/CompositeSystem.java +++ b/trunk/system/general/src/main/java/org/wamblee/system/CompositeSystem.java @@ -21,13 +21,14 @@ public class CompositeSystem extends AbstractSubSystem { /** * Construcst the composite system. * @param aName Name of the system. + * @param aRegistry Service registry. * @param aSystems Subsystems. * @param aProvided Provided services of the system. * @param aRequired Required services by the system. */ - public CompositeSystem(String aName, SubSystem[] aSystems, + public CompositeSystem(String aName, ServiceRegistry aRegistry, SubSystem[] aSystems, ServiceDescriptor[] aProvided, ServiceDescriptor[] aRequired) { - super(aName, aProvided, aRequired); + super(aName, aRegistry, aProvided, aRequired); _systems = aSystems; validate(); } @@ -78,18 +79,18 @@ public class CompositeSystem extends AbstractSubSystem { } @Override - protected void doStart(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices) { + protected void doStart(String aContext, Service[] aRequiredServices) { List descriptors = new ArrayList(); for (Service service : aRequiredServices) { descriptors.add(service.getDescriptor()); } SystemAssembler assembler = new SystemAssembler(aContext + "." + getName(), _systems, descriptors.toArray(new ServiceDescriptor[0])); - assembler.start(aRegistry, aRequiredServices); + assembler.start(getRegistry(), aRequiredServices); } @Override - protected void doStop(String aContext) { + protected void doStop() { // Empty. } diff --git a/trunk/system/general/src/main/java/org/wamblee/system/DefaultServiceRegistry.java b/trunk/system/general/src/main/java/org/wamblee/system/DefaultServiceRegistry.java index 4d8bd465..59515fec 100644 --- a/trunk/system/general/src/main/java/org/wamblee/system/DefaultServiceRegistry.java +++ b/trunk/system/general/src/main/java/org/wamblee/system/DefaultServiceRegistry.java @@ -33,9 +33,8 @@ public class DefaultServiceRegistry implements ServiceRegistry { } @Override - public synchronized Service find(String id) { - // TODO Auto-generated method stub - return null; + public synchronized Service find(String aId) { + return _services.get(aId); } @Override diff --git a/trunk/system/general/src/main/java/org/wamblee/system/SubSystem.java b/trunk/system/general/src/main/java/org/wamblee/system/SubSystem.java index 16b37aab..e25766d6 100644 --- a/trunk/system/general/src/main/java/org/wamblee/system/SubSystem.java +++ b/trunk/system/general/src/main/java/org/wamblee/system/SubSystem.java @@ -29,20 +29,16 @@ public interface SubSystem { * Initialises the subsytem by starting all the services that * it described as provided. * @param aContext Unique name for the subsystem. - * @param aRegistry Registry of service to which the subsystem must register the services it - * creates. * @param aRequiredServices Running services from other * subsystems that are required by this subsystem. * @return Services that are running in the subsystem. */ - Service[] start(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices); + Service[] start(String aContext, Service[] aRequiredServices); /** - * Stops a service. - * @param aContext Context - * @param aRegistry Registry from which services must be removed. + * Stops a subsystem. */ - void stop(String aContext, ServiceRegistry aRegistry); + void stop(); /** * Gets the list of running services in the subsystem. diff --git a/trunk/system/general/src/main/java/org/wamblee/system/SystemAssembler.java b/trunk/system/general/src/main/java/org/wamblee/system/SystemAssembler.java index 5b2c0e14..9e38a3e1 100644 --- a/trunk/system/general/src/main/java/org/wamblee/system/SystemAssembler.java +++ b/trunk/system/general/src/main/java/org/wamblee/system/SystemAssembler.java @@ -102,7 +102,7 @@ public class SystemAssembler { } services.add(required); } - Service[] provided = system.start(_context, aRegistry, services + Service[] provided = system.start(_context, services .toArray(new Service[0])); for (Service service : provided) { allProvided.put(service.getDescriptor(), service); diff --git a/trunk/system/general/src/test/java/org/wamblee/system/Application.java b/trunk/system/general/src/test/java/org/wamblee/system/Application.java index 3587917b..d5a3590d 100644 --- a/trunk/system/general/src/test/java/org/wamblee/system/Application.java +++ b/trunk/system/general/src/test/java/org/wamblee/system/Application.java @@ -9,17 +9,17 @@ public class Application extends AbstractSubSystem { new DefaultServiceDescriptor(Integer.class) }; - public Application() { - super("application", new ServiceDescriptor[0], REQUIRED); + public Application(ServiceRegistry aRegistry) { + super("application", aRegistry, new ServiceDescriptor[0], REQUIRED); } @Override - protected void doStart(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices) { + protected void doStart(String aContext, Service[] aRequiredServices) { // Empty, no services provided externally. } @Override - protected void doStop(String aContext) { + protected void doStop() { // Empty. } } diff --git a/trunk/system/general/src/test/java/org/wamblee/system/Environment.java b/trunk/system/general/src/test/java/org/wamblee/system/Environment.java index 901c67d7..a8231068 100644 --- a/trunk/system/general/src/test/java/org/wamblee/system/Environment.java +++ b/trunk/system/general/src/test/java/org/wamblee/system/Environment.java @@ -11,18 +11,18 @@ public class Environment extends AbstractSubSystem { new DefaultServiceDescriptor(Integer.class) }; - public Environment() { - super("environment", PROVIDED, new ServiceDescriptor[0]); + public Environment(ServiceRegistry aRegistry) { + super("environment", aRegistry, PROVIDED, new ServiceDescriptor[0]); } @Override - protected void doStart(String aContext, ServiceRegistry aRegistry, Service[] aRequiredServices) { - addService(aContext, aRegistry, PROVIDED[0], new Integer(1)); - addService(aContext, aRegistry, PROVIDED[1], new Integer(2)); + protected void doStart(String aContext, Service[] aRequiredServices) { + addService(aContext, PROVIDED[0], new Integer(1)); + addService(aContext, PROVIDED[1], new Integer(2)); } @Override - protected void doStop(String aContext) { + protected void doStop() { // Empty. } } diff --git a/trunk/system/general/src/test/java/org/wamblee/system/SystemAssemblerTest.java b/trunk/system/general/src/test/java/org/wamblee/system/SystemAssemblerTest.java index 70558250..1bd89ca5 100644 --- a/trunk/system/general/src/test/java/org/wamblee/system/SystemAssemblerTest.java +++ b/trunk/system/general/src/test/java/org/wamblee/system/SystemAssemblerTest.java @@ -13,8 +13,8 @@ public class SystemAssemblerTest extends TestCase { } 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]); assembler.start(_registry, new Service[0]); @@ -24,17 +24,17 @@ public class SystemAssemblerTest extends TestCase { 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(); + SubSystem environment = new Environment(_registry); + SubSystem application = new Application(_registry); SystemAssembler assembler = new SystemAssembler(new SubSystem[] { application, environment }, new ServiceDescriptor[0]); assembler.start(_registry, new Service[0]); @@ -46,12 +46,12 @@ public class SystemAssemblerTest extends TestCase { } public void testComposite() { - SubSystem environment = new Environment(); - SubSystem application = new Application(); - CompositeSystem system = new CompositeSystem("all", new SubSystem[] { + 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", _registry, new Service[0]); + system.start("root", new Service[0]); ServiceDescriptor[] required = system.getRequiredServices(); assertEquals(0, required.length); ServiceDescriptor[] provided = system.getProvidedServices(); @@ -60,9 +60,9 @@ 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]); @@ -73,13 +73,13 @@ public class SystemAssemblerTest extends TestCase { } public void testCompositeWithSuperfluousRequiredInfo() { - SubSystem environment = new Environment(); - SubSystem application = new Application(); - CompositeSystem system = new CompositeSystem("all", new SubSystem[] { + 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 DefaultServiceDescriptor( String.class) }); - system.start("root", _registry, new Service[0]); + system.start("root", new Service[0]); ServiceDescriptor[] required = system.getRequiredServices(); assertEquals(1, required.length); ServiceDescriptor[] provided = system.getProvidedServices(); @@ -88,12 +88,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; } @@ -103,13 +103,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(); diff --git a/trunk/system/spring/pom.xml b/trunk/system/spring/pom.xml index bc1737e9..0ee69c1d 100644 --- a/trunk/system/spring/pom.xml +++ b/trunk/system/spring/pom.xml @@ -1,24 +1,39 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> - + + org.wamblee + wamblee-utils + 0.2-SNAPSHOT + + + 4.0.0 org.wamblee - wamblee-utils + wamblee-system-spring + jar 0.2-SNAPSHOT - + wamblee.org system spring + http://wamblee.org + + + org.wamblee + wamblee-system-general + ${project.version} + + + commons-logging + commons-logging + + + org.springframework + spring-beans + + + org.springframework + spring-context + - 4.0.0 - org.wamblee - wamblee-system-spring - jar - wamblee.org system general - http://wamblee.org - - - commons-logging - commons-logging - - + diff --git a/trunk/system/spring/src/main/java/org/wamblee/system/spring/ProvidedServiceBean.java b/trunk/system/spring/src/main/java/org/wamblee/system/spring/ProvidedServiceBean.java new file mode 100644 index 00000000..31dd5768 --- /dev/null +++ b/trunk/system/spring/src/main/java/org/wamblee/system/spring/ProvidedServiceBean.java @@ -0,0 +1,28 @@ +package org.wamblee.system.spring; + +import org.springframework.beans.factory.FactoryBean; + +public class ProvidedServiceBean implements FactoryBean { + + private String _id; + + public ProvidedServiceBean(String aId) { + _id = aId; + } + + @Override + public Object getObject() throws Exception { + return SpringSystem.REGISTRY.get().find(_id).reference(Object.class); + } + + @Override + public Class getObjectType() { + return null; + } + + @Override + public boolean isSingleton() { + return true; + } + +} diff --git a/trunk/system/spring/src/main/java/org/wamblee/system/spring/SpringSystem.java b/trunk/system/spring/src/main/java/org/wamblee/system/spring/SpringSystem.java new file mode 100644 index 00000000..5de2a948 --- /dev/null +++ b/trunk/system/spring/src/main/java/org/wamblee/system/spring/SpringSystem.java @@ -0,0 +1,134 @@ +package org.wamblee.system.spring; + +import java.util.Map; + +import org.springframework.beans.MutablePropertyValues; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.beans.factory.config.ConstructorArgumentValues; +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.context.ApplicationContext; +import org.springframework.context.support.AbstractApplicationContext; +import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.context.support.GenericApplicationContext; +import org.wamblee.system.AbstractSubSystem; +import org.wamblee.system.CompositeSystem; +import org.wamblee.system.Service; +import org.wamblee.system.ServiceDescriptor; +import org.wamblee.system.ServiceRegistry; +import org.wamblee.system.SystemAssemblyException; + +/** + * Represents a system configured based on spring. + */ +public class SpringSystem extends AbstractSubSystem { + + /** + * Singleton access to the service registry. Required while starting up. + */ + static ThreadLocal REGISTRY = new ThreadLocal(); + + private String[] _configFiles; + private Map _provided; + private Map _required; + /** + * Parent application context containing required services. + */ + private GenericApplicationContext _parentContext; + + /** + * Application context containing parsed objects. + */ + private AbstractApplicationContext _context; + + /** + * Construcst a spring system. + * + * @param aName + * Name of the system. + * @param aConfigFiles + * Spring config files to read. + * @param aProvided + * Map of bean name to service descriptor describing the bean + * names that the spring config files use for each required + * service. + * @param aRequired + * Map of bean name to service descriptor describing the bean + * names that the spring config files use for each required + * service. + */ + public SpringSystem(String aName, ServiceRegistry aRegistry, String[] aConfigFiles, + Map aProvided, + Map aRequired) { + super(aName, aRegistry, aProvided.values().toArray(new ServiceDescriptor[0]), + aRequired.keySet().toArray(new ServiceDescriptor[0])); + _configFiles = aConfigFiles; + _provided = aProvided; + _required = aRequired; + } + + @Override + protected void doStart(String aContext, + Service[] aRequiredServices) { + ServiceRegistry old = REGISTRY.get(); + try { + REGISTRY.set(getRegistry()); + try { + + registerRequiredServices(aRequiredServices); + + parseConfigFiles(); + + exposeProvidedServices(aContext); + } catch (Exception e) { + throw new SystemAssemblyException( + "Failed to assemble spring system", e); + } + } finally { + REGISTRY.set(old); + } + } + + private void exposeProvidedServices(String aContext) { + // Call addService for each provided service. + + for (String name : _provided.keySet()) { + Object svc = _context.getBean(name); + if (svc == null) { + throw new IllegalArgumentException(aContext + ": service '" + + name + "' is null"); + } + addService(aContext, _provided.get(name), svc); + } + } + + private void parseConfigFiles() { + // Parse spring config files + + _context = new ClassPathXmlApplicationContext((String[]) _configFiles, + _parentContext); + } + + private void registerRequiredServices(Service[] aRequiredServices) { + // Register required services in a parent context + + // Register the Hibernate mapping files as a bean. + _parentContext = new GenericApplicationContext(); + + for (Service svc: aRequiredServices) { + String id = svc.getId(); + ServiceDescriptor descriptor = svc.getDescriptor(); + String beanName = _required.get(descriptor); + ConstructorArgumentValues cargs = new ConstructorArgumentValues(); + cargs.addGenericArgumentValue(id); + BeanDefinition definition = new RootBeanDefinition(ProvidedServiceBean.class, cargs, + new MutablePropertyValues()); + _parentContext.registerBeanDefinition(beanName, definition); + } + _parentContext.refresh(); + } + + @Override + protected void doStop() { + _context.close(); + } +} diff --git a/trunk/system/spring/src/test/java/org/wamblee/system/spring/BlaService.java b/trunk/system/spring/src/test/java/org/wamblee/system/spring/BlaService.java new file mode 100644 index 00000000..9b103b81 --- /dev/null +++ b/trunk/system/spring/src/test/java/org/wamblee/system/spring/BlaService.java @@ -0,0 +1,20 @@ +package org.wamblee.system.spring; + +public class BlaService { + private HelloService _hello; + + public BlaService(HelloService aService) { + if ( aService == null ) { + throw new IllegalArgumentException("helloService is null"); + } + _hello = aService; + } + + public String execute() { + return _hello.say(); + } + + public void stop() { + System.out.println("Blaservice stopping"); + } +} diff --git a/trunk/system/spring/src/test/java/org/wamblee/system/spring/HelloService.java b/trunk/system/spring/src/test/java/org/wamblee/system/spring/HelloService.java new file mode 100644 index 00000000..fe9b2d5d --- /dev/null +++ b/trunk/system/spring/src/test/java/org/wamblee/system/spring/HelloService.java @@ -0,0 +1,14 @@ +package org.wamblee.system.spring; + +public class HelloService { + + private String _msg; + + public HelloService(String aMsg) { + _msg = aMsg; + } + + public String say() { + return _msg; + } +} diff --git a/trunk/system/spring/src/test/java/org/wamblee/system/spring/SpringSystemTest.java b/trunk/system/spring/src/test/java/org/wamblee/system/spring/SpringSystemTest.java new file mode 100644 index 00000000..092a5bc1 --- /dev/null +++ b/trunk/system/spring/src/test/java/org/wamblee/system/spring/SpringSystemTest.java @@ -0,0 +1,108 @@ +package org.wamblee.system.spring; + +import java.util.HashMap; +import java.util.Map; + +import junit.framework.TestCase; + +import org.wamblee.system.DefaultServiceDescriptor; +import org.wamblee.system.DefaultServiceRegistry; +import org.wamblee.system.Service; +import org.wamblee.system.ServiceDescriptor; +import org.wamblee.system.ServiceRegistry; +import org.wamblee.system.SystemAssemblyException; + +public class SpringSystemTest extends TestCase { + + private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml"; + private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml"; + + private ServiceRegistry _registry; + + @Override + protected void setUp() throws Exception { + super.setUp(); + _registry = new DefaultServiceRegistry(); + } + + public void testBlackboxSystem() { + SpringSystem system = new SpringSystem("system", _registry, + new String[] { HELLO_SERVICE_SPRING_XML }, + new HashMap(), + new HashMap()); + system.start("Hello", new Service[0]); + Service[] services = system.getRunningServices(); + assertEquals(0, services.length); + + system.stop(); + } + + public void testOneProvidedService() { + Map provided = new HashMap(); + provided.put("helloService", new DefaultServiceDescriptor( + HelloService.class)); + + SpringSystem system = new SpringSystem("system", _registry, + new String[] { HELLO_SERVICE_SPRING_XML }, provided, + new HashMap()); + system.start("Hello", new Service[0]); + Service[] services = system.getRunningServices(); + assertEquals(1, services.length); + assertTrue(services[0].reference(HelloService.class) instanceof HelloService); + assertEquals("Hello world!", services[0].reference(HelloService.class) + .say()); + system.stop(); + } + + public void testWithMissingRequiremnt() { + try { + SpringSystem system = new SpringSystem("system", _registry, + new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML }, + new HashMap(), + new HashMap()); + system.start("Bla", new Service[0]); + } catch (SystemAssemblyException e) { + //e.printStackTrace(); + return; + } + fail(); + } + + public void testWithRequirement() { + Map required = new HashMap(); + required.put(new DefaultServiceDescriptor(HelloService.class), + "helloService"); + SpringSystem system = new SpringSystem("system", _registry, + new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML }, + new HashMap(), required); + + HelloService helloObject = new HelloService("ladida"); + Service helloService = _registry.register(new DefaultServiceDescriptor(HelloService.class), helloObject); + system.start("Bla", new Service[] { helloService } ); + system.stop(); + } + + public void testWithRequirementAndProvidedService() { + Map required = new HashMap(); + required.put(new DefaultServiceDescriptor(HelloService.class), + "helloService"); + Map provided = new HashMap(); + provided.put("blaService", new DefaultServiceDescriptor( + BlaService.class)); + + SpringSystem system = new SpringSystem("system", _registry, + new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML }, + provided, required); + + HelloService helloObject = new HelloService("ladida"); + Service helloService = _registry.register(new DefaultServiceDescriptor(HelloService.class), helloObject); + Service[] services = system.start("Bla", new Service[] { helloService } ); + assertEquals(1, services.length); + + assertTrue(services[0].reference(BlaService.class) instanceof BlaService); + assertEquals("ladida", services[0].reference(BlaService.class) + .execute()); + system.stop(); + } + +} diff --git a/trunk/system/spring/src/test/resources/test.org.wamblee.system.spring.xml b/trunk/system/spring/src/test/resources/test.org.wamblee.system.spring.xml new file mode 100644 index 00000000..238054b5 --- /dev/null +++ b/trunk/system/spring/src/test/resources/test.org.wamblee.system.spring.xml @@ -0,0 +1,13 @@ + + + + + + Hello world! + + + + diff --git a/trunk/system/spring/src/test/resources/test.org.wamblee.system.springWithRequirements.xml b/trunk/system/spring/src/test/resources/test.org.wamblee.system.springWithRequirements.xml new file mode 100644 index 00000000..a2f60d83 --- /dev/null +++ b/trunk/system/spring/src/test/resources/test.org.wamblee.system.springWithRequirements.xml @@ -0,0 +1,13 @@ + + + + + + + + + +