1 /*
2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /**
25  * @test
26  * @bug 8017231 8020977
27  * @summary test  StringJoiner::merge
28  * @run testng MergeTest
29  */
30 
31 import java.util.StringJoiner;
32 import java.util.stream.Stream;
33 import org.testng.annotations.Test;
34 import static org.testng.Assert.assertEquals;
35 import static org.testng.Assert.fail;
36 
37 @Test
38 public class MergeTest {
testNull()39     public void testNull() {
40         StringJoiner sj = new StringJoiner(",", "{", "}");
41         try {
42             sj.merge(null);
43             fail("Should throw NullPointerException!");
44         } catch (NullPointerException npe) {
45             // expected
46         }
47     }
48 
testSimple()49     public void testSimple() {
50         StringJoiner sj = new StringJoiner(",", "{", "}");
51         StringJoiner other = new StringJoiner(",", "[", "]");
52         Stream.of("a", "b", "c").forEachOrdered(sj::add);
53         Stream.of("d", "e", "f").forEachOrdered(other::add);
54 
55         sj.merge(other);
56         assertEquals(sj.toString(), "{a,b,c,d,e,f}");
57     }
58 
testEmptyOther()59     public void testEmptyOther() {
60         StringJoiner sj = new StringJoiner(",", "{", "}");
61         StringJoiner other = new StringJoiner(",", "[", "]");
62         Stream.of("a", "b", "c").forEachOrdered(sj::add);
63 
64         sj.merge(other);
65         assertEquals(sj.toString(), "{a,b,c}");
66 
67         other.setEmptyValue("EMPTY");
68         sj.merge(other);
69         assertEquals(sj.toString(), "{a,b,c}");
70     }
71 
testEmptyThis()72     public void testEmptyThis() {
73         StringJoiner sj = new StringJoiner(",", "{", "}");
74         StringJoiner other = new StringJoiner(":", "[", "]");
75         Stream.of("d", "e", "f").forEachOrdered(other::add);
76 
77         sj.merge(other);
78         assertEquals(sj.toString(), "{d:e:f}");
79 
80         sj = new StringJoiner(",", "{", "}").setEmptyValue("EMPTY");
81         assertEquals(sj.toString(), "EMPTY");
82         sj.merge(other);
83         assertEquals(sj.toString(), "{d:e:f}");
84     }
85 
testEmptyBoth()86     public void testEmptyBoth() {
87         StringJoiner sj = new StringJoiner(",", "{", "}");
88         StringJoiner other = new StringJoiner(":", "[", "]");
89 
90         sj.merge(other);
91         assertEquals(sj.toString(), "{}");
92 
93         other.setEmptyValue("NOTHING");
94         sj.merge(other);
95         assertEquals(sj.toString(), "{}");
96 
97         sj = new StringJoiner(",", "{", "}").setEmptyValue("EMPTY");
98         assertEquals(sj.toString(), "EMPTY");
99         sj.merge(other);
100         assertEquals(sj.toString(), "EMPTY");
101     }
102 
testCascadeEmpty()103     public void testCascadeEmpty() {
104         StringJoiner sj = new StringJoiner(",", "{", "}");
105         StringJoiner o1 = new StringJoiner(":", "[", "]").setEmptyValue("Empty1");
106         StringJoiner o2 = new StringJoiner(",", "<", ">").setEmptyValue("Empty2");
107 
108         o1.merge(o2);
109         assertEquals(o1.toString(), "Empty1");
110 
111         sj.merge(o1);
112         assertEquals(sj.toString(), "{}");
113     }
114 
testDelimiter()115     public void testDelimiter() {
116         StringJoiner sj = new StringJoiner(",", "{", "}");
117         StringJoiner other = new StringJoiner(":", "[", "]");
118         Stream.of("a", "b", "c").forEachOrdered(sj::add);
119         Stream.of("d", "e", "f").forEachOrdered(other::add);
120 
121         sj.merge(other);
122         assertEquals(sj.toString(), "{a,b,c,d:e:f}");
123     }
124 
testMergeSelf()125     public void testMergeSelf() {
126         final StringJoiner sj = new StringJoiner(",", "[", "]").add("a").add("b");
127         assertEquals(sj.merge(sj).toString(), "[a,b,a,b]");
128         assertEquals(sj.merge(sj).toString(), "[a,b,a,b,a,b,a,b]");
129 
130         final StringJoiner sj2 = new StringJoiner(",").add("c").add("d");
131         assertEquals(sj2.merge(sj2).toString(), "c,d,c,d");
132     }
133 }
134