• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 8054221
27  * @summary test  StringJoiner::merge
28  * @run testng MergeTest
29  */
30 
31 package test.java.util.StringJoiner;
32 
33 import java.util.StringJoiner;
34 import java.util.stream.Stream;
35 import org.testng.annotations.Test;
36 import static org.testng.Assert.assertEquals;
37 import static org.testng.Assert.fail;
38 
39 @Test
40 public class MergeTest {
41     private static final String[] PREFIXES = {"", "{", "@#$%"};
42     private static final String[] SUFFIXES = {"", "}", "*&%$"};
43 
44     private static class Fixes {
45         public String pre0, suf0;
46         public String pre1, suf1;
Fixes(String prefix0, String suffix0, String prefix1, String suffix1)47         public Fixes(String prefix0, String suffix0,
48                      String prefix1, String suffix1) {
49             this.pre0 = prefix0;
50             this.suf0 = suffix0;
51             this.pre1 = prefix1;
52             this.suf1 = suffix1;
53         }
54     }
55 
fixesStream()56     private static Stream<Fixes> fixesStream() {
57         Stream.Builder<Fixes> builder = Stream.builder();
58         for (final String prefix0 : PREFIXES) {
59             for (final String suffix0 : SUFFIXES) {
60                 for (final String prefix1 : PREFIXES) {
61                     for (final String suffix1 : SUFFIXES) {
62                         builder.accept(new Fixes(prefix0, suffix0,
63                                                  prefix1, suffix1));
64                     }
65                 }
66             }
67         }
68         return builder.build();
69     }
70 
71     @Test(expectedExceptions = {NullPointerException.class})
testNull()72     public void testNull() {
73         StringJoiner sj = new StringJoiner(",", "{", "}");
74         sj.merge(null);
75     }
76 
testSimple()77     public void testSimple() {
78         fixesStream().forEach(fixes -> {
79             StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0);
80             StringJoiner other = new StringJoiner(",", fixes.pre1, fixes.suf1);
81             Stream.of("a", "b", "c").forEachOrdered(sj::add);
82             Stream.of("d", "e", "f").forEachOrdered(other::add);
83 
84             sj.merge(other);
85             assertEquals(sj.toString(), fixes.pre0 + "a,b,c,d,e,f" + fixes.suf0);
86         });
87     }
88 
testEmptyOther()89     public void testEmptyOther() {
90         fixesStream().forEach(fixes -> {
91             StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0);
92             StringJoiner other = new StringJoiner(",", fixes.pre1, fixes.suf1);
93             Stream.of("a", "b", "c").forEachOrdered(sj::add);
94 
95             sj.merge(other);
96             assertEquals(sj.toString(), fixes.pre0 + "a,b,c" + fixes.suf0);
97 
98             other.setEmptyValue("EMPTY");
99             sj.merge(other);
100             assertEquals(sj.toString(), fixes.pre0 + "a,b,c" + fixes.suf0);
101         });
102     }
103 
testEmptyThis()104     public void testEmptyThis() {
105         fixesStream().forEach(fixes -> {
106             StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0);
107             StringJoiner other = new StringJoiner(":", fixes.pre1, fixes.suf1);
108             Stream.of("d", "e", "f").forEachOrdered(other::add);
109 
110             sj.merge(other);
111             assertEquals(sj.toString(), fixes.pre0 + "d:e:f" + fixes.suf0);
112 
113             sj = new StringJoiner(",", fixes.pre0, fixes.suf0).setEmptyValue("EMPTY");
114             assertEquals(sj.toString(), "EMPTY");
115             sj.merge(other);
116             assertEquals(sj.toString(), fixes.pre0 + "d:e:f" + fixes.suf0);
117         });
118     }
119 
testEmptyBoth()120     public void testEmptyBoth() {
121         fixesStream().forEach(fixes -> {
122             StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0);
123             StringJoiner other = new StringJoiner(":", fixes.pre1, fixes.suf1);
124 
125             sj.merge(other);
126             assertEquals(sj.toString(), fixes.pre0 + fixes.suf0);
127 
128             other.setEmptyValue("NOTHING");
129             sj.merge(other);
130             assertEquals(sj.toString(), fixes.pre0 + fixes.suf0);
131 
132             sj = new StringJoiner(",", fixes.pre0, fixes.suf0).setEmptyValue("EMPTY");
133             assertEquals(sj.toString(), "EMPTY");
134             sj.merge(other);
135             assertEquals(sj.toString(), "EMPTY");
136         });
137     }
138 
testCascadeEmpty()139     public void testCascadeEmpty() {
140         fixesStream().forEach(fixes -> {
141             StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0);
142             StringJoiner o1 = new StringJoiner(":", fixes.pre1, fixes.suf1).setEmptyValue("Empty1");
143             StringJoiner o2 = new StringJoiner(",", "<", ">").setEmptyValue("Empty2");
144 
145             o1.merge(o2);
146             assertEquals(o1.toString(), "Empty1");
147 
148             sj.merge(o1);
149             assertEquals(sj.toString(), fixes.pre0 + fixes.suf0);
150         });
151     }
152 
testDelimiter()153     public void testDelimiter() {
154         fixesStream().forEach(fixes -> {
155             StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0);
156             StringJoiner other = new StringJoiner(":", fixes.pre1, fixes.suf1);
157             Stream.of("a", "b", "c").forEachOrdered(sj::add);
158             Stream.of("d", "e", "f").forEachOrdered(other::add);
159 
160             sj.merge(other);
161             assertEquals(sj.toString(), fixes.pre0 + "a,b,c,d:e:f" + fixes.suf0);
162         });
163     }
164 
testMergeSelf()165     public void testMergeSelf() {
166         fixesStream().forEach(fixes -> {
167             final StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0).add("a").add("b");
168             assertEquals(sj.merge(sj).toString(), fixes.pre0 + "a,b,a,b" + fixes.suf0);
169             assertEquals(sj.merge(sj).toString(), fixes.pre0 + "a,b,a,b,a,b,a,b" + fixes.suf0);
170         });
171     }
172 }
173