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 26 * @bug 8017231 8020977 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 { 39 public void testNull() { 40 StringJoiner sj = new StringJoiner(",", "{", "}"); 41 try { 42 sj.merge(null); 43 fail("Should throw NullPointerException!"); 44 } catch (NullPointerException npe) { 45 // expected 46 } 47 } 48 49 public void testSimple() { 50 StringJoiner sj = new StringJoiner(",", "{", "}"); 51 StringJoiner other = new StringJoiner(",", "[", "]"); 52 Stream.of("a", "b", "c").forEachOrdered(sj::add); 53 Stream.of("d", "e", "f").forEachOrdered(other::add); 54 55 sj.merge(other); 56 assertEquals(sj.toString(), "{a,b,c,d,e,f}"); 57 } 58 59 public void testEmptyOther() { 60 StringJoiner sj = new StringJoiner(",", "{", "}"); 61 StringJoiner other = new StringJoiner(",", "[", "]"); 62 Stream.of("a", "b", "c").forEachOrdered(sj::add); 63 64 sj.merge(other); 65 assertEquals(sj.toString(), "{a,b,c}"); 66 67 other.setEmptyValue("EMPTY"); 68 sj.merge(other); 69 assertEquals(sj.toString(), "{a,b,c}"); 70 } 71 72 public void testEmptyThis() { 73 StringJoiner sj = new StringJoiner(",", "{", "}"); 74 StringJoiner other = new StringJoiner(":", "[", "]"); 75 Stream.of("d", "e", "f").forEachOrdered(other::add); 76 77 sj.merge(other); 78 assertEquals(sj.toString(), "{d:e:f}"); 79 80 sj = new StringJoiner(",", "{", "}").setEmptyValue("EMPTY"); 81 assertEquals(sj.toString(), "EMPTY"); 82 sj.merge(other); 83 assertEquals(sj.toString(), "{d:e:f}"); 84 } 85 86 public void testEmptyBoth() { 87 StringJoiner sj = new StringJoiner(",", "{", "}"); 88 StringJoiner other = new StringJoiner(":", "[", "]"); 89 90 sj.merge(other); 91 assertEquals(sj.toString(), "{}"); 92 93 other.setEmptyValue("NOTHING"); 94 sj.merge(other); 95 assertEquals(sj.toString(), "{}"); 96 97 sj = new StringJoiner(",", "{", "}").setEmptyValue("EMPTY"); 98 assertEquals(sj.toString(), "EMPTY"); 99 sj.merge(other); 100 assertEquals(sj.toString(), "EMPTY"); 101 } 102 103 public void testCascadeEmpty() { 104 StringJoiner sj = new StringJoiner(",", "{", "}"); 105 StringJoiner o1 = new StringJoiner(":", "[", "]").setEmptyValue("Empty1"); 106 StringJoiner o2 = new StringJoiner(",", "<", ">").setEmptyValue("Empty2"); 107 108 o1.merge(o2); 109 assertEquals(o1.toString(), "Empty1"); 110 111 sj.merge(o1); 112 assertEquals(sj.toString(), "{}"); 113 } 114 115 public void testDelimiter() { 116 StringJoiner sj = new StringJoiner(",", "{", "}"); 117 StringJoiner other = new StringJoiner(":", "[", "]"); 118 Stream.of("a", "b", "c").forEachOrdered(sj::add); 119 Stream.of("d", "e", "f").forEachOrdered(other::add); 120 121 sj.merge(other); 122 assertEquals(sj.toString(), "{a,b,c,d:e:f}"); 123 } 124 125 public void testMergeSelf() { 126 final StringJoiner sj = new StringJoiner(",", "[", "]").add("a").add("b"); 127 assertEquals(sj.merge(sj).toString(), "[a,b,a,b]"); 128 assertEquals(sj.merge(sj).toString(), "[a,b,a,b,a,b,a,b]"); 129 130 final StringJoiner sj2 = new StringJoiner(",").add("c").add("d"); 131 assertEquals(sj2.merge(sj2).toString(), "c,d,c,d"); 132 } 133 } | 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 26 * @bug 8017231 8020977 8054221 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 { 39 private final static String[] PREFIXES = {"", "{", "@#$%"}; 40 private final static String[] SUFFIXES = {"", "}", "*&%$"}; 41 42 private static class Fixes { 43 public String pre0, suf0; 44 public String pre1, suf1; 45 public Fixes(String prefix0, String suffix0, 46 String prefix1, String suffix1) { 47 this.pre0 = prefix0; 48 this.suf0 = suffix0; 49 this.pre1 = prefix1; 50 this.suf1 = suffix1; 51 } 52 } 53 54 private static Stream<Fixes> fixesStream() { 55 Stream.Builder<Fixes> builder = Stream.builder(); 56 for (final String prefix0 : PREFIXES) { 57 for (final String suffix0 : SUFFIXES) { 58 for (final String prefix1 : PREFIXES) { 59 for (final String suffix1 : SUFFIXES) { 60 builder.accept(new Fixes(prefix0, suffix0, 61 prefix1, suffix1)); 62 } 63 } 64 } 65 } 66 return builder.build(); 67 } 68 69 @Test(expectedExceptions = {NullPointerException.class}) 70 public void testNull() { 71 StringJoiner sj = new StringJoiner(",", "{", "}"); 72 sj.merge(null); 73 } 74 75 public void testSimple() { 76 fixesStream().forEach(fixes -> { 77 StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0); 78 StringJoiner other = new StringJoiner(",", fixes.pre1, fixes.suf1); 79 Stream.of("a", "b", "c").forEachOrdered(sj::add); 80 Stream.of("d", "e", "f").forEachOrdered(other::add); 81 82 sj.merge(other); 83 assertEquals(sj.toString(), fixes.pre0 + "a,b,c,d,e,f" + fixes.suf0); 84 }); 85 } 86 87 public void testEmptyOther() { 88 fixesStream().forEach(fixes -> { 89 StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0); 90 StringJoiner other = new StringJoiner(",", fixes.pre1, fixes.suf1); 91 Stream.of("a", "b", "c").forEachOrdered(sj::add); 92 93 sj.merge(other); 94 assertEquals(sj.toString(), fixes.pre0 + "a,b,c" + fixes.suf0); 95 96 other.setEmptyValue("EMPTY"); 97 sj.merge(other); 98 assertEquals(sj.toString(), fixes.pre0 + "a,b,c" + fixes.suf0); 99 }); 100 } 101 102 public void testEmptyThis() { 103 fixesStream().forEach(fixes -> { 104 StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0); 105 StringJoiner other = new StringJoiner(":", fixes.pre1, fixes.suf1); 106 Stream.of("d", "e", "f").forEachOrdered(other::add); 107 108 sj.merge(other); 109 assertEquals(sj.toString(), fixes.pre0 + "d:e:f" + fixes.suf0); 110 111 sj = new StringJoiner(",", fixes.pre0, fixes.suf0).setEmptyValue("EMPTY"); 112 assertEquals(sj.toString(), "EMPTY"); 113 sj.merge(other); 114 assertEquals(sj.toString(), fixes.pre0 + "d:e:f" + fixes.suf0); 115 }); 116 } 117 118 public void testEmptyBoth() { 119 fixesStream().forEach(fixes -> { 120 StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0); 121 StringJoiner other = new StringJoiner(":", fixes.pre1, fixes.suf1); 122 123 sj.merge(other); 124 assertEquals(sj.toString(), fixes.pre0 + fixes.suf0); 125 126 other.setEmptyValue("NOTHING"); 127 sj.merge(other); 128 assertEquals(sj.toString(), fixes.pre0 + fixes.suf0); 129 130 sj = new StringJoiner(",", fixes.pre0, fixes.suf0).setEmptyValue("EMPTY"); 131 assertEquals(sj.toString(), "EMPTY"); 132 sj.merge(other); 133 assertEquals(sj.toString(), "EMPTY"); 134 }); 135 } 136 137 public void testCascadeEmpty() { 138 fixesStream().forEach(fixes -> { 139 StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0); 140 StringJoiner o1 = new StringJoiner(":", fixes.pre1, fixes.suf1).setEmptyValue("Empty1"); 141 StringJoiner o2 = new StringJoiner(",", "<", ">").setEmptyValue("Empty2"); 142 143 o1.merge(o2); 144 assertEquals(o1.toString(), "Empty1"); 145 146 sj.merge(o1); 147 assertEquals(sj.toString(), fixes.pre0 + fixes.suf0); 148 }); 149 } 150 151 public void testDelimiter() { 152 fixesStream().forEach(fixes -> { 153 StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0); 154 StringJoiner other = new StringJoiner(":", fixes.pre1, fixes.suf1); 155 Stream.of("a", "b", "c").forEachOrdered(sj::add); 156 Stream.of("d", "e", "f").forEachOrdered(other::add); 157 158 sj.merge(other); 159 assertEquals(sj.toString(), fixes.pre0 + "a,b,c,d:e:f" + fixes.suf0); 160 }); 161 } 162 163 public void testMergeSelf() { 164 fixesStream().forEach(fixes -> { 165 final StringJoiner sj = new StringJoiner(",", fixes.pre0, fixes.suf0).add("a").add("b"); 166 assertEquals(sj.merge(sj).toString(), fixes.pre0 + "a,b,a,b" + fixes.suf0); 167 assertEquals(sj.merge(sj).toString(), fixes.pre0 + "a,b,a,b,a,b,a,b" + fixes.suf0); 168 }); 169 } 170 } |