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.doc.introduction;
011    
012    import junit.framework.TestCase;
013    import org.picocontainer.MutablePicoContainer;
014    import org.picocontainer.injectors.AbstractInjector;
015    import org.picocontainer.DefaultPicoContainer;
016    
017    /**
018     * @author Aslak Hellesøy
019     * @version $Revision: 3603 $
020     */
021    public class HierarchyTestCase extends TestCase {
022        public void testHierarchy() {
023            try {
024                // START SNIPPET: wontwork
025                // Create x hierarchy of containers
026                MutablePicoContainer x = new DefaultPicoContainer();
027                MutablePicoContainer y = new DefaultPicoContainer( x );
028                MutablePicoContainer z = new DefaultPicoContainer( x );
029    
030                // Assemble components
031                x.addComponent(Apple.class);
032                y.addComponent(Juicer.class);
033                z.addComponent(Peeler.class);
034    
035                // Instantiate components
036                Peeler peeler = z.getComponent(Peeler.class);
037                // WON'T WORK! peeler will be null
038                peeler = x.getComponent(Peeler.class);
039                // WON'T WORK! This will throw an exception
040                Juicer juicer = y.getComponent(Juicer.class);
041                // END SNIPPET: wontwork
042            } catch (AbstractInjector.UnsatisfiableDependenciesException e) {
043                // expected
044            }
045        }
046    
047    }