source code formatting.
[utils] / system / spring / src / test / java / org / wamblee / system / spring / SpringComponentTest.java
index dc7a5845c659f68c47bbcd7db8f855bd3fa1c5b2..6407c14463db1f80d98efaa38f61701f04592d0f 100644 (file)
+/*
+ * Copyright 2007 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.wamblee.system.spring;
 
+import junit.framework.TestCase;
+
+import org.wamblee.io.ClassPathResource;
+
+import org.wamblee.system.core.DefaultProvidedInterface;
+import org.wamblee.system.core.DefaultRequiredInterface;
+import org.wamblee.system.core.DefaultScope;
+import org.wamblee.system.core.ProvidedInterface;
+import org.wamblee.system.core.RequiredInterface;
+import org.wamblee.system.core.Scope;
+import org.wamblee.system.core.SystemAssemblyException;
+
+import org.wamblee.test.EventTracker;
+
 import java.io.IOException;
+
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.Properties;
 
-import junit.framework.TestCase;
-
-import org.wamblee.io.ClassPathResource;
-import org.wamblee.system.AbstractInterfaceDescriptor;
-import org.wamblee.system.DefaultProvidedInterfaceDescriptor;
-import org.wamblee.system.DefaultRequiredInterfaceDescriptor;
-import org.wamblee.system.DefaultServiceRegistry;
-import org.wamblee.system.ProvidedInterfaceDescriptor;
-import org.wamblee.system.RequiredInterfaceDescriptor;
-import org.wamblee.system.Service;
-import org.wamblee.system.InterfaceDescriptor;
-import org.wamblee.system.ServiceRegistry;
-import org.wamblee.system.SystemAssemblyException;
 
+/**
+ * DOCUMENT ME!
+ *
+ * @author $author$
+ * @version $Revision$
+ */
 public class SpringComponentTest extends TestCase {
+    /**
+     * DOCUMENT ME!
+     */
+    private static final String HELLO_SERVICE_SPRING_XML = "test.org.wamblee.system.spring.xml";
+
+    /**
+     * DOCUMENT ME!
+     */
+    private static final String HELLO_SERVICE_SPRING_WITH_REQS_XML = "test.org.wamblee.system.springWithRequirements.xml";
+
+    /**
+     * DOCUMENT ME!
+     */
+    private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml";
 
-       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 static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML = "test.org.wamblee.system.springWithProperties.xml";
+    /**
+     * DOCUMENT ME!
+     */
+    private static final String HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML2 = "test.org.wamblee.system.springWithProperties2.xml";
+
+    /**
+     * DOCUMENT ME!
+     */
     private static final String PROPERTY_FILE = "test.org.wamblee.system.spring.properties";
 
-       private ServiceRegistry _registry;
-
-       @Override
-       protected void setUp() throws Exception {
-               super.setUp();
-               _registry = new DefaultServiceRegistry();
-       }
-
-       public void testBlackboxSystem() {
-               SpringComponent system = new SpringComponent("system", _registry,
-                               new String[] { HELLO_SERVICE_SPRING_XML },
-                               new HashMap<String, ProvidedInterfaceDescriptor>(),
-                               new HashMap<RequiredInterfaceDescriptor, String>());
-               system.start("Hello", new Service[0]);
-               Service[] services = system.getRunningServices();
-               assertEquals(0, services.length);
-               
-               system.stop();
-       }
-
-       public void testOneProvidedService() {
-               Map<String, ProvidedInterfaceDescriptor> provided = new HashMap<String, ProvidedInterfaceDescriptor>();
-               provided.put("helloService", new DefaultProvidedInterfaceDescriptor(
-                               "hello", HelloService.class));
-
-               SpringComponent system = new SpringComponent("system", _registry,
-                               new String[] { HELLO_SERVICE_SPRING_XML }, provided,
-                               new HashMap<RequiredInterfaceDescriptor, String>());
-               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 testWithProperties() throws IOException {
-               Map<String, ProvidedInterfaceDescriptor> provided = new HashMap<String, ProvidedInterfaceDescriptor>();
-               provided.put("helloService", new DefaultProvidedInterfaceDescriptor(
-                               "hello", HelloService.class));
-               SpringComponent system = new SpringComponent("system", _registry,
-                               new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
-                               provided,
-                               new HashMap<RequiredInterfaceDescriptor, String>());
-               Properties props = new Properties();
-               props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
-               system.addProperties(props);
-               
-               system.start("Hello", new Service[0]);
-               Service[] services = system.getRunningServices();
-               assertEquals("Property Value", services[0].reference(HelloService.class).say());
-       }
-
-       public void testWithMissingRequirement() {
-               try {
-                       SpringComponent system = new SpringComponent("system", _registry,
-                                       new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
-                                       new HashMap<String, ProvidedInterfaceDescriptor>(),
-                                       new HashMap<RequiredInterfaceDescriptor, String>());
-                       system.start("Bla", new Service[0]);
-               } catch (SystemAssemblyException e) {
-                       //e.printStackTrace();
-                       return;
-               }
-               fail();
-       }
-
-       public void testWithRequirement() {
-               Map<RequiredInterfaceDescriptor, String> required = new HashMap<RequiredInterfaceDescriptor, String>();
-               required.put(new DefaultRequiredInterfaceDescriptor("hello", HelloService.class),
-                               "helloService");
-               SpringComponent system = new SpringComponent("system", _registry,
-                               new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
-                               new HashMap<String, ProvidedInterfaceDescriptor>(), required);
-               
-               HelloService helloObject = new HelloService("ladida"); 
-               Service helloService = _registry.register(new DefaultProvidedInterfaceDescriptor("hello", HelloService.class), helloObject);
-               system.start("Bla", new Service[] { helloService } );
-               system.stop();
-       }
-       
-       public void testWithRequirementAndProvidedService() {
-               Map<RequiredInterfaceDescriptor, String> required = new HashMap<RequiredInterfaceDescriptor, String>();
-               required.put(new DefaultRequiredInterfaceDescriptor("hello", HelloService.class),
-                               "helloService");
-               Map<String,ProvidedInterfaceDescriptor> provided = new HashMap<String, ProvidedInterfaceDescriptor>();
-               provided.put("blaService", new DefaultProvidedInterfaceDescriptor("bla",
-                               BlaService.class));
-
-               SpringComponent system = new SpringComponent("system", _registry,
-                               new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
-                               provided, required);
-               
-               HelloService helloObject = new HelloService("ladida"); 
-               Service helloService = _registry.register(new DefaultProvidedInterfaceDescriptor("hello", 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();
-       }
+    /**
+     * DOCUMENT ME!
+     */
+    public static EventTracker<String> EVENT_TRACKER;
+
+    /**
+     * DOCUMENT ME!
+     */
+    private Scope externalScope;
+
+    /**
+     * DOCUMENT ME!
+     *
+     * @throws Exception DOCUMENT ME!
+     */
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        EVENT_TRACKER     = new EventTracker<String>();
+        externalScope     = new DefaultScope(new ProvidedInterface[0]);
+    }
+
+    /**
+     * DOCUMENT ME!
+     */
+    public void testBlackboxSystem() {
+        SpringComponent system  = new SpringComponent("system",
+                new String[] { HELLO_SERVICE_SPRING_XML },
+                new HashMap<String, ProvidedInterface>(),
+                new HashMap<RequiredInterface, String>());
+
+        Scope           runtime = system.start(externalScope);
+        assertEquals(0, externalScope.getProvidedInterfaces().size());
+
+        system.stop(runtime);
+    }
+
+    /**
+     * DOCUMENT ME!
+     */
+    public void testOneProvidedService() {
+        Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
+        provided.put("helloService",
+            new DefaultProvidedInterface("hello", HelloService.class));
+
+        SpringComponent         system   = new SpringComponent("system",
+                new String[] { HELLO_SERVICE_SPRING_XML }, provided,
+                new HashMap<RequiredInterface, String>());
+        Scope                   runtime  = system.start(externalScope);
+        List<ProvidedInterface> services = runtime.getProvidedInterfaces();
+
+        assertEquals(1, services.size());
+
+        Object service = runtime.getInterfaceImplementation(services.get(0),
+                Object.class);
+        assertTrue(service instanceof HelloService);
+
+        // BUG; Provided services should be made available in the external
+        // scope.
+        Object service2 = externalScope.getInterfaceImplementation(provided.get(
+                    "helloService"), Object.class);
+        assertSame(service, service2);
+
+        assertEquals("Hello world!", ((HelloService) service).say());
+        system.stop(runtime);
+    }
+
+    /**
+     * DOCUMENT ME!
+     *
+     * @throws IOException DOCUMENT ME!
+     */
+    public void testWithProperties() throws IOException {
+        Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
+        provided.put("helloService",
+            new DefaultProvidedInterface("hello", HelloService.class));
+
+        SpringComponent system = new SpringComponent("system",
+                new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML },
+                provided, new HashMap<RequiredInterface, String>());
+        Properties      props  = new Properties();
+        props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
+        system.addProperties(props);
+
+        Scope scope = system.start(externalScope);
+        // BUG: Hello service was constructed multiple times. Once with the
+        // unprocessed property
+        // and another time with the processed property.
+        assertEquals(1, EVENT_TRACKER.getEventCount());
+
+        List<ProvidedInterface> services = scope.getProvidedInterfaces();
+        assertEquals("Property Value",
+            scope.getInterfaceImplementation(services.get(0), HelloService.class)
+            .say());
+    }
+
+    /**
+     * DOCUMENT ME!
+     *
+     * @throws IOException DOCUMENT ME!
+     */
+    public void testWithPropertiesAsBean() throws IOException {
+        Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
+        provided.put("helloService",
+            new DefaultProvidedInterface("hello", HelloService2.class));
+
+        SpringComponent system = new SpringComponent("system",
+                new String[] { HELLO_SERVICE_SPRING_WITH_PROPERTIES_XML2 },
+                provided, new HashMap<RequiredInterface, String>());
+        Properties      props  = new Properties();
+        props.load(new ClassPathResource(PROPERTY_FILE).getInputStream());
+        system.addProperties("properties", props);
+
+        Scope                   scope    = system.start(externalScope);
+
+        List<ProvidedInterface> services = scope.getProvidedInterfaces();
+
+        Properties              props2   = scope.getInterfaceImplementation(services
+                .get(0), HelloService2.class).getProperties();
+        assertEquals(props, props2);
+    }
+
+    /**
+     * DOCUMENT ME!
+     */
+    public void testWithMissingRequirement() {
+        try {
+            SpringComponent system = new SpringComponent("system",
+                    new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
+                    new HashMap<String, ProvidedInterface>(),
+                    new HashMap<RequiredInterface, String>());
+            system.start(externalScope);
+        } catch (SystemAssemblyException e) {
+            // e.printStackTrace();
+            return;
+        }
+
+        fail();
+    }
+
+    /**
+     * DOCUMENT ME!
+     */
+    public void testWithRequirement() {
+        Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
+        required.put(new DefaultRequiredInterface("hello", HelloService.class),
+            "helloService");
+
+        SpringComponent   system       = new SpringComponent("system",
+                new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
+                new HashMap<String, ProvidedInterface>(), required);
+
+        HelloService      helloObject  = new HelloService("ladida");
+        ProvidedInterface helloService = new DefaultProvidedInterface("hello",
+                HelloService.class);
+        Scope             scope        = new DefaultScope(new ProvidedInterface[] {
+                    helloService
+                });
+        scope.publishInterface(helloService, helloObject);
+        system.getRequiredInterfaces().get(0).setProvider(helloService);
+
+        Scope runtime = system.start(scope);
+        system.stop(runtime);
+    }
+
+    /**
+     * DOCUMENT ME!
+     */
+    public void testWithRequirementAndProvidedService() {
+        Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
+        required.put(new DefaultRequiredInterface("hello", HelloService.class),
+            "helloService");
+
+        Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
+        provided.put("blaService",
+            new DefaultProvidedInterface("bla", BlaService.class));
+
+        SpringComponent   system       = new SpringComponent("system",
+                new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML }, provided,
+                required);
+
+        HelloService      helloObject  = new HelloService("ladida");
+        ProvidedInterface helloService = new DefaultProvidedInterface("hello",
+                HelloService.class);
+        Scope             scope        = new DefaultScope(new ProvidedInterface[] {
+                    helloService
+                });
+        scope.publishInterface(helloService, helloObject);
+        system.getRequiredInterfaces().get(0).setProvider(helloService);
+
+        Scope             runtime = system.start(scope);
+        ProvidedInterface started = runtime.getProvidedInterfaces().get(0);
+
+        Object            impl    = runtime.getInterfaceImplementation(started,
+                BlaService.class);
+        assertNotNull(impl);
+        assertTrue(impl instanceof BlaService);
+        assertEquals("ladida", ((BlaService) impl).execute());
+        system.stop(runtime);
+    }
+
+    /**
+     * Tests a scenario where a subclass of SpringComponent adds a new
+     * provided interface where the interface is provided by the subclass
+     * itself and not by the spring configs inside.
+     */
+    public void testWithProvidedFromSubClassNotFromConfig() {
+        Map<String, ProvidedInterface> provided = new HashMap<String, ProvidedInterface>();
+        provided.put("helloService",
+            new DefaultProvidedInterface("hello", HelloService.class));
+
+        SubSpringComponent      system   = new SubSpringComponent("system",
+                new String[] { HELLO_SERVICE_SPRING_XML }, provided,
+                new HashMap<RequiredInterface, String>());
+
+        Scope                   runtime  = system.start(externalScope);
+        List<ProvidedInterface> services = runtime.getProvidedInterfaces();
+
+        assertEquals(2, services.size());
+
+        Object service = runtime.getInterfaceImplementation(services.get(0),
+                Object.class);
+        assertTrue(service instanceof HelloService);
+
+        // BUG; Provided services should be made available in the external
+        // scope.
+        Object service2 = externalScope.getInterfaceImplementation(provided.get(
+                    "helloService"), Object.class);
+        assertSame(service, service2);
+
+        Object floatsvc = externalScope.getInterfaceImplementation(system.getProvidedInterfaces()
+                .get(1), Object.class);
+        assertTrue(floatsvc instanceof Float);
+        assertTrue((((Float) floatsvc).floatValue() - 100.345f) < 0.00001);
+
+        assertEquals("Hello world!", ((HelloService) service).say());
+        system.stop(runtime);
+    }
+
+    /**
+     * Tests the spring component with an additional requirement from
+     * the subclass  which is not required by the spring config files inside.
+     */
+    public void testWithRequirementFromSubClass() {
+        Map<RequiredInterface, String> required = new HashMap<RequiredInterface, String>();
+        required.put(new DefaultRequiredInterface("hello", HelloService.class),
+            "helloService");
+
+        SpringComponent   system       = new SubSpringComponent2("system",
+                new String[] { HELLO_SERVICE_SPRING_WITH_REQS_XML },
+                new HashMap<String, ProvidedInterface>(), required);
+
+        HelloService      helloObject  = new HelloService("ladida");
+        ProvidedInterface helloService = new DefaultProvidedInterface("hello",
+                HelloService.class);
+
+        ProvidedInterface floatService = new DefaultProvidedInterface("float",
+                Float.class);
+
+        Scope             scope        = new DefaultScope(new ProvidedInterface[] {
+                    helloService
+                });
+        scope.publishInterface(helloService, helloObject);
+        scope.publishInterface(floatService, 100.234f);
+        system.getRequiredInterfaces().get(0).setProvider(helloService);
+        system.getRequiredInterfaces().get(1).setProvider(floatService);
+
+        Scope runtime = system.start(scope);
+        system.stop(runtime);
 
+        assertEquals(100.234f,
+            ((Float) runtime.get("floatValue")).floatValue(), 0.0001f);
+    }
 }