Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/StringJoiner/StringJoinerTest.java
41152 views
1
/*
2
* Copyright (c) 2013, 2020, 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 5015163 7172553 8249258
26
* @summary tests StringJoinerTest
27
* @modules java.base/jdk.internal.util
28
* @requires vm.bits == "64" & os.maxMemory > 4G
29
* @run testng/othervm -Xmx4g -XX:+CompactStrings StringJoinerTest
30
* @author Jim Gish
31
*/
32
import java.util.ArrayList;
33
import java.util.StringJoiner;
34
import org.testng.annotations.Test;
35
import static jdk.internal.util.ArraysSupport.SOFT_MAX_ARRAY_LENGTH;
36
import static org.testng.Assert.assertEquals;
37
import static org.testng.Assert.fail;
38
39
40
@Test(groups = {"unit","string","util","libs"})
41
public class StringJoinerTest {
42
43
private static final String EMPTY = "EMPTY";
44
private static final String ONE = "One";
45
private static final int ONE_LEN = ONE.length();
46
private static final String TWO = "Two";
47
private static final int TWO_LEN = TWO.length();
48
private static final String THREE = "Three";
49
private static final String FOUR = "Four";
50
private static final String FIVE = "Five";
51
private static final String DASH = "-";
52
private static final String MAX_STRING = "*".repeat(SOFT_MAX_ARRAY_LENGTH);
53
54
public void addAddAll() {
55
StringJoiner sj = new StringJoiner(DASH, "{", "}");
56
sj.add(ONE);
57
58
ArrayList<String> nextOne = new ArrayList<>();
59
nextOne.add(TWO);
60
nextOne.add(THREE);
61
nextOne.stream().forEachOrdered(sj::add);
62
63
String expected = "{"+ONE+DASH+TWO+DASH+THREE+"}";
64
assertEquals(sj.toString(), expected);
65
}
66
67
void addAlladd() {
68
StringJoiner sj = new StringJoiner(DASH, "{", "}");
69
70
ArrayList<String> firstOne = new ArrayList<>();
71
firstOne.add(ONE);
72
firstOne.add(TWO);
73
firstOne.stream().forEachOrdered(sj::add);
74
75
sj.add(THREE);
76
77
String expected = "{"+ONE+DASH+TWO+DASH+THREE+"}";
78
assertEquals(sj.toString(), expected);
79
}
80
81
// The following tests do two successive adds of different types
82
public void addAlladdAll() {
83
StringJoiner sj = new StringJoiner(DASH, "{", "}");
84
ArrayList<String> firstOne = new ArrayList<>();
85
firstOne.add(ONE);
86
firstOne.add(TWO);
87
firstOne.add(THREE);
88
firstOne.stream().forEachOrdered(sj::add);
89
90
ArrayList<String> nextOne = new ArrayList<>();
91
nextOne.add(FOUR);
92
nextOne.add(FIVE);
93
nextOne.stream().forEachOrdered(sj::add);
94
95
String expected = "{"+ONE+DASH+TWO+DASH+THREE+DASH+FOUR+DASH+FIVE+"}";
96
assertEquals(sj.toString(), expected);
97
}
98
99
public void addCharSequence() {
100
StringJoiner sj = new StringJoiner(",");
101
CharSequence cs_one = ONE;
102
CharSequence cs_two = TWO;
103
104
sj.add(cs_one);
105
sj.add(cs_two);
106
107
assertEquals(sj.toString(), ONE + "," + TWO);
108
109
sj = new StringJoiner(DASH, "{", "}");
110
sj.add(cs_one);
111
sj.add(cs_two);
112
113
assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");
114
115
StringBuilder builder = new StringBuilder(ONE);
116
StringBuffer buffer = new StringBuffer(THREE);
117
sj = new StringJoiner(", ", "{ ", " }");
118
sj.add(builder).add(buffer);
119
builder.append(TWO);
120
buffer.append(FOUR);
121
assertEquals(sj.toString(), "{ " + ONE + ", " + THREE + " }",
122
"CharSequence is copied when add");
123
sj.add(builder);
124
assertEquals(sj.toString(), "{ " + ONE + ", " + THREE + ", " + ONE +
125
TWO + " }");
126
}
127
128
public void addCharSequenceWithEmptyValue() {
129
StringJoiner sj = new StringJoiner(",").setEmptyValue(EMPTY);
130
CharSequence cs_one = ONE;
131
CharSequence cs_two = TWO;
132
133
sj.add(cs_one);
134
sj.add(cs_two);
135
136
assertEquals(sj.toString(), ONE + "," + TWO);
137
138
sj = new StringJoiner(DASH, "{", "}");
139
sj.add(cs_one);
140
sj.add(cs_two);
141
assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");
142
143
sj = new StringJoiner(DASH, "{", "}");
144
assertEquals(sj.toString(), "{}");
145
146
sj = new StringJoiner("=", "{", "}").setEmptyValue("");
147
assertEquals(sj.toString(), "");
148
149
sj = new StringJoiner(DASH, "{", "}").setEmptyValue(EMPTY);
150
assertEquals(sj.toString(), EMPTY);
151
152
sj.add(cs_one);
153
sj.add(cs_two);
154
assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");
155
}
156
157
public void addString() {
158
StringJoiner sj = new StringJoiner(DASH);
159
sj.add(ONE);
160
assertEquals(sj.toString(), ONE);
161
162
sj = new StringJoiner(DASH, "{", "}");
163
sj.add(ONE);
164
assertEquals(sj.toString(), "{" + ONE + "}");
165
166
sj.add(TWO);
167
assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");
168
}
169
170
public void lengthWithCustomEmptyValue() {
171
StringJoiner sj = new StringJoiner(DASH, "<", ">").setEmptyValue(EMPTY);
172
assertEquals(sj.length(), EMPTY.length());
173
sj.add("");
174
assertEquals(sj.length(), "<>".length());
175
sj.add("");
176
assertEquals(sj.length(), "<->".length());
177
sj.add(ONE);
178
assertEquals(sj.length(), 4 + ONE_LEN);
179
assertEquals(sj.toString().length(), sj.length());
180
sj.add(TWO);
181
assertEquals(sj.length(), 5 + ONE_LEN + TWO_LEN);
182
assertEquals(sj.toString().length(), sj.length());
183
sj = new StringJoiner("||", "<", "-->");
184
assertEquals(sj.length(), 4);
185
assertEquals(sj.toString().length(), sj.length());
186
sj.add("abcdef");
187
assertEquals(sj.length(), 10);
188
assertEquals(sj.toString().length(), sj.length());
189
sj.add("xyz");
190
assertEquals(sj.length(), 15);
191
assertEquals(sj.toString().length(), sj.length());
192
}
193
194
public void noAddAndEmptyValue() {
195
StringJoiner sj = new StringJoiner(DASH, "", "").setEmptyValue(EMPTY);
196
assertEquals(sj.toString(), EMPTY);
197
198
sj = new StringJoiner(DASH, "<..", "");
199
assertEquals(sj.toString(), "<..");
200
201
sj = new StringJoiner(DASH, "<..", "");
202
assertEquals(sj.toString(), "<..");
203
204
sj = new StringJoiner(DASH, "", "==>");
205
assertEquals(sj.toString(), "==>");
206
207
sj = new StringJoiner(DASH, "{", "}");
208
assertEquals(sj.toString(), "{}");
209
}
210
211
@Test(expectedExceptions = {NullPointerException.class})
212
public void setEmptyValueNull() {
213
new StringJoiner(DASH, "{", "}").setEmptyValue(null);
214
}
215
216
@Test(expectedExceptions = {NullPointerException.class})
217
public void setDelimiterNull() {
218
new StringJoiner(null);
219
}
220
221
@Test(expectedExceptions = {NullPointerException.class})
222
public void setPrefixNull() {
223
new StringJoiner(DASH, null, "}");
224
}
225
226
@Test(expectedExceptions = {NullPointerException.class})
227
public void setSuffixNull() {
228
new StringJoiner(DASH, "{", null);
229
}
230
231
public void stringFromtoString() {
232
StringJoiner sj = new StringJoiner(", ");
233
assertEquals(sj.toString(), "");
234
sj = new StringJoiner(",", "{", "}");
235
assertEquals(sj.toString(), "{}");
236
237
sj = new StringJoiner(",");
238
sj.add(ONE);
239
assertEquals(sj.toString(), ONE);
240
241
sj.add(TWO);
242
assertEquals(sj.toString(), ONE + "," + TWO);
243
244
sj = new StringJoiner(",", "{--", "--}");
245
sj.add(ONE);
246
sj.add(TWO);
247
assertEquals(sj.toString(), "{--" + ONE + "," + TWO + "--}");
248
249
}
250
251
public void stringFromtoStringWithEmptyValue() {
252
StringJoiner sj = new StringJoiner(" ", "", "");
253
assertEquals(sj.toString(), "");
254
sj = new StringJoiner(", ");
255
assertEquals(sj.toString(), "");
256
sj = new StringJoiner(",", "{", "}");
257
assertEquals(sj.toString(), "{}");
258
259
sj = new StringJoiner(",", "{", "}").setEmptyValue("");
260
assertEquals(sj.toString(), "");
261
262
sj = new StringJoiner(",");
263
sj.add(ONE);
264
assertEquals(sj.toString(), ONE);
265
266
sj.add(TWO);
267
assertEquals(sj.toString(), ONE + "," + TWO);
268
269
sj = new StringJoiner(",", "{--", "--}");
270
sj.add(ONE);
271
assertEquals(sj.toString(), "{--" + ONE + "--}" );
272
273
sj.add(TWO);
274
assertEquals(sj.toString(), "{--" + ONE + "," + TWO + "--}");
275
276
}
277
278
public void toStringWithCustomEmptyValue() {
279
StringJoiner sj = new StringJoiner(DASH, "<", ">").setEmptyValue(EMPTY);
280
assertEquals(sj.toString(), EMPTY);
281
sj.add("");
282
assertEquals(sj.toString(), "<>");
283
sj.add("");
284
assertEquals(sj.toString(), "<->");
285
}
286
287
private void testCombos(String infix, String prefix, String suffix) {
288
StringJoiner sj = new StringJoiner(infix, prefix, suffix);
289
assertEquals(sj.toString(), prefix + suffix);
290
assertEquals(sj.toString().length(), sj.length());
291
// EmptyValue
292
sj = new StringJoiner(infix, prefix, suffix).setEmptyValue("<NONE>");
293
assertEquals(sj.toString(), "<NONE>");
294
assertEquals(sj.toString().length(), sj.length());
295
296
// empty in front
297
sj.add("");
298
assertEquals(sj.toString(), prefix + suffix);
299
// empty in middle
300
sj.add("");
301
assertEquals(sj.toString(), prefix + infix + suffix);
302
sj.add("1");
303
assertEquals(sj.toString(), prefix + infix + infix + "1" + suffix);
304
// empty at end
305
sj.add("");
306
assertEquals(sj.toString(), prefix + infix + infix + "1" + infix + suffix);
307
308
sj = new StringJoiner(infix, prefix, suffix).setEmptyValue("<NONE>");
309
sj.add("1");
310
assertEquals(sj.toString(), prefix + "1" + suffix);
311
sj.add("2");
312
assertEquals(sj.toString(), prefix + "1" + infix + "2" + suffix);
313
sj.add("");
314
assertEquals(sj.toString(), prefix + "1" + infix + "2" + infix + suffix);
315
sj.add("3");
316
assertEquals(sj.toString(), prefix + "1" + infix + "2" + infix + infix + "3" + suffix);
317
}
318
319
public void testDelimiterCombinations() {
320
testCombos("", "", "");
321
testCombos("", "<", "");
322
testCombos("", "", ">");
323
testCombos("", "<", ">");
324
testCombos(",", "", "");
325
testCombos(",", "<", "");
326
testCombos(",", "", ">");
327
testCombos(",", "<", ">");
328
}
329
330
public void OOM1() {
331
try {
332
new StringJoiner(MAX_STRING, MAX_STRING, MAX_STRING).toString();
333
fail("Should have thrown OutOfMemoryError");
334
} catch (OutOfMemoryError ex) {
335
// okay
336
}
337
}
338
339
public void OOM2() {
340
try {
341
new StringJoiner(MAX_STRING, MAX_STRING, "").toString();
342
fail("Should have thrown OutOfMemoryError");
343
} catch (OutOfMemoryError ex) {
344
// okay
345
}
346
}
347
348
public void OOM3() {
349
try {
350
new StringJoiner(MAX_STRING, "", MAX_STRING).toString();
351
fail("Should have thrown OutOfMemoryError");
352
} catch (OutOfMemoryError ex) {
353
// okay
354
}
355
}
356
357
public void OOM4() {
358
try {
359
new StringJoiner("", MAX_STRING, MAX_STRING).toString();
360
fail("Should have thrown OutOfMemoryError");
361
} catch (OutOfMemoryError ex) {
362
// okay
363
}
364
}
365
}
366
367
368