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