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.defaults.issues;
011    
012    import static org.junit.Assert.assertNotNull;
013    import static org.picocontainer.tck.MockFactory.mockeryWithCountingNamingScheme;
014    
015    import java.lang.reflect.Constructor;
016    import java.lang.reflect.Method;
017    
018    import org.jmock.Expectations;
019    import org.jmock.Mockery;
020    import org.jmock.api.Invocation;
021    import org.jmock.api.Action;
022    import org.jmock.integration.junit4.JMock;
023    import org.junit.Test;
024    import org.junit.runner.RunWith;
025    import org.picocontainer.*;
026    import org.picocontainer.injectors.AbstractInjector;
027    import org.hamcrest.Description;
028    
029    @RunWith(JMock.class)
030    public class Issue0265TestCase {
031    
032            private Mockery mockery = mockeryWithCountingNamingScheme();
033            
034        @Test public void testCanReallyChangeMonitor() throws SecurityException, NoSuchMethodException {
035            final Method start = Startable.class.getMethod("start");
036            final Method stop = Startable.class.getMethod("stop");
037            final ComponentMonitor monitor1 = mockery.mock(ComponentMonitor.class, "Monitor1");
038            final ComponentMonitor monitor2 = mockery.mock(ComponentMonitor.class, "Monitor2");
039            DefaultPicoContainer pico = new DefaultPicoContainer(monitor1);
040            mockery.checking(new Expectations(){{
041                one(monitor1).newInjectionFactory(with(any(AbstractInjector.class)));
042                will(new returnParameterAction(0));
043                one(monitor1).instantiating(with(any(PicoContainer.class)), with(any(ComponentAdapter.class)), with(any(Constructor.class)));
044                will(returnValue(DefaultPicoContainerTestCase.MyStartable.class.getConstructor()));
045                one(monitor1).instantiated(with(any(PicoContainer.class)), with(any(ComponentAdapter.class)), with(any(Constructor.class)), 
046                            with(any(Object.class)), with(any(Object[].class)), with(any(Long.class)));
047                one(monitor1).invoking(with(any(PicoContainer.class)), with(any(ComponentAdapter.class)), with(equal(start)), 
048                            with(any(Object.class)));
049                one(monitor1).invoked(with(any(PicoContainer.class)), with(any(ComponentAdapter.class)), with(equal(start)), 
050                            with(any(Object.class)), with(any(Long.class)));
051                one(monitor1).invoking(with(any(PicoContainer.class)), with(any(ComponentAdapter.class)), with(equal(stop)), 
052                            with(any(Object.class)));
053                one(monitor1).invoked(with(any(PicoContainer.class)), with(any(ComponentAdapter.class)), with(equal(stop)), 
054                            with(any(Object.class)), with(any(Long.class)));                
055            }});
056            pico.as(Characteristics.CACHE).addComponent(DefaultPicoContainerTestCase.MyStartable.class);
057            pico.start();
058            pico.stop();
059            Startable startable = pico.getComponent(DefaultPicoContainerTestCase.MyStartable.class);
060            assertNotNull(startable);
061            pico.changeMonitor(monitor2);
062            mockery.checking(new Expectations(){{
063                one(monitor2).invoking(with(any(PicoContainer.class)), with(any(ComponentAdapter.class)), with(equal(start)), 
064                            with(any(Object.class)));
065                one(monitor2).invoked(with(any(PicoContainer.class)), with(any(ComponentAdapter.class)), with(equal(start)), 
066                            with(any(Object.class)), with(any(Long.class)));
067                one(monitor2).invoking(with(any(PicoContainer.class)), with(any(ComponentAdapter.class)), with(equal(stop)), 
068                            with(any(Object.class)));
069                one(monitor2).invoked(with(any(PicoContainer.class)), with(any(ComponentAdapter.class)), with(equal(stop)), 
070                            with(any(Object.class)), with(any(Long.class)));                
071            }});
072            pico.start();
073            pico.stop();
074        }
075    
076        public static class returnParameterAction implements Action {
077            private final int parameter;
078    
079            public returnParameterAction(int parameter) {
080                this.parameter = parameter;
081            }
082    
083            public void describeTo(Description description) {
084                description.appendText("returns param[")
085                        .appendValue(parameter)
086                        .appendText("]");
087            }
088    
089            public Object invoke(Invocation invocation) {
090                return invocation.getParameter(parameter);
091            }
092        }
093    
094    
095    }