    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.issues;
    009
    010    import org.picocontainer.ComponentMonitor;
    011    import org.picocontainer.DefaultPicoContainer;
    012    import org.picocontainer.Startable;
    013    import org.picocontainer.MutablePicoContainer;
    014    import org.picocontainer.ComponentAdapter;
    015    import org.picocontainer.monitors.NullComponentMonitor;
    016    import org.picocontainer.lifecycle.ReflectionLifecycleStrategy;
    017    import org.picocontainer.lifecycle.StartableLifecycleStrategy;
    018    import org.junit.Test;
    019
    020    import java.lang.reflect.Method;
    021
    022    @SuppressWarnings("serial")
    023    public 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    }