    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.resolveInstance(pico, new NullCA(String.class), Touchable.class, pn,
    054                                                                                         false, null);
    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.resolveInstance(pico, adapter, Touchable.class, pn,
    065                                                                                         false, null);
    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.resolveInstance(picoContainer, null, Object.class, pn, false, null));
    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.resolveInstance(picoContainer, adapter, TestCase.class, pn, false, null));
    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.resolveInstance(picoContainer,adapter,Integer.TYPE, pn, false, null));
    097                    assertEquals(239, ((Integer)parameter.resolveInstance(picoContainer,adapter,Integer.TYPE, pn, false, null)).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.isResolvable(picoContainer, adapter, TestCase.class, pn, false, null));
    106        }
    107
    108        @Test public void testParameterRespectsExpectedType() throws PicoCompositionException {
    109            Parameter parameter = new ConstantParameter(Touchable.class);
    110            MutablePicoContainer picoContainer = new DefaultPicoContainer();
    111            assertFalse(parameter.isResolvable(picoContainer, null, TestCase.class, pn, false, null));
    112
    113            ComponentAdapter adapter = picoContainer.addComponent(Touchable.class, SimpleTouchable.class).getComponentAdapter(Touchable.class,
    114                                                                                                                              (NameBinding) null);
    115
    116            assertNull(ComponentParameter.DEFAULT.resolveInstance(picoContainer, adapter, TestCase.class, pn, false, null));
    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.resolveInstance(picoContainer, null, Byte.TYPE, pn, false, null));
    124            assertSame(byteValue, parameter.resolveInstance(picoContainer, null, Byte.class, pn, false, null));
    125            Short shortValue = (short)5;
    126            parameter = new ConstantParameter(shortValue);
    127            assertSame(shortValue, parameter.resolveInstance(picoContainer, null, Short.TYPE, pn, false, null));
    128            assertSame(shortValue, parameter.resolveInstance(picoContainer, null, Short.class, pn, false, null));
    129            Integer intValue = 5;
    130            parameter = new ConstantParameter(intValue);
    131            assertSame(intValue, parameter.resolveInstance(picoContainer, null, Integer.TYPE, pn, false, null));
    132            assertSame(intValue, parameter.resolveInstance(picoContainer, null, Integer.class, pn, false, null));
    133            Long longValue = (long)5;
    134            parameter = new ConstantParameter(longValue);
    135            assertSame(longValue, parameter.resolveInstance(picoContainer, null, Long.TYPE, pn, false, null));
    136            assertSame(longValue, parameter.resolveInstance(picoContainer, null, Long.class, pn, false, null));
    137            Float floatValue = new Float(5.5);
    138            parameter = new ConstantParameter(floatValue);
    139            assertSame(floatValue, parameter.resolveInstance(picoContainer, null, Float.TYPE, pn, false, null));
    140            assertSame(floatValue, parameter.resolveInstance(picoContainer, null, Float.class, pn, false, null));
    141            Double doubleValue = 5.5;
    142            parameter = new ConstantParameter(doubleValue);
    143            assertSame(doubleValue, parameter.resolveInstance(picoContainer, null, Double.TYPE, pn, false, null));
    144            assertSame(doubleValue, parameter.resolveInstance(picoContainer, null, Double.class, pn, false, null));
    145            Boolean booleanValue = true;
    146            parameter = new ConstantParameter(booleanValue);
    147            assertSame(booleanValue, parameter.resolveInstance(picoContainer, null, Boolean.TYPE, pn, false, null));
    148            assertSame(booleanValue, parameter.resolveInstance(picoContainer, null, Boolean.class, pn, false, null));
    149            Character charValue = 'x';
    150            parameter = new ConstantParameter(charValue);
    151            assertSame(charValue, parameter.resolveInstance(picoContainer, null, Character.TYPE, pn, false, null));
    152            assertSame(charValue, parameter.resolveInstance(picoContainer, null, Character.class, pn, false, null));
    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.isResolvable(picoContainer, null, Integer.TYPE, pn, false, null));
    160            Short shortValue = (short)5;
    161            parameter = new ConstantParameter(shortValue);
    162            assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE, pn, false, null));
    163            Integer intValue = 5;
    164            parameter = new ConstantParameter(intValue);
    165            assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE, pn, false, null));
    166            Long longValue = (long)5;
    167            parameter = new ConstantParameter(longValue);
    168            assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE, pn, false, null));
    169            Float floatValue = new Float(5.5);
    170            parameter = new ConstantParameter(floatValue);
    171            assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE, pn, false, null));
    172            Double doubleValue = 5.5;
    173            parameter = new ConstantParameter(doubleValue);
    174            assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE, pn, false, null));
    175            Boolean booleanValue = true;
    176            parameter = new ConstantParameter(booleanValue);
    177            assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE, pn, false, null));
    178            Character charValue = 'x';
    179            parameter = new ConstantParameter(charValue);
    180            assertFalse(parameter.isResolvable(picoContainer, null, Byte.TYPE, pn, false, null));
    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    }