1 package org.picocontainer.defaults; 2 3 import org.jmock.Mock; 4 import org.jmock.MockObjectTestCase; 5 import org.picocontainer.ComponentAdapter; 6 import org.picocontainer.ComponentMonitor; 7 import org.picocontainer.LifecycleManager; 8 import org.picocontainer.PicoContainer; 9 import org.picocontainer.PicoIntrospectionException; 10 import org.picocontainer.testmodel.SimpleTouchable; 11 import org.picocontainer.testmodel.Touchable; 12 13 17 public class DecoratingComponentAdapterTestCase extends MockObjectTestCase { 18 19 public void testDecoratingComponentAdapterDelegatesToMonitorThatDoesSupportStrategy() { 20 DecoratingComponentAdapter adapter = new DecoratingComponentAdapter(mockComponentAdapterThatDoesSupportStrategy()); 21 adapter.changeMonitor(mockMonitorWithNoExpectedMethods()); 22 assertNotNull(adapter.currentMonitor()); 23 } 24 25 public void testDecoratingComponentAdapterDelegatesToMonitorThatDoesNotSupportStrategy() { 26 DecoratingComponentAdapter adapter = new DecoratingComponentAdapter(mockComponentAdapter()); 27 adapter.changeMonitor(mockMonitorWithNoExpectedMethods()); 28 try { 29 adapter.currentMonitor(); 30 fail("PicoIntrospectionException expected"); 31 } catch (PicoIntrospectionException e) { 32 assertEquals("No component monitor found in delegate", e.getMessage()); 33 } 34 } 35 36 public void testDecoratingComponentAdapterDelegatesLifecycleManagement() { 37 DecoratingComponentAdapter adapter = new DecoratingComponentAdapter(mockComponentAdapterThatCanManageLifecycle()); 38 PicoContainer pico = new DefaultPicoContainer(); 39 adapter.start(pico); 40 adapter.stop(pico); 41 adapter.dispose(pico); 42 Touchable touchable = new SimpleTouchable(); 43 adapter.start(touchable); 44 adapter.stop(touchable); 45 adapter.dispose(touchable); 46 } 47 48 public void testDecoratingComponentAdapterIgnoresLifecycleManagementIfDelegateDoesNotSupportIt() { 49 DecoratingComponentAdapter adapter = new DecoratingComponentAdapter(mockComponentAdapter()); 50 PicoContainer pico = new DefaultPicoContainer(); 51 adapter.start(pico); 52 adapter.stop(pico); 53 adapter.dispose(pico); 54 Touchable touchable = new SimpleTouchable(); 55 adapter.start(touchable); 56 adapter.stop(touchable); 57 adapter.dispose(touchable); 58 } 59 60 ComponentMonitor mockMonitorWithNoExpectedMethods() { 61 Mock mock = mock(ComponentMonitor.class); 62 return (ComponentMonitor)mock.proxy(); 63 } 64 65 private ComponentAdapter mockComponentAdapterThatDoesSupportStrategy() { 66 Mock mock = mock(ComponentAdapterThatSupportsStrategy.class); 67 mock.expects(once()).method("changeMonitor").withAnyArguments(); 68 mock.expects(once()).method("currentMonitor").will(returnValue(mockMonitorWithNoExpectedMethods())); 69 return (ComponentAdapter)mock.proxy(); 70 } 71 72 private ComponentAdapter mockComponentAdapter() { 73 Mock mock = mock(ComponentAdapter.class); 74 return (ComponentAdapter)mock.proxy(); 75 } 76 77 static interface ComponentAdapterThatSupportsStrategy extends ComponentAdapter, ComponentMonitorStrategy { 78 } 79 80 private ComponentAdapter mockComponentAdapterThatCanManageLifecycle() { 81 Mock mock = mock(ComponentAdapterThatCanManageLifecycle.class); 82 mock.expects(once()).method("start").with(isA(PicoContainer.class)); 83 mock.expects(once()).method("stop").with(isA(PicoContainer.class)); 84 mock.expects(once()).method("dispose").with(isA(PicoContainer.class)); 85 mock.expects(once()).method("start").with(isA(Touchable.class)); 86 mock.expects(once()).method("stop").with(isA(Touchable.class)); 87 mock.expects(once()).method("dispose").with(isA(Touchable.class)); 88 return (ComponentAdapter)mock.proxy(); 89 } 90 91 static interface ComponentAdapterThatCanManageLifecycle extends ComponentAdapter, LifecycleManager, LifecycleStrategy { 92 } 93 } 94 | Popular Tags |