Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/StringJoiner/MergeTest.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*/2223/**24* @test25* @bug 8017231 802097726* @summary test StringJoiner::merge27* @run testng MergeTest28*/2930import java.util.StringJoiner;31import java.util.stream.Stream;32import org.testng.annotations.Test;33import static org.testng.Assert.assertEquals;34import static org.testng.Assert.fail;3536@Test37public class MergeTest {38public void testNull() {39StringJoiner sj = new StringJoiner(",", "{", "}");40try {41sj.merge(null);42fail("Should throw NullPointerException!");43} catch (NullPointerException npe) {44// expected45}46}4748public void testSimple() {49StringJoiner sj = new StringJoiner(",", "{", "}");50StringJoiner other = new StringJoiner(",", "[", "]");51Stream.of("a", "b", "c").forEachOrdered(sj::add);52Stream.of("d", "e", "f").forEachOrdered(other::add);5354sj.merge(other);55assertEquals(sj.toString(), "{a,b,c,d,e,f}");56}5758public void testEmptyOther() {59StringJoiner sj = new StringJoiner(",", "{", "}");60StringJoiner other = new StringJoiner(",", "[", "]");61Stream.of("a", "b", "c").forEachOrdered(sj::add);6263sj.merge(other);64assertEquals(sj.toString(), "{a,b,c}");6566other.setEmptyValue("EMPTY");67sj.merge(other);68assertEquals(sj.toString(), "{a,b,c}");69}7071public void testEmptyThis() {72StringJoiner sj = new StringJoiner(",", "{", "}");73StringJoiner other = new StringJoiner(":", "[", "]");74Stream.of("d", "e", "f").forEachOrdered(other::add);7576sj.merge(other);77assertEquals(sj.toString(), "{d:e:f}");7879sj = new StringJoiner(",", "{", "}").setEmptyValue("EMPTY");80assertEquals(sj.toString(), "EMPTY");81sj.merge(other);82assertEquals(sj.toString(), "{d:e:f}");83}8485public void testEmptyBoth() {86StringJoiner sj = new StringJoiner(",", "{", "}");87StringJoiner other = new StringJoiner(":", "[", "]");8889sj.merge(other);90assertEquals(sj.toString(), "{}");9192other.setEmptyValue("NOTHING");93sj.merge(other);94assertEquals(sj.toString(), "{}");9596sj = new StringJoiner(",", "{", "}").setEmptyValue("EMPTY");97assertEquals(sj.toString(), "EMPTY");98sj.merge(other);99assertEquals(sj.toString(), "EMPTY");100}101102public void testCascadeEmpty() {103StringJoiner sj = new StringJoiner(",", "{", "}");104StringJoiner o1 = new StringJoiner(":", "[", "]").setEmptyValue("Empty1");105StringJoiner o2 = new StringJoiner(",", "<", ">").setEmptyValue("Empty2");106107o1.merge(o2);108assertEquals(o1.toString(), "Empty1");109110sj.merge(o1);111assertEquals(sj.toString(), "{}");112}113114public void testDelimiter() {115StringJoiner sj = new StringJoiner(",", "{", "}");116StringJoiner other = new StringJoiner(":", "[", "]");117Stream.of("a", "b", "c").forEachOrdered(sj::add);118Stream.of("d", "e", "f").forEachOrdered(other::add);119120sj.merge(other);121assertEquals(sj.toString(), "{a,b,c,d:e:f}");122}123124public void testMergeSelf() {125final StringJoiner sj = new StringJoiner(",", "[", "]").add("a").add("b");126assertEquals(sj.merge(sj).toString(), "[a,b,a,b]");127assertEquals(sj.merge(sj).toString(), "[a,b,a,b,a,b,a,b]");128129final StringJoiner sj2 = new StringJoiner(",").add("c").add("d");130assertEquals(sj2.merge(sj2).toString(), "c,d,c,d");131}132}133134135