author | martin |
Tue, 15 Sep 2015 21:56:04 -0700 | |
changeset 32649 | 2ee9017c7597 |
parent 25967 | 0060a996fe1c |
permissions | -rw-r--r-- |
18778 | 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 |
/** |
|
25 |
* @test |
|
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
26 |
* @bug 8017231 8020977 8054221 |
18778 | 27 |
* @summary test StringJoiner::merge |
28 |
* @run testng MergeTest |
|
29 |
*/ |
|
30 |
||
31 |
import java.util.StringJoiner; |
|
32 |
import java.util.stream.Stream; |
|
33 |
import org.testng.annotations.Test; |
|
34 |
import static org.testng.Assert.assertEquals; |
|
35 |
import static org.testng.Assert.fail; |
|
36 |
||
37 |
@Test |
|
38 |
public class MergeTest { |
|
32649
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25967
diff
changeset
|
39 |
private static final String[] PREFIXES = {"", "{", "@#$%"}; |
2ee9017c7597
8136583: Core libraries should use blessed modifier order
martin
parents:
25967
diff
changeset
|
40 |
private static final String[] SUFFIXES = {"", "}", "*&%$"}; |
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
41 |
|
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
42 |
private static class Fixes { |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
43 |
public String pre0, suf0; |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
44 |
public String pre1, suf1; |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
45 |
public Fixes(String prefix0, String suffix0, |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
46 |
String prefix1, String suffix1) { |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
47 |
this.pre0 = prefix0; |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
48 |
this.suf0 = suffix0; |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
49 |
this.pre1 = prefix1; |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
50 |
this.suf1 = suffix1; |
18778 | 51 |
} |
52 |
} |
|
53 |
||
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
54 |
private static Stream<Fixes> fixesStream() { |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
55 |
Stream.Builder<Fixes> builder = Stream.builder(); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
56 |
for (final String prefix0 : PREFIXES) { |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
57 |
for (final String suffix0 : SUFFIXES) { |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
58 |
for (final String prefix1 : PREFIXES) { |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
59 |
for (final String suffix1 : SUFFIXES) { |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
60 |
builder.accept(new Fixes(prefix0, suffix0, |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
61 |
prefix1, suffix1)); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
62 |
} |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
63 |
} |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
64 |
} |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
65 |
} |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
66 |
return builder.build(); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
67 |
} |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
68 |
|
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
69 |
@Test(expectedExceptions = {NullPointerException.class}) |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
70 |
public void testNull() { |
18778 | 71 |
StringJoiner sj = new StringJoiner(",", "{", "}"); |
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
72 |
sj.merge(null); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
73 |
} |
18778 | 74 |
|
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
75 |
public void testSimple() { |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
76 |
fixesStream().forEach(fixes -> { |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
77 |
StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
78 |
StringJoiner other = new StringJoiner(",", fixes.pre1, fixes.suf1); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
79 |
Stream.of("a", "b", "c").forEachOrdered(sj::add); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
80 |
Stream.of("d", "e", "f").forEachOrdered(other::add); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
81 |
|
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
82 |
sj.merge(other); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
83 |
assertEquals(sj.toString(), fixes.pre0 + "a,b,c,d,e,f" + fixes.suf0); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
84 |
}); |
18778 | 85 |
} |
86 |
||
87 |
public void testEmptyOther() { |
|
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
88 |
fixesStream().forEach(fixes -> { |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
89 |
StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
90 |
StringJoiner other = new StringJoiner(",", fixes.pre1, fixes.suf1); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
91 |
Stream.of("a", "b", "c").forEachOrdered(sj::add); |
18778 | 92 |
|
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
93 |
sj.merge(other); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
94 |
assertEquals(sj.toString(), fixes.pre0 + "a,b,c" + fixes.suf0); |
18778 | 95 |
|
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
96 |
other.setEmptyValue("EMPTY"); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
97 |
sj.merge(other); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
98 |
assertEquals(sj.toString(), fixes.pre0 + "a,b,c" + fixes.suf0); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
99 |
}); |
18778 | 100 |
} |
101 |
||
102 |
public void testEmptyThis() { |
|
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
103 |
fixesStream().forEach(fixes -> { |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
104 |
StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
105 |
StringJoiner other = new StringJoiner(":", fixes.pre1, fixes.suf1); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
106 |
Stream.of("d", "e", "f").forEachOrdered(other::add); |
18778 | 107 |
|
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
108 |
sj.merge(other); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
109 |
assertEquals(sj.toString(), fixes.pre0 + "d:e:f" + fixes.suf0); |
18778 | 110 |
|
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
111 |
sj = new StringJoiner(",", fixes.pre0, fixes.suf0).setEmptyValue("EMPTY"); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
112 |
assertEquals(sj.toString(), "EMPTY"); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
113 |
sj.merge(other); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
114 |
assertEquals(sj.toString(), fixes.pre0 + "d:e:f" + fixes.suf0); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
115 |
}); |
18778 | 116 |
} |
117 |
||
118 |
public void testEmptyBoth() { |
|
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
119 |
fixesStream().forEach(fixes -> { |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
120 |
StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
121 |
StringJoiner other = new StringJoiner(":", fixes.pre1, fixes.suf1); |
18778 | 122 |
|
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
123 |
sj.merge(other); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
124 |
assertEquals(sj.toString(), fixes.pre0 + fixes.suf0); |
18778 | 125 |
|
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
126 |
other.setEmptyValue("NOTHING"); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
127 |
sj.merge(other); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
128 |
assertEquals(sj.toString(), fixes.pre0 + fixes.suf0); |
18778 | 129 |
|
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
130 |
sj = new StringJoiner(",", fixes.pre0, fixes.suf0).setEmptyValue("EMPTY"); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
131 |
assertEquals(sj.toString(), "EMPTY"); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
132 |
sj.merge(other); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
133 |
assertEquals(sj.toString(), "EMPTY"); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
134 |
}); |
18778 | 135 |
} |
136 |
||
137 |
public void testCascadeEmpty() { |
|
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
138 |
fixesStream().forEach(fixes -> { |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
139 |
StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
140 |
StringJoiner o1 = new StringJoiner(":", fixes.pre1, fixes.suf1).setEmptyValue("Empty1"); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
141 |
StringJoiner o2 = new StringJoiner(",", "<", ">").setEmptyValue("Empty2"); |
18778 | 142 |
|
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
143 |
o1.merge(o2); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
144 |
assertEquals(o1.toString(), "Empty1"); |
18778 | 145 |
|
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
146 |
sj.merge(o1); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
147 |
assertEquals(sj.toString(), fixes.pre0 + fixes.suf0); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
148 |
}); |
18778 | 149 |
} |
150 |
||
151 |
public void testDelimiter() { |
|
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
152 |
fixesStream().forEach(fixes -> { |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
153 |
StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
154 |
StringJoiner other = new StringJoiner(":", fixes.pre1, fixes.suf1); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
155 |
Stream.of("a", "b", "c").forEachOrdered(sj::add); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
156 |
Stream.of("d", "e", "f").forEachOrdered(other::add); |
18778 | 157 |
|
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
158 |
sj.merge(other); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
159 |
assertEquals(sj.toString(), fixes.pre0 + "a,b,c,d:e:f" + fixes.suf0); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
160 |
}); |
18778 | 161 |
} |
19071
bc096b85d91d
8020977: StringJoiner merges with itself not as expected
henryjen
parents:
18778
diff
changeset
|
162 |
|
bc096b85d91d
8020977: StringJoiner merges with itself not as expected
henryjen
parents:
18778
diff
changeset
|
163 |
public void testMergeSelf() { |
25967
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
164 |
fixesStream().forEach(fixes -> { |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
165 |
final StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0).add("a").add("b"); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
166 |
assertEquals(sj.merge(sj).toString(), fixes.pre0 + "a,b,a,b" + fixes.suf0); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
167 |
assertEquals(sj.merge(sj).toString(), fixes.pre0 + "a,b,a,b,a,b,a,b" + fixes.suf0); |
0060a996fe1c
8054221: StringJoiner imlementation optimization
igerasim
parents:
19071
diff
changeset
|
168 |
}); |
19071
bc096b85d91d
8020977: StringJoiner merges with itself not as expected
henryjen
parents:
18778
diff
changeset
|
169 |
} |
bc096b85d91d
8020977: StringJoiner merges with itself not as expected
henryjen
parents:
18778
diff
changeset
|
170 |
} |