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.StringJoiner;
  31 import org.testng.annotations.Test;
  32 import static org.testng.Assert.assertEquals;
  33 
  34 @Test(groups = {"unit","string","util","libs"})
  35 public class StringJoinerTest {
  36 
  37     private static final String EMPTY = "EMPTY";
  38     private static final String ONE = "One";
  39     private static final int ONE_LEN = ONE.length();
  40     private static final String TWO = "Two";
  41     private static final int TWO_LEN = TWO.length();
  42     private static final String THREE = "Three";
  43     private static final String FOUR = "Four";
  44     private static final String FIVE = "Five";
  45     private static final String DASH = "-";
  46 
  47     /* Uncomment when we have streams
  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().forEach(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().forEach(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().forEach(sj::add);
  83 
  84         ArrayList<String> nextOne = new ArrayList<>();
  85         nextOne.add(FOUR);
  86         nextOne.add(FIVE);
  87         nextOne.stream().forEach(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 testInto() {
  94         ArrayList<String> list = new ArrayList<>();
  95         list.add(ONE);
  96         list.add(TWO);
  97         list.add(THREE);
  98 
  99         StringJoiner target = new StringJoiner(",", "{", "}");
 100         assertEquals(target.toString(), "{" + ONE + "," + TWO + "," + THREE +
 101             "}");
 102     }
 103     */
 104 
 105     public void addCharSequence() {
 106         StringJoiner sj = new StringJoiner(",");
 107         CharSequence cs_one = ONE;
 108         CharSequence cs_two = TWO;
 109 
 110         sj.add(cs_one);
 111         sj.add(cs_two);
 112 
 113         assertEquals(sj.toString(), ONE + "," + TWO);
 114 
 115         sj = new StringJoiner(DASH, "{", "}");
 116         sj.add(cs_one);
 117         sj.add(cs_two);
 118 
 119         assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");
 120 
 121         StringBuilder builder = new StringBuilder(ONE);
 122         StringBuffer buffer = new StringBuffer(THREE);
 123         sj = new StringJoiner(", ", "{ ", " }");
 124         sj.add(builder).add(buffer);
 125         builder.append(TWO);
 126         buffer.append(FOUR);
 127         assertEquals(sj.toString(), "{ " + ONE + ", " + THREE + " }",
 128                 "CharSequence is copied when add");
 129         sj.add(builder);
 130         assertEquals(sj.toString(), "{ " + ONE + ", " + THREE + ", " + ONE +
 131                 TWO + " }");
 132     }
 133 
 134     public void addCharSequenceWithEmptyValue() {
 135         StringJoiner sj = new StringJoiner(",").setEmptyValue(EMPTY);
 136         CharSequence cs_one = ONE;
 137         CharSequence cs_two = TWO;
 138 
 139         sj.add(cs_one);
 140         sj.add(cs_two);
 141 
 142         assertEquals(sj.toString(), ONE + "," + TWO);
 143 
 144         sj = new StringJoiner(DASH, "{", "}");
 145         sj.add(cs_one);
 146         sj.add(cs_two);
 147         assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");
 148 
 149         sj = new StringJoiner(DASH, "{", "}");
 150         assertEquals(sj.toString(), "{}");
 151 
 152         sj = new StringJoiner("=", "{", "}").setEmptyValue("");
 153         assertEquals(sj.toString(), "");
 154 
 155         sj = new StringJoiner(DASH, "{", "}").setEmptyValue(EMPTY);
 156         assertEquals(sj.toString(), EMPTY);
 157 
 158         sj.add(cs_one);
 159         sj.add(cs_two);
 160         assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");
 161     }
 162 
 163     public void addString() {
 164         StringJoiner sj = new StringJoiner(DASH);
 165         sj.add(ONE);
 166         assertEquals(sj.toString(), ONE);
 167 
 168         sj = new StringJoiner(DASH, "{", "}");
 169         sj.add(ONE);
 170         assertEquals(sj.toString(), "{" + ONE + "}");
 171 
 172         sj.add(TWO);
 173         assertEquals(sj.toString(), "{" + ONE + DASH + TWO + "}");
 174     }
 175 
 176     public void lengthWithCustomEmptyValue() {
 177         StringJoiner sj = new StringJoiner(DASH, "<", ">").setEmptyValue(EMPTY);
 178         assertEquals(sj.length(), EMPTY.length());
 179         sj.add("");
 180         assertEquals(sj.length(), "<>".length());
 181         sj.add("");
 182         assertEquals(sj.length(), "<->".length());
 183         sj.add(ONE);
 184         assertEquals(sj.length(), 4 + ONE_LEN);
 185         assertEquals(sj.toString().length(), sj.length());
 186         sj.add(TWO);
 187         assertEquals(sj.length(), 5 + ONE_LEN + TWO_LEN);
 188         assertEquals(sj.toString().length(), sj.length());
 189         sj = new StringJoiner("||", "<", "-->");
 190         assertEquals(sj.length(), 4);
 191         assertEquals(sj.toString().length(), sj.length());
 192         sj.add("abcdef");
 193         assertEquals(sj.length(), 10);
 194         assertEquals(sj.toString().length(), sj.length());
 195         sj.add("xyz");
 196         assertEquals(sj.length(), 15);
 197         assertEquals(sj.toString().length(), sj.length());
 198     }
 199 
 200     public void noAddAndEmptyValue() {
 201         StringJoiner sj = new StringJoiner(DASH, "", "").setEmptyValue(EMPTY);
 202         assertEquals(sj.toString(), EMPTY);
 203 
 204         sj = new StringJoiner(DASH, "<..", "");
 205         assertEquals(sj.toString(), "<..");
 206 
 207         sj = new StringJoiner(DASH, "<..", "");
 208         assertEquals(sj.toString(), "<..");
 209 
 210         sj = new StringJoiner(DASH, "", "==>");
 211         assertEquals(sj.toString(), "==>");
 212 
 213         sj = new StringJoiner(DASH, "{", "}");
 214         assertEquals(sj.toString(), "{}");
 215     }
 216 
 217     @Test(expectedExceptions = {NullPointerException.class})
 218     public void setEmptyValueNull() {
 219         new StringJoiner(DASH, "{", "}").setEmptyValue(null);
 220     }
 221 
 222     @Test(expectedExceptions = {NullPointerException.class})
 223     public void setDelimiterNull() {
 224         new StringJoiner(null);
 225     }
 226 
 227     @Test(expectedExceptions = {NullPointerException.class})
 228     public void setPrefixNull() {
 229         new StringJoiner(DASH, null, "}");
 230     }
 231 
 232     @Test(expectedExceptions = {NullPointerException.class})
 233     public void setSuffixNull() {
 234         new StringJoiner(DASH, "{", null);
 235     }
 236 
 237     public void stringFromtoString() {
 238         StringJoiner sj = new StringJoiner(", ");
 239         assertEquals(sj.toString(), "");
 240         sj = new StringJoiner(",", "{", "}");
 241         assertEquals(sj.toString(), "{}");
 242 
 243         sj = new StringJoiner(",");
 244         sj.add(ONE);
 245         assertEquals(sj.toString(), ONE);
 246 
 247         sj.add(TWO);
 248         assertEquals(sj.toString(), ONE + "," + TWO);
 249 
 250         sj = new StringJoiner(",", "{--", "--}");
 251         sj.add(ONE);
 252         sj.add(TWO);
 253         assertEquals(sj.toString(), "{--" + ONE + "," + TWO + "--}");
 254 
 255     }
 256 
 257     public void stringFromtoStringWithEmptyValue() {
 258         StringJoiner sj = new StringJoiner(" ", "", "");
 259         assertEquals(sj.toString(), "");
 260         sj = new StringJoiner(", ");
 261         assertEquals(sj.toString(), "");
 262         sj = new StringJoiner(",", "{", "}");
 263         assertEquals(sj.toString(), "{}");
 264 
 265         sj = new StringJoiner(",", "{", "}").setEmptyValue("");
 266         assertEquals(sj.toString(), "");
 267 
 268         sj = new StringJoiner(",");
 269         sj.add(ONE);
 270         assertEquals(sj.toString(), ONE);
 271 
 272         sj.add(TWO);
 273         assertEquals(sj.toString(), ONE + "," + TWO);
 274 
 275         sj = new StringJoiner(",", "{--", "--}");
 276         sj.add(ONE);
 277         assertEquals(sj.toString(), "{--" + ONE + "--}" );
 278 
 279         sj.add(TWO);
 280         assertEquals(sj.toString(), "{--" + ONE + "," + TWO + "--}");
 281 
 282     }
 283 
 284     public void toStringWithCustomEmptyValue() {
 285         StringJoiner sj = new StringJoiner(DASH, "<", ">").setEmptyValue(EMPTY);
 286         assertEquals(sj.toString(), EMPTY);
 287         sj.add("");
 288         assertEquals(sj.toString(), "<>");
 289         sj.add("");
 290         assertEquals(sj.toString(), "<->");
 291     }
 292 
 293     private void testCombos(String infix, String prefix, String suffix) {
 294         StringJoiner sj = new StringJoiner(infix, prefix, suffix);
 295         assertEquals(sj.toString(), prefix + suffix);
 296         assertEquals(sj.toString().length(), sj.length());
 297         // EmptyValue
 298         sj = new StringJoiner(infix, prefix, suffix).setEmptyValue("<NONE>");
 299         assertEquals(sj.toString(), "<NONE>");
 300         assertEquals(sj.toString().length(), sj.length());
 301 
 302         // empty in front
 303         sj.add("");
 304         assertEquals(sj.toString(), prefix + suffix);
 305         // empty in middle
 306         sj.add("");
 307         assertEquals(sj.toString(), prefix + infix + suffix);
 308         sj.add("1");
 309         assertEquals(sj.toString(), prefix + infix + infix + "1" + suffix);
 310         // empty at end
 311         sj.add("");
 312         assertEquals(sj.toString(), prefix + infix + infix + "1" + infix + suffix);
 313 
 314         sj = new StringJoiner(infix, prefix, suffix).setEmptyValue("<NONE>");
 315         sj.add("1");
 316         assertEquals(sj.toString(), prefix + "1" + suffix);
 317         sj.add("2");
 318         assertEquals(sj.toString(), prefix + "1" + infix + "2" + suffix);
 319         sj.add("");
 320         assertEquals(sj.toString(), prefix + "1" + infix + "2" +infix + suffix);
 321         sj.add("3");
 322         assertEquals(sj.toString(), prefix + "1" + infix + "2" +infix + infix + "3" + suffix);
 323     }
 324 
 325     public void testDelimiterCombinations() {
 326         testCombos("", "", "");
 327         testCombos("", "<", "");
 328         testCombos("", "", ">");
 329         testCombos("", "<", ">");
 330         testCombos(",", "", "");
 331         testCombos(",", "<", "");
 332         testCombos(",", "", ">");
 333         testCombos(",", "<", ">");
 334     }
 335 }
 336