Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/StringJoiner/StringJoinerTest.java
46989 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* @test24* @bug 5015163 717255325* @summary tests StringJoinerTest26* @run testng StringJoinerTest27* @author Jim Gish28*/29import java.util.ArrayList;30import java.util.StringJoiner;31import org.testng.annotations.Test;32import static org.testng.Assert.assertEquals;3334@Test(groups = {"unit","string","util","libs"})35public class StringJoinerTest {3637private static final String EMPTY = "EMPTY";38private static final String ONE = "One";39private static final int ONE_LEN = ONE.length();40private static final String TWO = "Two";41private static final int TWO_LEN = TWO.length();42private static final String THREE = "Three";43private static final String FOUR = "Four";44private static final String FIVE = "Five";45private static final String DASH = "-";4647public void addAddAll() {48StringJoiner sj = new StringJoiner(DASH, "{", "}");49sj.add(ONE);5051ArrayList<String> nextOne = new ArrayList<>();52nextOne.add(TWO);53nextOne.add(THREE);54nextOne.stream().forEachOrdered(sj::add);5556String expected = "{"+ONE+DASH+TWO+DASH+THREE+"}";57assertEquals(sj.toString(), expected);58}5960void addAlladd() {61StringJoiner sj = new StringJoiner(DASH, "{", "}");6263ArrayList<String> firstOne = new ArrayList<>();64firstOne.add(ONE);65firstOne.add(TWO);66firstOne.stream().forEachOrdered(sj::add);6768sj.add(THREE);6970String expected = "{"+ONE+DASH+TWO+DASH+THREE+"}";71assertEquals(sj.toString(), expected);72}7374// The following tests do two successive adds of different types75public void addAlladdAll() {76StringJoiner sj = new StringJoiner(DASH, "{", "}");77ArrayList<String> firstOne = new ArrayList<>();78firstOne.add(ONE);79firstOne.add(TWO);80firstOne.add(THREE);81firstOne.stream().forEachOrdered(sj::add);8283ArrayList<String> nextOne = new ArrayList<>();84nextOne.add(FOUR);85nextOne.add(FIVE);86nextOne.stream().forEachOrdered(sj::add);8788String expected = "{"+ONE+DASH+TWO+DASH+THREE+DASH+FOUR+DASH+FIVE+"}";89assertEquals(sj.toString(), expected);90}9192public void addCharSequence() {93StringJoiner sj = new StringJoiner(",");94CharSequence cs_one = ONE;95CharSequence cs_two = TWO;9697sj.add(cs_one);98sj.add(cs_two);99100assertEquals(sj.toString(), ONE + "," + TWO);101102sj = new StringJoiner(DASH, "{", "}");103sj.add(cs_one);104sj.add(cs_two);105106assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");107108StringBuilder builder = new StringBuilder(ONE);109StringBuffer buffer = new StringBuffer(THREE);110sj = new StringJoiner(", ", "{ ", " }");111sj.add(builder).add(buffer);112builder.append(TWO);113buffer.append(FOUR);114assertEquals(sj.toString(), "{ " + ONE + ", " + THREE + " }",115"CharSequence is copied when add");116sj.add(builder);117assertEquals(sj.toString(), "{ " + ONE + ", " + THREE + ", " + ONE +118TWO + " }");119}120121public void addCharSequenceWithEmptyValue() {122StringJoiner sj = new StringJoiner(",").setEmptyValue(EMPTY);123CharSequence cs_one = ONE;124CharSequence cs_two = TWO;125126sj.add(cs_one);127sj.add(cs_two);128129assertEquals(sj.toString(), ONE + "," + TWO);130131sj = new StringJoiner(DASH, "{", "}");132sj.add(cs_one);133sj.add(cs_two);134assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");135136sj = new StringJoiner(DASH, "{", "}");137assertEquals(sj.toString(), "{}");138139sj = new StringJoiner("=", "{", "}").setEmptyValue("");140assertEquals(sj.toString(), "");141142sj = new StringJoiner(DASH, "{", "}").setEmptyValue(EMPTY);143assertEquals(sj.toString(), EMPTY);144145sj.add(cs_one);146sj.add(cs_two);147assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");148}149150public void addString() {151StringJoiner sj = new StringJoiner(DASH);152sj.add(ONE);153assertEquals(sj.toString(), ONE);154155sj = new StringJoiner(DASH, "{", "}");156sj.add(ONE);157assertEquals(sj.toString(), "{" + ONE + "}");158159sj.add(TWO);160assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");161}162163public void lengthWithCustomEmptyValue() {164StringJoiner sj = new StringJoiner(DASH, "<", ">").setEmptyValue(EMPTY);165assertEquals(sj.length(), EMPTY.length());166sj.add("");167assertEquals(sj.length(), "<>".length());168sj.add("");169assertEquals(sj.length(), "<->".length());170sj.add(ONE);171assertEquals(sj.length(), 4 + ONE_LEN);172assertEquals(sj.toString().length(), sj.length());173sj.add(TWO);174assertEquals(sj.length(), 5 + ONE_LEN + TWO_LEN);175assertEquals(sj.toString().length(), sj.length());176sj = new StringJoiner("||", "<", "-->");177assertEquals(sj.length(), 4);178assertEquals(sj.toString().length(), sj.length());179sj.add("abcdef");180assertEquals(sj.length(), 10);181assertEquals(sj.toString().length(), sj.length());182sj.add("xyz");183assertEquals(sj.length(), 15);184assertEquals(sj.toString().length(), sj.length());185}186187public void noAddAndEmptyValue() {188StringJoiner sj = new StringJoiner(DASH, "", "").setEmptyValue(EMPTY);189assertEquals(sj.toString(), EMPTY);190191sj = new StringJoiner(DASH, "<..", "");192assertEquals(sj.toString(), "<..");193194sj = new StringJoiner(DASH, "<..", "");195assertEquals(sj.toString(), "<..");196197sj = new StringJoiner(DASH, "", "==>");198assertEquals(sj.toString(), "==>");199200sj = new StringJoiner(DASH, "{", "}");201assertEquals(sj.toString(), "{}");202}203204@Test(expectedExceptions = {NullPointerException.class})205public void setEmptyValueNull() {206new StringJoiner(DASH, "{", "}").setEmptyValue(null);207}208209@Test(expectedExceptions = {NullPointerException.class})210public void setDelimiterNull() {211new StringJoiner(null);212}213214@Test(expectedExceptions = {NullPointerException.class})215public void setPrefixNull() {216new StringJoiner(DASH, null, "}");217}218219@Test(expectedExceptions = {NullPointerException.class})220public void setSuffixNull() {221new StringJoiner(DASH, "{", null);222}223224public void stringFromtoString() {225StringJoiner sj = new StringJoiner(", ");226assertEquals(sj.toString(), "");227sj = new StringJoiner(",", "{", "}");228assertEquals(sj.toString(), "{}");229230sj = new StringJoiner(",");231sj.add(ONE);232assertEquals(sj.toString(), ONE);233234sj.add(TWO);235assertEquals(sj.toString(), ONE + "," + TWO);236237sj = new StringJoiner(",", "{--", "--}");238sj.add(ONE);239sj.add(TWO);240assertEquals(sj.toString(), "{--" + ONE + "," + TWO + "--}");241242}243244public void stringFromtoStringWithEmptyValue() {245StringJoiner sj = new StringJoiner(" ", "", "");246assertEquals(sj.toString(), "");247sj = new StringJoiner(", ");248assertEquals(sj.toString(), "");249sj = new StringJoiner(",", "{", "}");250assertEquals(sj.toString(), "{}");251252sj = new StringJoiner(",", "{", "}").setEmptyValue("");253assertEquals(sj.toString(), "");254255sj = new StringJoiner(",");256sj.add(ONE);257assertEquals(sj.toString(), ONE);258259sj.add(TWO);260assertEquals(sj.toString(), ONE + "," + TWO);261262sj = new StringJoiner(",", "{--", "--}");263sj.add(ONE);264assertEquals(sj.toString(), "{--" + ONE + "--}" );265266sj.add(TWO);267assertEquals(sj.toString(), "{--" + ONE + "," + TWO + "--}");268269}270271public void toStringWithCustomEmptyValue() {272StringJoiner sj = new StringJoiner(DASH, "<", ">").setEmptyValue(EMPTY);273assertEquals(sj.toString(), EMPTY);274sj.add("");275assertEquals(sj.toString(), "<>");276sj.add("");277assertEquals(sj.toString(), "<->");278}279280private void testCombos(String infix, String prefix, String suffix) {281StringJoiner sj = new StringJoiner(infix, prefix, suffix);282assertEquals(sj.toString(), prefix + suffix);283assertEquals(sj.toString().length(), sj.length());284// EmptyValue285sj = new StringJoiner(infix, prefix, suffix).setEmptyValue("<NONE>");286assertEquals(sj.toString(), "<NONE>");287assertEquals(sj.toString().length(), sj.length());288289// empty in front290sj.add("");291assertEquals(sj.toString(), prefix + suffix);292// empty in middle293sj.add("");294assertEquals(sj.toString(), prefix + infix + suffix);295sj.add("1");296assertEquals(sj.toString(), prefix + infix + infix + "1" + suffix);297// empty at end298sj.add("");299assertEquals(sj.toString(), prefix + infix + infix + "1" + infix + suffix);300301sj = new StringJoiner(infix, prefix, suffix).setEmptyValue("<NONE>");302sj.add("1");303assertEquals(sj.toString(), prefix + "1" + suffix);304sj.add("2");305assertEquals(sj.toString(), prefix + "1" + infix + "2" + suffix);306sj.add("");307assertEquals(sj.toString(), prefix + "1" + infix + "2" +infix + suffix);308sj.add("3");309assertEquals(sj.toString(), prefix + "1" + infix + "2" +infix + infix + "3" + suffix);310}311312public void testDelimiterCombinations() {313testCombos("", "", "");314testCombos("", "<", "");315testCombos("", "", ">");316testCombos("", "<", ">");317testCombos(",", "", "");318testCombos(",", "<", "");319testCombos(",", "", ">");320testCombos(",", "<", ">");321}322}323324325326