    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 Joerg Schaible                                           *
    009     *****************************************************************************/
    010    package org.picocontainer.adapters;
    011
    012
    013    import static org.junit.Assert.assertEquals;
    014    import static org.junit.Assert.assertSame;
    015    import static org.junit.Assert.fail;
    016
    017    import java.util.Map;
    018
    019    import org.junit.Test;
    020    import org.picocontainer.ComponentAdapter;
    021    import org.picocontainer.DefaultPicoContainer;
    022    import org.picocontainer.Disposable;
    023    import org.picocontainer.MutablePicoContainer;
    024    import org.picocontainer.PicoContainer;
    025    import org.picocontainer.Startable;
    026    import org.picocontainer.lifecycle.NullLifecycleStrategy;
    027    import org.picocontainer.lifecycle.StartableLifecycleStrategy;
    028    import org.picocontainer.monitors.NullComponentMonitor;
    029    import org.picocontainer.tck.AbstractComponentAdapterTest;
    030    import org.picocontainer.testmodel.NullLifecycle;
    031    import org.picocontainer.testmodel.SimpleTouchable;
    032    import org.picocontainer.testmodel.Touchable;
    033
    034
    035    /**
    036     * Test the InstanceAdapter.
    037     *
    038     * @author J&ouml;rg Schaible
    039     */
    040    public final class InstanceAdapterTestCase extends AbstractComponentAdapterTest {
    041
    042        @Test public void testComponentAdapterReturnsSame() {
    043            final Touchable touchable = new SimpleTouchable();
    044            final ComponentAdapter componentAdapter = new InstanceAdapter(Touchable.class, touchable, new NullLifecycleStrategy(),
    045                                                                            new NullComponentMonitor());
    046            assertSame(touchable, componentAdapter.getComponentInstance(null, null));
    047        }
    048
    049        @Test public void testDefaultLifecycleStrategy() {
    050            LifecycleComponent component = new LifecycleComponent();
    051            InstanceAdapter adapter =
    052                new InstanceAdapter(LifecycleComponent.class, component, new StartableLifecycleStrategy(new NullComponentMonitor()),
    053                                                                            new NullComponentMonitor());
    054            PicoContainer pico = new DefaultPicoContainer();
    055            adapter.start(pico);
    056            adapter.stop(pico);
    057            adapter.dispose(pico);
    058            assertEquals("start>stop>dispose>", component.buffer.toString());
    059            adapter.start(component);
    060            adapter.stop(component);
    061            adapter.dispose(component);
    062            assertEquals("start>stop>dispose>start>stop>dispose>", component.buffer.toString());
    063        }
    064
    065        private static final class LifecycleComponent implements Startable, Disposable {
    066            final StringBuffer buffer = new StringBuffer();
    067
    068            public void start() {
    069                buffer.append("start>");
    070            }
    071
    072            public void stop() {
    073                buffer.append("stop>");
    074            }
    075
    076            public void dispose() {
    077                buffer.append("dispose>");
    078            }
    079        }
    080
    081        @Test public void testCustomLifecycleCanBeInjected() {
    082            NullLifecycle component = new NullLifecycle();
    083            RecordingLifecycleStrategy strategy = new RecordingLifecycleStrategy(new StringBuffer());
    084            InstanceAdapter adapter = new InstanceAdapter(NullLifecycle.class, component, strategy, new NullComponentMonitor());
    085            PicoContainer pico = new DefaultPicoContainer();
    086            adapter.start(pico);
    087            adapter.stop(pico);
    088            adapter.dispose(pico);
    089            assertEquals("<start<stop<dispose", strategy.recording());
    090            adapter.start(component);
    091            adapter.stop(component);
    092            adapter.dispose(component);
    093            assertEquals("<start<stop<dispose<start<stop<dispose", strategy.recording());
    094        }
    095
    096        @Test public void testComponentAdapterCanIgnoreLifecycle() {
    097            final Touchable touchable = new SimpleTouchable();
    098            InstanceAdapter adapter = new InstanceAdapter(Touchable.class, touchable, new NullLifecycleStrategy(),
    099                                                                            new NullComponentMonitor());
    100            PicoContainer pico = new DefaultPicoContainer();
    101            adapter.start(pico);
    102            adapter.stop(pico);
    103            adapter.dispose(pico);
    104            adapter.start(touchable);
    105            adapter.stop(touchable);
    106            adapter.dispose(touchable);
    107        }
    108
    109        @Test public void testGuardAgainstNullInstance() {
    110            try {
    111                new InstanceAdapter(Map.class, null, new NullLifecycleStrategy(),
    112                                                                            new NullComponentMonitor());
    113                fail("should have barfed");
    114            } catch (NullPointerException e) {
    115                assertEquals("componentInstance cannot be null", e.getMessage());
    116            }
    117        }
    118
    119
    120        /**
    121         * {@inheritDoc}
    122         * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#getComponentAdapterType()
    123         */
    124        protected Class getComponentAdapterType() {
    125            return InstanceAdapter.class;
    126        }
    127
    128        /**
    129         * {@inheritDoc}
    130         * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#getComponentAdapterNature()
    131         */
    132        protected int getComponentAdapterNature() {
    133            return super.getComponentAdapterNature() & ~(RESOLVING | VERIFYING | INSTANTIATING );
    134        }
    135
    136        /**
    137         * {@inheritDoc}
    138         * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#prepDEF_verifyWithoutDependencyWorks(org.picocontainer.MutablePicoContainer)
    139         */
    140        protected ComponentAdapter prepDEF_verifyWithoutDependencyWorks(MutablePicoContainer picoContainer) {
    141            return new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(),
    142                                                                            new NullComponentMonitor());
    143        }
    144
    145        /**
    146         * {@inheritDoc}
    147         * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#prepDEF_verifyDoesNotInstantiate(org.picocontainer.MutablePicoContainer)
    148         */
    149        protected ComponentAdapter prepDEF_verifyDoesNotInstantiate(
    150                MutablePicoContainer picoContainer) {
    151            return new InstanceAdapter("Key", 4711, new NullLifecycleStrategy(),
    152                                                                            new NullComponentMonitor());
    153        }
    154
    155        /**
    156         * {@inheritDoc}
    157         * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#prepDEF_visitable()
    158         */
    159        protected ComponentAdapter prepDEF_visitable() {
    160            return new InstanceAdapter("Key", 4711, new NullLifecycleStrategy(),
    161                                                                            new NullComponentMonitor());
    162        }
    163
    164        /**
    165         * {@inheritDoc}
    166         * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#prepSER_isSerializable(org.picocontainer.MutablePicoContainer)
    167         */
    168        protected ComponentAdapter prepSER_isSerializable(MutablePicoContainer picoContainer) {
    169            return new InstanceAdapter("Key", 4711, new NullLifecycleStrategy(),
    170                                                                            new NullComponentMonitor());
    171        }
    172
    173        /**
    174         * {@inheritDoc}
    175         * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#prepSER_isXStreamSerializable(org.picocontainer.MutablePicoContainer)
    176         */
    177        protected ComponentAdapter prepSER_isXStreamSerializable(MutablePicoContainer picoContainer) {
    178            return new InstanceAdapter("Key", 4711, new NullLifecycleStrategy(),
    179                                                                            new NullComponentMonitor());
    180        }
    181
    182    }