    001    package org.picocontainer.containers;
    002
    003    import static org.junit.Assert.assertEquals;
    004    import static org.junit.Assert.assertSame;
    005
    006    import java.util.Properties;
    007    import org.junit.Test;
    008    import org.picocontainer.DefaultPicoContainer;
    009    import org.picocontainer.Characteristics;
    010
    011    /**
    012     * test that properties container works properly
    013     * @author k.pribluda
    014     */
    015    public class PropertiesPicoContainerTestCase {
    016            /**
    017             * all properties specified in constructor shall be
    018             * placed into container as strings
    019             *
    020             */
    021            @Test public void testThatAllPropertiesAreAdded() {
    022                    Properties properties = new Properties();
    023
    024                    properties.put("foo","bar");
    025                    properties.put("blurge","bang");
    026
    027
    028                    PropertiesPicoContainer container = new PropertiesPicoContainer(properties);
    029                    assertEquals("bar",container.getComponent("foo"));
    030                    assertEquals("bang",container.getComponent("blurge"));
    031            }
    032
    033            /**
    034             * inquiry shall be delegated to parent container
    035             */
    036            @Test public void testThatParentDelegationWorks() {
    037                    DefaultPicoContainer parent = new DefaultPicoContainer();
    038                    String stored = new String("glam");
    039                    parent.addComponent("glam",stored);
    040
    041                    PropertiesPicoContainer contaienr = new PropertiesPicoContainer(new Properties(),parent);
    042
    043                    assertSame(stored,contaienr.getComponent("glam"));
    044            }
    045
    046
    047        @Test public void thatParanamerBehavesForASpecialCase() {
    048
    049           Properties properties = new Properties();
    050           properties.put("portNumber", 1);
    051           properties.put("hostName", "string");
    052           properties.put("agentName", "agent0");
    053           DefaultPicoContainer container = new DefaultPicoContainer(new PropertiesPicoContainer(properties));
    054           container.as(Characteristics.USE_NAMES).addComponent(Dependant.class);
    055           container.as(Characteristics.USE_NAMES).addComponent(Dependency.class);
    056           Dependant dependant = (Dependant) container.getComponent(Dependant.class);
    057           System.out.println(dependant);
    058       }
    059
    060        public static class Dependency {
    061               private final String name;
    062               public Dependency(final String agentName) {
    063                   this.name = agentName;
    064               }
    065               public String toString() {
    066                   return name;
    067               }
    068           }
    069
    070           public static class Dependant /*  */ {
    071               private final int number;
    072               private final String string;
    073               private final Dependency dependency;
    074
    075               public Dependant(final String hostName, final int portNumber, final Dependency dependency) {
    076                   this.number = portNumber;
    077                   this.string = hostName;
    078                   this.dependency = dependency;
    079               }
    080
    081               public String toString() {
    082                   return "Number: " + number + " String: " + string + " Dependency: " + dependency;
    083               }
    084           }
    085
    086        @Test public void testRepresentationOfContainerTree() {
    087            Properties properties = new Properties();
    088            properties.put("portNumber", 1);
    089            properties.put("hostName", "string");
    090            properties.put("agentName", "agent0");
    091
    092            PropertiesPicoContainer parent = new PropertiesPicoContainer(properties);
    093            parent.setName("parent");
    094            DefaultPicoContainer child = new DefaultPicoContainer(parent);
    095            child.setName("child");
    096                    child.addComponent("hello", "goodbye");
    097            child.addComponent("bonjour", "aurevior");
    098            assertEquals("child:2<I<D<parent:3<|", child.toString());
    099        }
    100
    101
    102    }