Path: blob/master/test/jdk/java/util/StringJoiner/StringJoinerTest.java
41152 views
/*1* Copyright (c) 2013, 2020, 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 7172553 824925825* @summary tests StringJoinerTest26* @modules java.base/jdk.internal.util27* @requires vm.bits == "64" & os.maxMemory > 4G28* @run testng/othervm -Xmx4g -XX:+CompactStrings StringJoinerTest29* @author Jim Gish30*/31import java.util.ArrayList;32import java.util.StringJoiner;33import org.testng.annotations.Test;34import static jdk.internal.util.ArraysSupport.SOFT_MAX_ARRAY_LENGTH;35import static org.testng.Assert.assertEquals;36import static org.testng.Assert.fail;373839@Test(groups = {"unit","string","util","libs"})40public class StringJoinerTest {4142private static final String EMPTY = "EMPTY";43private static final String ONE = "One";44private static final int ONE_LEN = ONE.length();45private static final String TWO = "Two";46private static final int TWO_LEN = TWO.length();47private static final String THREE = "Three";48private static final String FOUR = "Four";49private static final String FIVE = "Five";50private static final String DASH = "-";51private static final String MAX_STRING = "*".repeat(SOFT_MAX_ARRAY_LENGTH);5253public void addAddAll() {54StringJoiner sj = new StringJoiner(DASH, "{", "}");55sj.add(ONE);5657ArrayList<String> nextOne = new ArrayList<>();58nextOne.add(TWO);59nextOne.add(THREE);60nextOne.stream().forEachOrdered(sj::add);6162String expected = "{"+ONE+DASH+TWO+DASH+THREE+"}";63assertEquals(sj.toString(), expected);64}6566void addAlladd() {67StringJoiner sj = new StringJoiner(DASH, "{", "}");6869ArrayList<String> firstOne = new ArrayList<>();70firstOne.add(ONE);71firstOne.add(TWO);72firstOne.stream().forEachOrdered(sj::add);7374sj.add(THREE);7576String expected = "{"+ONE+DASH+TWO+DASH+THREE+"}";77assertEquals(sj.toString(), expected);78}7980// The following tests do two successive adds of different types81public void addAlladdAll() {82StringJoiner sj = new StringJoiner(DASH, "{", "}");83ArrayList<String> firstOne = new ArrayList<>();84firstOne.add(ONE);85firstOne.add(TWO);86firstOne.add(THREE);87firstOne.stream().forEachOrdered(sj::add);8889ArrayList<String> nextOne = new ArrayList<>();90nextOne.add(FOUR);91nextOne.add(FIVE);92nextOne.stream().forEachOrdered(sj::add);9394String expected = "{"+ONE+DASH+TWO+DASH+THREE+DASH+FOUR+DASH+FIVE+"}";95assertEquals(sj.toString(), expected);96}9798public void addCharSequence() {99StringJoiner sj = new StringJoiner(",");100CharSequence cs_one = ONE;101CharSequence cs_two = TWO;102103sj.add(cs_one);104sj.add(cs_two);105106assertEquals(sj.toString(), ONE + "," + TWO);107108sj = new StringJoiner(DASH, "{", "}");109sj.add(cs_one);110sj.add(cs_two);111112assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");113114StringBuilder builder = new StringBuilder(ONE);115StringBuffer buffer = new StringBuffer(THREE);116sj = new StringJoiner(", ", "{ ", " }");117sj.add(builder).add(buffer);118builder.append(TWO);119buffer.append(FOUR);120assertEquals(sj.toString(), "{ " + ONE + ", " + THREE + " }",121"CharSequence is copied when add");122sj.add(builder);123assertEquals(sj.toString(), "{ " + ONE + ", " + THREE + ", " + ONE +124TWO + " }");125}126127public void addCharSequenceWithEmptyValue() {128StringJoiner sj = new StringJoiner(",").setEmptyValue(EMPTY);129CharSequence cs_one = ONE;130CharSequence cs_two = TWO;131132sj.add(cs_one);133sj.add(cs_two);134135assertEquals(sj.toString(), ONE + "," + TWO);136137sj = new StringJoiner(DASH, "{", "}");138sj.add(cs_one);139sj.add(cs_two);140assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");141142sj = new StringJoiner(DASH, "{", "}");143assertEquals(sj.toString(), "{}");144145sj = new StringJoiner("=", "{", "}").setEmptyValue("");146assertEquals(sj.toString(), "");147148sj = new StringJoiner(DASH, "{", "}").setEmptyValue(EMPTY);149assertEquals(sj.toString(), EMPTY);150151sj.add(cs_one);152sj.add(cs_two);153assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");154}155156public void addString() {157StringJoiner sj = new StringJoiner(DASH);158sj.add(ONE);159assertEquals(sj.toString(), ONE);160161sj = new StringJoiner(DASH, "{", "}");162sj.add(ONE);163assertEquals(sj.toString(), "{" + ONE + "}");164165sj.add(TWO);166assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");167}168169public void lengthWithCustomEmptyValue() {170StringJoiner sj = new StringJoiner(DASH, "<", ">").setEmptyValue(EMPTY);171assertEquals(sj.length(), EMPTY.length());172sj.add("");173assertEquals(sj.length(), "<>".length());174sj.add("");175assertEquals(sj.length(), "<->".length());176sj.add(ONE);177assertEquals(sj.length(), 4 + ONE_LEN);178assertEquals(sj.toString().length(), sj.length());179sj.add(TWO);180assertEquals(sj.length(), 5 + ONE_LEN + TWO_LEN);181assertEquals(sj.toString().length(), sj.length());182sj = new StringJoiner("||", "<", "-->");183assertEquals(sj.length(), 4);184assertEquals(sj.toString().length(), sj.length());185sj.add("abcdef");186assertEquals(sj.length(), 10);187assertEquals(sj.toString().length(), sj.length());188sj.add("xyz");189assertEquals(sj.length(), 15);190assertEquals(sj.toString().length(), sj.length());191}192193public void noAddAndEmptyValue() {194StringJoiner sj = new StringJoiner(DASH, "", "").setEmptyValue(EMPTY);195assertEquals(sj.toString(), EMPTY);196197sj = new StringJoiner(DASH, "<..", "");198assertEquals(sj.toString(), "<..");199200sj = new StringJoiner(DASH, "<..", "");201assertEquals(sj.toString(), "<..");202203sj = new StringJoiner(DASH, "", "==>");204assertEquals(sj.toString(), "==>");205206sj = new StringJoiner(DASH, "{", "}");207assertEquals(sj.toString(), "{}");208}209210@Test(expectedExceptions = {NullPointerException.class})211public void setEmptyValueNull() {212new StringJoiner(DASH, "{", "}").setEmptyValue(null);213}214215@Test(expectedExceptions = {NullPointerException.class})216public void setDelimiterNull() {217new StringJoiner(null);218}219220@Test(expectedExceptions = {NullPointerException.class})221public void setPrefixNull() {222new StringJoiner(DASH, null, "}");223}224225@Test(expectedExceptions = {NullPointerException.class})226public void setSuffixNull() {227new StringJoiner(DASH, "{", null);228}229230public void stringFromtoString() {231StringJoiner sj = new StringJoiner(", ");232assertEquals(sj.toString(), "");233sj = new StringJoiner(",", "{", "}");234assertEquals(sj.toString(), "{}");235236sj = new StringJoiner(",");237sj.add(ONE);238assertEquals(sj.toString(), ONE);239240sj.add(TWO);241assertEquals(sj.toString(), ONE + "," + TWO);242243sj = new StringJoiner(",", "{--", "--}");244sj.add(ONE);245sj.add(TWO);246assertEquals(sj.toString(), "{--" + ONE + "," + TWO + "--}");247248}249250public void stringFromtoStringWithEmptyValue() {251StringJoiner sj = new StringJoiner(" ", "", "");252assertEquals(sj.toString(), "");253sj = new StringJoiner(", ");254assertEquals(sj.toString(), "");255sj = new StringJoiner(",", "{", "}");256assertEquals(sj.toString(), "{}");257258sj = new StringJoiner(",", "{", "}").setEmptyValue("");259assertEquals(sj.toString(), "");260261sj = new StringJoiner(",");262sj.add(ONE);263assertEquals(sj.toString(), ONE);264265sj.add(TWO);266assertEquals(sj.toString(), ONE + "," + TWO);267268sj = new StringJoiner(",", "{--", "--}");269sj.add(ONE);270assertEquals(sj.toString(), "{--" + ONE + "--}" );271272sj.add(TWO);273assertEquals(sj.toString(), "{--" + ONE + "," + TWO + "--}");274275}276277public void toStringWithCustomEmptyValue() {278StringJoiner sj = new StringJoiner(DASH, "<", ">").setEmptyValue(EMPTY);279assertEquals(sj.toString(), EMPTY);280sj.add("");281assertEquals(sj.toString(), "<>");282sj.add("");283assertEquals(sj.toString(), "<->");284}285286private void testCombos(String infix, String prefix, String suffix) {287StringJoiner sj = new StringJoiner(infix, prefix, suffix);288assertEquals(sj.toString(), prefix + suffix);289assertEquals(sj.toString().length(), sj.length());290// EmptyValue291sj = new StringJoiner(infix, prefix, suffix).setEmptyValue("<NONE>");292assertEquals(sj.toString(), "<NONE>");293assertEquals(sj.toString().length(), sj.length());294295// empty in front296sj.add("");297assertEquals(sj.toString(), prefix + suffix);298// empty in middle299sj.add("");300assertEquals(sj.toString(), prefix + infix + suffix);301sj.add("1");302assertEquals(sj.toString(), prefix + infix + infix + "1" + suffix);303// empty at end304sj.add("");305assertEquals(sj.toString(), prefix + infix + infix + "1" + infix + suffix);306307sj = new StringJoiner(infix, prefix, suffix).setEmptyValue("<NONE>");308sj.add("1");309assertEquals(sj.toString(), prefix + "1" + suffix);310sj.add("2");311assertEquals(sj.toString(), prefix + "1" + infix + "2" + suffix);312sj.add("");313assertEquals(sj.toString(), prefix + "1" + infix + "2" + infix + suffix);314sj.add("3");315assertEquals(sj.toString(), prefix + "1" + infix + "2" + infix + infix + "3" + suffix);316}317318public void testDelimiterCombinations() {319testCombos("", "", "");320testCombos("", "<", "");321testCombos("", "", ">");322testCombos("", "<", ">");323testCombos(",", "", "");324testCombos(",", "<", "");325testCombos(",", "", ">");326testCombos(",", "<", ">");327}328329public void OOM1() {330try {331new StringJoiner(MAX_STRING, MAX_STRING, MAX_STRING).toString();332fail("Should have thrown OutOfMemoryError");333} catch (OutOfMemoryError ex) {334// okay335}336}337338public void OOM2() {339try {340new StringJoiner(MAX_STRING, MAX_STRING, "").toString();341fail("Should have thrown OutOfMemoryError");342} catch (OutOfMemoryError ex) {343// okay344}345}346347public void OOM3() {348try {349new StringJoiner(MAX_STRING, "", MAX_STRING).toString();350fail("Should have thrown OutOfMemoryError");351} catch (OutOfMemoryError ex) {352// okay353}354}355356public void OOM4() {357try {358new StringJoiner("", MAX_STRING, MAX_STRING).toString();359fail("Should have thrown OutOfMemoryError");360} catch (OutOfMemoryError ex) {361// okay362}363}364}365366367368