Path: blob/master/test/jdk/java/util/StringJoiner/MergeTest.java
41149 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*/2223/**24* @test25* @bug 8017231 8020977 805422126* @summary test StringJoiner::merge27* @modules java.base/jdk.internal.util28* @requires vm.bits == "64" & os.maxMemory > 4G29* @run testng/othervm -Xmx4g -XX:+CompactStrings MergeTest30*/3132import java.util.StringJoiner;33import java.util.stream.Stream;34import org.testng.annotations.Test;35import static jdk.internal.util.ArraysSupport.SOFT_MAX_ARRAY_LENGTH;36import static org.testng.Assert.assertEquals;37import static org.testng.Assert.fail;3839@Test40public class MergeTest {41private static final String[] PREFIXES = {"", "{", "@#$%"};42private static final String[] SUFFIXES = {"", "}", "*&%$"};4344private static class Fixes {45public String pre0, suf0;46public String pre1, suf1;47public Fixes(String prefix0, String suffix0,48String prefix1, String suffix1) {49this.pre0 = prefix0;50this.suf0 = suffix0;51this.pre1 = prefix1;52this.suf1 = suffix1;53}54}5556private static Stream<Fixes> fixesStream() {57Stream.Builder<Fixes> builder = Stream.builder();58for (final String prefix0 : PREFIXES) {59for (final String suffix0 : SUFFIXES) {60for (final String prefix1 : PREFIXES) {61for (final String suffix1 : SUFFIXES) {62builder.accept(new Fixes(prefix0, suffix0,63prefix1, suffix1));64}65}66}67}68return builder.build();69}7071@Test(expectedExceptions = {NullPointerException.class})72public void testNull() {73StringJoiner sj = new StringJoiner(",", "{", "}");74sj.merge(null);75}7677public void testSimple() {78fixesStream().forEach(fixes -> {79StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0);80StringJoiner other = new StringJoiner(",", fixes.pre1, fixes.suf1);81Stream.of("a", "b", "c").forEachOrdered(sj::add);82Stream.of("d", "e", "f").forEachOrdered(other::add);8384sj.merge(other);85assertEquals(sj.toString(), fixes.pre0 + "a,b,c,d,e,f" + fixes.suf0);86});87}8889public void testEmptyOther() {90fixesStream().forEach(fixes -> {91StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0);92StringJoiner other = new StringJoiner(",", fixes.pre1, fixes.suf1);93Stream.of("a", "b", "c").forEachOrdered(sj::add);9495sj.merge(other);96assertEquals(sj.toString(), fixes.pre0 + "a,b,c" + fixes.suf0);9798other.setEmptyValue("EMPTY");99sj.merge(other);100assertEquals(sj.toString(), fixes.pre0 + "a,b,c" + fixes.suf0);101});102}103104public void testEmptyThis() {105fixesStream().forEach(fixes -> {106StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0);107StringJoiner other = new StringJoiner(":", fixes.pre1, fixes.suf1);108Stream.of("d", "e", "f").forEachOrdered(other::add);109110sj.merge(other);111assertEquals(sj.toString(), fixes.pre0 + "d:e:f" + fixes.suf0);112113sj = new StringJoiner(",", fixes.pre0, fixes.suf0).setEmptyValue("EMPTY");114assertEquals(sj.toString(), "EMPTY");115sj.merge(other);116assertEquals(sj.toString(), fixes.pre0 + "d:e:f" + fixes.suf0);117});118}119120public void testEmptyBoth() {121fixesStream().forEach(fixes -> {122StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0);123StringJoiner other = new StringJoiner(":", fixes.pre1, fixes.suf1);124125sj.merge(other);126assertEquals(sj.toString(), fixes.pre0 + fixes.suf0);127128other.setEmptyValue("NOTHING");129sj.merge(other);130assertEquals(sj.toString(), fixes.pre0 + fixes.suf0);131132sj = new StringJoiner(",", fixes.pre0, fixes.suf0).setEmptyValue("EMPTY");133assertEquals(sj.toString(), "EMPTY");134sj.merge(other);135assertEquals(sj.toString(), "EMPTY");136});137}138139public void testCascadeEmpty() {140fixesStream().forEach(fixes -> {141StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0);142StringJoiner o1 = new StringJoiner(":", fixes.pre1, fixes.suf1).setEmptyValue("Empty1");143StringJoiner o2 = new StringJoiner(",", "<", ">").setEmptyValue("Empty2");144145o1.merge(o2);146assertEquals(o1.toString(), "Empty1");147148sj.merge(o1);149assertEquals(sj.toString(), fixes.pre0 + fixes.suf0);150});151}152153public void testDelimiter() {154fixesStream().forEach(fixes -> {155StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0);156StringJoiner other = new StringJoiner(":", fixes.pre1, fixes.suf1);157Stream.of("a", "b", "c").forEachOrdered(sj::add);158Stream.of("d", "e", "f").forEachOrdered(other::add);159160sj.merge(other);161assertEquals(sj.toString(), fixes.pre0 + "a,b,c,d:e:f" + fixes.suf0);162});163}164165public void testMergeSelf() {166fixesStream().forEach(fixes -> {167final StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0).add("a").add("b");168assertEquals(sj.merge(sj).toString(), fixes.pre0 + "a,b,a,b" + fixes.suf0);169assertEquals(sj.merge(sj).toString(), fixes.pre0 + "a,b,a,b,a,b,a,b" + fixes.suf0);170});171}172173public void OOM() {174String maxString = "*".repeat(SOFT_MAX_ARRAY_LENGTH);175176try {177StringJoiner sj1 = new StringJoiner("", "", "");178sj1.add(maxString);179StringJoiner sj2 = new StringJoiner("", "", "");180sj2.add(maxString);181sj1.merge(sj2);182fail("Should have thrown OutOfMemoryError");183} catch (OutOfMemoryError ex) {184// okay185}186}187}188189190