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