    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.parameters;
    011
    012    import static org.junit.Assert.assertEquals;
    013    import static org.junit.Assert.assertFalse;
    014    import static org.junit.Assert.assertNotNull;
    015    import static org.junit.Assert.assertNull;
    016    import static org.junit.Assert.assertSame;
    017    import static org.junit.Assert.assertTrue;
    018    import junit.framework.TestCase;
    019
    020    import org.junit.Test;
    021    import org.picocontainer.ComponentAdapter;
    022    import org.picocontainer.DefaultPicoContainer;
    023    import org.picocontainer.MutablePicoContainer;
    024    import org.picocontainer.NameBinding;
    025    import org.picocontainer.Parameter;
    026    import org.picocontainer.PicoCompositionException;
    027    import org.picocontainer.adapters.NullCA;
    028    import org.picocontainer.testmodel.DependsOnTouchable;
    029    import org.picocontainer.testmodel.SimpleTouchable;
    030    import org.picocontainer.testmodel.Touchable;
    031    import org.picocontainer.visitors.VerifyingVisitor;
    032
    033
    034    /**
    035     * @author Jon Tirsen (tirsen@codehaus.org)
    036     */
    037    public final class ParameterTestCase {
    038
    039        public static class FooNameBinding implements NameBinding {
    040            public String getName() {
    041                return "";
    042            }
    043        }
    044
    045        final NameBinding pn = new FooNameBinding();
    046
    047        @Test public void testComponentParameterFetches() throws PicoCompositionException {
    048            DefaultPicoContainer pico = new DefaultPicoContainer();
    049            ComponentAdapter adapter = pico.addComponent(Touchable.class, SimpleTouchable.class).getComponentAdapter(Touchable.class,
    050                                                                                                                     (NameBinding) null);
    051            assertNotNull(adapter);
    052            assertNotNull(pico.getComponent(Touchable.class));
    053            Touchable touchable = (Touchable) ComponentParameter.DEFAULT.resolve(pico, new NullCA(String.class), null, Touchable.class, pn,
    054                                                                                         false, null).resolveInstance();
    055            assertNotNull(touchable);
    056        }
    057
    058        @Test public void testComponentParameterExcludesSelf() throws PicoCompositionException {
    059            DefaultPicoContainer pico = new DefaultPicoContainer();
    060            ComponentAdapter adapter = pico.addComponent(Touchable.class, SimpleTouchable.class).getComponentAdapter(Touchable.class,
    061                                                                                                                     (NameBinding) null);
    062
    063            assertNotNull(pico.getComponent(Touchable.class));
    064            Touchable touchable = (Touchable) ComponentParameter.DEFAULT.resolve(pico, adapter, null, Touchable.class, pn,
    065                                                                                         false, null).resolveInstance();
    066            assertNull(touchable);
    067        }
    068
    069        @Test public void testConstantParameter() throws PicoCompositionException {
    070            Object value = new Object();
    071            ConstantParameter parameter = new ConstantParameter(value);
    072            MutablePicoContainer picoContainer = new DefaultPicoContainer();
    073            assertSame(value, parameter.resolve(picoContainer, null, null, Object.class, pn, false, null).resolveInstance());
    074        }
    075
    076        @Test public void testDependsOnTouchableWithTouchableSpecifiedAsConstant() throws PicoCompositionException {
    077            DefaultPicoContainer pico = new DefaultPicoContainer();
    078            SimpleTouchable touchable = new SimpleTouchable();
    079            pico.addComponent(DependsOnTouchable.class, DependsOnTouchable.class, new ConstantParameter(touchable));
    080            pico.getComponents();
    081            assertTrue(touchable.wasTouched);
    082        }
    083
    084        @Test public void testComponentParameterRespectsExpectedType() {
    085            MutablePicoContainer picoContainer = new DefaultPicoContainer();
    086            ComponentAdapter adapter = picoContainer.addComponent(Touchable.class, SimpleTouchable.class).getComponentAdapter(Touchable.class,
    087                                                                                                                              (NameBinding) null);
    088            assertNull(ComponentParameter.DEFAULT.resolve(picoContainer, adapter, null, TestCase.class, pn, false, null).resolveInstance());
    089        }
    090
    091            @Test public void testComponentParameterResolvesPrimitiveType() {
    092            MutablePicoContainer picoContainer = new DefaultPicoContainer();
    093            ComponentAdapter adapter = picoContainer.addComponent("glarch", 239).getComponentAdapter("glarch");
    094            assertNotNull(adapter);
    095                    Parameter parameter = new ComponentParameter("glarch");
    096                    assertNotNull(parameter.resolve(picoContainer,adapter, null, Integer.TYPE, pn, false, null).resolveInstance());
    097                    assertEquals(239, ((Integer)parameter.resolve(picoContainer,adapter, null, Integer.TYPE, pn, false, null).resolveInstance()).intValue());
    098            }
    099
    100        @Test public void testConstantParameterRespectsExpectedType() {
    101            MutablePicoContainer picoContainer = new DefaultPicoContainer();
    102            Parameter parameter = new ConstantParameter(new SimpleTouchable());
    103            ComponentAdapter adapter = picoContainer.addComponent(Touchable.class, SimpleTouchable.class).getComponentAdapter(Touchable.class,
    104                                                                                                                              (NameBinding) null);
    105            assertFalse(parameter.resolve(picoContainer, adapter, null, TestCase.class, pn, false, null).isResolved());
    106        }
    107
    108        @Test public void testParameterRespectsExpectedType() throws PicoCompositionException {
    109            Parameter parameter = new ConstantParameter(Touchable.class);
    110            MutablePicoContainer picoContainer = new DefaultPicoContainer();
    111            assertFalse(parameter.resolve(picoContainer, null, null, TestCase.class, pn, false, null).isResolved());
    112
    113            ComponentAdapter adapter = picoContainer.addComponent(Touchable.class, SimpleTouchable.class).getComponentAdapter(Touchable.class,
    114                                                                                                                              (NameBinding) null);
    115
    116            assertNull(ComponentParameter.DEFAULT.resolve(picoContainer, adapter, null, TestCase.class, pn, false, null).resolveInstance());
    117        }
    118
    119        @Test public void testConstantParameterWithPrimitives() throws PicoCompositionException {
    120            MutablePicoContainer picoContainer = new DefaultPicoContainer();
    121            Byte byteValue = (byte)5;
    122            ConstantParameter parameter = new ConstantParameter(byteValue);
    123            assertSame(byteValue, parameter.resolve(picoContainer, null, null, Byte.TYPE, pn, false, null).resolveInstance());
    124            assertSame(byteValue, parameter.resolve(picoContainer, null, null, Byte.class, pn, false, null).resolveInstance());
    125            Short shortValue = (short)5;
    126            parameter = new ConstantParameter(shortValue);
    127            assertSame(shortValue, parameter.resolve(picoContainer, null, null, Short.TYPE, pn, false, null).resolveInstance());
    128            assertSame(shortValue, parameter.resolve(picoContainer, null, null, Short.class, pn, false, null).resolveInstance());
    129            Integer intValue = 5;
    130            parameter = new ConstantParameter(intValue);
    131            assertSame(intValue, parameter.resolve(picoContainer, null, null, Integer.TYPE, pn, false, null).resolveInstance());
    132            assertSame(intValue, parameter.resolve(picoContainer, null, null, Integer.class, pn, false, null).resolveInstance());
    133            Long longValue = (long)5;
    134            parameter = new ConstantParameter(longValue);
    135            assertSame(longValue, parameter.resolve(picoContainer, null, null, Long.TYPE, pn, false, null).resolveInstance());
    136            assertSame(longValue, parameter.resolve(picoContainer, null, null, Long.class, pn, false, null).resolveInstance());
    137            Float floatValue = new Float(5.5);
    138            parameter = new ConstantParameter(floatValue);
    139            assertSame(floatValue, parameter.resolve(picoContainer, null, null, Float.TYPE, pn, false, null).resolveInstance());
    140            assertSame(floatValue, parameter.resolve(picoContainer, null, null, Float.class, pn, false, null).resolveInstance());
    141            Double doubleValue = 5.5;
    142            parameter = new ConstantParameter(doubleValue);
    143            assertSame(doubleValue, parameter.resolve(picoContainer, null, null, Double.TYPE, pn, false, null).resolveInstance());
    144            assertSame(doubleValue, parameter.resolve(picoContainer, null, null, Double.class, pn, false, null).resolveInstance());
    145            Boolean booleanValue = true;
    146            parameter = new ConstantParameter(booleanValue);
    147            assertSame(booleanValue, parameter.resolve(picoContainer, null, null, Boolean.TYPE, pn, false, null).resolveInstance());
    148            assertSame(booleanValue, parameter.resolve(picoContainer, null, null, Boolean.class, pn, false, null).resolveInstance());
    149            Character charValue = 'x';
    150            parameter = new ConstantParameter(charValue);
    151            assertSame(charValue, parameter.resolve(picoContainer, null, null, Character.TYPE, pn, false, null).resolveInstance());
    152            assertSame(charValue, parameter.resolve(picoContainer, null, null, Character.class, pn, false, null).resolveInstance());
    153        }
    154
    155        @Test public void testConstantParameterWithPrimitivesRejectsUnexpectedType() throws PicoCompositionException {
    156            MutablePicoContainer picoContainer = new DefaultPicoContainer();
    157            Byte byteValue = (byte)5;
    158            ConstantParameter parameter = new ConstantParameter(byteValue);
    159            assertFalse(parameter.resolve(picoContainer, null, null, Integer.TYPE, pn, false, null).isResolved());
    160            Short shortValue = (short)5;
    161            parameter = new ConstantParameter(shortValue);
    162            assertFalse(parameter.resolve(picoContainer, null, null, Byte.TYPE, pn, false, null).isResolved());
    163            Integer intValue = 5;
    164            parameter = new ConstantParameter(intValue);
    165            assertFalse(parameter.resolve(picoContainer, null, null, Byte.TYPE, pn, false, null).isResolved());
    166            Long longValue = (long)5;
    167            parameter = new ConstantParameter(longValue);
    168            assertFalse(parameter.resolve(picoContainer, null, null, Byte.TYPE, pn, false, null).isResolved());
    169            Float floatValue = new Float(5.5);
    170            parameter = new ConstantParameter(floatValue);
    171            assertFalse(parameter.resolve(picoContainer, null, null, Byte.TYPE, pn, false, null).isResolved());
    172            Double doubleValue = 5.5;
    173            parameter = new ConstantParameter(doubleValue);
    174            assertFalse(parameter.resolve(picoContainer, null, null, Byte.TYPE, pn, false, null).isResolved());
    175            Boolean booleanValue = true;
    176            parameter = new ConstantParameter(booleanValue);
    177            assertFalse(parameter.resolve(picoContainer, null, null, Byte.TYPE, pn, false, null).isResolved());
    178            Character charValue = 'x';
    179            parameter = new ConstantParameter(charValue);
    180            assertFalse(parameter.resolve(picoContainer, null, null, Byte.TYPE, pn, false, null).isResolved());
    181        }
    182
    183        @Test public void testKeyClashBug118() throws PicoCompositionException {
    184            DefaultPicoContainer pico = new DefaultPicoContainer();
    185            pico.addComponent("A", String.class, new ConstantParameter("A"));
    186            pico.addComponent("B", String.class, new ConstantParameter("A"));
    187            new VerifyingVisitor().traverse(pico);
    188        }
    189
    190    }