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    package org.picocontainer.lifecycle;
009    
010    import static org.picocontainer.tck.MockFactory.mockeryWithCountingNamingScheme;
011    
012    import java.io.Serializable;
013    
014    import org.jmock.Expectations;
015    import org.jmock.Mockery;
016    import org.jmock.integration.junit4.JMock;
017    import org.junit.Before;
018    import org.junit.Test;
019    import org.junit.runner.RunWith;
020    import org.picocontainer.Disposable;
021    import org.picocontainer.Startable;
022    import org.picocontainer.monitors.NullComponentMonitor;
023    
024    /**
025     * 
026     * @author Mauro Talevi
027     */
028    @RunWith(JMock.class)
029    public class StartableLifecycleStrategyTestCase {
030    
031            private Mockery mockery = mockeryWithCountingNamingScheme();
032            
033        private StartableLifecycleStrategy strategy;
034        
035        @Before
036        public void setUp(){
037            strategy = new StartableLifecycleStrategy(new NullComponentMonitor());
038        }
039    
040        @Test public void testStartable(){
041            Object startable = mockComponent(true, false);
042            strategy.start(startable);
043            strategy.stop(startable);
044        }
045    
046        @Test public void testDisposable(){
047            Object startable = mockComponent(false, true);
048            strategy.dispose(startable);
049        }
050    
051        @Test public void testSerializable(){
052            Object serializable = mockComponent(false, false);
053            strategy.start(serializable);
054            strategy.stop(serializable);
055            strategy.dispose(serializable);
056        }
057        
058        private Object mockComponent(boolean startable, boolean disposeable) {
059            if ( startable ) {
060                     final Startable mock = mockery.mock(Startable.class);
061                     mockery.checking(new Expectations() {{
062                     one(mock).start(); 
063                     one(mock).stop(); 
064                 }});
065                     return mock;
066            }
067            if ( disposeable ) {
068             final Disposable mock = mockery.mock(Disposable.class);
069             mockery.checking(new Expectations() {{
070                 one(mock).dispose(); 
071             }});
072             return mock;
073            }
074            return mockery.mock(Serializable.class);
075        }
076    }