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.defaults.issues;
011    
012    import static org.junit.Assert.assertFalse;
013    import static org.junit.Assert.assertTrue;
014    
015    import org.junit.Test;
016    import org.picocontainer.DefaultPicoContainer;
017    import org.picocontainer.MutablePicoContainer;
018    import org.picocontainer.PicoCompositionException;
019    
020    /**
021     * Test case for issue http://jira.codehaus.org/browse/PICO-280
022     */
023    public class Issue0280TestCase {
024    
025            @Test
026            public void testShouldFailIfInstantiationInChildContainerFails() {
027                    MutablePicoContainer parent = new DefaultPicoContainer();
028                    MutablePicoContainer child = new DefaultPicoContainer(parent);
029    
030                    parent.addComponent(CommonInterface.class, ParentImplementation.class);
031                    child.addComponent(CommonInterface.class, ChildImplementation.class);
032    
033                    parent.start();
034    
035                    try {
036                            Object result = child.getComponent(CommonInterface.class);
037    
038                            // should never get here
039                            assertFalse(result.getClass() == ParentImplementation.class);
040                    } catch (Exception e) {
041                            assertTrue(e.getClass() == PicoCompositionException.class);
042                    }
043    
044            }
045    
046            public interface CommonInterface {
047    
048            }
049    
050            public static class ParentImplementation implements CommonInterface {
051            }
052    
053            public static class ChildImplementation implements CommonInterface {
054                    public ChildImplementation() {
055                            throw new PicoCompositionException("Problem during initialization");
056                    }
057            }
058    
059    }