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  * @test
  25  * @bug 5015163 7172553
  26  * @summary tests StringJoinerTest
  27  * @run testng StringJoinerTest
  28  * @author Jim Gish
  29  */
  30 import java.util.ArrayList;
  31 import java.util.StringJoiner;
  32 import org.testng.annotations.Test;
  33 import static org.testng.Assert.assertEquals;
  34 
  35 @Test(groups = {"unit","string","util","libs"})
  36 public class StringJoinerTest {
  37 
  38     private static final String EMPTY = "EMPTY";
  39     private static final String ONE = "One";
  40     private static final int ONE_LEN = ONE.length();
  41     private static final String TWO = "Two";
  42     private static final int TWO_LEN = TWO.length();
  43     private static final String THREE = "Three";
  44     private static final String FOUR = "Four";
  45     private static final String FIVE = "Five";
  46     private static final String DASH = "-";
  47 
  48     public void addAddAll() {
  49         StringJoiner sj = new StringJoiner(DASH, "{", "}");
  50         sj.add(ONE);
  51 
  52         ArrayList<String> nextOne = new ArrayList<>();
  53         nextOne.add(TWO);
  54         nextOne.add(THREE);
  55         nextOne.stream().forEachOrdered(sj::add);
  56 
  57         String expected = "{"+ONE+DASH+TWO+DASH+THREE+"}";
  58         assertEquals(sj.toString(), expected);
  59     }
  60 
  61     void addAlladd() {
  62         StringJoiner sj = new StringJoiner(DASH, "{", "}");
  63 
  64         ArrayList<String> firstOne = new ArrayList<>();
  65         firstOne.add(ONE);
  66         firstOne.add(TWO);
  67         firstOne.stream().forEachOrdered(sj::add);
  68 
  69         sj.add(THREE);
  70 
  71         String expected = "{"+ONE+DASH+TWO+DASH+THREE+"}";
  72         assertEquals(sj.toString(), expected);
  73     }
  74 
  75     // The following tests do two successive adds of different types
  76     public void addAlladdAll() {
  77         StringJoiner sj = new StringJoiner(DASH, "{", "}");
  78         ArrayList<String> firstOne = new ArrayList<>();
  79         firstOne.add(ONE);
  80         firstOne.add(TWO);
  81         firstOne.add(THREE);
  82         firstOne.stream().forEachOrdered(sj::add);
  83 
  84         ArrayList<String> nextOne = new ArrayList<>();
  85         nextOne.add(FOUR);
  86         nextOne.add(FIVE);
  87         nextOne.stream().forEachOrdered(sj::add);
  88 
  89         String expected = "{"+ONE+DASH+TWO+DASH+THREE+DASH+FOUR+DASH+FIVE+"}";
  90         assertEquals(sj.toString(), expected);
  91     }
  92 
  93     public void addCharSequence() {
  94         StringJoiner sj = new StringJoiner(",");
  95         CharSequence cs_one = ONE;
  96         CharSequence cs_two = TWO;
  97 
  98         sj.add(cs_one);
  99         sj.add(cs_two);
 100 
 101         assertEquals(sj.toString(), ONE + "," + TWO);
 102 
 103         sj = new StringJoiner(DASH, "{", "}");
 104         sj.add(cs_one);
 105         sj.add(cs_two);
 106 
 107         assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");
 108 
 109         StringBuilder builder = new StringBuilder(ONE);
 110         StringBuffer buffer = new StringBuffer(THREE);
 111         sj = new StringJoiner(", ", "{ ", " }");
 112         sj.add(builder).add(buffer);
 113         builder.append(TWO);
 114         buffer.append(FOUR);
 115         assertEquals(sj.toString(), "{ " + ONE + ", " + THREE + " }",
 116                 "CharSequence is copied when add");
 117         sj.add(builder);
 118         assertEquals(sj.toString(), "{ " + ONE + ", " + THREE + ", " + ONE +
 119                 TWO + " }");
 120     }
 121 
 122     public void addCharSequenceWithEmptyValue() {
 123         StringJoiner sj = new StringJoiner(",").setEmptyValue(EMPTY);
 124         CharSequence cs_one = ONE;
 125         CharSequence cs_two = TWO;
 126 
 127         sj.add(cs_one);
 128         sj.add(cs_two);
 129 
 130         assertEquals(sj.toString(), ONE + "," + TWO);
 131 
 132         sj = new StringJoiner(DASH, "{", "}");
 133         sj.add(cs_one);
 134         sj.add(cs_two);
 135         assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");
 136 
 137         sj = new StringJoiner(DASH, "{", "}");
 138         assertEquals(sj.toString(), "{}");
 139 
 140         sj = new StringJoiner("=", "{", "}").setEmptyValue("");
 141         assertEquals(sj.toString(), "");
 142 
 143         sj = new StringJoiner(DASH, "{", "}").setEmptyValue(EMPTY);
 144         assertEquals(sj.toString(), EMPTY);
 145 
 146         sj.add(cs_one);
 147         sj.add(cs_two);
 148         assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");
 149     }
 150 
 151     public void addString() {
 152         StringJoiner sj = new StringJoiner(DASH);
 153         sj.add(ONE);
 154         assertEquals(sj.toString(), ONE);
 155 
 156         sj = new StringJoiner(DASH, "{", "}");
 157         sj.add(ONE);
 158         assertEquals(sj.toString(), "{" + ONE + "}");
 159 
 160         sj.add(TWO);
 161         assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");
 162     }
 163 
 164     public void lengthWithCustomEmptyValue() {
 165         StringJoiner sj = new StringJoiner(DASH, "<", ">").setEmptyValue(EMPTY);
 166         assertEquals(sj.length(), EMPTY.length());
 167         sj.add("");
 168         assertEquals(sj.length(), "<>".length());
 169         sj.add("");
 170         assertEquals(sj.length(), "<->".length());
 171         sj.add(ONE);
 172         assertEquals(sj.length(), 4 + ONE_LEN);
 173         assertEquals(sj.toString().length(), sj.length());
 174         sj.add(TWO);
 175         assertEquals(sj.length(), 5 + ONE_LEN + TWO_LEN);
 176         assertEquals(sj.toString().length(), sj.length());
 177         sj = new StringJoiner("||", "<", "-->");
 178         assertEquals(sj.length(), 4);
 179         assertEquals(sj.toString().length(), sj.length());
 180         sj.add("abcdef");
 181         assertEquals(sj.length(), 10);
 182         assertEquals(sj.toString().length(), sj.length());
 183         sj.add("xyz");
 184         assertEquals(sj.length(), 15);
 185         assertEquals(sj.toString().length(), sj.length());
 186     }
 187 
 188     public void noAddAndEmptyValue() {
 189         StringJoiner sj = new StringJoiner(DASH, "", "").setEmptyValue(EMPTY);
 190         assertEquals(sj.toString(), EMPTY);
 191 
 192         sj = new StringJoiner(DASH, "<..", "");
 193         assertEquals(sj.toString(), "<..");
 194 
 195         sj = new StringJoiner(DASH, "<..", "");
 196         assertEquals(sj.toString(), "<..");
 197 
 198         sj = new StringJoiner(DASH, "", "==>");
 199         assertEquals(sj.toString(), "==>");
 200 
 201         sj = new StringJoiner(DASH, "{", "}");
 202         assertEquals(sj.toString(), "{}");
 203     }
 204 
 205     @Test(expectedExceptions = {NullPointerException.class})
 206     public void setEmptyValueNull() {
 207         new StringJoiner(DASH, "{", "}").setEmptyValue(null);
 208     }
 209 
 210     @Test(expectedExceptions = {NullPointerException.class})
 211     public void setDelimiterNull() {
 212         new StringJoiner(null);
 213     }
 214 
 215     @Test(expectedExceptions = {NullPointerException.class})
 216     public void setPrefixNull() {
 217         new StringJoiner(DASH, null, "}");
 218     }
 219 
 220     @Test(expectedExceptions = {NullPointerException.class})
 221     public void setSuffixNull() {
 222         new StringJoiner(DASH, "{", null);
 223     }
 224 
 225     public void stringFromtoString() {
 226         StringJoiner sj = new StringJoiner(", ");
 227         assertEquals(sj.toString(), "");
 228         sj = new StringJoiner(",", "{", "}");
 229         assertEquals(sj.toString(), "{}");
 230 
 231         sj = new StringJoiner(",");
 232         sj.add(ONE);
 233         assertEquals(sj.toString(), ONE);
 234 
 235         sj.add(TWO);
 236         assertEquals(sj.toString(), ONE + "," + TWO);
 237 
 238         sj = new StringJoiner(",", "{--", "--}");
 239         sj.add(ONE);
 240         sj.add(TWO);
 241         assertEquals(sj.toString(), "{--" + ONE + "," + TWO + "--}");
 242 
 243     }
 244 
 245     public void stringFromtoStringWithEmptyValue() {
 246         StringJoiner sj = new StringJoiner(" ", "", "");
 247         assertEquals(sj.toString(), "");
 248         sj = new StringJoiner(", ");
 249         assertEquals(sj.toString(), "");
 250         sj = new StringJoiner(",", "{", "}");
 251         assertEquals(sj.toString(), "{}");
 252 
 253         sj = new StringJoiner(",", "{", "}").setEmptyValue("");
 254         assertEquals(sj.toString(), "");
 255 
 256         sj = new StringJoiner(",");
 257         sj.add(ONE);
 258         assertEquals(sj.toString(), ONE);
 259 
 260         sj.add(TWO);
 261         assertEquals(sj.toString(), ONE + "," + TWO);
 262 
 263         sj = new StringJoiner(",", "{--", "--}");
 264         sj.add(ONE);
 265         assertEquals(sj.toString(), "{--" + ONE + "--}" );
 266 
 267         sj.add(TWO);
 268         assertEquals(sj.toString(), "{--" + ONE + "," + TWO + "--}");
 269 
 270     }
 271 
 272     public void toStringWithCustomEmptyValue() {
 273         StringJoiner sj = new StringJoiner(DASH, "<", ">").setEmptyValue(EMPTY);
 274         assertEquals(sj.toString(), EMPTY);
 275         sj.add("");
 276         assertEquals(sj.toString(), "<>");
 277         sj.add("");
 278         assertEquals(sj.toString(), "<->");
 279     }
 280 
 281     private void testCombos(String infix, String prefix, String suffix) {
 282         StringJoiner sj = new StringJoiner(infix, prefix, suffix);
 283         assertEquals(sj.toString(), prefix + suffix);
 284         assertEquals(sj.toString().length(), sj.length());
 285         // EmptyValue
 286         sj = new StringJoiner(infix, prefix, suffix).setEmptyValue("<NONE>");
 287         assertEquals(sj.toString(), "<NONE>");
 288         assertEquals(sj.toString().length(), sj.length());
 289 
 290         // empty in front
 291         sj.add("");
 292         assertEquals(sj.toString(), prefix + suffix);
 293         // empty in middle
 294         sj.add("");
 295         assertEquals(sj.toString(), prefix + infix + suffix);
 296         sj.add("1");
 297         assertEquals(sj.toString(), prefix + infix + infix + "1" + suffix);
 298         // empty at end
 299         sj.add("");
 300         assertEquals(sj.toString(), prefix + infix + infix + "1" + infix + suffix);
 301 
 302         sj = new StringJoiner(infix, prefix, suffix).setEmptyValue("<NONE>");
 303         sj.add("1");
 304         assertEquals(sj.toString(), prefix + "1" + suffix);
 305         sj.add("2");
 306         assertEquals(sj.toString(), prefix + "1" + infix + "2" + suffix);
 307         sj.add("");
 308         assertEquals(sj.toString(), prefix + "1" + infix + "2" +infix + suffix);
 309         sj.add("3");
 310         assertEquals(sj.toString(), prefix + "1" + infix + "2" +infix + infix + "3" + suffix);
 311     }
 312 
 313     public void testDelimiterCombinations() {
 314         testCombos("", "", "");
 315         testCombos("", "<", "");
 316         testCombos("", "", ">");
 317         testCombos("", "<", ">");
 318         testCombos(",", "", "");
 319         testCombos(",", "<", "");
 320         testCombos(",", "", ">");
 321         testCombos(",", "<", ">");
 322     }
 323 }
 324 
--- EOF ---