1 16 17 package org.springframework.beans.factory.config; 18 19 import java.util.Properties ; 20 21 import junit.framework.TestCase; 22 23 import org.springframework.core.JdkVersion; 24 import org.springframework.core.io.ClassPathResource; 25 26 30 public class PropertiesFactoryBeanTests extends TestCase { 31 32 public void testWithPropertiesFile() throws Exception { 33 PropertiesFactoryBean pfb = new PropertiesFactoryBean(); 34 pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties")); 35 pfb.afterPropertiesSet(); 36 Properties props = (Properties ) pfb.getObject(); 37 assertEquals("99", props.getProperty("tb.array[0].age")); 38 } 39 40 public void testWithPropertiesXmlFile() throws Exception { 41 if (JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_15) { 43 return; 44 } 45 46 PropertiesFactoryBean pfb = new PropertiesFactoryBean(); 47 pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test-properties.xml")); 48 pfb.afterPropertiesSet(); 49 Properties props = (Properties ) pfb.getObject(); 50 assertEquals("99", props.getProperty("tb.array[0].age")); 51 } 52 53 public void testWithLocalProperties() throws Exception { 54 PropertiesFactoryBean pfb = new PropertiesFactoryBean(); 55 Properties localProps = new Properties (); 56 localProps.setProperty("key2", "value2"); 57 pfb.setProperties(localProps); 58 pfb.afterPropertiesSet(); 59 Properties props = (Properties ) pfb.getObject(); 60 assertEquals("value2", props.getProperty("key2")); 61 } 62 63 public void testWithPropertiesFileAndLocalProperties() throws Exception { 64 PropertiesFactoryBean pfb = new PropertiesFactoryBean(); 65 pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties")); 66 Properties localProps = new Properties (); 67 localProps.setProperty("key2", "value2"); 68 localProps.setProperty("tb.array[0].age", "0"); 69 pfb.setProperties(localProps); 70 pfb.afterPropertiesSet(); 71 Properties props = (Properties ) pfb.getObject(); 72 assertEquals("99", props.getProperty("tb.array[0].age")); 73 assertEquals("value2", props.getProperty("key2")); 74 } 75 76 public void testWithPropertiesFileAndLocalPropertiesAndLocalOverride() throws Exception { 77 PropertiesFactoryBean pfb = new PropertiesFactoryBean(); 78 pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties")); 79 Properties localProps = new Properties (); 80 localProps.setProperty("key2", "value2"); 81 localProps.setProperty("tb.array[0].age", "0"); 82 pfb.setProperties(localProps); 83 pfb.setLocalOverride(true); 84 pfb.afterPropertiesSet(); 85 Properties props = (Properties ) pfb.getObject(); 86 assertEquals("0", props.getProperty("tb.array[0].age")); 87 assertEquals("value2", props.getProperty("key2")); 88 } 89 90 public void testWithPrototype() throws Exception { 91 PropertiesFactoryBean pfb = new PropertiesFactoryBean(); 92 pfb.setSingleton(false); 93 pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties")); 94 Properties localProps = new Properties (); 95 localProps.setProperty("key2", "value2"); 96 pfb.setProperties(localProps); 97 pfb.afterPropertiesSet(); 98 Properties props = (Properties ) pfb.getObject(); 99 assertEquals("99", props.getProperty("tb.array[0].age")); 100 assertEquals("value2", props.getProperty("key2")); 101 Properties newProps = (Properties ) pfb.getObject(); 102 assertTrue(props != newProps); 103 assertEquals("99", newProps.getProperty("tb.array[0].age")); 104 assertEquals("value2", newProps.getProperty("key2")); 105 } 106 107 } 108 | Popular Tags |