incubator-composer-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From hamm...@apache.org
Subject svn commit: r613775 [13/15] - in /incubator/composer: ./ core/ core/src/ core/src/java/ core/src/java/org/ core/src/java/org/apache/ core/src/java/org/apache/composer/ core/src/java/org/apache/composer/core/ core/src/java/org/apache/composer/core/adapt...
Date Mon, 21 Jan 2008 06:41:50 GMT
Added: incubator/composer/core/src/test/org/apache/composer/core/injectors/SetterInjectionTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/injectors/SetterInjectionTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/injectors/SetterInjectionTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/injectors/SetterInjectionTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,130 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Idea by Rachel Davies, Original code by Aslak Hellesoy and Paul Hammant   *
+ *****************************************************************************/
+package org.apache.composer.core.injectors;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.fail;
+
+import java.util.Properties;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.apache.composer.core.ComponentFactory;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.CompositionException;
+import org.apache.composer.core.monitors.NullComponentMonitor;
+import org.apache.composer.core.tck.AbstractComponentFactoryTest;
+import org.apache.composer.core.tck.AbstractComponentAdapterTest.RecordingLifecycleStrategy;
+import org.apache.composer.core.testmodel.NullLifecycle;
+import org.apache.composer.core.testmodel.RecordingLifecycle;
+import org.apache.composer.core.testmodel.RecordingLifecycle.One;
+
+/**
+ * @author Jörg Schaible
+ */
+public class SetterInjectionTestCase extends AbstractComponentFactoryTest {
+	
+	@Before
+    public void setUp() throws Exception {
+        picoContainer = new DefaultContainer(createComponentFactory());
+    }
+
+    protected ComponentFactory createComponentFactory() {
+        return new SetterInjection();
+    }
+
+    public static interface Bean {
+    }
+
+    public static class NamedBean implements Bean {
+        private String name;
+
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+    }
+
+    public static class NamedBeanWithPossibleDefault extends NamedBean {
+        private boolean byDefault;
+
+        public NamedBeanWithPossibleDefault() {
+        }
+
+        public NamedBeanWithPossibleDefault(String name) {
+            setName(name);
+            byDefault = true;
+        }
+
+        public boolean getByDefault() {
+            return byDefault;
+        }
+    }
+
+    public static class NoBean extends NamedBean {
+        public NoBean(String name) {
+            setName(name);
+        }
+    }
+
+    @Test public void testContainerUsesStandardConstructor() {
+        picoContainer.addComponent(Bean.class, NamedBeanWithPossibleDefault.class);
+        picoContainer.addComponent("Tom");
+        NamedBeanWithPossibleDefault bean = (NamedBeanWithPossibleDefault) picoContainer.getComponent(Bean.class);
+        assertFalse(bean.getByDefault());
+    }
+
+    @Test public void testContainerUsesOnlyStandardConstructor() {
+        picoContainer.addComponent(Bean.class, NoBean.class);
+        picoContainer.addComponent("Tom");
+        try {
+            picoContainer.getComponent(Bean.class);
+            fail("Instantiation should have failed.");
+        } catch (CompositionException e) {
+        }
+    }
+
+    @Test public void testCustomLifecycleCanBeInjected() throws NoSuchMethodException {
+        RecordingLifecycleStrategy strategy = new RecordingLifecycleStrategy(new StringBuffer());
+        SetterInjection componentFactory = new SetterInjection();
+        SetterInjector sica = (SetterInjector)componentFactory.createComponentAdapter(new NullComponentMonitor(), strategy, new Properties(), NullLifecycle.class, NullLifecycle.class);
+        One one = new RecordingLifecycle.One(new StringBuffer());
+        sica.start(one);
+        sica.stop(one);        
+        sica.dispose(one);
+        assertEquals("<start<stop<dispose", strategy.recording());
+    }
+
+    public static class AnotherNamedBean implements Bean {
+        private String name;
+
+        public String getName() {
+            return name;
+        }
+
+        public void initName(String name) {
+            this.name = name;
+        }
+    }
+
+    @Test public void testAlternatePrefixWorks() {
+        picoContainer = new DefaultContainer(new SetterInjection("init"));
+        picoContainer.addComponent(Bean.class, AnotherNamedBean.class);
+        picoContainer.addComponent("Tom");
+        AnotherNamedBean bean = picoContainer.getComponent(AnotherNamedBean.class);
+        assertEquals("Tom", bean.getName());
+    }
+
+
+}
\ No newline at end of file

Added: incubator/composer/core/src/test/org/apache/composer/core/injectors/SetterInjectorTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/injectors/SetterInjectorTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/injectors/SetterInjectorTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/injectors/SetterInjectorTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,465 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by                                                          *
+ *****************************************************************************/
+package org.apache.composer.core.injectors;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import static org.apache.composer.core.parameters.ComponentParameter.DEFAULT;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.Test;
+import org.apache.composer.core.Characteristics;
+import org.apache.composer.core.ComponentAdapter;
+import org.apache.composer.core.ComponentFactory;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.NameBinding;
+import org.apache.composer.core.Parameter;
+import org.apache.composer.core.behaviors.Caching;
+import org.apache.composer.core.lifecycle.NullLifecycleStrategy;
+import org.apache.composer.core.monitors.AbstractComponentMonitor;
+import org.apache.composer.core.monitors.NullComponentMonitor;
+import org.apache.composer.core.parameters.ConstantParameter;
+import org.apache.composer.core.tck.AbstractComponentAdapterTest;
+import org.apache.composer.core.testmodel.NullLifecycle;
+import org.apache.composer.core.testmodel.PersonBean;
+import org.apache.composer.core.testmodel.PurseBean;
+import org.apache.composer.core.testmodel.SimpleTouchable;
+import org.apache.composer.core.testmodel.Touchable;
+
+
+
+public class SetterInjectorTestCase
+    extends AbstractComponentAdapterTest {
+
+    protected Class getComponentAdapterType() {
+        return SetterInjector.class;
+    }
+
+    protected ComponentFactory createDefaultComponentFactory() {
+        return new Caching().wrap(new SetterInjection());
+    }
+
+    protected ComponentAdapter prepDEF_verifyWithoutDependencyWorks(MutableContainer picoContainer) {
+        return new SetterInjector(PersonBean.class, PersonBean.class, new Parameter[] {new ConstantParameter(
+                "Pico Container")}, new NullComponentMonitor(), new NullLifecycleStrategy(), "set", false);
+    }
+
+    protected ComponentAdapter prepDEF_verifyDoesNotInstantiate(MutableContainer picoContainer) {
+        picoContainer.addComponent("Pico Container");
+        return new SetterInjector(DeadBody.class, DeadBody.class, new Parameter[] {DEFAULT}, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                                  "set", false);
+    }
+
+    protected ComponentAdapter prepDEF_visitable() {
+        return new SetterInjector(PersonBean.class, PersonBean.class, new Parameter[] {new ConstantParameter(
+                "Pico Container")}, new NullComponentMonitor(), new NullLifecycleStrategy(), "set", false);
+
+    }
+
+    protected ComponentAdapter prepSER_isSerializable(MutableContainer picoContainer) {
+        picoContainer.addComponent("Pico Container");
+        return new SetterInjector(PersonBean.class, PersonBean.class, new Parameter[] {DEFAULT}, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                                  "set", false);
+    }
+
+    protected ComponentAdapter prepSER_isXStreamSerializable(MutableContainer picoContainer) {
+        return prepSER_isSerializable(picoContainer);
+    }
+
+    protected ComponentAdapter prepDEF_isAbleToTakeParameters(MutableContainer picoContainer) {
+        picoContainer.addComponent("Pico Container");
+        picoContainer.addComponent(PersonBean.class);
+        SetterInjector componentAdapter = new SetterInjector(
+                PurseBean.class, MoneyPurse.class, new Parameter[] {DEFAULT, new ConstantParameter(100.0)}, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                "set", false);
+        return picoContainer.as(Characteristics.NO_CACHE).addAdapter(componentAdapter).getComponentAdapter(PurseBean.class, (NameBinding) null);
+    }
+
+    public static class MoneyPurse
+            extends PurseBean {
+        double money;
+
+        public double getMoney() {
+            return money;
+        }
+
+        public void setMoney(double money) {
+            this.money = money;
+        }
+    }
+
+    protected ComponentAdapter prepVER_verificationFails(MutableContainer picoContainer) {
+        picoContainer.addComponent("Pico Container");
+        picoContainer.addComponent(PersonBean.class);
+        SetterInjector componentAdapter = new SetterInjector(
+                PurseBean.class, MoneyPurse.class, new Parameter[] {DEFAULT},new NullComponentMonitor(), new NullLifecycleStrategy(),
+                "set", false);
+        return picoContainer.addAdapter(componentAdapter).getComponentAdapter(PurseBean.class, (NameBinding) null);
+    }
+
+    protected ComponentAdapter prepINS_createsNewInstances(MutableContainer picoContainer) {
+        picoContainer.addComponent("Pico Container");
+        return new SetterInjector(PersonBean.class, PersonBean.class, new Parameter[] {DEFAULT}, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                                  "set", false);
+    }
+
+    public static class Ghost
+            extends PersonBean {
+        public Ghost() {
+            throw new VerifyError("test");
+        }
+    }
+
+    protected ComponentAdapter prepINS_errorIsRethrown(MutableContainer picoContainer) {
+        picoContainer.addComponent("Pico Container");
+        return new SetterInjector(Ghost.class, Ghost.class, new Parameter[] {DEFAULT}, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                                  "set", false);
+    }
+
+    public static class DeadBody
+            extends PersonBean {
+        public DeadBody() {
+            throw new RuntimeException("test");
+        }
+    }
+
+    protected ComponentAdapter prepINS_runtimeExceptionIsRethrown(MutableContainer picoContainer) {
+        picoContainer.addComponent("Pico Container");
+        return new SetterInjector(DeadBody.class, DeadBody.class, new Parameter[] {DEFAULT}, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                                  "set", false);
+    }
+
+    public static class HidingPersion
+            extends PersonBean {
+        public HidingPersion() throws Exception {
+            throw new Exception("test");
+        }
+    }
+
+    protected ComponentAdapter prepINS_normalExceptionIsRethrownInsidePicoInitializationException(
+            MutableContainer picoContainer) {
+        picoContainer.addComponent("Pico Container");
+        return new SetterInjector(
+                HidingPersion.class, HidingPersion.class, new Parameter[] {DEFAULT}, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                "set", false);
+    }
+
+    protected ComponentAdapter prepRES_dependenciesAreResolved(MutableContainer picoContainer) {
+        picoContainer.addComponent("Pico Container");
+        picoContainer.addComponent(PersonBean.class);
+        return new SetterInjector(PurseBean.class, PurseBean.class, new Parameter[] {DEFAULT}, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                                  "set", false);
+    }
+
+    public static class WealthyPerson
+            extends PersonBean {
+        PurseBean purse;
+
+        public PurseBean getPurse() {
+            return purse;
+        }
+
+        public void setPurse(PurseBean purse) {
+            this.purse = purse;
+        }
+    }
+
+    protected ComponentAdapter prepRES_failingVerificationWithCyclicDependencyException(MutableContainer picoContainer) {
+        picoContainer.addComponent("Pico Container");
+        picoContainer.addComponent(PersonBean.class, WealthyPerson.class);
+        SetterInjector componentAdapter = new SetterInjector(
+                PurseBean.class, PurseBean.class, new Parameter[] {DEFAULT}, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                "set", false);
+        return picoContainer.as(Characteristics.NO_CACHE).addAdapter(componentAdapter).getComponentAdapter(PurseBean.class, (NameBinding) null);
+    }
+
+    protected ComponentAdapter prepRES_failingInstantiationWithCyclicDependencyException(MutableContainer picoContainer) {
+        picoContainer.addComponent("Pico Container");
+        picoContainer.addComponent(PersonBean.class, WealthyPerson.class);
+        SetterInjector componentAdapter = new SetterInjector(
+                PurseBean.class, PurseBean.class, new Parameter[] {DEFAULT}, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                "set", false);
+        return picoContainer.as(Characteristics.NO_CACHE).addAdapter(componentAdapter).getComponentAdapter(PurseBean.class, (NameBinding) null);
+    }
+
+    public static class A {
+        private B b;
+        private String string;
+        private List list;
+
+        public void setB(B b) {
+            this.b = b;
+        }
+
+        public B getB() {
+            return b;
+        }
+
+        public String getString() {
+            return string;
+        }
+
+        public void setString(String string) {
+            this.string = string;
+        }
+
+        public List getList() {
+            return list;
+        }
+
+        public void setList(List list) {
+            this.list = list;
+        }
+    }
+
+    public static class A2 {
+        private B b;
+        private String string;
+        private List list;
+
+        public void injectB(B b) {
+            this.b = b;
+        }
+
+        public B getB() {
+            return b;
+        }
+
+        public String getString() {
+            return string;
+        }
+
+        public void injectString(String string) {
+            this.string = string;
+        }
+
+        public List getList() {
+            return list;
+        }
+
+        public void injectList(List list) {
+            this.list = list;
+        }
+    }
+
+
+    public static class B {
+    }
+
+    @Test public void testAllUnsatisfiableDependenciesAreSignalled() {
+        SetterInjector aAdapter = new SetterInjector("a", A.class, Parameter.DEFAULT, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                                                     "set", false);
+        SetterInjector bAdapter = new SetterInjector("b", B.class, Parameter.DEFAULT, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                                                     "set", false);
+
+        MutableContainer pico = new DefaultContainer();
+        pico.addAdapter(bAdapter);
+        pico.addAdapter(aAdapter);
+
+        try {
+            aAdapter.getComponentInstance(pico);
+        } catch (AbstractInjector.UnsatisfiableDependenciesException e) {
+            assertTrue(e.getUnsatisfiableDependencies().contains(List.class));
+            assertTrue(e.getUnsatisfiableDependencies().contains(String.class));
+        }
+    }
+
+    @Test public void testAllUnsatisfiableDependenciesAreSignalled2() {
+        SetterInjector aAdapter = new SetterInjector(A2.class, A2.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                                                     "set", false);
+        SetterInjector bAdapter = new SetterInjector("b", B.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                                                     "set", false);
+
+        MutableContainer pico = new DefaultContainer();
+        pico.addComponent(List.class, ArrayList.class)
+            .addComponent(String.class, "foo")
+            .addAdapter(bAdapter)
+            .addAdapter(aAdapter);
+
+        aAdapter.getComponentInstance(pico);
+
+        assertNotNull(aAdapter);
+
+        A2 a = pico.getComponent(A2.class);
+        assertTrue(a.getList() == null);
+        assertTrue(a.getString() == null);
+    }
+
+    public static class InitBurp {
+
+        private Wind wind;
+
+        public void initWind(Wind wind) {
+            this.wind = wind;
+        }
+    }
+
+    public static class SetterBurp {
+
+        private Wind wind;
+
+        public void setWind(Wind wind) {
+            this.wind = wind;
+        }
+    }
+
+    public static class Wind {
+
+    }
+
+    @Test public void testSetterMethodInjectionToContrastWithThatBelow() {
+
+        MutableContainer pico = new DefaultContainer();
+        pico.addAdapter(new SetterInjector(SetterBurp.class, SetterBurp.class, Parameter.DEFAULT, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                                           "set", false));
+        pico.addComponent(Wind.class, new Wind());
+        SetterBurp burp = pico.getComponent(SetterBurp.class);
+        assertNotNull(burp);
+        assertNotNull(burp.wind);
+    }
+
+    @Test public void testNonSetterMethodInjection() {
+        MutableContainer pico = new DefaultContainer();
+        pico.addAdapter(new SetterInjector(InitBurp.class, InitBurp.class, Parameter.DEFAULT, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                                           "set", false) {
+            protected String getInjectorPrefix() {
+                return "init";
+            }
+        });
+        pico.addComponent(Wind.class, new Wind());
+        InitBurp burp = pico.getComponent(InitBurp.class);
+        assertNotNull(burp);
+        assertNotNull(burp.wind);
+    }
+
+    @Test public void testNonSetterMethodInjectionWithoutOverridingSetterPrefix() {
+        MutableContainer pico = new DefaultContainer();
+        pico.addAdapter(new SetterInjector(InitBurp.class, InitBurp.class, Parameter.ZERO, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                                           "set", false));
+        pico.addComponent(Wind.class, new Wind());
+        InitBurp burp = pico.getComponent(InitBurp.class);
+        assertNotNull(burp);
+        assertTrue(burp.wind == null);
+    }
+
+
+    public static class C {
+        private B b;
+        private List l;
+        private final boolean asBean;
+
+        public C() {
+            asBean = true;
+        }
+
+        public C(B b) {
+            this.l = new ArrayList();
+            this.b = b;
+            asBean = false;
+        }
+
+        public void setB(B b) {
+            this.b = b;
+        }
+
+        public B getB() {
+            return b;
+        }
+
+        public void setList(List l) {
+            this.l = l;
+        }
+
+        public List getList() {
+            return l;
+        }
+
+        public boolean instantiatedAsBean() {
+            return asBean;
+        }
+    }
+
+    @Test public void testHybridBeans() {
+        SetterInjector bAdapter = new SetterInjector("b", B.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                                                     "set", false);
+        SetterInjector cAdapter = new SetterInjector("c", C.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                                                     "set", false);
+        SetterInjector cNullAdapter = new SetterInjector("c0", C.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(),
+                                                         "set", false);
+
+        MutableContainer pico = new DefaultContainer();
+        pico.addAdapter(bAdapter);
+        pico.addAdapter(cAdapter);
+        pico.addAdapter(cNullAdapter);
+        pico.addComponent(ArrayList.class);
+
+        C c = (C) cAdapter.getComponentInstance(pico);
+        assertTrue(c.instantiatedAsBean());
+        C c0 = (C) cNullAdapter.getComponentInstance(pico);
+        assertTrue(c0.instantiatedAsBean());
+    }
+
+    public static class Yin {
+        private Yang yang;
+
+        public void setYin(Yang yang) {
+            this.yang = yang;
+        }
+
+        public Yang getYang() {
+            return yang;
+        }
+    }
+
+    public static class Yang {
+        private Yin yin;
+
+        public void setYang(Yin yin) {
+            this.yin = yin;
+        }
+
+        public Yin getYin() {
+            return yin;
+        }
+    }
+
+    // TODO PICO-188
+    // http://jira.codehaus.org/browse/PICO-188
+    public void FIXME_testShouldBeAbleToHandleMutualDependenciesWithSetterInjection() {
+        MutableContainer pico = new DefaultContainer(new Caching().wrap(new SetterInjection()));
+
+        pico.addComponent(Yin.class);
+        pico.addComponent(Yang.class);
+
+        Yin yin = pico.getComponent(Yin.class);
+        Yang yang = pico.getComponent(Yang.class);
+
+        assertSame(yin, yang.getYin());
+        assertSame(yang, yin.getYang());
+    }
+    
+    @Test public void testCustomLifecycleCanBeInjected() throws NoSuchMethodException {
+        RecordingLifecycleStrategy strategy = new RecordingLifecycleStrategy(new StringBuffer());
+        SetterInjector sica = new SetterInjector(
+                NullLifecycle.class, NullLifecycle.class, new Parameter[0],
+                new AbstractComponentMonitor(), strategy, "set", false);
+        Touchable touchable = new SimpleTouchable();
+        sica.start(touchable);
+        sica.stop(touchable);
+        sica.dispose(touchable);
+        assertEquals("<start<stop<dispose", strategy.recording());
+    }
+
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/injectors/ThreadLocalCyclicDependencyGuardTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/injectors/ThreadLocalCyclicDependencyGuardTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/injectors/ThreadLocalCyclicDependencyGuardTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/injectors/ThreadLocalCyclicDependencyGuardTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,109 @@
+/*****************************************************************************
+ * Copyright (c) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by Joerg Schaible                                           *
+ *****************************************************************************/
+package org.apache.composer.core.injectors;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+
+import org.junit.Test;
+
+/**
+ * Test the CyclicDependecy.
+ */
+public final class ThreadLocalCyclicDependencyGuardTestCase {
+      
+    private final Runnable[] runner = new Runnable[3];
+    
+    class ThreadLocalRunner implements Runnable {
+        public AbstractInjector.CyclicDependencyException exception;
+        private final Blocker blocker;
+        private final AbstractInjector.ThreadLocalCyclicDependencyGuard guard;
+
+        public ThreadLocalRunner() {
+            this.blocker = new Blocker();
+            this.guard = new AbstractInjector.ThreadLocalCyclicDependencyGuard() {
+                public Object run() {
+                    try {
+                        blocker.block();
+                    } catch (InterruptedException e) {
+                    }
+                    return null;
+                }
+            };
+        }
+
+        public void run() {
+            try {
+                guard.observe(ThreadLocalRunner.class);
+            } catch (AbstractInjector.CyclicDependencyException e) {
+                exception = e;
+            }
+        }
+    }
+
+    public class Blocker {
+        public void block() throws InterruptedException {
+            final Thread thread = Thread.currentThread();
+            synchronized (thread) {
+                thread.wait();
+            }
+        }
+    }
+
+    private void initTest(final Runnable[] runner) throws InterruptedException {
+
+        Thread racer[] = new Thread[runner.length];
+        for(int i = 0; i < racer.length; ++i) {
+            racer[i] =  new Thread(runner[i]);
+        }
+
+        for (Thread aRacer : racer) {
+            aRacer.start();
+            Thread.sleep(200);
+        }
+
+        for (Thread aRacer : racer) {
+            synchronized (aRacer) {
+                aRacer.notify();
+            }
+        }
+
+        for (Thread aRacer : racer) {
+            aRacer.join();
+        }
+    }
+    
+    @Test public void testCyclicDependencyWithThreadSafeGuard() throws InterruptedException {
+        for(int i = 0; i < runner.length; ++i) {
+            runner[i] = new ThreadLocalRunner();
+        }
+        
+        initTest(runner);
+
+        for (Runnable aRunner : runner) {
+            assertNull(((ThreadLocalRunner) aRunner).exception);
+        }
+    }
+
+    @Test public void testCyclicDependencyException() {
+        final AbstractInjector.CyclicDependencyException cdEx = new AbstractInjector.CyclicDependencyException(getClass());
+        cdEx.push(String.class);
+        final Class[] classes = cdEx.getDependencies();
+        assertEquals(2, classes.length);
+        assertSame(getClass(), classes[0]);
+        assertSame(String.class, classes[1]);
+        assertTrue(cdEx.getMessage().indexOf(getClass().getName()) >= 0);
+    }
+
+
+
+}

Propchange: incubator/composer/core/src/test/org/apache/composer/core/injectors/ThreadLocalCyclicDependencyGuardTestCase.java
------------------------------------------------------------------------------
    svn:executable = *

Added: incubator/composer/core/src/test/org/apache/composer/core/lifecycle/LifecycleStateTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/lifecycle/LifecycleStateTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/lifecycle/LifecycleStateTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/lifecycle/LifecycleStateTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,45 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *****************************************************************************/
+package org.apache.composer.core.lifecycle;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.apache.composer.core.lifecycle.LifecycleState.CONSTRUCTED;
+import static org.apache.composer.core.lifecycle.LifecycleState.DISPOSED;
+import static org.apache.composer.core.lifecycle.LifecycleState.STARTED;
+import static org.apache.composer.core.lifecycle.LifecycleState.STOPPED;
+
+import org.junit.Test;
+
+/**
+ * @author Michael Rimov
+ */
+public class LifecycleStateTestCase {
+
+	@Test public void testIsStartAllowedOptions() {
+		assertTrue(CONSTRUCTED.isStartAllowed());
+		assertFalse(STARTED.isStartAllowed());
+		assertTrue(STOPPED.isStartAllowed());
+		assertFalse(DISPOSED.isStartAllowed());
+	}
+
+	@Test public void testIsStopAllowedOptions() {
+		assertFalse(CONSTRUCTED.isStopAllowed());
+		assertTrue(STARTED.isStopAllowed());
+		assertFalse(STOPPED.isStopAllowed());
+		assertFalse(DISPOSED.isStopAllowed());
+	}
+
+	@Test public void testIsDisposeAllowedOptions() {
+		assertTrue(CONSTRUCTED.isDisposedAllowed());
+		assertFalse(STARTED.isDisposedAllowed());
+		assertTrue(STOPPED.isDisposedAllowed());
+		assertFalse(DISPOSED.isDisposedAllowed());
+	}
+	
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/lifecycle/ReflectionLifecycleStrategyTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/lifecycle/ReflectionLifecycleStrategyTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/lifecycle/ReflectionLifecycleStrategyTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/lifecycle/ReflectionLifecycleStrategyTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,258 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *****************************************************************************/
+package org.apache.composer.core.lifecycle;
+
+import static org.junit.Assert.assertFalse;
+import static org.apache.composer.core.tck.MockFactory.mockeryWithCountingNamingScheme;
+
+import java.io.Serializable;
+import java.lang.reflect.Member;
+import java.lang.reflect.Method;
+
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.hamcrest.Matcher;
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.apache.composer.core.ComponentAdapter;
+import org.apache.composer.core.ComponentMonitor;
+import org.apache.composer.core.Disposable;
+import org.apache.composer.core.Container;
+import org.apache.composer.core.Startable;
+
+/**
+ * @author Paul Hammant
+ * @author Mauro Talevi
+ * @author J&ouml;rg Schaible
+ */
+@RunWith(JMock.class)
+public class ReflectionLifecycleStrategyTestCase {
+
+	private Mockery mockery = mockeryWithCountingNamingScheme();
+
+	private ReflectionLifecycleStrategy strategy;
+	private ComponentMonitor componentMonitor;
+
+	@Before
+	public void setUp() {
+		componentMonitor = mockery.mock(ComponentMonitor.class);
+		strategy = new ReflectionLifecycleStrategy(componentMonitor);
+	}
+
+	 @Test
+	public void testStartable() {
+		Object startable = mockComponent(true, false);
+		strategy.start(startable);
+		strategy.stop(startable);
+		strategy.dispose(startable);
+	}
+
+	@Test
+	public void testDisposable() {
+		Object disposable = mockComponent(false, true);
+		strategy.start(disposable);
+		strategy.stop(disposable);
+		strategy.dispose(disposable);
+	}
+
+	@Test
+	public void testNotStartableNorDisposable() {
+		Object serializable = mockery.mock(Serializable.class);
+		assertFalse(strategy.hasLifecycle(serializable.getClass()));
+		strategy.start(serializable);
+		strategy.stop(serializable);
+		strategy.dispose(serializable);
+	}
+
+	@Test
+	public void testMonitorChanges() {
+		final ComponentMonitor componentMonitor2 = mockery
+				.mock(ComponentMonitor.class);
+		final Disposable disposable = mockery.mock(Disposable.class);
+		final Matcher<Member> isDisposeMember = new IsMember("dispose");
+		final Matcher<Method> isDisposeMethod = new IsMethod("dispose");
+		mockery.checking(new Expectations() {
+			{
+				atLeast(1).of(disposable).dispose();
+				one(componentMonitor).invoking(
+						with(aNull(Container.class)),
+						with(aNull(ComponentAdapter.class)),
+						with(isDisposeMember), with(same(disposable)));
+				one(componentMonitor).invoked(with(aNull(Container.class)),
+						with(aNull(ComponentAdapter.class)),
+						with(isDisposeMethod), with(same(disposable)),
+						with(any(Long.class)));
+				one(componentMonitor2).invoking(
+						with(aNull(Container.class)),
+						with(aNull(ComponentAdapter.class)),
+						with(isDisposeMember), with(same(disposable)));
+				one(componentMonitor2).invoked(
+						with(aNull(Container.class)),
+						with(aNull(ComponentAdapter.class)),
+						with(isDisposeMethod), with(same(disposable)),
+						with(any(Long.class)));
+			}
+		});
+		strategy.dispose(disposable);
+		strategy.changeMonitor(componentMonitor2);
+		strategy.dispose(disposable);
+	}
+
+	@Test
+	public void testWithDifferentTypes() {
+		final MyLifecycle lifecycle = mockery.mock(MyLifecycle.class);
+		final Matcher<Member> isStartMember = new IsMember("start");
+		final Matcher<Method> isStartMethod = new IsMethod("start");
+		final Matcher<Member> isStopMember = new IsMember("stop");
+		final Matcher<Method> isStopMethod = new IsMethod("stop");
+		final Matcher<Member> isDisposeMember = new IsMember("dispose");
+		final Matcher<Method> isDisposeMethod = new IsMethod("dispose");
+		mockery.checking(new Expectations() {
+			{
+				one(lifecycle).start();
+				one(lifecycle).stop();
+				one(lifecycle).dispose();
+				one(componentMonitor).invoking(
+						with(aNull(Container.class)),
+						with(aNull(ComponentAdapter.class)),
+						with(isStartMember), with(same(lifecycle)));
+				one(componentMonitor).invoked(with(aNull(Container.class)),
+						with(aNull(ComponentAdapter.class)),
+						with(isStartMethod), with(same(lifecycle)),
+						with(any(Long.class)));
+				one(componentMonitor).invoking(
+						with(aNull(Container.class)),
+						with(aNull(ComponentAdapter.class)),
+						with(isStopMember), with(same(lifecycle)));
+				one(componentMonitor).invoked(with(aNull(Container.class)),
+						with(aNull(ComponentAdapter.class)),
+						with(isStopMethod), with(same(lifecycle)),
+						with(any(Long.class)));
+				one(componentMonitor).invoking(
+						with(aNull(Container.class)),
+						with(aNull(ComponentAdapter.class)),
+						with(isDisposeMember), with(same(lifecycle)));
+				one(componentMonitor).invoked(with(aNull(Container.class)),
+						with(aNull(ComponentAdapter.class)),
+						with(isDisposeMethod), with(same(lifecycle)),
+						with(any(Long.class)));
+			}
+		});
+
+		Object startable = mockComponent(true, false);
+		strategy.start(startable);
+		strategy.stop(startable);
+		strategy.dispose(startable);
+		startable = lifecycle;
+		strategy.start(startable);
+		strategy.stop(startable);
+		strategy.dispose(startable);
+	}
+
+	private Object mockComponent(boolean startable, boolean disposable) {
+		final Matcher<Member> isStartMember = new IsMember("start");
+		final Matcher<Method> isStartMethod = new IsMethod("start");
+		final Matcher<Member> isStopMember = new IsMember("stop");
+		final Matcher<Method> isStopMethod = new IsMethod("stop");
+		final Matcher<Member> isDisposeMember = new IsMember("dispose");
+		final Matcher<Method> isDisposeMethod = new IsMethod("dispose");
+		if (startable) {
+			final Startable mock = mockery.mock(Startable.class);
+			mockery.checking(new Expectations() {
+				{
+					atLeast(1).of(mock).start();
+					atLeast(1).of(mock).stop();
+					one(componentMonitor).invoking(
+							with(aNull(Container.class)),
+							with(aNull(ComponentAdapter.class)),
+							with(isStartMember), with(same(mock)));
+					one(componentMonitor)
+							.invoked(with(aNull(Container.class)),
+									with(aNull(ComponentAdapter.class)),
+									with(isStartMethod), with(same(mock)),
+									with(any(Long.class)));
+					one(componentMonitor).invoking(
+							with(aNull(Container.class)),
+							with(aNull(ComponentAdapter.class)),
+							with(isStopMember), with(same(mock)));
+					one(componentMonitor).invoked(
+							with(aNull(Container.class)),
+							with(aNull(ComponentAdapter.class)),
+							with(isStopMethod), with(same(mock)), with(any(Long.class)));
+				}
+			});
+			return mock;
+		}
+		if (disposable) {
+			final Disposable mock = mockery.mock(Disposable.class);
+			mockery.checking(new Expectations() {
+				{
+					atLeast(1).of(mock).dispose();
+					one(componentMonitor).invoking(
+							with(aNull(Container.class)),
+							with(aNull(ComponentAdapter.class)),
+							with(isDisposeMember), with(same(mock)));
+					one(componentMonitor)
+							.invoked(with(aNull(Container.class)),
+									with(aNull(ComponentAdapter.class)),
+									with(isDisposeMethod), with(same(mock)),
+									with(any(Long.class)));
+				}
+			});
+			return mock;
+		}
+		return mockery.mock(Serializable.class);
+	}
+
+	public static interface MyLifecycle {
+		void start();
+
+		void stop();
+
+		void dispose();
+	}
+
+	static class IsMember extends BaseMatcher<Member> {
+		private String name;
+
+		public IsMember(String name) {
+			this.name = name;
+		}
+
+		public boolean matches(Object item) {
+			return ((Member) item).getName().equals(name);
+		}
+
+		public void describeTo(Description description) {
+			description.appendText("Should have been a member of name ");
+			description.appendText(name);
+		}
+	};
+
+	static class IsMethod extends BaseMatcher<Method> {
+		private String name;
+
+		public IsMethod(String name) {
+			this.name = name;
+		}
+
+		public boolean matches(Object item) {
+			return ((Method) item).getName().equals(name);
+		}
+
+		public void describeTo(Description description) {
+			description.appendText("Should have been a method of name ");
+			description.appendText(name);
+		}
+	};
+
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/lifecycle/StartableLifecycleStrategyTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/lifecycle/StartableLifecycleStrategyTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/lifecycle/StartableLifecycleStrategyTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/lifecycle/StartableLifecycleStrategyTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,76 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *****************************************************************************/
+package org.apache.composer.core.lifecycle;
+
+import static org.apache.composer.core.tck.MockFactory.mockeryWithCountingNamingScheme;
+
+import java.io.Serializable;
+
+import org.jmock.Expectations;
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.apache.composer.core.Disposable;
+import org.apache.composer.core.Startable;
+import org.apache.composer.core.monitors.NullComponentMonitor;
+
+/**
+ * 
+ * @author Mauro Talevi
+ */
+@RunWith(JMock.class)
+public class StartableLifecycleStrategyTestCase {
+
+	private Mockery mockery = mockeryWithCountingNamingScheme();
+	
+    private StartableLifecycleStrategy strategy;
+    
+    @Before
+    public void setUp(){
+        strategy = new StartableLifecycleStrategy(new NullComponentMonitor());
+    }
+
+    @Test public void testStartable(){
+        Object startable = mockComponent(true, false);
+        strategy.start(startable);
+        strategy.stop(startable);
+    }
+
+    @Test public void testDisposable(){
+        Object startable = mockComponent(false, true);
+        strategy.dispose(startable);
+    }
+
+    @Test public void testSerializable(){
+        Object serializable = mockComponent(false, false);
+        strategy.start(serializable);
+        strategy.stop(serializable);
+        strategy.dispose(serializable);
+    }
+    
+    private Object mockComponent(boolean startable, boolean disposeable) {
+        if ( startable ) {
+        	 final Startable mock = mockery.mock(Startable.class);
+        	 mockery.checking(new Expectations() {{
+                 one(mock).start(); 
+                 one(mock).stop(); 
+             }});
+        	 return mock;
+        }
+        if ( disposeable ) {
+       	 final Disposable mock = mockery.mock(Disposable.class);
+    	 mockery.checking(new Expectations() {{
+             one(mock).dispose(); 
+         }});
+    	 return mock;
+        }
+        return mockery.mock(Serializable.class);
+    }
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/monitors/ConsoleComponentMonitorTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/monitors/ConsoleComponentMonitorTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/monitors/ConsoleComponentMonitorTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/monitors/ConsoleComponentMonitorTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,61 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by                                                          *
+ *****************************************************************************/
+package org.apache.composer.core.monitors;
+
+import java.io.PrintStream;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.apache.composer.core.ComponentMonitor;
+
+/**
+ * @author Aslak Helles&oslash;y
+ * @author Mauro Talevi
+ */
+public class ConsoleComponentMonitorTestCase {
+    private ComponentMonitor componentMonitor;
+    private Constructor constructor;
+    private Method method;
+
+    @Before
+    public void setUp() throws Exception {
+        PrintStream out = System.out;
+        constructor = getClass().getConstructor((Class[])null);
+        method = getClass().getDeclaredMethod("setUp", (Class[])null);
+        componentMonitor = new ConsoleComponentMonitor(out);
+    }
+
+    @Test public void testShouldTraceInstantiating() {
+        componentMonitor.instantiating(null, null, constructor);
+    }
+
+    @Test public void testShouldTraceInstantiatedWithInjected() {
+        componentMonitor.instantiated(null, null, constructor, new Object(), new Object[0], 543);
+    }
+
+    @Test public void testShouldTraceInstantiationFailed() {
+        componentMonitor.instantiationFailed(null, null, constructor, new RuntimeException("doh"));
+    }
+
+    @Test public void testShouldTraceInvoking() {
+        componentMonitor.invoking(null, null, method, this);
+    }
+
+    @Test public void testShouldTraceInvoked() {
+        componentMonitor.invoked(null, null, method, this, 543);
+    }
+
+    @Test public void testShouldTraceInvocatiationFailed() {
+        componentMonitor.invocationFailed(method, this, new RuntimeException("doh"));
+    }
+
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/monitors/NullComponentMonitorTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/monitors/NullComponentMonitorTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/monitors/NullComponentMonitorTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/monitors/NullComponentMonitorTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,69 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by                                                          *
+ *****************************************************************************/
+package org.apache.composer.core.monitors;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.apache.composer.core.tck.MockFactory.mockeryWithCountingNamingScheme;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+
+import org.jmock.Mockery;
+import org.jmock.integration.junit4.JMock;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.apache.composer.core.ComponentAdapter;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.LifecycleException;
+
+@RunWith(JMock.class)
+public class NullComponentMonitorTestCase {
+
+	private Mockery mockery = mockeryWithCountingNamingScheme();
+	
+    @Test public void testItAll() throws NoSuchMethodException {
+
+        NullComponentMonitor ncm = new NullComponentMonitor();
+        ncm.instantiated(makePico(), makeCA(), makeConstructor(), "foo", new Object[0], 10);
+        assertEquals(makeConstructor(), ncm.instantiating(makePico(), makeCA(), makeConstructor()));
+        ncm.instantiationFailed(makePico(), makeCA(), makeConstructor(), new Exception());
+        ncm.invocationFailed(makeConstructor(), "foo", new Exception());
+        ncm.invoked(makePico(), makeCA(), makeMethod(), "foo", 10);
+        ncm.invoking(makePico(), makeCA(), makeMethod(), "foo");
+        try {
+            ncm.lifecycleInvocationFailed(makePico(), makeCA(), makeMethod(), "foo", new RuntimeException());
+        } catch (LifecycleException e) {
+            assertEquals(makeMethod(), e.getMethod());
+            assertEquals("foo", e.getInstance());
+            assertEquals("PicoLifecycleException: method 'public java.lang.String java.lang.String.toString()', instance 'foo, java.lang.RuntimeException", e.getMessage());
+        }
+        assertNull(ncm.noComponentFound(makePico(), String.class));
+
+    }
+
+    private MutableContainer makePico() {
+        return mockery.mock(MutableContainer.class);
+    }
+
+    private ComponentAdapter makeCA() {
+        return mockery.mock(ComponentAdapter.class);
+    }
+
+    private Constructor makeConstructor() {
+        return String.class.getConstructors()[0];
+    }
+
+    private Method makeMethod() throws NoSuchMethodException {
+        return String.class.getMethod("toString");
+    }
+
+
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/monitors/WriterComponentMonitorTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/monitors/WriterComponentMonitorTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/monitors/WriterComponentMonitorTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/monitors/WriterComponentMonitorTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,132 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by                                                          *
+ *****************************************************************************/
+package org.apache.composer.core.monitors;
+
+import static org.junit.Assert.assertEquals;
+import static org.apache.composer.core.monitors.ComponentMonitorHelper.ctorToString;
+import static org.apache.composer.core.monitors.ComponentMonitorHelper.format;
+import static org.apache.composer.core.monitors.ComponentMonitorHelper.methodToString;
+import static org.apache.composer.core.monitors.ComponentMonitorHelper.parmsToString;
+
+import java.io.StringWriter;
+import java.io.Writer;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.apache.composer.core.ComponentMonitor;
+import org.apache.composer.core.CompositionException;
+import org.apache.composer.core.Container;
+import org.apache.composer.core.LifecycleException;
+import org.apache.composer.core.adapters.AbstractAdapter;
+import org.apache.composer.core.containers.TransientContainer;
+
+/**
+ * @author Aslak Helles&oslash;y
+ * @author Mauro Talevi
+ */
+public class WriterComponentMonitorTestCase  {
+	
+    private Writer out;
+    private ComponentMonitor componentMonitor;
+    private static final String NL = System.getProperty("line.separator");
+    private Constructor constructor;
+    private Method method;
+
+    @Before
+    public void setUp() throws Exception {
+        out = new StringWriter();
+        constructor = getClass().getConstructor((Class[])null);
+        method = getClass().getDeclaredMethod("setUp", (Class[])null);
+        componentMonitor = new WriterComponentMonitor(out);
+    }
+
+    @Test public void testShouldTraceInstantiating() {
+        componentMonitor.instantiating(null, null, constructor);
+        assertEquals(format(ComponentMonitorHelper.INSTANTIATING, ctorToString(constructor)) +NL,  out.toString());
+    }
+
+    @Test public void testShouldTraceInstantiatedWithInjected() {
+        Object[] injected = new Object[0];
+        Object instantiated = new Object();
+        componentMonitor.instantiated(null, null, constructor, instantiated, injected, 543);
+        Assert.assertEquals(format(ComponentMonitorHelper.INSTANTIATED,
+                                                   ctorToString(constructor),
+                                                   (long)543,
+                                                   instantiated.getClass().getName(), parmsToString(injected)) +NL,  out.toString());
+    }
+
+    @Test public void testShouldTraceInstantiationFailed() {
+        componentMonitor.instantiationFailed(null, null, constructor, new RuntimeException("doh"));
+        Assert.assertEquals(format(ComponentMonitorHelper.INSTANTIATION_FAILED,
+                                                   ctorToString(constructor), "doh") +NL,  out.toString());
+    }
+
+    @Test public void testShouldTraceInvoking() {
+        componentMonitor.invoking(null, null, method, this);
+        Assert.assertEquals(format(ComponentMonitorHelper.INVOKING,
+                                                   methodToString(method), this) +NL,  out.toString());
+    }
+
+    @Test public void testShouldTraceInvoked() {
+        componentMonitor.invoked(null, null, method, this, 543);
+        Assert.assertEquals(format(ComponentMonitorHelper.INVOKED,
+                                                   methodToString(method), this,
+                                                   (long)543) +NL,  out.toString());
+    }
+
+    @Test public void testShouldTraceInvocatiationFailed() {
+        componentMonitor.invocationFailed(method, this, new RuntimeException("doh"));
+        Assert.assertEquals(format(ComponentMonitorHelper.INVOCATION_FAILED,
+                                                   methodToString(method), this, "doh") +NL,  out.toString());
+    }
+
+    @Test public void testShouldTraceLifecycleInvocationFailed() {
+        try {
+            componentMonitor.lifecycleInvocationFailed(new TransientContainer(),
+                                                       new AbstractAdapter(Map.class, HashMap.class) {
+                                                           public Object getComponentInstance(Container container)
+                                                               throws CompositionException {
+                                                               return "x";
+                                                           }
+
+                                                           public void verify(Container container)
+                                                               throws CompositionException {
+                                                           }
+
+                                                           public String getDescriptor() {
+                                                               return null;
+                                                           }
+                                                       },
+                                                       method,
+                                                       "fooooo",
+                                                       new RuntimeException("doh"));
+            Assert.fail("should have barfed");
+        } catch (LifecycleException e) {
+            //expected
+        }
+        Assert.assertEquals(format(ComponentMonitorHelper.LIFECYCLE_INVOCATION_FAILED,
+                                                   methodToString(method), "fooooo", "doh") + NL,
+                     out.toString());
+    }
+
+    @Test public void testNoComponent() {
+        
+        componentMonitor.noComponentFound(new TransientContainer(), "foo");
+        Assert.assertEquals(format(ComponentMonitorHelper.NO_COMPONENT,
+                                                   "foo") +NL,  out.toString());
+    }
+
+
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/parameters/ConfigParameterTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/parameters/ConfigParameterTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/parameters/ConfigParameterTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/parameters/ConfigParameterTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,208 @@
+package org.apache.composer.core.parameters;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+import java.io.File;
+import java.util.List;
+
+import org.junit.Test;
+import org.apache.composer.core.Characteristics;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.NameBinding;
+import org.apache.composer.core.Container;
+
+/**
+ * test that config parameter does the right job
+ * 
+ * @author k.pribluda
+ * 
+ */
+public class ConfigParameterTestCase {
+
+
+	// defaultparameter name, just for convenience
+	NameBinding paramNameBinding = new NameBinding() {
+		public String getName() {
+			return "gloo.blum";
+		}
+
+	};
+
+	@Test public void testThatNoEntryIsWorkingProperly() throws Exception {
+		Container container = new DefaultContainer();
+		ComponentParameter parameter = new ComponentParameter("gloo.blum");
+
+		// shall be not resolvable
+		assertFalse(parameter.isResolvable(container, null, String.class,
+                                           paramNameBinding, false, null));
+
+		// shall resolve instance as null
+		assertNull(parameter.resolveInstance(container, null, String.class,
+                                             paramNameBinding, false, null));
+	}
+
+	@Test public void testThatNotStringEntryIsNotResolved() throws Exception {
+		MutableContainer container = new DefaultContainer();
+		container.addComponent("gloo.blum", new Integer(239));
+
+		ComponentParameter parameter = new ComponentParameter("gloo.blum");
+
+		// shall be not resolvable
+		assertFalse(parameter.isResolvable(container, null, String.class,
+                                           paramNameBinding, false, null));
+
+		// shall resolve instance as null
+		assertNull(parameter.resolveInstance(container, null, String.class,
+                                             paramNameBinding, false, null));
+
+	}
+
+	/**
+	 * shall resolve as ddifferent classes
+	 * 
+	 * @throws Exception
+	 */
+	@Test public void testThatResolvedSuccessfully() throws Exception {
+		MutableContainer container = new DefaultContainer();
+		container.addComponent("gloo.blum", "239");
+
+		ComponentParameter parameter = new ComponentParameter("gloo.blum");
+
+		assertEquals(new Integer(239), parameter.resolveInstance(container,
+				null, Integer.class, paramNameBinding, false, null));
+		assertEquals("239", parameter.resolveInstance(container, null,
+				String.class, paramNameBinding, false, null));
+	}
+
+	/**
+	 * shall bomb properly if no suitable converter found
+	 * 
+	 */
+	@Test public void testThatUnavailableConverterProducesCorrectException() {
+		MutableContainer container = new DefaultContainer();
+		container.addComponent("gloo.blum", "239");
+
+		ComponentParameter parameter = new ComponentParameter("gloo.blum");
+
+//		try {
+//			Object foo = parameter.resolveInstance(container, null, List.class, paramNameBinding, false);
+//			fail("failed to bomb on unavailable converter");
+//		} catch (ConfigParameter.NoConverterAvailableException ex) {
+//			// that's anticipated
+//		}
+	    Object foo = parameter.resolveInstance(container, null, List.class, paramNameBinding, false, null);
+        assertNull(foo);
+
+    }
+	
+	@Test public void testComponentInstantiation() {
+		DefaultContainer properties = new DefaultContainer();
+		properties.addComponent("numericProperty", "239");
+		properties.addComponent("doubleProperty", "17.95");
+		properties.addComponent("stringProperty", "foo.bar");
+
+		DefaultContainer container = new DefaultContainer(properties);
+		container.addComponent("configured", ExternallyConfiguredComponent.class,
+						new ComponentParameter("numericProperty"),
+						// resolves as string
+						new ComponentParameter("stringProperty"),
+						// resolves as file
+						new ComponentParameter("stringProperty"),
+						// resolves as double
+						new ComponentParameter("doubleProperty")
+					
+				);
+		
+		
+		ExternallyConfiguredComponent component = (ExternallyConfiguredComponent) container.getComponent("configured");
+		
+		assertNotNull(component);
+		assertEquals(239,component.getLongValue());
+		assertEquals("foo.bar",component.getStringParameter());
+		assertEquals(new File("foo.bar"),component.getFileParameter());
+		assertEquals(17.95,component.getDoubleParameter(),0);
+	}
+
+    @Test public void testComponentInstantiationViaParamNameAssociations() {
+        DefaultContainer properties = new DefaultContainer();
+        properties.addConfig("longValue", "239");
+        properties.addConfig("doubleParameter", "17.95");
+        properties.addConfig("stringParameter", "foo.bar");
+        properties.addConfig("fileParameter", "bar.txt");
+
+        DefaultContainer container = new DefaultContainer(properties);
+        container.as(Characteristics.USE_NAMES).addComponent(ExternallyConfiguredComponent.class);
+
+        ExternallyConfiguredComponent component = container.getComponent(ExternallyConfiguredComponent.class);
+		
+        assertNotNull(component);
+        assertEquals(239,component.getLongValue());
+        assertEquals("foo.bar",component.getStringParameter());
+        assertEquals(new File("bar.txt"),component.getFileParameter());
+        assertEquals(17.95,component.getDoubleParameter(),0);
+    }
+
+
+
+	/**
+	 * test component to show automatic conversion
+	 * 
+	 * @author ko5tik
+	 */
+
+	public static class ExternallyConfiguredComponent {
+		long longValue;
+
+		String stringParameter;
+
+		File fileParameter;
+
+		double doubleParameter;
+
+		public ExternallyConfiguredComponent(long longValue, String stringParameter, File fileParameter, double doubleParameter) {
+			super();
+			this.longValue = longValue;
+			this.stringParameter = stringParameter;
+			this.fileParameter = fileParameter;
+			this.doubleParameter = doubleParameter;
+		}
+
+		public double getDoubleParameter() {
+			return doubleParameter;
+		}
+
+		public File getFileParameter() {
+			return fileParameter;
+		}
+
+		public long getLongValue() {
+			return longValue;
+		}
+
+		public String getStringParameter() {
+			return stringParameter;
+		}
+
+		public void setDoubleParameter(double doubleParameter) {
+			this.doubleParameter = doubleParameter;
+		}
+
+		public void setFileParameter(File fileParameter) {
+			this.fileParameter = fileParameter;
+		}
+
+		public void setLongValue(long longValue) {
+			this.longValue = longValue;
+		}
+
+		public void setStringParameter(String stringParameter) {
+			this.stringParameter = stringParameter;
+		}
+
+	}
+
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/parameters/ConstantParameterTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/parameters/ConstantParameterTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/parameters/ConstantParameterTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/parameters/ConstantParameterTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,36 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by                                                          *
+ *****************************************************************************/
+package org.apache.composer.core.parameters;
+
+import static org.junit.Assert.fail;
+
+import org.junit.Test;
+import org.apache.composer.core.CompositionException;
+
+
+/**
+ * test that constant parameter behaves well.
+ * @author Konstantin Pribluda
+ */
+public class ConstantParameterTestCase {
+    
+    /**
+    *  constant parameter with instance type shall verify for expected primitives
+     * @throws Exception
+     */
+    @Test public void testThatInstaceTypeAcceptedForPrimitives() throws Exception {
+        ConstantParameter param = new ConstantParameter(239);
+        try{
+            param.verify(null,null,Integer.TYPE, null, false, null);
+        } catch(CompositionException ex) {
+            fail("failed verification for primitive / instance ");
+        }
+    }
+}

Added: incubator/composer/core/src/test/org/apache/composer/core/parameters/ParameterTestCase.java
URL: http://svn.apache.org/viewvc/incubator/composer/core/src/test/org/apache/composer/core/parameters/ParameterTestCase.java?rev=613775&view=auto
==============================================================================
--- incubator/composer/core/src/test/org/apache/composer/core/parameters/ParameterTestCase.java (added)
+++ incubator/composer/core/src/test/org/apache/composer/core/parameters/ParameterTestCase.java Sun Jan 20 22:41:26 2008
@@ -0,0 +1,189 @@
+/*****************************************************************************
+ * Copyright (C) PicoContainer Organization. All rights reserved.            *
+ * ------------------------------------------------------------------------- *
+ * The software in this package is published under the terms of the BSD      *
+ * style license a copy of which has been included with this distribution in *
+ * the LICENSE.txt file.                                                     *
+ *                                                                           *
+ * Original code by                                                          *
+ *****************************************************************************/
+package org.apache.composer.core.parameters;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertTrue;
+import junit.framework.TestCase;
+
+import org.junit.Test;
+import org.apache.composer.core.ComponentAdapter;
+import org.apache.composer.core.DefaultContainer;
+import org.apache.composer.core.MutableContainer;
+import org.apache.composer.core.NameBinding;
+import org.apache.composer.core.Parameter;
+import org.apache.composer.core.CompositionException;
+import org.apache.composer.core.testmodel.DependsOnTouchable;
+import org.apache.composer.core.testmodel.SimpleTouchable;
+import org.apache.composer.core.testmodel.Touchable;
+import org.apache.composer.core.visitors.VerifyingVisitor;
+
+
+/**
+ * @author Jon Tirsen (tirsen@codehaus.org)
+ */
+public final class ParameterTestCase {
+
+    public static class FooNameBinding implements NameBinding {
+        public String getName() {
+            return "";
+        }
+    }
+
+    final NameBinding pn = new FooNameBinding();
+
+    @Test public void testComponentParameterFetches() throws CompositionException {
+        DefaultContainer pico = new DefaultContainer();
+        ComponentAdapter adapter = pico.addComponent(Touchable.class, SimpleTouchable.class).getComponentAdapter(Touchable.class,
+                                                                                                                 (NameBinding) null);
+        assertNotNull(adapter);
+        assertNotNull(pico.getComponent(Touchable.class));
+        Touchable touchable = (Touchable) ComponentParameter.DEFAULT.resolveInstance(pico, null, Touchable.class, pn,
+                                                                                     false, null);
+        assertNotNull(touchable);
+    }
+
+    @Test public void testComponentParameterExcludesSelf() throws CompositionException {
+        DefaultContainer pico = new DefaultContainer();
+        ComponentAdapter adapter = pico.addComponent(Touchable.class, SimpleTouchable.class).getComponentAdapter(Touchable.class,
+                                                                                                                 (NameBinding) null);
+
+        assertNotNull(pico.getComponent(Touchable.class));
+        Touchable touchable = (Touchable) ComponentParameter.DEFAULT.resolveInstance(pico, adapter, Touchable.class, pn,
+                                                                                     false, null);
+        assertNull(touchable);
+    }
+
+    @Test public void testConstantParameter() throws CompositionException {
+        Object value = new Object();
+        ConstantParameter parameter = new ConstantParameter(value);
+        MutableContainer picoContainer = new DefaultContainer();
+        assertSame(value, parameter.resolveInstance(picoContainer, null, Object.class, pn, false, null));
+    }
+
+    @Test public void testDependsOnTouchableWithTouchableSpecifiedAsConstant() throws CompositionException {
+        DefaultContainer pico = new DefaultContainer();
+        SimpleTouchable touchable = new SimpleTouchable();
+        pico.addComponent(DependsOnTouchable.class, DependsOnTouchable.class, new ConstantParameter(touchable));
+        pico.getComponents();
+        assertTrue(touchable.wasTouched);
+    }
+
+    @Test public void testComponentParameterRespectsExpectedType() {
+        MutableContainer picoContainer = new DefaultContainer();
+        ComponentAdapter adapter = picoContainer.addComponent(Touchable.class, SimpleTouchable.class).getComponentAdapter(Touchable.class,
+                                                                                                                          (NameBinding) null);
+        assertNull(ComponentParameter.DEFAULT.resolveInstance(picoContainer, adapter, TestCase.class, pn, false, null));
+    }
+	
+	@Test public void testComponentParameterResolvesPrimitiveType() {
+        MutableContainer picoContainer = new DefaultContainer();
+        ComponentAdapter adapter = picoContainer.addComponent("glarch", 239).getComponentAdapter("glarch");
+        assertNotNull(adapter);
+		Parameter parameter = new ComponentParameter("glarch");
+		assertNotNull(parameter.resolveInstance(picoContainer,null,Integer.TYPE, pn, false, null));
+		assertEquals(239, ((Integer)parameter.resolveInstance(picoContainer,null,Integer.TYPE, pn, false, null)).intValue());
+	}
+
+    @Test public void testConstantParameterRespectsExpectedType() {
+        MutableContainer picoContainer = new DefaultContainer();
+        Parameter parameter = new ConstantParameter(new SimpleTouchable());
+        ComponentAdapter adapter = picoContainer.addComponent(Touchable.class, SimpleTouchable.class).getComponentAdapter(Touchable.class,
+                                                                                                                          (NameBinding) null);
+        assertFalse(parameter.isResolvable(picoContainer, adapter, TestCase.class, pn, false, null));
+    }
+
+    @Test public void testParameterRespectsExpectedType() throws CompositionException {
+        Parameter parameter = new ConstantParameter(Touchable.class);
+        MutableContainer picoContainer = new DefaultContainer();
+        assertFalse(parameter.isResolvable(picoContainer, null, TestCase.class, pn, false, null));
+
+        ComponentAdapter adapter = picoContainer.addComponent(Touchable.class, SimpleTouchable.class).getComponentAdapter(Touchable.class,
+                                                                                                                          (NameBinding) null);
+
+        assertNull(ComponentParameter.DEFAULT.resolveInstance(picoContainer, adapter, TestCase.class, pn, false, null));
+    }
+
+    @Test public void testConstantParameterWithPrimitives() throws CompositionException {
+        MutableContainer picoContainer = new DefaultContainer();
+        Byte byteValue = (byte)5;
+        ConstantParameter parameter = new ConstantParameter(byteValue);
+        assertSame(byteValue, parameter.resolveInstance(picoContainer, null, Byte.TYPE, pn, false, null));
+        assertSame(byteValue, parameter.resolveInstance(picoContainer, null, Byte.class, pn, false, null));
+        Short shortValue = (short)5;
+        parameter = new ConstantParameter(shortValue);
+        assertSame(shortValue, parameter.resolveInstance(picoContainer, null, Short.TYPE, pn, false, null));
+        assertSame(shortValue, parameter.resolveInstance(picoContainer, null, Short.class, pn, false, null));
+        Integer intValue = 5;
+        parameter = new ConstantParameter(intValue);
+        assertSame(intValue, parameter.resolveInstance(picoContainer, null, Integer.TYPE, pn, false, null));
+        assertSame(intValue, parameter.resolveInstance(picoContainer, null, Integer.class, pn, false, null));
+        Long longValue = (long)5;
+        parameter = new ConstantParameter(longValue);
+        assertSame(longValue, parameter.resolveInstance(picoContainer, null, Long.TYPE, pn, false, null));
+        assertSame(longValue, parameter.resolveInstance(picoContainer, null, Long.class, pn, false, null));
+        Float floatValue = new Float(5.5);
+        parameter = new ConstantParameter(floatValue);
+        assertSame(floatValue, parameter.resolveInstance(picoContainer, null, Float.TYPE, pn, false, null));
+        assertSame(floatValue, parameter.resolveInstance(picoContainer, null, Float.class, pn, false, null));
+        Double doubleValue = 5.5;
+        parameter = new ConstantParameter(doubleValue);
+        assertSame(doubleValue, parameter.resolveInstance(picoContainer, null, Double.TYPE, pn, false, null));
+        assertSame(doubleValue, parameter.resolveInstance(picoContainer, null, Double.class, pn, false, null));
+        Boolean booleanValue = true;
+        parameter = new ConstantParameter(booleanValue);
+        assertSame(booleanValue, parameter.resolveInstance(picoContainer, null, Boolean.TYPE, pn, false, null));
+        assertSame(booleanValue, parameter.resolveInstance(picoContainer, null, Boolean.class, pn, false, null));
+        Character charValue = 'x';
+        parameter = new ConstantParameter(charValue);
+        assertSame(charValue, parameter.resolveInstance(picoContainer, null, Character.TYPE, pn, false, null));
+        assertSame(charValue, parameter.resolveInstance(picoContainer, null, Character.class, pn, false, null));
+    }
+
+    @Test public void testConstantParameterWithPrimitivesRejectsUnexpectedType() throws CompositionException {
+        MutableContainer picoContainer = new DefaultContainer();
+        Byte byteValue = (byte)5;
+        ConstantParameter parameter = new ConstantParameter(byteValue);
+        assertFalse(parameter.isResolvable(picoContainer, null, Integer.TYPE, pn, false, null));
+        Short shortValue = (short)5;
+        parameter = new ConstantParameter(shortValue);
+        assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE, pn, false, null));
+        Integer intValue = 5;
+        parameter = new ConstantParameter(intValue);
+        assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE, pn, false, null));
+        Long longValue = (long)5;
+        parameter = new ConstantParameter(longValue);
+        assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE, pn, false, null));
+        Float floatValue = new Float(5.5);
+        parameter = new ConstantParameter(floatValue);
+        assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE, pn, false, null));
+        Double doubleValue = 5.5;
+        parameter = new ConstantParameter(doubleValue);
+        assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE, pn, false, null));
+        Boolean booleanValue = true;
+        parameter = new ConstantParameter(booleanValue);
+        assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE, pn, false, null));
+        Character charValue = 'x';
+        parameter = new ConstantParameter(charValue);
+        assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE, pn, false, null));
+    }
+
+    @Test public void testKeyClashBug118() throws CompositionException {
+        DefaultContainer pico = new DefaultContainer();
+        pico.addComponent("A", String.class, new ConstantParameter("A"));
+        pico.addComponent("B", String.class, new ConstantParameter("A"));
+        new VerifyingVisitor().traverse(pico);
+    }
+
+}



Mime
View raw message