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