    001    /*****************************************************************************
    002     * Copyright (C) PicoContainer Organization. All rights reserved.            *
    003     * ------------------------------------------------------------------------- *
    004     * The software in this package is published under the terms of the BSD      *
    005     * style license a copy of which has been included with this distribution in *
    006     * the LICENSE.txt file.                                                     *
    007     *                                                                           *
    008     * Original code by                                                          *
    009     *****************************************************************************/
    010    package org.picocontainer.tck;
    011
    012    import static org.junit.Assert.assertEquals;
    013    import static org.junit.Assert.fail;
    014
    015    import org.junit.Test;
    016    import org.picocontainer.MutablePicoContainer;
    017    import org.picocontainer.Parameter;
    018    import org.picocontainer.PicoCompositionException;
    019    import org.picocontainer.PicoException;
    020    import org.picocontainer.parameters.ComponentParameter;
    021    import org.picocontainer.parameters.ConstantParameter;
    022
    023
    024    /**
    025     * @author Aslak Helles&oslash;y
    026     */
    027    public abstract class AbstractMultipleConstructorTest {
    028
    029        protected abstract MutablePicoContainer createPicoContainer();
    030
    031        public static final class Multi {
    032            public final String message;
    033
    034            public Multi(One one, Two two, Three three) {
    035                message = "one two three";
    036            }
    037
    038            public Multi(One one, Two two) {
    039                message = "one two";
    040            }
    041
    042            public Multi(Two two, One one) {
    043                message = "two one";
    044            }
    045
    046            public Multi(Two two, Three three) {
    047                message = "two three";
    048            }
    049
    050            public Multi(Three three, One one) {
    051                message = "three one";
    052            }
    053
    054            public Multi(One one, String string) {
    055                message = "one string";
    056            }
    057
    058            public Multi(One one, int i) {
    059                message = "one int";
    060            }
    061
    062            public Multi() {
    063                message = "none";
    064            }
    065        }
    066
    067        public static class One {
    068        }
    069
    070        public static class Two {
    071        }
    072
    073        public static class Three {
    074        }
    075
    076
    077        @Test public void testStringWorks() throws PicoException {
    078            MutablePicoContainer pico = createPicoContainer();
    079            pico.addComponent(String.class);
    080            assertEquals("", pico.getComponent(String.class));
    081        }
    082
    083        @Test public void testMultiWithOnlySmallSatisfiedDependencyWorks() throws PicoException {
    084            MutablePicoContainer pico = createPicoContainer();
    085            pico.addComponent(Multi.class);
    086            pico.addComponent(One.class);
    087            pico.addComponent(Three.class);
    088
    089            Multi multi = pico.getComponent(Multi.class);
    090            assertEquals("three one", multi.message);
    091        }
    092
    093        @Test public void testMultiWithBothSatisfiedDependencyWorks() throws PicoException {
    094            MutablePicoContainer pico = createPicoContainer();
    095            pico.addComponent(Multi.class);
    096            pico.addComponent(One.class);
    097            pico.addComponent(Two.class);
    098            pico.addComponent(Three.class);
    099
    100            Multi multi = pico.getComponent(Multi.class);
    101            assertEquals("one two three", multi.message);
    102        }
    103
    104        @Test public void testMultiWithTwoEquallyBigSatisfiedDependenciesFails() throws PicoException {
    105            MutablePicoContainer pico = createPicoContainer();
    106            pico.addComponent(Multi.class);
    107            pico.addComponent(One.class);
    108            pico.addComponent(Two.class);
    109
    110            try {
    111                pico.getComponent(Multi.class);
    112                fail();
    113            } catch (PicoCompositionException e) {
    114                assertEquals("3 satisfiable constructors is too many for 'class org.picocontainer.tck.AbstractMultipleConstructorTest$Multi'. Constructor List:[<init>(), <init>(org.picocontainer.tck.AbstractMultipleConstructorTest$One,org.picocontainer.tck.AbstractMultipleConstructorTest$Two), <init>(org.picocontainer.tck.AbstractMultipleConstructorTest$Two,org.picocontainer.tck.AbstractMultipleConstructorTest$One)]",
    115                        e.getMessage());
    116            }
    117        }
    118
    119        @Test public void testMultiWithSatisfyingDependencyAndParametersWorks() throws PicoException {
    120            MutablePicoContainer pico = createPicoContainer();
    121            pico.addComponent("MultiOneTwo", Multi.class, ComponentParameter.DEFAULT,
    122                    new ComponentParameter("Two"));
    123            pico.addComponent("MultiTwoOne", Multi.class, new ComponentParameter("Two"),
    124                    ComponentParameter.DEFAULT);
    125            pico.addComponent("MultiOneString", Multi.class, ComponentParameter.DEFAULT,
    126                    new ConstantParameter(""));
    127            pico.addComponent("MultiOneInt", Multi.class, ComponentParameter.DEFAULT,
    128                    new ConstantParameter(5));
    129            pico.addComponent("MultiNone", Multi.class, Parameter.ZERO);
    130            pico.addComponent(One.class);
    131            pico.addComponent("Two", Two.class);
    132
    133            Multi multiOneTwo = (Multi) pico.getComponent("MultiOneTwo");
    134            assertEquals("one two", multiOneTwo.message);
    135            Multi multiTwoOne = (Multi) pico.getComponent("MultiTwoOne");
    136            assertEquals("two one", multiTwoOne.message);
    137            Multi multiOneString = (Multi) pico.getComponent("MultiOneString");
    138            assertEquals("one string", multiOneString.message);
    139            Multi multiOneInt = (Multi) pico.getComponent("MultiOneInt");
    140            assertEquals("one int", multiOneInt.message);
    141            Multi multiNone = (Multi) pico.getComponent("MultiNone");
    142            assertEquals("none", multiNone.message);
    143        }
    144    }