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 *****************************************************************************/
008package org.picocontainer.defaults.issues;
009
010import org.picocontainer.ComponentMonitor;
011import org.picocontainer.DefaultPicoContainer;
012import org.picocontainer.Startable;
013import org.picocontainer.MutablePicoContainer;
014import org.picocontainer.ComponentAdapter;
015import org.picocontainer.monitors.NullComponentMonitor;
016import org.picocontainer.lifecycle.ReflectionLifecycleStrategy;
017import org.picocontainer.lifecycle.StartableLifecycleStrategy;
018import org.junit.Test;
019
020import java.lang.reflect.Method;
021
022@SuppressWarnings("serial")
023public class Issue0303TestCase {
024    
025        public static class SwallowingComponentMonitor extends NullComponentMonitor {
026                @Override
027                public void lifecycleInvocationFailed(MutablePicoContainer container,
028                                ComponentAdapter<?> componentAdapter, Method method, Object instance,
029                                RuntimeException cause) {
030                        // swallow it
031                }
032        }
033
034        public static class Starter implements Startable {
035
036                public void start() {
037                        throw new RuntimeException("deliberate exception");
038                }
039
040                /**
041                 * {@inheritDoc}
042                 */
043                public void stop() {
044                        // empty
045                }
046        }
047
048        @Test
049        public void testCanSwallowExceptionFromReflectionLifecycleStrategy() {
050                ComponentMonitor monitor = new SwallowingComponentMonitor();
051                DefaultPicoContainer container =
052                                new DefaultPicoContainer(monitor, new StartableLifecycleStrategy(monitor), null);
053                container.addComponent(new Starter());
054                container.start();
055        }
056
057        @Test
058        // @Ignore("filed as PICO-313 on jira.codehaus.org")
059        public void testCanSwallowExceptionFromStarableLifecycleStrategy() {
060                ComponentMonitor monitor = new SwallowingComponentMonitor();
061                DefaultPicoContainer container =
062                                new DefaultPicoContainer(monitor, new ReflectionLifecycleStrategy(monitor), null);
063                container.addComponent(new Starter());
064                container.start();
065        }
066}