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