1 /* 2 * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 8058779 8054307 8222955 26 * @library /test/lib 27 * @build jdk.test.lib.RandomFactory 28 * @run testng LiteralReplace 29 * @summary Basic tests of String.replace(CharSequence, CharSequence) 30 * @key randomness 31 */ 32 33 import java.util.ArrayList; 34 import java.util.Arrays; 35 import java.util.Iterator; 36 import java.util.regex.Matcher; 37 import java.util.regex.Pattern; 38 import java.util.Random; 39 40 import jdk.test.lib.RandomFactory; 41 42 import org.testng.annotations.Test; 43 import org.testng.annotations.DataProvider; 44 import static org.testng.Assert.fail; 45 46 public class LiteralReplace { 47 48 @Test(dataProvider="sourceTargetReplacementExpected") 49 public void testExpected(String source, String target, 50 String replacement, String expected) 51 { 52 String canonical = canonicalReplace(source, target, replacement); 53 if (!canonical.equals(expected)) { 54 fail("Canonical: " + canonical + " != " + expected); 55 } 56 test0(source, target, replacement, expected); 57 } 58 59 @Test(dataProvider="sourceTargetReplacement") 60 public void testCanonical(String source, String target, 61 String replacement) 62 { 63 String canonical = canonicalReplace(source, target, replacement); 64 test0(source, target, replacement, canonical); 65 } 66 67 private void test0(String source, String target, String replacement, 68 String expected) 69 { 70 String result = source.replace(target, replacement); 71 if (!result.equals(expected)) { 72 fail(result + " != " + expected); 73 } 74 } 75 76 @Test(dataProvider="sourceTargetReplacementWithNull", 77 expectedExceptions = {NullPointerException.class}) 78 public void testNPE(String source, String target, String replacement) { 79 source.replace(target, replacement); 80 } 81 82 83 @DataProvider 84 public static Object[][] sourceTargetReplacementExpected() { 85 return new Object[][] { 86 {"aaa", "aa", "b", "ba"}, 87 {"abcdefgh", "def", "DEF", "abcDEFgh"}, 88 {"abcdefgh", "123", "DEF", "abcdefgh"}, 89 {"abcdefgh", "abcdefghi", "DEF", "abcdefgh"}, 90 {"abcdefghabc", "abc", "DEF", "DEFdefghDEF"}, 91 {"abcdefghdef", "def", "", "abcgh"}, 92 {"abcdefgh", "", "_", "_a_b_c_d_e_f_g_h_"}, 93 {"", "", "", ""}, 94 {"", "a", "b", ""}, 95 {"", "", "abc", "abc"}, 96 {"abcdefgh", "abcdefgh", "abcdefgh", "abcdefgh"}, 97 {"abcdefgh", "abcdefgh", "abcdefghi", "abcdefghi"}, 98 {"abcdefgh", "abcdefgh", "", ""}, 99 {"abcdabcd", "abcd", "", ""}, 100 {"aaaaaaaaa", "aa", "_X_", "_X__X__X__X_a"}, 101 {"aaaaaaaaa", "aa", "aaa", "aaaaaaaaaaaaa"}, 102 {"aaaaaaaaa", "aa", "aa", "aaaaaaaaa"}, 103 {"a.c.e.g.", ".", "-", "a-c-e-g-"}, 104 {"abcdefgh", "[a-h]", "X", "abcdefgh"}, 105 {"aa+", "a+", "", "a"}, 106 {"^abc$", "abc", "x", "^x$"}, 107 {"abc", "b", "_", "a_c"}, 108 {"abc", "bc", "_", "a_"}, 109 {"abc".repeat(65537) + "end", "b", "_XYZ_", "a_XYZ_c".repeat(65537) + "end"}, 110 {"abc".repeat(65537) + "end", "a", "_", "_bc".repeat(65537) + "end"}, 111 {"a".repeat(65537), "a", "", ""}, 112 {"ab".repeat(65537), "a", "", "b".repeat(65537)}, 113 {"ab".repeat(65537), "ab", "", ""}, 114 {"b" + "ab".repeat(65537), "ab", "", "b"}, 115 116 // more with non-latin1 characters 117 {"\u4e00\u4e00\u4e00", 118 "\u4e00\u4e00", 119 "\u4e01", 120 "\u4e01\u4e00"}, 121 122 {"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07\u4e08", 123 "\u4e03\u4e04\u4e05", 124 "\u4e10\u4e11\u4e12", 125 "\u4e00\u4e01\u4e02\u4e10\u4e11\u4e12\u4e06\u4e07\u4e08"}, 126 127 {"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07\u4e08", 128 "ABC", 129 "\u4e10\u4e11\u4e12", 130 "\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07\u4e08"}, 131 132 {"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e02\u4e03\u4e07\u4e08", 133 "\u4e02\u4e03", 134 "\u4e12\u4e13", 135 "\u4e00\u4e01\u4e12\u4e13\u4e04\u4e12\u4e13\u4e07\u4e08"}, 136 137 {"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e02\u4e03\u4e07\u4e08", 138 "\u4e02\u4e03", 139 "ab", 140 "\u4e00\u4e01ab\u4e04ab\u4e07\u4e08"}, 141 142 {"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07", 143 "", 144 "_", 145 "_\u4e00_\u4e01_\u4e02_\u4e03_\u4e04_\u4e05_\u4e06_\u4e07_"}, 146 {"^\u4e00\u4e01\u4e02$", 147 "\u4e00\u4e01\u4e02", 148 "\u4e03", 149 "^\u4e03$"}, 150 151 {"", "\u4e00", "\u4e01", ""}, 152 {"", "", "\u4e00\u4e01\u4e02", "\u4e00\u4e01\u4e02"}, 153 154 {"^\u4e00\u4e01\u4e02$", 155 "\u4e00\u4e01\u4e02", 156 "X", 157 "^X$"}, 158 159 {"abcdefgh", 160 "def", 161 "\u4e01", 162 "abc\u4e01gh"}, 163 164 {"abcdefgh", 165 "def", 166 "\u4e01\u4e02", 167 "abc\u4e01\u4e02gh"}, 168 169 {"abcdefabcgh", 170 "abc", 171 "\u4e01\u4e02", 172 "\u4e01\u4e02def\u4e01\u4e02gh"}, 173 174 {"abcdefabcghabc", 175 "abc", 176 "\u4e01\u4e02", 177 "\u4e01\u4e02def\u4e01\u4e02gh\u4e01\u4e02"}, 178 179 {"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05", 180 "\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05", 181 "abcd", 182 "abcd"}, 183 184 {"\u4e00\u4e01", 185 "\u4e00\u4e01", 186 "abcdefg", 187 "abcdefg"}, 188 189 {"\u4e00\u4e01xyz", 190 "\u4e00\u4e01", 191 "abcdefg", 192 "abcdefgxyz"}, 193 194 {"\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00", 195 "\u4e00\u4e00", 196 "\u4e00\u4e00\u4e00", 197 "\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00"}, 198 199 {"\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00", 200 "\u4e00\u4e00\u4e00", 201 "\u4e00\u4e00", 202 "\u4e00\u4e00\u4e00\u4e00"}, 203 204 {"\u4e00.\u4e01.\u4e02.\u4e03.\u4e04.", 205 ".", 206 "-", 207 "\u4e00-\u4e01-\u4e02-\u4e03-\u4e04-"}, 208 209 {"\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00", 210 "\u4e00", 211 "", 212 ""}, 213 214 {"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05", 215 "\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05", 216 "", 217 ""}, 218 }; 219 } 220 221 @DataProvider 222 public static Iterator<Object[]> sourceTargetReplacement() { 223 ArrayList<Object[]> list = new ArrayList<>(); 224 for (int maxSrcLen = 1; maxSrcLen <= (1 << 10); maxSrcLen <<= 1) { 225 for (int maxTrgLen = 1; maxTrgLen <= (1 << 10); maxTrgLen <<= 1) { 226 for (int maxPrlLen = 1; maxPrlLen <= (1 << 10); maxPrlLen <<= 1) { 227 list.add(makeArray(makeRandomString(maxSrcLen), 228 makeRandomString(maxTrgLen), 229 makeRandomString(maxPrlLen))); 230 231 String source = makeRandomString(maxSrcLen); 232 list.add(makeArray(source, 233 mekeRandomSubstring(source, maxTrgLen), 234 makeRandomString(maxPrlLen))); 235 } 236 } 237 } 238 return list.iterator(); 239 } 240 241 @DataProvider 242 public static Iterator<Object[]> sourceTargetReplacementWithNull() { 243 ArrayList<Object[]> list = new ArrayList<>(); 244 Object[] arr = {null, "", "a", "b", "string", "str", "ababstrstr"}; 245 for (int i = 0; i < arr.length; ++i) { 246 for (int j = 0; j < arr.length; ++j) { 247 for (int k = 0; k < arr.length; ++k) { 248 if (arr[i] != null && (arr[j] == null || arr[k] == null)) { 249 list.add(makeArray(arr[i], arr[j], arr[k])); 250 } 251 } 252 } 253 } 254 return list.iterator(); 255 } 256 257 // utilities 258 259 /** 260 * How the String.replace(CharSequence, CharSequence) used to be implemented 261 */ 262 private static String canonicalReplace(String source, String target, String replacement) { 263 return Pattern.compile(target.toString(), Pattern.LITERAL).matcher( 264 source).replaceAll(Matcher.quoteReplacement(replacement.toString())); 265 } 266 267 private static final Random random = RandomFactory.getRandom(); 268 269 private static final char[] CHARS = ("qwertyuiop[]12345678" + 270 "90-=\\`asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+|QWERTYUIOP{" + 271 "}ASDFGHJKL:\"ZXCVBNM<>?\n\r\t\u0444\u044B\u0432\u0430").toCharArray(); 272 273 private static String makeRandomString(int maxLen) { 274 int len = random.nextInt(maxLen); 275 char[] buf = new char[len]; 276 for (int i = 0; i < len; ++i) { 277 buf[i] = CHARS[random.nextInt(CHARS.length)]; 278 } 279 return new String(buf); 280 } 281 282 private static String mekeRandomSubstring(String source, int maxLen) { 283 if (source.isEmpty()) { 284 return source; 285 } 286 int pos = random.nextInt(source.length()); 287 int len = Integer.min(source.length() - pos, 288 random.nextInt(maxLen)); 289 return source.substring(pos, pos + len); 290 } 291 292 private static Object[] makeArray(Object... array) { 293 return array; 294 } 295 }