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.integration.junit4.JMock;
021 import org.junit.Test;
022 import org.junit.runner.RunWith;
023 import org.picocontainer.Characteristics;
024 import org.picocontainer.ComponentAdapter;
025 import org.picocontainer.ComponentMonitor;
026 import org.picocontainer.DefaultPicoContainer;
027 import org.picocontainer.DefaultPicoContainerTestCase;
028 import org.picocontainer.PicoContainer;
029 import org.picocontainer.Startable;
030
031 @RunWith(JMock.class)
032 public class Issue0265TestCase {
033
034 private Mockery mockery = mockeryWithCountingNamingScheme();
035
036 @Test public void testCanReallyChangeMonitor() throws SecurityException, NoSuchMethodException {
037 final Method start = Startable.class.getMethod("start");
038 final Method stop = Startable.class.getMethod("stop");
039 final ComponentMonitor monitor1 = mockery.mock(ComponentMonitor.class, "Monitor1");
040 final ComponentMonitor monitor2 = mockery.mock(ComponentMonitor.class, "Monitor2");
041 DefaultPicoContainer pico = new DefaultPicoContainer(monitor1);
042 pico.as(Characteristics.CACHE).addComponent(DefaultPicoContainerTestCase.MyStartable.class);
043 mockery.checking(new Expectations(){{
044 one(monitor1).instantiating(with(any(PicoContainer.class)), with(any(ComponentAdapter.class)), with(any(Constructor.class)));
045 will(returnValue(DefaultPicoContainerTestCase.MyStartable.class.getConstructor()));
046 one(monitor1).instantiated(with(any(PicoContainer.class)), with(any(ComponentAdapter.class)), with(any(Constructor.class)),
047 with(any(Object.class)), with(any(Object[].class)), with(any(Long.class)));
048 one(monitor1).invoking(with(any(PicoContainer.class)), with(any(ComponentAdapter.class)), with(equal(start)),
049 with(any(Object.class)));
050 one(monitor1).invoked(with(any(PicoContainer.class)), with(any(ComponentAdapter.class)), with(equal(start)),
051 with(any(Object.class)), with(any(Long.class)));
052 one(monitor1).invoking(with(any(PicoContainer.class)), with(any(ComponentAdapter.class)), with(equal(stop)),
053 with(any(Object.class)));
054 one(monitor1).invoked(with(any(PicoContainer.class)), with(any(ComponentAdapter.class)), with(equal(stop)),
055 with(any(Object.class)), with(any(Long.class)));
056 }});
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 }