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 */
020 public class HierarchyTestCase extends TestCase {
021 public void testHierarchy() {
022 try {
023 // START SNIPPET: wontwork
024 // Create x hierarchy of containers
025 MutablePicoContainer x = new DefaultPicoContainer();
026 MutablePicoContainer y = new DefaultPicoContainer( x );
027 MutablePicoContainer z = new DefaultPicoContainer( x );
028
029 // Assemble components
030 x.addComponent(Apple.class);
031 y.addComponent(Juicer.class);
032 z.addComponent(Peeler.class);
033
034 // Instantiate components
035 Peeler peeler = z.getComponent(Peeler.class);
036 // WON'T WORK! peeler will be null
037 peeler = x.getComponent(Peeler.class);
038 // WON'T WORK! This will throw an exception
039 Juicer juicer = y.getComponent(Juicer.class);
040 // END SNIPPET: wontwork
041 } catch (AbstractInjector.UnsatisfiableDependenciesException e) {
042 // expected
043 }
044 }
045
046 }