    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    import static org.junit.Assert.assertEquals;
    013    import static org.junit.Assert.assertNotNull;
    014    import static org.picocontainer.Characteristics.AUTOMATIC;
    015    import static org.picocontainer.behaviors.Behaviors.automatic;
    016    import static org.picocontainer.behaviors.Behaviors.caching;
    017
    018    import org.junit.Test;
    019    import org.picocontainer.DefaultPicoContainer;
    020    import org.picocontainer.MutablePicoContainer;
    021    import org.picocontainer.PicoBuilder;
    022    import org.picocontainer.injectors.ConstructorInjector;
    023    import org.picocontainer.lifecycle.NullLifecycleStrategy;
    024    import org.picocontainer.monitors.NullComponentMonitor;
    025
    026    public class AutomatingTestCase {
    027
    028        private static String MESSAGE =
    029            "Foo was instantiated, even though it was not required to be given it was not depended on by anything looked up";
    030
    031        public static class Foo {
    032            public Foo(StringBuilder sb) {
    033                sb.append(MESSAGE);
    034            }
    035        }
    036
    037        public static class Bar {
    038        }
    039
    040        @Test public void testAutomaticBehavior() {
    041            DefaultPicoContainer pico = new DefaultPicoContainer(new Caching().wrap(new Automating()));
    042            pico.addComponent(StringBuilder.class);
    043            pico.addComponent(Foo.class);
    044            pico.addComponent(Bar.class);
    045            pico.start();
    046            assertNotNull(pico.getComponent(Bar.class));
    047            StringBuilder sb = pico.getComponent(StringBuilder.class);
    048            assertEquals(MESSAGE, sb.toString());
    049            assertEquals("Cached+Lifecycle:Automated:ConstructorInjector-class org.picocontainer.behaviors.AutomatingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
    050        }
    051
    052        @Test public void testAutomaticBehaviorViaAdapter() {
    053            DefaultPicoContainer pico = new DefaultPicoContainer(new Caching().wrap(new Automating()));
    054            pico.addComponent(StringBuilder.class);
    055            pico.addAdapter(new ConstructorInjector(Foo.class, Foo.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(), false));
    056            pico.addComponent(Bar.class);
    057            pico.start();
    058            assertNotNull(pico.getComponent(Bar.class));
    059            StringBuilder sb = pico.getComponent(StringBuilder.class);
    060            assertEquals(MESSAGE, sb.toString());
    061            assertEquals("Cached+Lifecycle:Automated:ConstructorInjector-class org.picocontainer.behaviors.AutomatingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
    062        }
    063
    064        @Test public void testNonAutomaticBehaviorAsContrastToTheAbove() {
    065            DefaultPicoContainer pico = new DefaultPicoContainer(new Caching());
    066            pico.addComponent(StringBuilder.class);
    067            pico.addComponent(Foo.class);
    068            pico.addComponent(Bar.class);
    069            pico.start();
    070            assertNotNull(pico.getComponent(Bar.class));
    071            StringBuilder sb = pico.getComponent(StringBuilder.class);
    072            assertEquals("", sb.toString());
    073        }
    074
    075        @Test public void testNonAutomaticBehaviorAsContrastToTheAboveViaAdapter() {
    076            DefaultPicoContainer pico = new DefaultPicoContainer(new Caching());
    077            pico.addComponent(StringBuilder.class);
    078            pico.addAdapter(new ConstructorInjector(Foo.class, Foo.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(), false));
    079            pico.addComponent(Bar.class);
    080            pico.start();
    081            assertNotNull(pico.getComponent(Bar.class));
    082            StringBuilder sb = pico.getComponent(StringBuilder.class);
    083            assertEquals("", sb.toString());
    084        }
    085
    086        @Test public void testAutomaticBehaviorByBuilder() {
    087            MutablePicoContainer pico = new PicoBuilder().withCaching().withAutomatic().build();
    088            pico.addComponent(StringBuilder.class);
    089            pico.addComponent(Foo.class);
    090            pico.addComponent(Bar.class);
    091            pico.start();
    092            assertNotNull(pico.getComponent(Bar.class));
    093            StringBuilder sb = pico.getComponent(StringBuilder.class);
    094            assertEquals(MESSAGE, sb.toString());
    095            assertEquals("Cached+Lifecycle:Automated:ConstructorInjector-class org.picocontainer.behaviors.AutomatingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
    096        }
    097
    098        @Test public void testAutomaticBehaviorByBuilderViaAdapter() {
    099            MutablePicoContainer pico = new PicoBuilder().withCaching().withAutomatic().build();
    100            pico.addComponent(StringBuilder.class);
    101            pico.addAdapter(new ConstructorInjector(Foo.class, Foo.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(), false));
    102            pico.addComponent(Bar.class);
    103            pico.start();
    104            assertNotNull(pico.getComponent(Bar.class));
    105            StringBuilder sb = pico.getComponent(StringBuilder.class);
    106            assertEquals(MESSAGE, sb.toString());
    107            assertEquals("Cached+Lifecycle:Automated:ConstructorInjector-class org.picocontainer.behaviors.AutomatingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
    108        }
    109
    110        @Test public void testAutomaticBehaviorByBuilderADifferentWay() {
    111            MutablePicoContainer pico = new PicoBuilder().withBehaviors(caching(), automatic()).build();
    112            pico.addComponent(StringBuilder.class);
    113            pico.addComponent(Foo.class);
    114            pico.addComponent(Bar.class);
    115            pico.start();
    116            assertNotNull(pico.getComponent(Bar.class));
    117            StringBuilder sb = pico.getComponent(StringBuilder.class);
    118            assertEquals(MESSAGE, sb.toString());
    119            assertEquals("Cached+Lifecycle:Automated:ConstructorInjector-class org.picocontainer.behaviors.AutomatingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
    120        }
    121
    122            @Test public void testAutomaticBehaviorByBuilderADifferentWayViaAdapter() {
    123            MutablePicoContainer pico = new PicoBuilder().withBehaviors(caching(), automatic()).build();
    124            pico.addComponent(StringBuilder.class);
    125            pico.addAdapter(new ConstructorInjector(Foo.class, Foo.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(), false));
    126            pico.addComponent(Bar.class);
    127            pico.start();
    128            assertNotNull(pico.getComponent(Bar.class));
    129            StringBuilder sb = pico.getComponent(StringBuilder.class);
    130            assertEquals(MESSAGE, sb.toString());
    131                assertEquals("Cached+Lifecycle:Automated:ConstructorInjector-class org.picocontainer.behaviors.AutomatingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
    132        }
    133
    134        @Test public void testAutomaticBehaviorWorksForAdaptiveBehaviorToo() {
    135            MutablePicoContainer pico = new PicoBuilder().withBehaviors(caching(), automatic()).build();
    136            pico.addComponent(StringBuilder.class);
    137            pico.as(AUTOMATIC).addComponent(Foo.class);
    138            pico.addComponent(Bar.class);
    139            pico.start();
    140            assertNotNull(pico.getComponent(Bar.class));
    141            StringBuilder sb = pico.getComponent(StringBuilder.class);
    142            assertEquals(MESSAGE, sb.toString());
    143            assertEquals("Cached+Lifecycle:Automated:ConstructorInjector-class org.picocontainer.behaviors.AutomatingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
    144        }
    145
    146        @Test public void testAutomaticBehaviorWorksForAdaptiveBehaviorTooViaAdapter() {
    147            MutablePicoContainer pico = new PicoBuilder().withBehaviors(caching(), automatic()).build();
    148            pico.addComponent(StringBuilder.class);
    149            pico.as(AUTOMATIC).addAdapter(new ConstructorInjector(Foo.class, Foo.class, null, new NullComponentMonitor(), new NullLifecycleStrategy(), false));
    150            pico.addComponent(Bar.class);
    151            pico.start();
    152            assertNotNull(pico.getComponent(Bar.class));
    153            StringBuilder sb = pico.getComponent(StringBuilder.class);
    154            assertEquals(MESSAGE, sb.toString());
    155            assertEquals("Cached+Lifecycle:Automated:ConstructorInjector-class org.picocontainer.behaviors.AutomatingTestCase$Foo", pico.getComponentAdapter(Foo.class).toString());
    156        }
    157
    158    }