View Javadoc
1   /**
2    * Copyright 2010-2014 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.nio.charset.Charset;
19  import java.util.Arrays;
20  import java.util.List;
21  import java.util.Properties;
22  
23  import org.apache.commons.lang3.StringUtils;
24  import org.junit.Assert;
25  import org.junit.Test;
26  import org.kuali.common.util.property.Constants;
27  import org.springframework.util.PropertyPlaceholderHelper;
28  
29  public class SingleWildcardMatchTest {
30  	@Test
31  	public void testWildcards() {
32  		Assert.assertTrue(PropertyUtils.isSingleWildcardMatch((String) null, (String) null));
33  		Assert.assertFalse(PropertyUtils.isSingleWildcardMatch(null, ""));
34  		Assert.assertFalse(PropertyUtils.isSingleWildcardMatch("", (String) null));
35  		Assert.assertFalse(PropertyUtils.isSingleWildcardMatch(null, "*"));
36  		Assert.assertTrue(PropertyUtils.isSingleWildcardMatch("", "*"));
37  		Assert.assertFalse(PropertyUtils.isSingleWildcardMatch("*", ""));
38  		Assert.assertTrue(PropertyUtils.isSingleWildcardMatch("*", "*"));
39  	}
40  
41  	@Test
42  	public void testWildcards1() {
43  		List<String> includes = Arrays.asList("*.home");
44  		Properties properties = PropertyUtils.duplicate(System.getProperties());
45  		PropertyUtils.trim(properties, includes, null);
46  		PropertyUtils.info(properties);
47  	}
48  
49  	@Test
50  	public void testWildcards2() {
51  		List<String> includes = Arrays.asList("java.*.version");
52  		Properties properties = PropertyUtils.duplicate(System.getProperties());
53  		PropertyUtils.trim(properties, includes, null);
54  		PropertyUtils.info(properties);
55  	}
56  
57  	@Test
58  	public void testUserHomeWildcard() {
59  		List<String> includes = Arrays.asList("user.*.home");
60  		Properties properties = PropertyUtils.duplicate(System.getProperties());
61  		PropertyUtils.trim(properties, includes, null);
62  		int size = properties.size();
63  		Assert.assertEquals(1, size);
64  	}
65  
66  	@Test
67  	public void testUserHome() {
68  		List<String> includes = Arrays.asList("user.home");
69  		Properties properties = PropertyUtils.duplicate(System.getProperties());
70  		PropertyUtils.trim(properties, includes, null);
71  		int size = properties.size();
72  		Assert.assertEquals(1, size);
73  	}
74  
75  	@Test
76  	public void testMultipleWildcards() {
77  		List<String> includes = Arrays.asList("java.*vm*.version");
78  		Properties properties = PropertyUtils.duplicate(System.getProperties());
79  		try {
80  			PropertyUtils.trim(properties, includes, null);
81  			Assert.fail("Should fail on multiple wildcards");
82  		} catch (IllegalArgumentException ignored) {
83  			; // ignore
84  		}
85  	}
86  
87  	@Test
88  	public void testPropertyKeysContainingWildardsAndNormalSearchIsUsed() {
89  		List<String> includes = Arrays.asList("user.home");
90  		Properties properties = PropertyUtils.duplicate(System.getProperties());
91  		properties.setProperty("user.*.home", "foo");
92  		// Non-wildcard searches on property keys containing wildcards is ok
93  		PropertyUtils.trim(properties, includes, null);
94  		int size = properties.size();
95  		Assert.assertEquals(1, size);
96  	}
97  
98  	// @Test
99  	public void testPropertyPlaceholderHelper() {
100 		Properties properties = new Properties();
101 		// properties.setProperty("foo", "bar");
102 		PropertyPlaceholderHelper helper = Constants.DEFAULT_PROPERTY_PLACEHOLDER_HELPER;
103 		String original = "I went to the \\${foo:mall}";
104 		String resolved = helper.replacePlaceholders(original, properties);
105 		System.out.println(resolved);
106 	}
107 
108 	// @Test
109 	public void testDefaultFileEncoding() {
110 		System.out.println("file.encoding=" + System.getProperty("file.encoding"));
111 		System.out.println("Charset.defaultCharset().name()=" + Charset.defaultCharset().name());
112 	}
113 
114 	// @Test
115 	public void testEncoding() {
116 		String[] encodings = new String[] { "UTF-8", "UTF-16", "UTF-32" };
117 		String s1 = "123";
118 		String s2 = "𝟙𝟚𝟛";
119 		String s = s1 + s2;
120 		StringBuilder sb = new StringBuilder();
121 		sb.append(rpad("s=" + s, 15));
122 		sb.append(rpad("s.length()=" + s.length(), 15));
123 		sb.append(rpad("s.substring(4, 6)=" + s.substring(4, 6), 25));
124 		sb.append("\n\n");
125 		StringBuilder characters = new StringBuilder();
126 		for (String encoding : encodings) {
127 			byte[] bytes = s.getBytes(Charset.forName(encoding));
128 			char[] chars = s.toCharArray();
129 			int[] codePoints = getCodePoints(chars);
130 			sb.append(rpad("encoding=" + encoding, 20));
131 			sb.append(rpad("bytes=" + bytes.length, 15));
132 			sb.append(rpad("chars=" + chars.length, 15));
133 			sb.append(rpad(HexUtils.toHexString(bytes), 55));
134 			characters.append(getString(codePoints) + "\n");
135 			sb.append("\n");
136 		}
137 		System.out.println("\n" + sb + "\n" + characters);
138 	}
139 
140 	protected String getString(int[] codePoints) {
141 		StringBuilder sb = new StringBuilder();
142 		sb.append("[");
143 		for (int i = 0; i < codePoints.length; i++) {
144 			if (i != 0) {
145 				sb.append(",");
146 			}
147 			int cp = codePoints[i];
148 			int charCount = Character.charCount(cp);
149 			sb.append(cp + ":charCount=" + charCount);
150 		}
151 		sb.append("]");
152 		return sb.toString();
153 	}
154 
155 	protected int[] getCodePoints(char[] chars) {
156 		int[] codePoints = new int[chars.length];
157 		for (int i = 0; i < chars.length; i++) {
158 			int codePoint = Character.codePointAt(chars, i);
159 			codePoints[i] = codePoint;
160 		}
161 		return codePoints;
162 	}
163 
164 	protected String getHex(byte[] bytes) {
165 		int mask = 0x000000ff;
166 		StringBuilder sb = new StringBuilder();
167 		for (byte b : bytes) {
168 			int masked = mask & b;
169 			String hex = Integer.toHexString(masked).toUpperCase();
170 			String padded = StringUtils.leftPad(hex, 2, "0");
171 			sb.append(padded);
172 		}
173 		return sb.toString();
174 	}
175 
176 	protected String rpad(String s, int padding) {
177 		return StringUtils.rightPad(s, padding, " ");
178 	}
179 
180 }