    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.behaviors;
    011
    012
    013    import static org.junit.Assert.assertEquals;
    014    import static org.junit.Assert.fail;
    015
    016    import org.junit.Test;
    017    import org.picocontainer.Characteristics;
    018    import org.picocontainer.ComponentAdapter;
    019    import org.picocontainer.ComponentFactory;
    020    import org.picocontainer.DefaultPicoContainer;
    021    import static org.picocontainer.Characteristics.GUARD;
    022    import org.picocontainer.adapters.InstanceAdapter;
    023    import org.picocontainer.injectors.ConstructorInjection;
    024    import org.picocontainer.injectors.ConstructorInjector;
    025    import org.picocontainer.lifecycle.NullLifecycleStrategy;
    026    import org.picocontainer.monitors.NullComponentMonitor;
    027    import org.picocontainer.tck.AbstractComponentFactoryTest;
    028
    029
    030    /**
    031     * @author Paul Hammant
    032     */
    033    public class GuardingTestCase extends AbstractComponentFactoryTest {
    034
    035        protected ComponentFactory createComponentFactory() {
    036            return new Guarding().wrap(new ConstructorInjection());
    037        }
    038
    039        @Test public void testAddComponentUsesGuardingBehavior() {
    040            DefaultPicoContainer pico = new DefaultPicoContainer(createComponentFactory());
    041            pico.addComponent("guard", MyGuard.class);
    042            pico.as(GUARD).addComponent("foo", String.class);
    043            ComponentAdapter fooAdapter = pico.getComponentAdapter("foo");
    044            assertEquals(Guarded.class, fooAdapter.getClass());
    045            assertEquals(ConstructorInjector.class, ((AbstractBehavior) fooAdapter).getDelegate().getClass());
    046            try {
    047                String foo = (String) pico.getComponent("foo");
    048                fail("should have barfed");
    049            } catch (Exception e) {
    050                assertEquals("not so fast", e.getMessage());
    051            }
    052        }
    053
    054        public static class MyGuard {
    055            public MyGuard() {
    056                throw new RuntimeException("not so fast");
    057            }
    058        }
    059
    060        @Test public void testAddComponentDoesNotUseGuardingBehaviorIfNoProperty() {
    061            DefaultPicoContainer pico = new DefaultPicoContainer(createComponentFactory());
    062            pico.addComponent("guard", MyGuard.class);
    063            pico.addComponent("foo", String.class);
    064            ComponentAdapter fooAdapter = pico.getComponentAdapter("foo");
    065            assertEquals(ConstructorInjector.class, fooAdapter.getClass());
    066            String foo = (String) pico.getComponent("foo");
    067        }
    068
    069        @Test public void testAddComponentUsesGuardingBehaviorWithCustomGuardKey() {
    070            DefaultPicoContainer pico = new DefaultPicoContainer(createComponentFactory());
    071            pico.addComponent("my_guard", MyGuard.class);
    072            pico.as(GUARD("my_guard")).addComponent("foo", String.class);
    073            ComponentAdapter fooAdapter = pico.getComponentAdapter("foo");
    074            assertEquals(Guarded.class, fooAdapter.getClass());
    075            assertEquals(ConstructorInjector.class, ((AbstractBehavior) fooAdapter).getDelegate().getClass());
    076            try {
    077                String foo = (String) pico.getComponent("foo");
    078                fail("should have barfed");
    079            } catch (Exception e) {
    080                assertEquals("not so fast", e.getMessage());
    081            }
    082        }
    083
    084        @Test public void testAddComponentUsesGuardingBehaviorByAdapitveDefault() {
    085            DefaultPicoContainer pico = new DefaultPicoContainer();
    086            pico.addComponent("guard", MyGuard.class);
    087            pico.as(GUARD).addComponent("foo", String.class);
    088            ComponentAdapter fooAdapter = pico.getComponentAdapter("foo");
    089            assertEquals(Guarded.class, fooAdapter.getClass());
    090            assertEquals(ConstructorInjector.class, ((AbstractBehavior) fooAdapter).getDelegate().getClass());
    091            try {
    092                String foo = (String) pico.getComponent("foo");
    093                fail("should have barfed");
    094            } catch (Exception e) {
    095                assertEquals("not so fast", e.getMessage());
    096            }
    097        }
    098
    099    }