Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/String/StringJoinTest.java
41149 views
1
/*
2
* Copyright (c) 2013, 2021, 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 8267529
26
* @summary test String merge/join that is the inverse of String.split()
27
* @run testng StringJoinTest
28
* @author Jim Gish
29
*/
30
import java.util.ArrayList;
31
import java.util.List;
32
import java.util.StringJoiner;
33
import org.testng.annotations.Test;
34
35
import static org.testng.Assert.*;
36
37
@Test(groups = {"unit","string","lang","libs"})
38
public class StringJoinTest {
39
private static final String DASH = "-";
40
private static final String BEGIN = "Hi there";
41
private static final String JIM = "Jim";
42
private static final String JOHN = "John";
43
private static final String AND_JOE = "and Joe";
44
private static final String BILL = "Bill";
45
private static final String BOB = "Bob";
46
private static final String AND_BO = "and Bo";
47
private static final String ZEKE = "Zeke";
48
private static final String ZACK = "Zack";
49
private static final String AND_ZOE = "and Zoe";
50
51
/**
52
* Tests the join() methods on String
53
*/
54
public void testJoinStringVarargs() {
55
// check a non-null join of String array (var-args) elements
56
String expectedResult = BEGIN + DASH + JIM + DASH + JOHN + DASH + AND_JOE;
57
String result = String.join(DASH, BEGIN, JIM, JOHN, AND_JOE);
58
59
assertEquals(result, expectedResult, "BEGIN.join(DASH, JIM, JOHN, AND_JOE)");
60
// test with just one element
61
assertEquals(String.join(DASH, BEGIN), BEGIN);
62
}
63
64
public void testJoinStringArray() {
65
// check a non-null join of Object[] with String elements
66
String[] theBs = {BILL, BOB, AND_BO};
67
String result = String.join(DASH, theBs);
68
String expectedResult = BILL + DASH + BOB + DASH + AND_BO;
69
assertEquals(result, expectedResult, "String.join(DASH, theBs)");
70
}
71
72
public void testJoinEmptyStringArray() {
73
// check a non-null join of Object[] with String elements
74
String[] empties = {};
75
String result = String.join(DASH, empties);
76
assertEquals(result, "", "String.join(DASH, empties)");
77
}
78
79
@Test(expectedExceptions = {NullPointerException.class})
80
public void testJoinNullStringArray() {
81
// check a non-null join of Object[] with String elements
82
String[] empties = null;
83
String result = String.join(DASH, empties);
84
}
85
86
@Test(expectedExceptions = {NullPointerException.class})
87
public void testJoinNullIterableStringList() {
88
// check join of an Iterables
89
List<CharSequence> theZsList = null;
90
String.join(DASH, theZsList);
91
}
92
93
public void testJoinIterableStringList() {
94
// check join of an Iterables
95
List<CharSequence> theZsList = new ArrayList<>();
96
theZsList.add(ZEKE);
97
theZsList.add(ZACK);
98
theZsList.add(AND_ZOE);
99
assertEquals(String.join(DASH, theZsList), ZEKE + DASH + ZACK + DASH
100
+ AND_ZOE, "String.join(DASH, theZsList))");
101
}
102
103
public void testJoinNullStringList() {
104
List<CharSequence> nullList = null;
105
try {
106
assertEquals( String.join( DASH, nullList ), "null" );
107
fail("Null container should cause NPE");
108
} catch (NullPointerException npe) {}
109
assertEquals(String.join(DASH, null, null), "null" + DASH + "null");
110
}
111
112
@Test(expectedExceptions = {NullPointerException.class})
113
public void testJoinNullDelimiter() {
114
String.join(null, JIM, JOHN);
115
}
116
117
public void testIgnoreDelimiterCoderJoin() {
118
// 8267529: Ensure that joining zero or one latin-1 Strings with a UTF-16
119
// delimiter produce a String with a latin-1 coder, since the delimiter
120
// is not added.
121
assertEquals("", new StringJoiner("\u2013").toString());
122
assertEquals("foo", new StringJoiner("\u2013").add("foo").toString());
123
assertEquals("", String.join("\u2013"));
124
assertEquals("foo", String.join("\u2013", "foo"));
125
}
126
}
127
128