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.kuali.common.util.properties.rice.RiceLoader;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  import org.springframework.beans.factory.annotation.Autowired;
32  import org.springframework.test.context.ContextConfiguration;
33  import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
34  
35  @RunWith(SpringJUnit4ClassRunner.class)
36  @ContextConfiguration
37  public class PropertyUtilsTest {
38  	private static final Logger logger = LoggerFactory.getLogger(PropertyUtilsTest.class);
39  
40  	@Autowired
41  	private String buildDir = null;
42  
43  	String location = "classpath:org/kuali/common/util/simple.properties";
44  	String xmlLocation = "classpath:org/kuali/common/util/simple.xml";
45  	String encoding = "UTF-8";
46  
47  	protected File getTestDir() {
48  		return new File(buildDir + File.separator + "properties");
49  	}
50  
51  	@Test
52  	public void testRiceProperties2() {
53  		Properties props = RiceLoader.load("classpath:rice/common-config.xml");
54  		Assert.assertEquals("", props.getProperty("app.context.name"));
55  		System.out.println();
56  	}
57  
58  	@Test
59  	@Deprecated
60  	public void testRiceProperties() {
61  		Properties props = PropertyUtils.loadRiceProperties("classpath:rice-properties.xml");
62  		String value = props.getProperty("foo");
63  		Assert.assertEquals("bar", value);
64  		try {
65  			PropertyUtils.loadRiceProperties("classpath:rice-unsupported-config-location.xml");
66  			Assert.fail("config.location should not be allowed");
67  		} catch (IllegalArgumentException e) {
68  			; // Ignore
69  		}
70  		try {
71  			PropertyUtils.loadRiceProperties("classpath:rice-unsupported-override.xml");
72  			Assert.fail("override attribute should not be allowed");
73  		} catch (IllegalArgumentException e) {
74  			; // Ignore
75  		}
76  		try {
77  			PropertyUtils.loadRiceProperties("classpath:rice-unsupported-system.xml");
78  			Assert.fail("system attribute should not be allowed");
79  		} catch (IllegalArgumentException e) {
80  			; // Ignore
81  		}
82  		try {
83  			PropertyUtils.loadRiceProperties("classpath:rice-unsupported-random.xml");
84  			Assert.fail("random attribute should not be allowed");
85  		} catch (IllegalArgumentException e) {
86  			; // Ignore
87  		}
88  	}
89  
90  	@Test
91  	public void testBuildDir() throws IOException {
92  		Assert.assertNotNull(buildDir);
93  		logger.info("Creating " + getTestDir());
94  		FileUtils.forceMkdir(getTestDir());
95  	}
96  
97  	@Test
98  	public void storeUTF8XMLPropertiesTest() throws IOException {
99  		File temp = File.createTempFile("temporary.", ".xml", getTestDir());
100 		Properties props = PropertyUtils.load(xmlLocation);
101 		String foo = props.getProperty("foo");
102 		Assert.assertEquals("bar", foo);
103 		PropertyUtils.store(props, temp, encoding);
104 	}
105 
106 	@Test
107 	public void storeXMLPropertiesTest() throws IOException {
108 		File temp = File.createTempFile("temporary.", ".xml", getTestDir());
109 		temp.deleteOnExit();
110 		Properties props = PropertyUtils.load(xmlLocation);
111 		String foo = props.getProperty("foo");
112 		Assert.assertEquals("bar", foo);
113 		PropertyUtils.store(props, temp);
114 	}
115 
116 	@Test
117 	public void storeUTF8PropertiesTest() throws IOException {
118 		File temp = File.createTempFile("temporary.", ".properties", getTestDir());
119 		temp.deleteOnExit();
120 		Properties props = PropertyUtils.load(xmlLocation);
121 		String foo = props.getProperty("foo");
122 		Assert.assertEquals("bar", foo);
123 		PropertyUtils.store(props, temp, encoding);
124 	}
125 
126 	@Test
127 	public void storePropertiesTest() throws IOException {
128 		File temp = File.createTempFile("temporary.", ".properties", getTestDir());
129 		temp.deleteOnExit();
130 		Properties props = PropertyUtils.load(xmlLocation);
131 		String foo = props.getProperty("foo");
132 		Assert.assertEquals("bar", foo);
133 		PropertyUtils.store(props, temp);
134 	}
135 
136 	@Test
137 	public void getXMLPropertiesTest() {
138 		Properties props = PropertyUtils.load(xmlLocation);
139 		String foo = props.getProperty("foo");
140 		Assert.assertEquals("bar", foo);
141 	}
142 
143 	@Test
144 	public void getPropertiesTest() {
145 		Properties props = PropertyUtils.load(location);
146 		String foo = props.getProperty("foo");
147 		Assert.assertEquals("bar", foo);
148 	}
149 
150 	@Test
151 	public void getPropertiesUTF8Test() {
152 		Properties props = PropertyUtils.load(location, "UTF-8");
153 		String foo = props.getProperty("foo");
154 		Assert.assertEquals("bar", foo);
155 	}
156 
157 	@Test
158 	@Deprecated
159 	public void encryptTest() {
160 		TextEncryptor encryptor = EncUtils.getTextEncryptor("password");
161 		Properties props = new Properties();
162 		props.setProperty("foo", "bar");
163 		PropertyUtils.encrypt(props, encryptor);
164 		PropertyUtils.info(props);
165 		List<String> keys = PropertyUtils.getSortedKeys(props);
166 		for (String key : keys) {
167 			String value = props.getProperty(key);
168 			Assert.assertTrue("property value " + value + " is not encrypted correctly", PropertyUtils.isEncryptedPropertyValue(value));
169 		}
170 		PropertyUtils.decrypt(props, encryptor);
171 		PropertyUtils.info(props);
172 		keys = PropertyUtils.getSortedKeys(props);
173 		for (String key : keys) {
174 			String value = props.getProperty(key);
175 			Assert.assertFalse("property value " + value + " is encrypted", PropertyUtils.isEncryptedPropertyValue(value));
176 		}
177 	}
178 }