    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        @Test
    120        public void testFindAdapterOfType() {
    121            ComponentAdapter adapter = new InstanceAdapter("test", "test");
    122            assertEquals(adapter, adapter.findAdapterOfType(InstanceAdapter.class));
    123        }
    124
    125
    126        /**
    127         * {@inheritDoc}
    128         * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#getComponentAdapterType()
    129         */
    130        protected Class getComponentAdapterType() {
    131            return InstanceAdapter.class;
    132        }
    133
    134        /**
    135         * {@inheritDoc}
    136         * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#getComponentAdapterNature()
    137         */
    138        protected int getComponentAdapterNature() {
    139            return super.getComponentAdapterNature() & ~(RESOLVING | VERIFYING | INSTANTIATING );
    140        }
    141
    142        /**
    143         * {@inheritDoc}
    144         * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#prepDEF_verifyWithoutDependencyWorks(org.picocontainer.MutablePicoContainer)
    145         */
    146        protected ComponentAdapter prepDEF_verifyWithoutDependencyWorks(MutablePicoContainer picoContainer) {
    147            return new InstanceAdapter("foo", "bar", new NullLifecycleStrategy(),
    148                                                                            new NullComponentMonitor());
    149        }
    150
    151        /**
    152         * {@inheritDoc}
    153         * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#prepDEF_verifyDoesNotInstantiate(org.picocontainer.MutablePicoContainer)
    154         */
    155        protected ComponentAdapter prepDEF_verifyDoesNotInstantiate(
    156                MutablePicoContainer picoContainer) {
    157            return new InstanceAdapter("Key", 4711, new NullLifecycleStrategy(),
    158                                                                            new NullComponentMonitor());
    159        }
    160
    161        /**
    162         * {@inheritDoc}
    163         * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#prepDEF_visitable()
    164         */
    165        protected ComponentAdapter prepDEF_visitable() {
    166            return new InstanceAdapter("Key", 4711, new NullLifecycleStrategy(),
    167                                                                            new NullComponentMonitor());
    168        }
    169
    170        /**
    171         * {@inheritDoc}
    172         * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#prepSER_isSerializable(org.picocontainer.MutablePicoContainer)
    173         */
    174        protected ComponentAdapter prepSER_isSerializable(MutablePicoContainer picoContainer) {
    175            return new InstanceAdapter("Key", 4711, new NullLifecycleStrategy(),
    176                                                                            new NullComponentMonitor());
    177        }
    178
    179        /**
    180         * {@inheritDoc}
    181         * @see org.picocontainer.tck.AbstractComponentAdapterTestCase#prepSER_isXStreamSerializable(org.picocontainer.MutablePicoContainer)
    182         */
    183        protected ComponentAdapter prepSER_isXStreamSerializable(MutablePicoContainer picoContainer) {
    184            return new InstanceAdapter("Key", 4711, new NullLifecycleStrategy(),
    185                                                                            new NullComponentMonitor());
    186        }
    187
    188    }