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                                                          *
009     *****************************************************************************/
010    package org.picocontainer.behaviors;
011    
012    import static org.junit.Assert.assertEquals;
013    import static org.junit.Assert.assertNotNull;
014    import static org.junit.Assert.fail;
015    import static org.picocontainer.tck.MockFactory.mockeryWithCountingNamingScheme;
016    
017    import org.jmock.Expectations;
018    import org.jmock.Mockery;
019    import org.jmock.integration.junit4.JMock;
020    import org.junit.Test;
021    import org.junit.runner.RunWith;
022    import org.picocontainer.Behavior;
023    import org.picocontainer.ComponentAdapter;
024    import org.picocontainer.ComponentMonitor;
025    import org.picocontainer.ComponentMonitorStrategy;
026    import org.picocontainer.DefaultPicoContainer;
027    import org.picocontainer.LifecycleStrategy;
028    import org.picocontainer.PicoCompositionException;
029    import org.picocontainer.PicoContainer;
030    import org.picocontainer.testmodel.SimpleTouchable;
031    import org.picocontainer.testmodel.Touchable;
032    
033    /**
034     * @author Mauro Talevi
035     */
036    @RunWith(JMock.class)
037    @SuppressWarnings("serial")
038    public class BehaviorAdapterTestCase {
039            
040            private Mockery mockery = mockeryWithCountingNamingScheme();
041            
042        @Test public void testDecoratingComponentAdapterDelegatesToMonitorThatDoesSupportStrategy() {
043            AbstractBehavior adapter = new FooAbstractBehavior(mockComponentAdapterThatDoesSupportStrategy());
044            adapter.changeMonitor(mockMonitorWithNoExpectedMethods());
045            assertNotNull(adapter.currentMonitor());
046        }
047        
048        @Test public void testDecoratingComponentAdapterDelegatesToMonitorThatDoesNotSupportStrategy() {
049            AbstractBehavior adapter = new FooAbstractBehavior(mockComponentAdapter());
050            adapter.changeMonitor(mockMonitorWithNoExpectedMethods());
051            try {
052                adapter.currentMonitor();
053                fail("PicoCompositionException expected");
054            } catch (PicoCompositionException e) {
055                assertEquals("No component monitor found in delegate", e.getMessage());
056            }
057        }
058        
059        @Test public void testDecoratingComponentAdapterDelegatesLifecycleManagement() {
060            AbstractBehavior adapter = new FooAbstractBehavior(mockComponentAdapterThatCanManageLifecycle());
061            PicoContainer pico = new DefaultPicoContainer();
062            adapter.start(pico);
063            adapter.stop(pico);
064            adapter.dispose(pico);
065            Touchable touchable = new SimpleTouchable();
066            adapter.start(touchable);
067            adapter.stop(touchable);
068            adapter.dispose(touchable);
069        }
070    
071        @Test public void testDecoratingComponentAdapterIgnoresLifecycleManagementIfDelegateDoesNotSupportIt() {
072            AbstractBehavior adapter = new FooAbstractBehavior(mockComponentAdapter());
073            PicoContainer pico = new DefaultPicoContainer();
074            adapter.start(pico);
075            adapter.stop(pico);
076            adapter.dispose(pico);
077            Touchable touchable = new SimpleTouchable();
078            adapter.start(touchable);
079            adapter.stop(touchable);
080            adapter.dispose(touchable);
081        }
082        
083        ComponentMonitor mockMonitorWithNoExpectedMethods() {
084            return mockery.mock(ComponentMonitor.class);
085        }
086    
087        private ComponentAdapter mockComponentAdapterThatDoesSupportStrategy() {
088            final ComponentAdapterThatSupportsStrategy ca = mockery.mock(ComponentAdapterThatSupportsStrategy.class);
089            mockery.checking(new Expectations(){{
090                    one(ca).changeMonitor(with(any(ComponentMonitor.class)));
091                    one(ca).currentMonitor();
092                    will(returnValue(mockMonitorWithNoExpectedMethods()));
093            }});
094            return ca;
095        }
096    
097        private ComponentAdapter mockComponentAdapter() {
098             return mockery.mock(ComponentAdapter.class);
099        }
100        
101        public static interface ComponentAdapterThatSupportsStrategy extends ComponentAdapter, ComponentMonitorStrategy {
102        }
103    
104        private ComponentAdapter mockComponentAdapterThatCanManageLifecycle() {
105            final ComponentAdapterThatCanManageLifecycle ca = mockery.mock(ComponentAdapterThatCanManageLifecycle.class);
106            mockery.checking(new Expectations(){{
107                    one(ca).start(with(any(PicoContainer.class)));
108                    one(ca).stop(with(any(PicoContainer.class)));
109                    one(ca).dispose(with(any(PicoContainer.class)));
110                    one(ca).start(with(any(Touchable.class)));
111                    one(ca).stop(with(any(Touchable.class)));
112                    one(ca).dispose(with(any(Touchable.class)));
113            }});
114            return ca;
115        }
116    
117        public static interface ComponentAdapterThatCanManageLifecycle extends ComponentAdapter, org.picocontainer.Behavior, LifecycleStrategy {
118        }
119    
120        static class FooAbstractBehavior extends AbstractBehavior {
121    
122            public FooAbstractBehavior(ComponentAdapter delegate) {
123                super(delegate);
124            }
125    
126            public String getDescriptor() {
127                return null;
128            }
129        }
130    }