1 /*
2  * Copyright (c) 2015, 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 import org.testng.annotations.DataProvider;
25 import org.testng.annotations.Test;
26 
27 import static org.testng.Assert.assertEquals;
28 
29 /*
30  * @test
31  * @bug 8077559
32  * @summary Tests Compact String. This one is for String.replace.
33  * @run testng/othervm -XX:+CompactStrings Replace
34  * @run testng/othervm -XX:-CompactStrings Replace
35  */
36 
37 public class Replace extends CompactString {
38 
39     @DataProvider
provider()40     public Object[][] provider() {
41         return new Object[][] {
42 
43                 new Object[] { STRING_L1, 'A', 'B', "B" },
44                 new Object[] { STRING_L1, 'A', 'A', "A" },
45                 new Object[] { STRING_L1, 'A', '\uFF21', "\uFF21" },
46                 new Object[] { STRING_L2, 'A', 'B', "BB" },
47                 new Object[] { STRING_L2, 'B', 'A', "AA" },
48                 new Object[] { STRING_L2, 'C', 'A', "AB" },
49                 new Object[] { STRING_L2, 'B', '\uFF21', "A\uFF21" },
50                 new Object[] { STRING_U1, '\uFF21', 'A', "A" },
51                 new Object[] { STRING_U1, '\uFF22', 'A', "\uFF21" },
52                 new Object[] { STRING_U2, '\uFF22', 'A', "\uFF21A" },
53                 new Object[] { STRING_M12, 'A', '\uFF21', "\uFF21\uFF21" },
54                 new Object[] { STRING_M11, '\uFF21', 'A', "AA" },
55                 new Object[] { STRING_UDUPLICATE, '\uFF21', 'A',
56                         "A\uFF22A\uFF22A\uFF22A\uFF22A\uFF22" },
57                 new Object[] { STRING_MDUPLICATE1, '\uFF21', 'A', "AAAAAAAAAA" },
58                 new Object[] { STRING_MDUPLICATE1, 'A', '\uFF21',
59                         "\uFF21\uFF21\uFF21\uFF21\uFF21\uFF21\uFF21\uFF21\uFF21\uFF21" }, };
60     }
61 
62     @Test(dataProvider = "provider")
testReplace(String str, char oldChar, char newChar, String expected)63     public void testReplace(String str, char oldChar, char newChar,
64             String expected) {
65         map.get(str)
66                 .forEach(
67                         (source, data) -> {
68                             assertEquals(
69                                     data.replace(oldChar, newChar),
70                                     expected,
71                                     String.format(
72                                             "testing String(%s).replace(%s, %s), source : %s, ",
73                                             escapeNonASCIIs(data),
74                                             escapeNonASCII(oldChar),
75                                             escapeNonASCII(newChar), source));
76                         });
77     }
78 
79     @DataProvider
provider2()80     public Object[][] provider2() {
81         return new Object[][] {
82 
83                 new Object[] { STRING_EMPTY, "", "ABC", "ABC" },
84                 new Object[] { STRING_EMPTY, "", "", "" },
85                 new Object[] { STRING_L1, "A", "B", "B" },
86                 new Object[] { STRING_L1, "A", "A", "A" },
87                 new Object[] { STRING_L2, "B", "\uFF21", "A\uFF21" },
88                 new Object[] { STRING_LLONG, "BCD", "\uFF21", "A\uFF21EFGH" },
89                 new Object[] { STRING_U1, "\uFF21", "A", "A" },
90                 new Object[] { STRING_U1, "\uFF21", "A\uFF21", "A\uFF21" },
91                 new Object[] { STRING_U2, "\uFF21", "A", "A\uFF22" },
92                 new Object[] { STRING_U2, "\uFF22", "A", "\uFF21A" },
93                 new Object[] { STRING_UDUPLICATE, "\uFF21\uFF22", "AB",
94                         "ABABABABAB" },
95                 new Object[] { STRING_MDUPLICATE1, "\uFF21", "A", "AAAAAAAAAA" },
96                 new Object[] { STRING_MDUPLICATE1, "A", "\uFF21",
97                         "\uFF21\uFF21\uFF21\uFF21\uFF21\uFF21\uFF21\uFF21\uFF21\uFF21" }, };
98     }
99 
100     @Test(dataProvider = "provider2")
testReplace(String str, CharSequence target, CharSequence replacement, String expected)101     public void testReplace(String str, CharSequence target,
102             CharSequence replacement, String expected) {
103         map.get(str)
104                 .forEach(
105                         (source, data) -> {
106                             assertEquals(
107                                     data.replace(target, replacement),
108                                     expected,
109                                     String.format(
110                                             "testing String(%s).replace(%s, %s), source : %s, ",
111                                             escapeNonASCIIs(data),
112                                             escapeNonASCIIs(target.toString()),
113                                             escapeNonASCIIs(replacement
114                                                     .toString()), source));
115                         });
116     }
117 }
118