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