Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/String/StringJoinTest.java
38811 views
/*1* Copyright (c) 2013, 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*/22/**23* @test @bug 501516324* @summary test String merge/join that is the inverse of String.split()25* @run testng StringJoinTest26* @author Jim Gish27*/28import java.util.ArrayList;29import java.util.List;30import org.testng.annotations.Test;3132import static org.testng.Assert.*;3334@Test(groups = {"unit","string","lang","libs"})35public class StringJoinTest {36private final static String DASH = "-";37private final static String BEGIN = "Hi there";38private final static String JIM = "Jim";39private final static String JOHN = "John";40private final static String AND_JOE = "and Joe";41private final static String BILL = "Bill";42private final static String BOB = "Bob";43private final static String AND_BO = "and Bo";44private final static String ZEKE = "Zeke";45private final static String ZACK = "Zack";46private final static String AND_ZOE = "and Zoe";4748/**49* Tests the join() methods on String50*/51public void testJoinStringVarargs() {52// check a non-null join of String array (var-args) elements53String expectedResult = BEGIN + DASH + JIM + DASH + JOHN + DASH + AND_JOE;54String result = String.join(DASH, BEGIN, JIM, JOHN, AND_JOE);5556assertEquals(result, expectedResult, "BEGIN.join(DASH, JIM, JOHN, AND_JOE)");57// test with just one element58assertEquals(String.join(DASH, BEGIN), BEGIN);59}6061public void testJoinStringArray() {62// check a non-null join of Object[] with String elements63String[] theBs = {BILL, BOB, AND_BO};64String result = String.join(DASH, theBs);65String expectedResult = BILL + DASH + BOB + DASH + AND_BO;66assertEquals(result, expectedResult, "String.join(DASH, theBs)");67}6869public void testJoinEmptyStringArray() {70// check a non-null join of Object[] with String elements71String[] empties = {};72String result = String.join(DASH, empties);73assertEquals(result, "", "String.join(DASH, empties)");74}7576@Test(expectedExceptions = {NullPointerException.class})77public void testJoinNullStringArray() {78// check a non-null join of Object[] with String elements79String[] empties = null;80String result = String.join(DASH, empties);81}8283@Test(expectedExceptions = {NullPointerException.class})84public void testJoinNullIterableStringList() {85// check join of an Iterables86List<CharSequence> theZsList = null;87String.join(DASH, theZsList);88}8990public void testJoinIterableStringList() {91// check join of an Iterables92List<CharSequence> theZsList = new ArrayList<>();93theZsList.add(ZEKE);94theZsList.add(ZACK);95theZsList.add(AND_ZOE);96assertEquals(String.join(DASH, theZsList), ZEKE + DASH + ZACK + DASH97+ AND_ZOE, "String.join(DASH, theZsList))");98}99100public void testJoinNullStringList() {101List<CharSequence> nullList = null;102try {103assertEquals( String.join( DASH, nullList ), "null" );104fail("Null container should cause NPE");105} catch (NullPointerException npe) {}106assertEquals(String.join(DASH, null, null), "null" + DASH + "null");107}108109@Test(expectedExceptions = {NullPointerException.class})110public void testJoinNullDelimiter() {111String.join(null, JIM, JOHN);112}113}114115116