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 org.jmock.MockObjectTestCase;
013    import org.jmock.Mock;
014    import org.jmock.core.Constraint;
015    
016    import org.picocontainer.ComponentMonitor;
017    import org.picocontainer.Startable;
018    import org.picocontainer.DefaultPicoContainer;
019    import org.picocontainer.DefaultPicoContainerTestCase;
020    import org.picocontainer.Characteristics;
021    
022    import java.lang.reflect.Method;
023    
024    public class Issue0265TestCase extends MockObjectTestCase {
025    
026        public void testCanReallyChangeMonitor() throws SecurityException, NoSuchMethodException {
027            Method start = Startable.class.getMethod("start");
028            Method stop = Startable.class.getMethod("stop");
029            Mock mockMonitor1 = mock(ComponentMonitor.class, "Monitor1");
030            Mock mockMonitor2 = mock(ComponentMonitor.class, "Monitor2");
031            DefaultPicoContainer pico = new DefaultPicoContainer((ComponentMonitor) mockMonitor1.proxy());
032            pico.as(Characteristics.CACHE).addComponent(DefaultPicoContainerTestCase.MyStartable.class);
033            mockMonitor1.expects(once()).method("instantiating").will(returnValue(DefaultPicoContainerTestCase.MyStartable.class.getConstructor()));
034            mockMonitor1.expects(once()).method("instantiated");
035            mockMonitor1.expects(once()).method("invoking").with(NULL, NULL, eq(start), ANYTHING);
036            mockMonitor1.expects(once()).method("invoked").with(new Constraint[] {NULL, NULL, eq(start), ANYTHING, ANYTHING});
037            mockMonitor1.expects(once()).method("invoking").with(NULL, NULL, eq(stop), ANYTHING);
038            mockMonitor1.expects(once()).method("invoked").with(new Constraint[] {NULL, NULL, eq(stop), ANYTHING, ANYTHING});
039            pico.start();
040            pico.stop();
041            Startable startable = pico.getComponent(DefaultPicoContainerTestCase.MyStartable.class);
042            assertNotNull(startable);
043            pico.changeMonitor((ComponentMonitor) mockMonitor2.proxy());
044            mockMonitor2.expects(once()).method("invoking").with(NULL, NULL, eq(start), ANYTHING);
045            mockMonitor2.expects(once()).method("invoked").with(new Constraint[] {NULL, NULL, eq(start), ANYTHING, ANYTHING});
046            mockMonitor2.expects(once()).method("invoking").with(NULL, NULL, eq(stop), ANYTHING);
047            mockMonitor2.expects(once()).method("invoked").with(new Constraint[] {NULL, NULL, eq(stop), ANYTHING, ANYTHING});
048            pico.start();
049            pico.stop();
050        }
051    
052    }