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.behaviors;
011
012 import org.jmock.Mock;
013 import org.jmock.MockObjectTestCase;
014 import org.picocontainer.ComponentAdapter;
015 import org.picocontainer.ComponentMonitor;
016 import org.picocontainer.Behavior;
017 import org.picocontainer.PicoContainer;
018 import org.picocontainer.PicoCompositionException;
019 import org.picocontainer.behaviors.AbstractBehavior;
020 import org.picocontainer.DefaultPicoContainer;
021 import org.picocontainer.ComponentMonitorStrategy;
022 import org.picocontainer.LifecycleStrategy;
023 import org.picocontainer.testmodel.SimpleTouchable;
024 import org.picocontainer.testmodel.Touchable;
025
026 /**
027 * @author Mauro Talevi
028 */
029 public class BehaviorAdapterTestCase extends MockObjectTestCase {
030
031 public void testDecoratingComponentAdapterDelegatesToMonitorThatDoesSupportStrategy() {
032 AbstractBehavior adapter = new FooAbstractBehavior(mockComponentAdapterThatDoesSupportStrategy());
033 adapter.changeMonitor(mockMonitorWithNoExpectedMethods());
034 assertNotNull(adapter.currentMonitor());
035 }
036
037 public void testDecoratingComponentAdapterDelegatesToMonitorThatDoesNotSupportStrategy() {
038 AbstractBehavior adapter = new FooAbstractBehavior(mockComponentAdapter());
039 adapter.changeMonitor(mockMonitorWithNoExpectedMethods());
040 try {
041 adapter.currentMonitor();
042 fail("PicoCompositionException expected");
043 } catch (PicoCompositionException e) {
044 assertEquals("No component monitor found in delegate", e.getMessage());
045 }
046 }
047
048 public void testDecoratingComponentAdapterDelegatesLifecycleManagement() {
049 AbstractBehavior adapter = new FooAbstractBehavior(mockComponentAdapterThatCanManageLifecycle());
050 PicoContainer pico = new DefaultPicoContainer();
051 adapter.start(pico);
052 adapter.stop(pico);
053 adapter.dispose(pico);
054 Touchable touchable = new SimpleTouchable();
055 adapter.start(touchable);
056 adapter.stop(touchable);
057 adapter.dispose(touchable);
058 }
059
060 public void testDecoratingComponentAdapterIgnoresLifecycleManagementIfDelegateDoesNotSupportIt() {
061 AbstractBehavior adapter = new FooAbstractBehavior(mockComponentAdapter());
062 PicoContainer pico = new DefaultPicoContainer();
063 adapter.start(pico);
064 adapter.stop(pico);
065 adapter.dispose(pico);
066 Touchable touchable = new SimpleTouchable();
067 adapter.start(touchable);
068 adapter.stop(touchable);
069 adapter.dispose(touchable);
070 }
071
072 ComponentMonitor mockMonitorWithNoExpectedMethods() {
073 Mock mock = mock(ComponentMonitor.class);
074 return (ComponentMonitor)mock.proxy();
075 }
076
077 private ComponentAdapter mockComponentAdapterThatDoesSupportStrategy() {
078 Mock mock = mock(ComponentAdapterThatSupportsStrategy.class);
079 mock.expects(once()).method("changeMonitor").withAnyArguments();
080 mock.expects(once()).method("currentMonitor").will(returnValue(mockMonitorWithNoExpectedMethods()));
081 return (ComponentAdapter)mock.proxy();
082 }
083
084 private ComponentAdapter mockComponentAdapter() {
085 Mock mock = mock(ComponentAdapter.class);
086 return (ComponentAdapter)mock.proxy();
087 }
088
089 static interface ComponentAdapterThatSupportsStrategy extends ComponentAdapter, ComponentMonitorStrategy {
090 }
091
092 private ComponentAdapter mockComponentAdapterThatCanManageLifecycle() {
093 Mock mock = mock(ComponentAdapterThatCanManageLifecycle.class);
094 mock.expects(once()).method("start").with(isA(PicoContainer.class));
095 mock.expects(once()).method("stop").with(isA(PicoContainer.class));
096 mock.expects(once()).method("dispose").with(isA(PicoContainer.class));
097 mock.expects(once()).method("start").with(isA(Touchable.class));
098 mock.expects(once()).method("stop").with(isA(Touchable.class));
099 mock.expects(once()).method("dispose").with(isA(Touchable.class));
100 return (ComponentAdapter)mock.proxy();
101 }
102
103 static interface ComponentAdapterThatCanManageLifecycle extends ComponentAdapter, Behavior, LifecycleStrategy {
104 }
105
106 static class FooAbstractBehavior extends AbstractBehavior {
107
108 public FooAbstractBehavior(ComponentAdapter delegate) {
109 super(delegate);
110 }
111 }
112 }