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