1/*
2 * Copyright (c) 2015, 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
25   @bug 8030179
26   @summary test if the charset encoder deails with surrogate correctly
27 * @run testng/othervm -esa StreamEncoderOut
28 */
29import org.testng.annotations.DataProvider;
30import org.testng.annotations.Test;
31
32import java.io.IOException;
33import java.io.OutputStream;
34import java.io.OutputStreamWriter;
35import java.nio.charset.Charset;
36import java.util.stream.Stream;
37
38import static java.util.stream.Collectors.joining;
39
40@Test
41public class StreamEncoderOut {
42
43    enum Input {
44        HIGH("\ud834"),
45        LOW("\udd1e"),
46        HIGH_LOW("\ud834\udd1e");
47
48        final String value;
49
50        Input(String value) {
51            this.value = value;
52        }
53
54        @Override
55        public String toString() {
56            return name() + " : \'" + value + "\"";
57        }
58    }
59
60    @DataProvider(name = "CharsetAndString")
61    // [Charset, Input]
62    public static Object[][] makeStreamTestData() {
63        // Cross product of supported charsets and inputs
64        return Charset.availableCharsets().values().stream().
65                filter(Charset::canEncode).
66                flatMap(cs -> Stream.of(Input.values()).map(i -> new Object[]{cs, i})).
67                toArray(Object[][]::new);
68    }
69
70    private static String generate(String s, int n) {
71        return Stream.generate(() -> s).limit(n).collect(joining());
72    }
73
74    static final OutputStream DEV_NULL = new OutputStream() {
75        @Override
76        public void write(byte b[], int off, int len) throws IOException {}
77
78        @Override
79        public void write(int b) throws IOException {}
80    };
81
82    @Test(dataProvider = "CharsetAndString")
83    public void test(Charset cs, Input input) throws IOException {
84        OutputStreamWriter w = new OutputStreamWriter(DEV_NULL, cs);
85        String t = generate(input.value, 8193);
86        for (int i = 0; i < 10; i++) {
87            w.append(t);
88        }
89    }
90}
91