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 import javax.inject.Named; 012 013 /** 014 * test that properties container works properly 015 * @author Konstantin Pribluda 016 */ 017 public class PropertiesPicoContainerTestCase { 018 /** 019 * all properties specified in constructor shall be 020 * placed into container as strings 021 * 022 */ 023 @Test public void testThatAllPropertiesAreAdded() { 024 Properties properties = new Properties(); 025 026 properties.put("foo","bar"); 027 properties.put("blurge","bang"); 028 029 030 PropertiesPicoContainer container = new PropertiesPicoContainer(properties); 031 assertEquals("bar",container.getComponent("foo")); 032 assertEquals("bang",container.getComponent("blurge")); 033 } 034 035 /** 036 * inquiry shall be delegated to parent container 037 */ 038 @Test public void testThatParentDelegationWorks() { 039 DefaultPicoContainer parent = new DefaultPicoContainer(); 040 String stored = new String("glam"); 041 parent.addComponent("glam",stored); 042 043 PropertiesPicoContainer contaienr = new PropertiesPicoContainer(new Properties(),parent); 044 045 assertSame(stored,contaienr.getComponent("glam")); 046 } 047 048 049 @Test public void thatParanamerBehavesForASpecialCase() { 050 051 Properties properties = new Properties(); 052 properties.put("portNumber", 1); 053 properties.put("hostName", "string"); 054 properties.put("agentName", "agent0"); 055 DefaultPicoContainer container = new DefaultPicoContainer(new PropertiesPicoContainer(properties)); 056 container.as(Characteristics.USE_NAMES).addComponent(Dependant.class); 057 container.as(Characteristics.USE_NAMES).addComponent(Dependency.class); 058 Dependant dependant = (Dependant) container.getComponent(Dependant.class); 059 assertEquals(1, dependant.pn); 060 assertEquals("string", dependant.hn); 061 062 } 063 064 public static class Dependency { 065 private final String name; 066 067 public Dependency(final String agentName) { 068 this.name = agentName; 069 } 070 071 public String toString() { 072 return name; 073 } 074 } 075 076 public static class Dependant { 077 final int pn; 078 final String hn; 079 final Dependency dependency; 080 081 public Dependant(final String hostName, final int portNumber, final Dependency dependency) { 082 this.pn = portNumber; 083 this.hn = hostName; 084 this.dependency = dependency; 085 } 086 087 public String toString() { 088 return "Number: " + pn + " String: " + hn + " Dependency: " + dependency; 089 } 090 } 091 092 @Test 093 public void thatParanamerHonorsNamedAnnotationFromJSR330() { 094 095 Properties properties = new Properties(); 096 properties.put("portNumber", "1"); 097 properties.put("hostName", "string"); 098 properties.put("agentName", "agent0"); 099 DefaultPicoContainer container = new DefaultPicoContainer(new PropertiesPicoContainer(properties)); 100 container.as(Characteristics.USE_NAMES).addComponent(Dependant2.class); 101 container.as(Characteristics.USE_NAMES).addComponent(Dependency.class); 102 Dependant2 dependant = (Dependant2) container.getComponent(Dependant2.class); 103 assertEquals(1, dependant.pn); 104 assertEquals("string", dependant.hn); 105 } 106 107 108 public static class Dependant2 extends Dependant { 109 public Dependant2(@Named("hostName") String hn, @Named("portNumber") String pn, Dependency d) { 110 super(hn, Integer.parseInt(pn), d); 111 } 112 } 113 114 @Test public void testRepresentationOfContainerTree() { 115 Properties properties = new Properties(); 116 properties.put("portNumber", 1); 117 properties.put("hostName", "string"); 118 properties.put("agentName", "agent0"); 119 120 PropertiesPicoContainer parent = new PropertiesPicoContainer(properties); 121 parent.setName("parent"); 122 DefaultPicoContainer child = new DefaultPicoContainer(parent); 123 child.setName("child"); 124 child.addComponent("hello", "goodbye"); 125 child.addComponent("bonjour", "aurevior"); 126 assertEquals("child:2<I<D<parent:3<|", child.toString()); 127 } 128 129 130 }