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(", ", "{ ", " }");
|
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(", ", "{ ", " }");
|