Path: blob/master/test/jdk/java/lang/String/StringJoinTest.java
41149 views
/*1* Copyright (c) 2013, 2021, 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* @test24* @bug 5015163 826752925* @summary test String merge/join that is the inverse of String.split()26* @run testng StringJoinTest27* @author Jim Gish28*/29import java.util.ArrayList;30import java.util.List;31import java.util.StringJoiner;32import org.testng.annotations.Test;3334import static org.testng.Assert.*;3536@Test(groups = {"unit","string","lang","libs"})37public class StringJoinTest {38private static final String DASH = "-";39private static final String BEGIN = "Hi there";40private static final String JIM = "Jim";41private static final String JOHN = "John";42private static final String AND_JOE = "and Joe";43private static final String BILL = "Bill";44private static final String BOB = "Bob";45private static final String AND_BO = "and Bo";46private static final String ZEKE = "Zeke";47private static final String ZACK = "Zack";48private static final String AND_ZOE = "and Zoe";4950/**51* Tests the join() methods on String52*/53public void testJoinStringVarargs() {54// check a non-null join of String array (var-args) elements55String expectedResult = BEGIN + DASH + JIM + DASH + JOHN + DASH + AND_JOE;56String result = String.join(DASH, BEGIN, JIM, JOHN, AND_JOE);5758assertEquals(result, expectedResult, "BEGIN.join(DASH, JIM, JOHN, AND_JOE)");59// test with just one element60assertEquals(String.join(DASH, BEGIN), BEGIN);61}6263public void testJoinStringArray() {64// check a non-null join of Object[] with String elements65String[] theBs = {BILL, BOB, AND_BO};66String result = String.join(DASH, theBs);67String expectedResult = BILL + DASH + BOB + DASH + AND_BO;68assertEquals(result, expectedResult, "String.join(DASH, theBs)");69}7071public void testJoinEmptyStringArray() {72// check a non-null join of Object[] with String elements73String[] empties = {};74String result = String.join(DASH, empties);75assertEquals(result, "", "String.join(DASH, empties)");76}7778@Test(expectedExceptions = {NullPointerException.class})79public void testJoinNullStringArray() {80// check a non-null join of Object[] with String elements81String[] empties = null;82String result = String.join(DASH, empties);83}8485@Test(expectedExceptions = {NullPointerException.class})86public void testJoinNullIterableStringList() {87// check join of an Iterables88List<CharSequence> theZsList = null;89String.join(DASH, theZsList);90}9192public void testJoinIterableStringList() {93// check join of an Iterables94List<CharSequence> theZsList = new ArrayList<>();95theZsList.add(ZEKE);96theZsList.add(ZACK);97theZsList.add(AND_ZOE);98assertEquals(String.join(DASH, theZsList), ZEKE + DASH + ZACK + DASH99+ AND_ZOE, "String.join(DASH, theZsList))");100}101102public void testJoinNullStringList() {103List<CharSequence> nullList = null;104try {105assertEquals( String.join( DASH, nullList ), "null" );106fail("Null container should cause NPE");107} catch (NullPointerException npe) {}108assertEquals(String.join(DASH, null, null), "null" + DASH + "null");109}110111@Test(expectedExceptions = {NullPointerException.class})112public void testJoinNullDelimiter() {113String.join(null, JIM, JOHN);114}115116public void testIgnoreDelimiterCoderJoin() {117// 8267529: Ensure that joining zero or one latin-1 Strings with a UTF-16118// delimiter produce a String with a latin-1 coder, since the delimiter119// is not added.120assertEquals("", new StringJoiner("\u2013").toString());121assertEquals("foo", new StringJoiner("\u2013").add("foo").toString());122assertEquals("", String.join("\u2013"));123assertEquals("foo", String.join("\u2013", "foo"));124}125}126127128