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