author | jlaskey |
Fri, 18 May 2018 08:43:49 -0300 | |
changeset 50175 | 589ed2770141 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
17181
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
1 |
/* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
2 |
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
4 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
8 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
13 |
* accompanied this code). |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
14 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
18 |
* |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
21 |
* questions. |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
22 |
*/ |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
23 |
/** |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
24 |
* @test @bug 5015163 |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
25 |
* @summary test String merge/join that is the inverse of String.split() |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
26 |
* @run testng StringJoinTest |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
27 |
* @author Jim Gish |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
28 |
*/ |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
29 |
import java.util.ArrayList; |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
30 |
import java.util.List; |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
31 |
import org.testng.annotations.Test; |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
32 |
|
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
33 |
import static org.testng.Assert.*; |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
34 |
|
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
35 |
@Test(groups = {"unit","string","lang","libs"}) |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
36 |
public class StringJoinTest { |
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
17181
diff
changeset
|
37 |
private static final String DASH = "-"; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
17181
diff
changeset
|
38 |
private static final String BEGIN = "Hi there"; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
17181
diff
changeset
|
39 |
private static final String JIM = "Jim"; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
17181
diff
changeset
|
40 |
private static final String JOHN = "John"; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
17181
diff
changeset
|
41 |
private static final String AND_JOE = "and Joe"; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
17181
diff
changeset
|
42 |
private static final String BILL = "Bill"; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
17181
diff
changeset
|
43 |
private static final String BOB = "Bob"; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
17181
diff
changeset
|
44 |
private static final String AND_BO = "and Bo"; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
17181
diff
changeset
|
45 |
private static final String ZEKE = "Zeke"; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
17181
diff
changeset
|
46 |
private static final String ZACK = "Zack"; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
17181
diff
changeset
|
47 |
private static final String AND_ZOE = "and Zoe"; |
17181
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
48 |
|
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
49 |
/** |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
50 |
* Tests the join() methods on String |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
51 |
*/ |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
52 |
public void testJoinStringVarargs() { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
53 |
// check a non-null join of String array (var-args) elements |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
54 |
String expectedResult = BEGIN + DASH + JIM + DASH + JOHN + DASH + AND_JOE; |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
55 |
String result = String.join(DASH, BEGIN, JIM, JOHN, AND_JOE); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
56 |
|
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
57 |
assertEquals(result, expectedResult, "BEGIN.join(DASH, JIM, JOHN, AND_JOE)"); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
58 |
// test with just one element |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
59 |
assertEquals(String.join(DASH, BEGIN), BEGIN); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
60 |
} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
61 |
|
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
62 |
public void testJoinStringArray() { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
63 |
// check a non-null join of Object[] with String elements |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
64 |
String[] theBs = {BILL, BOB, AND_BO}; |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
65 |
String result = String.join(DASH, theBs); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
66 |
String expectedResult = BILL + DASH + BOB + DASH + AND_BO; |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
67 |
assertEquals(result, expectedResult, "String.join(DASH, theBs)"); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
68 |
} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
69 |
|
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
70 |
public void testJoinEmptyStringArray() { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
71 |
// check a non-null join of Object[] with String elements |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
72 |
String[] empties = {}; |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
73 |
String result = String.join(DASH, empties); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
74 |
assertEquals(result, "", "String.join(DASH, empties)"); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
75 |
} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
76 |
|
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
77 |
@Test(expectedExceptions = {NullPointerException.class}) |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
78 |
public void testJoinNullStringArray() { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
79 |
// check a non-null join of Object[] with String elements |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
80 |
String[] empties = null; |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
81 |
String result = String.join(DASH, empties); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
82 |
} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
83 |
|
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
84 |
@Test(expectedExceptions = {NullPointerException.class}) |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
85 |
public void testJoinNullIterableStringList() { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
86 |
// check join of an Iterables |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
87 |
List<CharSequence> theZsList = null; |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
88 |
String.join(DASH, theZsList); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
89 |
} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
90 |
|
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
91 |
public void testJoinIterableStringList() { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
92 |
// check join of an Iterables |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
93 |
List<CharSequence> theZsList = new ArrayList<>(); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
94 |
theZsList.add(ZEKE); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
95 |
theZsList.add(ZACK); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
96 |
theZsList.add(AND_ZOE); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
97 |
assertEquals(String.join(DASH, theZsList), ZEKE + DASH + ZACK + DASH |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
98 |
+ AND_ZOE, "String.join(DASH, theZsList))"); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
99 |
} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
100 |
|
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
101 |
public void testJoinNullStringList() { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
102 |
List<CharSequence> nullList = null; |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
103 |
try { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
104 |
assertEquals( String.join( DASH, nullList ), "null" ); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
105 |
fail("Null container should cause NPE"); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
106 |
} catch (NullPointerException npe) {} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
107 |
assertEquals(String.join(DASH, null, null), "null" + DASH + "null"); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
108 |
} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
109 |
|
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
110 |
@Test(expectedExceptions = {NullPointerException.class}) |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
111 |
public void testJoinNullDelimiter() { |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
112 |
String.join(null, JIM, JOHN); |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
113 |
} |
e3d13a15c5c0
5015163: (str) String merge/join that is the inverse of String.split()
jgish
parents:
diff
changeset
|
114 |
} |