View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
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  package org.kuali.common.util;
17  
18  import java.io.File;
19  import java.io.IOException;
20  import java.util.Properties;
21  
22  import org.apache.commons.io.FileUtils;
23  import org.junit.Assert;
24  import org.junit.Test;
25  import org.junit.runner.RunWith;
26  import org.slf4j.Logger;
27  import org.slf4j.LoggerFactory;
28  import org.springframework.beans.factory.annotation.Autowired;
29  import org.springframework.test.context.ContextConfiguration;
30  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
31  
32  @RunWith(SpringJUnit4ClassRunner.class)
33  @ContextConfiguration
34  public class PropertyUtilsTest {
35  	private static final Logger logger = LoggerFactory.getLogger(PropertyUtilsTest.class);
36  
37  	@Autowired
38  	private String buildDir = null;
39  
40  	String location = "classpath:org/kuali/common/util/simple.properties";
41  	String xmlLocation = "classpath:org/kuali/common/util/simple.xml";
42  	String encoding = "UTF-8";
43  
44  	protected File getTestDir() {
45  		return new File(buildDir + File.separator + "properties");
46  	}
47  
48  	@Test
49  	public void testBuildDir() throws IOException {
50  		Assert.assertNotNull(buildDir);
51  		logger.info("Creating " + getTestDir());
52  		FileUtils.forceMkdir(getTestDir());
53  	}
54  
55  	@Test
56  	public void storeUTF8XMLPropertiesTest() throws IOException {
57  		File temp = File.createTempFile("temporary.", ".xml", getTestDir());
58  		Properties props = PropertyUtils.load(xmlLocation);
59  		String foo = props.getProperty("foo");
60  		Assert.assertEquals("bar", foo);
61  		PropertyUtils.store(props, temp, encoding);
62  	}
63  
64  	@Test
65  	public void storeXMLPropertiesTest() throws IOException {
66  		File temp = File.createTempFile("temporary.", ".xml", getTestDir());
67  		temp.deleteOnExit();
68  		Properties props = PropertyUtils.load(xmlLocation);
69  		String foo = props.getProperty("foo");
70  		Assert.assertEquals("bar", foo);
71  		PropertyUtils.store(props, temp);
72  	}
73  
74  	@Test
75  	public void storeUTF8PropertiesTest() throws IOException {
76  		File temp = File.createTempFile("temporary.", ".properties", getTestDir());
77  		temp.deleteOnExit();
78  		Properties props = PropertyUtils.load(xmlLocation);
79  		String foo = props.getProperty("foo");
80  		Assert.assertEquals("bar", foo);
81  		PropertyUtils.store(props, temp, encoding);
82  	}
83  
84  	@Test
85  	public void storePropertiesTest() throws IOException {
86  		File temp = File.createTempFile("temporary.", ".properties", getTestDir());
87  		temp.deleteOnExit();
88  		Properties props = PropertyUtils.load(xmlLocation);
89  		String foo = props.getProperty("foo");
90  		Assert.assertEquals("bar", foo);
91  		PropertyUtils.store(props, temp);
92  	}
93  
94  	@Test
95  	public void getXMLPropertiesTest() {
96  		Properties props = PropertyUtils.load(xmlLocation);
97  		String foo = props.getProperty("foo");
98  		Assert.assertEquals("bar", foo);
99  	}
100 
101 	@Test
102 	public void getPropertiesTest() {
103 		Properties props = PropertyUtils.load(location);
104 		String foo = props.getProperty("foo");
105 		Assert.assertEquals("bar", foo);
106 	}
107 
108 	@Test
109 	public void getPropertiesUTF8Test() {
110 		Properties props = PropertyUtils.load(location, "UTF-8");
111 		String foo = props.getProperty("foo");
112 		Assert.assertEquals("bar", foo);
113 	}
114 }