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