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