View Javadoc

1   /**
2    * Copyright 2010-2013 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.List;
21  import java.util.Properties;
22  
23  import org.apache.commons.io.FileUtils;
24  import org.jasypt.util.text.TextEncryptor;
25  import org.junit.Assert;
26  import org.junit.Test;
27  import org.junit.runner.RunWith;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.beans.factory.annotation.Autowired;
31  import org.springframework.test.context.ContextConfiguration;
32  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
33  
34  @RunWith(SpringJUnit4ClassRunner.class)
35  @ContextConfiguration
36  public class PropertyUtilsTest {
37  	private static final Logger logger = LoggerFactory.getLogger(PropertyUtilsTest.class);
38  
39  	@Autowired
40  	private String buildDir = null;
41  
42  	String location = "classpath:org/kuali/common/util/simple.properties";
43  	String xmlLocation = "classpath:org/kuali/common/util/simple.xml";
44  	String encoding = "UTF-8";
45  
46  	protected File getTestDir() {
47  		return new File(buildDir + File.separator + "properties");
48  	}
49  
50  	@Test
51  	public void testBuildDir() throws IOException {
52  		Assert.assertNotNull(buildDir);
53  		logger.info("Creating " + getTestDir());
54  		FileUtils.forceMkdir(getTestDir());
55  	}
56  
57  	@Test
58  	public void storeUTF8XMLPropertiesTest() throws IOException {
59  		File temp = File.createTempFile("temporary.", ".xml", getTestDir());
60  		Properties props = PropertyUtils.load(xmlLocation);
61  		String foo = props.getProperty("foo");
62  		Assert.assertEquals("bar", foo);
63  		PropertyUtils.store(props, temp, encoding);
64  	}
65  
66  	@Test
67  	public void storeXMLPropertiesTest() throws IOException {
68  		File temp = File.createTempFile("temporary.", ".xml", getTestDir());
69  		temp.deleteOnExit();
70  		Properties props = PropertyUtils.load(xmlLocation);
71  		String foo = props.getProperty("foo");
72  		Assert.assertEquals("bar", foo);
73  		PropertyUtils.store(props, temp);
74  	}
75  
76  	@Test
77  	public void storeUTF8PropertiesTest() throws IOException {
78  		File temp = File.createTempFile("temporary.", ".properties", getTestDir());
79  		temp.deleteOnExit();
80  		Properties props = PropertyUtils.load(xmlLocation);
81  		String foo = props.getProperty("foo");
82  		Assert.assertEquals("bar", foo);
83  		PropertyUtils.store(props, temp, encoding);
84  	}
85  
86  	@Test
87  	public void storePropertiesTest() throws IOException {
88  		File temp = File.createTempFile("temporary.", ".properties", getTestDir());
89  		temp.deleteOnExit();
90  		Properties props = PropertyUtils.load(xmlLocation);
91  		String foo = props.getProperty("foo");
92  		Assert.assertEquals("bar", foo);
93  		PropertyUtils.store(props, temp);
94  	}
95  
96  	@Test
97  	public void getXMLPropertiesTest() {
98  		Properties props = PropertyUtils.load(xmlLocation);
99  		String foo = props.getProperty("foo");
100 		Assert.assertEquals("bar", foo);
101 	}
102 
103 	@Test
104 	public void getPropertiesTest() {
105 		Properties props = PropertyUtils.load(location);
106 		String foo = props.getProperty("foo");
107 		Assert.assertEquals("bar", foo);
108 	}
109 
110 	@Test
111 	public void getPropertiesUTF8Test() {
112 		Properties props = PropertyUtils.load(location, "UTF-8");
113 		String foo = props.getProperty("foo");
114 		Assert.assertEquals("bar", foo);
115 	}
116 
117 	@Test
118 	public void encryptTest() {
119 		TextEncryptor encryptor = EncUtils.getTextEncryptor("password");
120 		Properties props = new Properties();
121 		props.setProperty("foo", "bar");
122 		PropertyUtils.encrypt(props, encryptor);
123 		PropertyUtils.info(props);
124 		List<String> keys = PropertyUtils.getSortedKeys(props);
125 		for (String key : keys) {
126 			String value = props.getProperty(key);
127 			Assert.assertTrue("property value " + value + " is not encrypted correctly", PropertyUtils.isEncryptedPropertyValue(value));
128 		}
129 		PropertyUtils.decrypt(props, encryptor);
130 		PropertyUtils.info(props);
131 		keys = PropertyUtils.getSortedKeys(props);
132 		for (String key : keys) {
133 			String value = props.getProperty(key);
134 			Assert.assertFalse("property value " + value + " is encrypted", PropertyUtils.isEncryptedPropertyValue(value));
135 		}
136 	}
137 }