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 package org.picocontainer.lifecycle;
009
010 import java.io.Serializable;
011
012 import org.jmock.Mock;
013 import org.jmock.MockObjectTestCase;
014 import org.picocontainer.Disposable;
015 import org.picocontainer.Startable;
016 import org.picocontainer.lifecycle.StartableLifecycleStrategy;
017 import org.picocontainer.monitors.NullComponentMonitor;
018
019 /**
020 *
021 * @author Mauro Talevi
022 */
023 public class StartableLifecycleStrategyTestCase extends MockObjectTestCase {
024
025 private StartableLifecycleStrategy strategy;
026
027 public void setUp(){
028 strategy = new StartableLifecycleStrategy(new NullComponentMonitor());
029 }
030
031 public void testStartable(){
032 Object startable = mockComponent(true, false);
033 strategy.start(startable);
034 strategy.stop(startable);
035 }
036
037 public void testDisposable(){
038 Object startable = mockComponent(false, true);
039 strategy.dispose(startable);
040 }
041
042 public void testSerializable(){
043 Object serializable = mockComponent(false, false);
044 strategy.start(serializable);
045 strategy.stop(serializable);
046 strategy.dispose(serializable);
047 }
048
049 private Object mockComponent(boolean startable, boolean disposeable) {
050 Mock mock = mock(Serializable.class);
051 if ( startable ) {
052 mock = mock(Startable.class);
053 mock.expects(atLeastOnce()).method("start");
054 mock.expects(atLeastOnce()).method("stop");
055 }
056 if ( disposeable ) {
057 mock = mock(Disposable.class);
058 mock.expects(atLeastOnce()).method("dispose");
059 }
060 return mock.proxy();
061 }
062 }