Path: blob/master/test/jdk/java/lang/String/LiteralReplace.java
41152 views
/*1* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24* @bug 8058779 8054307 822295525* @library /test/lib26* @build jdk.test.lib.RandomFactory27* @run testng LiteralReplace28* @summary Basic tests of String.replace(CharSequence, CharSequence)29* @key randomness30*/3132import java.util.ArrayList;33import java.util.Arrays;34import java.util.Iterator;35import java.util.regex.Matcher;36import java.util.regex.Pattern;37import java.util.Random;3839import jdk.test.lib.RandomFactory;4041import org.testng.annotations.Test;42import org.testng.annotations.DataProvider;43import static org.testng.Assert.fail;4445public class LiteralReplace {4647@Test(dataProvider="sourceTargetReplacementExpected")48public void testExpected(String source, String target,49String replacement, String expected)50{51String canonical = canonicalReplace(source, target, replacement);52if (!canonical.equals(expected)) {53fail("Canonical: " + canonical + " != " + expected);54}55test0(source, target, replacement, expected);56}5758@Test(dataProvider="sourceTargetReplacement")59public void testCanonical(String source, String target,60String replacement)61{62String canonical = canonicalReplace(source, target, replacement);63test0(source, target, replacement, canonical);64}6566private void test0(String source, String target, String replacement,67String expected)68{69String result = source.replace(target, replacement);70if (!result.equals(expected)) {71fail(result + " != " + expected);72}73}7475@Test(dataProvider="sourceTargetReplacementWithNull",76expectedExceptions = {NullPointerException.class})77public void testNPE(String source, String target, String replacement) {78source.replace(target, replacement);79}8081@Test(expectedExceptions = {OutOfMemoryError.class})82public void testOOM() {83"1".repeat(65537).replace("1", "2".repeat(65537));84}8586@DataProvider87public static Object[][] sourceTargetReplacementExpected() {88return new Object[][] {89{"aaa", "aa", "b", "ba"},90{"abcdefgh", "def", "DEF", "abcDEFgh"},91{"abcdefgh", "123", "DEF", "abcdefgh"},92{"abcdefgh", "abcdefghi", "DEF", "abcdefgh"},93{"abcdefghabc", "abc", "DEF", "DEFdefghDEF"},94{"abcdefghdef", "def", "", "abcgh"},95{"abcdefgh", "", "_", "_a_b_c_d_e_f_g_h_"},96{"", "", "", ""},97{"", "a", "b", ""},98{"", "", "abc", "abc"},99{"abcdefgh", "abcdefgh", "abcdefgh", "abcdefgh"},100{"abcdefgh", "abcdefgh", "abcdefghi", "abcdefghi"},101{"abcdefgh", "abcdefgh", "", ""},102{"abcdabcd", "abcd", "", ""},103{"aaaaaaaaa", "aa", "_X_", "_X__X__X__X_a"},104{"aaaaaaaaa", "aa", "aaa", "aaaaaaaaaaaaa"},105{"aaaaaaaaa", "aa", "aa", "aaaaaaaaa"},106{"a.c.e.g.", ".", "-", "a-c-e-g-"},107{"abcdefgh", "[a-h]", "X", "abcdefgh"},108{"aa+", "a+", "", "a"},109{"^abc$", "abc", "x", "^x$"},110{"abc", "b", "_", "a_c"},111{"abc", "bc", "_", "a_"},112{"abc".repeat(65537) + "end", "b", "_XYZ_", "a_XYZ_c".repeat(65537) + "end"},113{"abc".repeat(65537) + "end", "a", "_", "_bc".repeat(65537) + "end"},114{"a".repeat(65537), "a", "", ""},115{"ab".repeat(65537), "a", "", "b".repeat(65537)},116{"ab".repeat(65537), "ab", "", ""},117{"b" + "ab".repeat(65537), "ab", "", "b"},118119// more with non-latin1 characters120{"\u4e00\u4e00\u4e00",121"\u4e00\u4e00",122"\u4e01",123"\u4e01\u4e00"},124125{"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07\u4e08",126"\u4e03\u4e04\u4e05",127"\u4e10\u4e11\u4e12",128"\u4e00\u4e01\u4e02\u4e10\u4e11\u4e12\u4e06\u4e07\u4e08"},129130{"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07\u4e08",131"ABC",132"\u4e10\u4e11\u4e12",133"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07\u4e08"},134135{"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e02\u4e03\u4e07\u4e08",136"\u4e02\u4e03",137"\u4e12\u4e13",138"\u4e00\u4e01\u4e12\u4e13\u4e04\u4e12\u4e13\u4e07\u4e08"},139140{"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e02\u4e03\u4e07\u4e08",141"\u4e02\u4e03",142"ab",143"\u4e00\u4e01ab\u4e04ab\u4e07\u4e08"},144145{"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07",146"",147"_",148"_\u4e00_\u4e01_\u4e02_\u4e03_\u4e04_\u4e05_\u4e06_\u4e07_"},149{"^\u4e00\u4e01\u4e02$",150"\u4e00\u4e01\u4e02",151"\u4e03",152"^\u4e03$"},153154{"", "\u4e00", "\u4e01", ""},155{"", "", "\u4e00\u4e01\u4e02", "\u4e00\u4e01\u4e02"},156157{"^\u4e00\u4e01\u4e02$",158"\u4e00\u4e01\u4e02",159"X",160"^X$"},161162{"abcdefgh",163"def",164"\u4e01",165"abc\u4e01gh"},166167{"abcdefgh",168"def",169"\u4e01\u4e02",170"abc\u4e01\u4e02gh"},171172{"abcdefabcgh",173"abc",174"\u4e01\u4e02",175"\u4e01\u4e02def\u4e01\u4e02gh"},176177{"abcdefabcghabc",178"abc",179"\u4e01\u4e02",180"\u4e01\u4e02def\u4e01\u4e02gh\u4e01\u4e02"},181182{"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05",183"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05",184"abcd",185"abcd"},186187{"\u4e00\u4e01",188"\u4e00\u4e01",189"abcdefg",190"abcdefg"},191192{"\u4e00\u4e01xyz",193"\u4e00\u4e01",194"abcdefg",195"abcdefgxyz"},196197{"\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00",198"\u4e00\u4e00",199"\u4e00\u4e00\u4e00",200"\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00"},201202{"\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00",203"\u4e00\u4e00\u4e00",204"\u4e00\u4e00",205"\u4e00\u4e00\u4e00\u4e00"},206207{"\u4e00.\u4e01.\u4e02.\u4e03.\u4e04.",208".",209"-",210"\u4e00-\u4e01-\u4e02-\u4e03-\u4e04-"},211212{"\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00",213"\u4e00",214"",215""},216217{"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05",218"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05",219"",220""},221222{"\u4e01\u4e02\u4e03", "\u4e02", "\u4e02", "\u4e01\u4e02\u4e03"},223{"\u4e01\u4e02\u4e03", "\u4e02", "\u4e04", "\u4e01\u4e04\u4e03"},224{"\u4e01\u4e02\u4e03", "\u4e02", "_", "\u4e01_\u4e03"},225{"a\u4e02c", "\u4e02", "_", "a_c"},226{"\u4e01@\u4e03", "@", "_", "\u4e01_\u4e03"},227{"\u4e01@\u4e03", "@", "\u4e02", "\u4e01\u4e02\u4e03"},228{"\u4e01\u4e02\u4e03", "\u4e02\u4e03", "\u4e02\u4e03", "\u4e01\u4e02\u4e03"},229{"\u4e01\u4e02\u4e03", "\u4e02\u4e03", "\u4e04\u4e05", "\u4e01\u4e04\u4e05"},230{"\u4e01\u4e02\u4e03", "\u4e02\u4e03", "\u4e06", "\u4e01\u4e06"},231{"\u4e01\u4e02\u4e03", "\u4e02\u4e03", "<>", "\u4e01<>"},232{"@\u4e02\u4e03", "\u4e02\u4e03", "<>", "@<>"},233{"\u4e01@@", "\u4e01@", "", "@"},234{"\u4e01@@", "\u4e01@", "#", "#@"},235{"\u4e01@@", "\u4e01@", "\u4e09", "\u4e09@"},236{"\u4e01@@", "\u4e01@", "#\u4e09", "#\u4e09@"},237{"\u4e01\u4e02\u4e03".repeat(32771) + "end", "\u4e02", "\u4e02", "\u4e01\u4e02\u4e03".repeat(32771) + "end"},238{"\u4e01\u4e02\u4e03".repeat(32771) + "end", "\u4e02", "\u4e04", "\u4e01\u4e04\u4e03".repeat(32771) + "end"},239{"\u4e01\u4e02\u4e03".repeat(32771) + "end", "\u4e02", "\u4e04\u4e05", "\u4e01\u4e04\u4e05\u4e03".repeat(32771) + "end"},240{"\u4e01\u4e02\u4e03".repeat(32771) + "end", "\u4e02", "_", "\u4e01_\u4e03".repeat(32771) + "end"},241{"\u4e01_\u4e03".repeat(32771) + "end", "_", "_", "\u4e01_\u4e03".repeat(32771) + "end"},242{"\u4e01_\u4e03".repeat(32771) + "end", "_", "\u4e06", "\u4e01\u4e06\u4e03".repeat(32771) + "end"},243{"\u4e01_\u4e03".repeat(32771) + "end", "_", "\u4e06\u4e06", "\u4e01\u4e06\u4e06\u4e03".repeat(32771) + "end"},244{"X_Y".repeat(32771) + "end", "_", "\u4e07", "X\u4e07Y".repeat(32771) + "end"},245{"X_Y".repeat(32771) + "end", "_", "\u4e07\u4e08", "X\u4e07\u4e08Y".repeat(32771) + "end"},246{"X_Y".repeat(32771) + "end", "_", ".\u4e08.", "X.\u4e08.Y".repeat(32771) + "end"},247{"\u4e0a".repeat(32771), "\u4e0a", "", ""},248{"\u4e0a\u4e0b".repeat(32771), "\u4e0a", "", "\u4e0b".repeat(32771)},249{"\u4e0a\u4e0b".repeat(32771), "\u4e0b", "", "\u4e0a".repeat(32771)},250{"\u4e0a\u4e0b".repeat(32771), "\u4e0a\u4e0b", "", ""},251{"\u4e0b" + "\u4e0a\u4e0b".repeat(32771), "\u4e0a\u4e0b", "", "\u4e0b"},252{"\u4e0a\u4e0b".repeat(32771) + "\u4e0a", "\u4e0a\u4e0b", "", "\u4e0a"},253{"\u4e0b" + "\u4e0a\u4e0b".repeat(32771) + "\u4e0a", "\u4e0a\u4e0b", "", "\u4e0b\u4e0a"},254{"b" + "\u4e0a\u4e0b".repeat(32771), "\u4e0a\u4e0b", "", "b"},255{"\u4e0a\u4e0b".repeat(32771) + "a", "\u4e0a\u4e0b", "", "a"},256{"b" + "\u4e0a\u4e0b".repeat(32771) + "a", "\u4e0a\u4e0b", "", "ba"},257};258}259260@DataProvider261public static Iterator<Object[]> sourceTargetReplacement() {262ArrayList<Object[]> list = new ArrayList<>();263for (int maxSrcLen = 1; maxSrcLen <= (1 << 10); maxSrcLen <<= 1) {264for (int maxTrgLen = 1; maxTrgLen <= (1 << 10); maxTrgLen <<= 1) {265for (int maxPrlLen = 1; maxPrlLen <= (1 << 10); maxPrlLen <<= 1) {266list.add(makeArray(makeRandomString(maxSrcLen),267makeRandomString(maxTrgLen),268makeRandomString(maxPrlLen)));269270String source = makeRandomString(maxSrcLen);271list.add(makeArray(source,272mekeRandomSubstring(source, maxTrgLen),273makeRandomString(maxPrlLen)));274}275}276}277return list.iterator();278}279280@DataProvider281public static Iterator<Object[]> sourceTargetReplacementWithNull() {282ArrayList<Object[]> list = new ArrayList<>();283Object[] arr = {null, "", "a", "b", "string", "str", "ababstrstr"};284for (int i = 0; i < arr.length; ++i) {285for (int j = 0; j < arr.length; ++j) {286for (int k = 0; k < arr.length; ++k) {287if (arr[i] != null && (arr[j] == null || arr[k] == null)) {288list.add(makeArray(arr[i], arr[j], arr[k]));289}290}291}292}293return list.iterator();294}295296// utilities297298/**299* How the String.replace(CharSequence, CharSequence) used to be implemented300*/301private static String canonicalReplace(String source, String target, String replacement) {302return Pattern.compile(target.toString(), Pattern.LITERAL).matcher(303source).replaceAll(Matcher.quoteReplacement(replacement.toString()));304}305306private static final Random random = RandomFactory.getRandom();307308private static final char[] CHARS = ("qwertyuiop[]12345678" +309"90-=\\`asdfghjkl;'zxcvbnm,./~!@#$%^&*()_+|QWERTYUIOP{" +310"}ASDFGHJKL:\"ZXCVBNM<>?\n\r\t\u0444\u044B\u0432\u0430").toCharArray();311312private static String makeRandomString(int maxLen) {313int len = random.nextInt(maxLen);314char[] buf = new char[len];315for (int i = 0; i < len; ++i) {316buf[i] = CHARS[random.nextInt(CHARS.length)];317}318return new String(buf);319}320321private static String mekeRandomSubstring(String source, int maxLen) {322if (source.isEmpty()) {323return source;324}325int pos = random.nextInt(source.length());326int len = Integer.min(source.length() - pos,327random.nextInt(maxLen));328return source.substring(pos, pos + len);329}330331private static Object[] makeArray(Object... array) {332return array;333}334}335336337