KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > config > PropertiesFactoryBeanTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.factory.config;
18
19 import java.util.Properties JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.springframework.core.JdkVersion;
24 import org.springframework.core.io.ClassPathResource;
25
26 /**
27  * @author Juergen Hoeller
28  * @since 01.11.2003
29  */

30 public class PropertiesFactoryBeanTests extends TestCase {
31
32     public void testWithPropertiesFile() throws Exception JavaDoc {
33         PropertiesFactoryBean pfb = new PropertiesFactoryBean();
34         pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
35         pfb.afterPropertiesSet();
36         Properties JavaDoc props = (Properties JavaDoc) pfb.getObject();
37         assertEquals("99", props.getProperty("tb.array[0].age"));
38     }
39
40     public void testWithPropertiesXmlFile() throws Exception JavaDoc {
41         // ignore for JDK < 1.5
42
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 JavaDoc props = (Properties JavaDoc) pfb.getObject();
50         assertEquals("99", props.getProperty("tb.array[0].age"));
51     }
52
53     public void testWithLocalProperties() throws Exception JavaDoc {
54         PropertiesFactoryBean pfb = new PropertiesFactoryBean();
55         Properties JavaDoc localProps = new Properties JavaDoc();
56         localProps.setProperty("key2", "value2");
57         pfb.setProperties(localProps);
58         pfb.afterPropertiesSet();
59         Properties JavaDoc props = (Properties JavaDoc) pfb.getObject();
60         assertEquals("value2", props.getProperty("key2"));
61     }
62
63     public void testWithPropertiesFileAndLocalProperties() throws Exception JavaDoc {
64         PropertiesFactoryBean pfb = new PropertiesFactoryBean();
65         pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
66         Properties JavaDoc localProps = new Properties JavaDoc();
67         localProps.setProperty("key2", "value2");
68         localProps.setProperty("tb.array[0].age", "0");
69         pfb.setProperties(localProps);
70         pfb.afterPropertiesSet();
71         Properties JavaDoc props = (Properties JavaDoc) 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 JavaDoc {
77         PropertiesFactoryBean pfb = new PropertiesFactoryBean();
78         pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
79         Properties JavaDoc localProps = new Properties JavaDoc();
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 JavaDoc props = (Properties JavaDoc) 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 JavaDoc {
91         PropertiesFactoryBean pfb = new PropertiesFactoryBean();
92         pfb.setSingleton(false);
93         pfb.setLocation(new ClassPathResource("/org/springframework/beans/factory/config/test.properties"));
94         Properties JavaDoc localProps = new Properties JavaDoc();
95         localProps.setProperty("key2", "value2");
96         pfb.setProperties(localProps);
97         pfb.afterPropertiesSet();
98         Properties JavaDoc props = (Properties JavaDoc) pfb.getObject();
99         assertEquals("99", props.getProperty("tb.array[0].age"));
100         assertEquals("value2", props.getProperty("key2"));
101         Properties JavaDoc newProps = (Properties JavaDoc) 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