1
2
3
4
5
6
7
8
9
10
11
12
13
14
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 testRiceProperties() {
52 Properties props = PropertyUtils.loadRiceProperties("classpath:rice-properties.xml");
53 String value = props.getProperty("foo");
54 Assert.assertEquals("bar", value);
55 try {
56 PropertyUtils.loadRiceProperties("classpath:rice-unsupported-config-location.xml");
57 Assert.fail("config.location should not be allowed");
58 } catch (IllegalArgumentException e) {
59 ;
60 }
61 try {
62 PropertyUtils.loadRiceProperties("classpath:rice-unsupported-override.xml");
63 Assert.fail("override attribute should not be allowed");
64 } catch (IllegalArgumentException e) {
65 ;
66 }
67 try {
68 PropertyUtils.loadRiceProperties("classpath:rice-unsupported-system.xml");
69 Assert.fail("system attribute should not be allowed");
70 } catch (IllegalArgumentException e) {
71 ;
72 }
73 try {
74 PropertyUtils.loadRiceProperties("classpath:rice-unsupported-random.xml");
75 Assert.fail("random attribute should not be allowed");
76 } catch (IllegalArgumentException e) {
77 ;
78 }
79 }
80
81 @Test
82 public void testBuildDir() throws IOException {
83 Assert.assertNotNull(buildDir);
84 logger.info("Creating " + getTestDir());
85 FileUtils.forceMkdir(getTestDir());
86 }
87
88 @Test
89 public void storeUTF8XMLPropertiesTest() throws IOException {
90 File temp = File.createTempFile("temporary.", ".xml", getTestDir());
91 Properties props = PropertyUtils.load(xmlLocation);
92 String foo = props.getProperty("foo");
93 Assert.assertEquals("bar", foo);
94 PropertyUtils.store(props, temp, encoding);
95 }
96
97 @Test
98 public void storeXMLPropertiesTest() throws IOException {
99 File temp = File.createTempFile("temporary.", ".xml", getTestDir());
100 temp.deleteOnExit();
101 Properties props = PropertyUtils.load(xmlLocation);
102 String foo = props.getProperty("foo");
103 Assert.assertEquals("bar", foo);
104 PropertyUtils.store(props, temp);
105 }
106
107 @Test
108 public void storeUTF8PropertiesTest() throws IOException {
109 File temp = File.createTempFile("temporary.", ".properties", getTestDir());
110 temp.deleteOnExit();
111 Properties props = PropertyUtils.load(xmlLocation);
112 String foo = props.getProperty("foo");
113 Assert.assertEquals("bar", foo);
114 PropertyUtils.store(props, temp, encoding);
115 }
116
117 @Test
118 public void storePropertiesTest() throws IOException {
119 File temp = File.createTempFile("temporary.", ".properties", getTestDir());
120 temp.deleteOnExit();
121 Properties props = PropertyUtils.load(xmlLocation);
122 String foo = props.getProperty("foo");
123 Assert.assertEquals("bar", foo);
124 PropertyUtils.store(props, temp);
125 }
126
127 @Test
128 public void getXMLPropertiesTest() {
129 Properties props = PropertyUtils.load(xmlLocation);
130 String foo = props.getProperty("foo");
131 Assert.assertEquals("bar", foo);
132 }
133
134 @Test
135 public void getPropertiesTest() {
136 Properties props = PropertyUtils.load(location);
137 String foo = props.getProperty("foo");
138 Assert.assertEquals("bar", foo);
139 }
140
141 @Test
142 public void getPropertiesUTF8Test() {
143 Properties props = PropertyUtils.load(location, "UTF-8");
144 String foo = props.getProperty("foo");
145 Assert.assertEquals("bar", foo);
146 }
147
148 @Test
149 public void encryptTest() {
150 TextEncryptor encryptor = EncUtils.getTextEncryptor("password");
151 Properties props = new Properties();
152 props.setProperty("foo", "bar");
153 PropertyUtils.encrypt(props, encryptor);
154 PropertyUtils.info(props);
155 List<String> keys = PropertyUtils.getSortedKeys(props);
156 for (String key : keys) {
157 String value = props.getProperty(key);
158 Assert.assertTrue("property value " + value + " is not encrypted correctly", PropertyUtils.isEncryptedPropertyValue(value));
159 }
160 PropertyUtils.decrypt(props, encryptor);
161 PropertyUtils.info(props);
162 keys = PropertyUtils.getSortedKeys(props);
163 for (String key : keys) {
164 String value = props.getProperty(key);
165 Assert.assertFalse("property value " + value + " is encrypted", PropertyUtils.isEncryptedPropertyValue(value));
166 }
167 }
168 }