001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util; 017 018import java.nio.charset.Charset; 019import java.util.Arrays; 020import java.util.List; 021import java.util.Properties; 022 023import org.apache.commons.lang3.StringUtils; 024import org.junit.Assert; 025import org.junit.Test; 026import org.kuali.common.util.property.Constants; 027import org.springframework.util.PropertyPlaceholderHelper; 028 029public class SingleWildcardMatchTest { 030 @Test 031 public void testWildcards() { 032 Assert.assertTrue(PropertyUtils.isSingleWildcardMatch((String) null, (String) null)); 033 Assert.assertFalse(PropertyUtils.isSingleWildcardMatch(null, "")); 034 Assert.assertFalse(PropertyUtils.isSingleWildcardMatch("", (String) null)); 035 Assert.assertFalse(PropertyUtils.isSingleWildcardMatch(null, "*")); 036 Assert.assertTrue(PropertyUtils.isSingleWildcardMatch("", "*")); 037 Assert.assertFalse(PropertyUtils.isSingleWildcardMatch("*", "")); 038 Assert.assertTrue(PropertyUtils.isSingleWildcardMatch("*", "*")); 039 } 040 041 @Test 042 public void testWildcards1() { 043 List<String> includes = Arrays.asList("*.home"); 044 Properties properties = PropertyUtils.duplicate(System.getProperties()); 045 PropertyUtils.trim(properties, includes, null); 046 PropertyUtils.info(properties); 047 } 048 049 @Test 050 public void testWildcards2() { 051 List<String> includes = Arrays.asList("java.*.version"); 052 Properties properties = PropertyUtils.duplicate(System.getProperties()); 053 PropertyUtils.trim(properties, includes, null); 054 PropertyUtils.info(properties); 055 } 056 057 @Test 058 public void testUserHomeWildcard() { 059 List<String> includes = Arrays.asList("user.*.home"); 060 Properties properties = PropertyUtils.duplicate(System.getProperties()); 061 PropertyUtils.trim(properties, includes, null); 062 int size = properties.size(); 063 Assert.assertEquals(1, size); 064 } 065 066 @Test 067 public void testUserHome() { 068 List<String> includes = Arrays.asList("user.home"); 069 Properties properties = PropertyUtils.duplicate(System.getProperties()); 070 PropertyUtils.trim(properties, includes, null); 071 int size = properties.size(); 072 Assert.assertEquals(1, size); 073 } 074 075 @Test 076 public void testMultipleWildcards() { 077 List<String> includes = Arrays.asList("java.*vm*.version"); 078 Properties properties = PropertyUtils.duplicate(System.getProperties()); 079 try { 080 PropertyUtils.trim(properties, includes, null); 081 Assert.fail("Should fail on multiple wildcards"); 082 } catch (IllegalArgumentException ignored) { 083 ; // ignore 084 } 085 } 086 087 @Test 088 public void testPropertyKeysContainingWildardsAndNormalSearchIsUsed() { 089 List<String> includes = Arrays.asList("user.home"); 090 Properties properties = PropertyUtils.duplicate(System.getProperties()); 091 properties.setProperty("user.*.home", "foo"); 092 // Non-wildcard searches on property keys containing wildcards is ok 093 PropertyUtils.trim(properties, includes, null); 094 int size = properties.size(); 095 Assert.assertEquals(1, size); 096 } 097 098 // @Test 099 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}