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 static org.junit.Assert.assertEquals;
013 import static org.junit.Assert.assertNotNull;
014 import static org.junit.Assert.fail;
015 import static org.picocontainer.tck.MockFactory.mockeryWithCountingNamingScheme;
016
017 import org.jmock.Expectations;
018 import org.jmock.Mockery;
019 import org.jmock.integration.junit4.JMock;
020 import org.junit.Test;
021 import org.junit.runner.RunWith;
022 import org.picocontainer.Behavior;
023 import org.picocontainer.ComponentAdapter;
024 import org.picocontainer.ComponentMonitor;
025 import org.picocontainer.ComponentMonitorStrategy;
026 import org.picocontainer.DefaultPicoContainer;
027 import org.picocontainer.LifecycleStrategy;
028 import org.picocontainer.PicoCompositionException;
029 import org.picocontainer.PicoContainer;
030 import org.picocontainer.testmodel.SimpleTouchable;
031 import org.picocontainer.testmodel.Touchable;
032
033 /**
034 * @author Mauro Talevi
035 */
036 @RunWith(JMock.class)
037 public class BehaviorAdapterTestCase {
038
039 private Mockery mockery = mockeryWithCountingNamingScheme();
040
041 @Test public void testDecoratingComponentAdapterDelegatesToMonitorThatDoesSupportStrategy() {
042 AbstractBehavior adapter = new FooAbstractBehavior(mockComponentAdapterThatDoesSupportStrategy());
043 adapter.changeMonitor(mockMonitorWithNoExpectedMethods());
044 assertNotNull(adapter.currentMonitor());
045 }
046
047 @Test public void testDecoratingComponentAdapterDelegatesToMonitorThatDoesNotSupportStrategy() {
048 AbstractBehavior adapter = new FooAbstractBehavior(mockComponentAdapter());
049 adapter.changeMonitor(mockMonitorWithNoExpectedMethods());
050 try {
051 adapter.currentMonitor();
052 fail("PicoCompositionException expected");
053 } catch (PicoCompositionException e) {
054 assertEquals("No component monitor found in delegate", e.getMessage());
055 }
056 }
057
058 @Test public void testDecoratingComponentAdapterDelegatesLifecycleManagement() {
059 AbstractBehavior adapter = new FooAbstractBehavior(mockComponentAdapterThatCanManageLifecycle());
060 PicoContainer pico = new DefaultPicoContainer();
061 adapter.start(pico);
062 adapter.stop(pico);
063 adapter.dispose(pico);
064 Touchable touchable = new SimpleTouchable();
065 adapter.start(touchable);
066 adapter.stop(touchable);
067 adapter.dispose(touchable);
068 }
069
070 @Test public void testDecoratingComponentAdapterIgnoresLifecycleManagementIfDelegateDoesNotSupportIt() {
071 AbstractBehavior adapter = new FooAbstractBehavior(mockComponentAdapter());
072 PicoContainer pico = new DefaultPicoContainer();
073 adapter.start(pico);
074 adapter.stop(pico);
075 adapter.dispose(pico);
076 Touchable touchable = new SimpleTouchable();
077 adapter.start(touchable);
078 adapter.stop(touchable);
079 adapter.dispose(touchable);
080 }
081
082 ComponentMonitor mockMonitorWithNoExpectedMethods() {
083 return mockery.mock(ComponentMonitor.class);
084 }
085
086 private ComponentAdapter mockComponentAdapterThatDoesSupportStrategy() {
087 final ComponentAdapterThatSupportsStrategy ca = mockery.mock(ComponentAdapterThatSupportsStrategy.class);
088 mockery.checking(new Expectations(){{
089 one(ca).changeMonitor(with(any(ComponentMonitor.class)));
090 one(ca).currentMonitor();
091 will(returnValue(mockMonitorWithNoExpectedMethods()));
092 }});
093 return ca;
094 }
095
096 private ComponentAdapter mockComponentAdapter() {
097 return mockery.mock(ComponentAdapter.class);
098 }
099
100 public static interface ComponentAdapterThatSupportsStrategy extends ComponentAdapter, ComponentMonitorStrategy {
101 }
102
103 private ComponentAdapter mockComponentAdapterThatCanManageLifecycle() {
104 final ComponentAdapterThatCanManageLifecycle ca = mockery.mock(ComponentAdapterThatCanManageLifecycle.class);
105 mockery.checking(new Expectations(){{
106 one(ca).start(with(any(PicoContainer.class)));
107 one(ca).stop(with(any(PicoContainer.class)));
108 one(ca).dispose(with(any(PicoContainer.class)));
109 one(ca).start(with(any(Touchable.class)));
110 one(ca).stop(with(any(Touchable.class)));
111 one(ca).dispose(with(any(Touchable.class)));
112 }});
113 return ca;
114 }
115
116 public static interface ComponentAdapterThatCanManageLifecycle extends ComponentAdapter, Behavior, LifecycleStrategy {
117 }
118
119 static class FooAbstractBehavior extends AbstractBehavior {
120
121 public FooAbstractBehavior(ComponentAdapter delegate) {
122 super(delegate);
123 }
124
125 public String getDescriptor() {
126 return null;
127 }
128 }
129 }