StringJoinTest.java revision 12745:f068a4ffddd2
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 @bug 5015163
25 * @summary test String merge/join that is the inverse of String.split()
26 * @run testng StringJoinTest
27 * @author Jim Gish
28 */
29import java.util.ArrayList;
30import java.util.List;
31import org.testng.annotations.Test;
32
33import static org.testng.Assert.*;
34
35@Test(groups = {"unit","string","lang","libs"})
36public class StringJoinTest {
37    private static final String DASH = "-";
38    private static final String BEGIN = "Hi there";
39    private static final String JIM = "Jim";
40    private static final String JOHN = "John";
41    private static final String AND_JOE = "and Joe";
42    private static final String BILL = "Bill";
43    private static final String BOB = "Bob";
44    private static final String AND_BO = "and Bo";
45    private static final String ZEKE = "Zeke";
46    private static final String ZACK = "Zack";
47    private static final String AND_ZOE = "and Zoe";
48
49    /**
50     * Tests the join() methods on String
51     */
52    public void testJoinStringVarargs() {
53        // check a non-null join of String array (var-args) elements
54        String expectedResult = BEGIN + DASH + JIM + DASH + JOHN + DASH + AND_JOE;
55        String result = String.join(DASH, BEGIN, JIM, JOHN, AND_JOE);
56
57        assertEquals(result, expectedResult, "BEGIN.join(DASH, JIM, JOHN, AND_JOE)");
58        // test with just one element
59        assertEquals(String.join(DASH, BEGIN), BEGIN);
60    }
61
62    public void testJoinStringArray() {
63        // check a non-null join of Object[] with String elements
64        String[] theBs = {BILL, BOB, AND_BO};
65        String result = String.join(DASH, theBs);
66        String expectedResult = BILL + DASH + BOB + DASH + AND_BO;
67        assertEquals(result, expectedResult, "String.join(DASH, theBs)");
68    }
69
70    public void testJoinEmptyStringArray() {
71        // check a non-null join of Object[] with String elements
72        String[] empties = {};
73        String result = String.join(DASH, empties);
74        assertEquals(result, "", "String.join(DASH, empties)");
75    }
76
77    @Test(expectedExceptions = {NullPointerException.class})
78    public void testJoinNullStringArray() {
79        // check a non-null join of Object[] with String elements
80        String[] empties = null;
81        String result = String.join(DASH, empties);
82    }
83
84    @Test(expectedExceptions = {NullPointerException.class})
85    public void testJoinNullIterableStringList() {
86        // check join of an Iterables
87        List<CharSequence> theZsList = null;
88        String.join(DASH, theZsList);
89    }
90
91    public void testJoinIterableStringList() {
92        // check join of an Iterables
93        List<CharSequence> theZsList = new ArrayList<>();
94        theZsList.add(ZEKE);
95        theZsList.add(ZACK);
96        theZsList.add(AND_ZOE);
97        assertEquals(String.join(DASH, theZsList), ZEKE + DASH + ZACK + DASH
98                + AND_ZOE, "String.join(DASH, theZsList))");
99    }
100
101    public void testJoinNullStringList() {
102        List<CharSequence> nullList = null;
103        try {
104            assertEquals( String.join( DASH, nullList ), "null" );
105            fail("Null container should cause NPE");
106        } catch (NullPointerException npe) {}
107        assertEquals(String.join(DASH, null, null), "null" + DASH + "null");
108    }
109
110    @Test(expectedExceptions = {NullPointerException.class})
111    public void testJoinNullDelimiter() {
112        String.join(null, JIM, JOHN);
113    }
114}
115