Path: blob/master/test/jdk/sun/nio/cs/StreamEncoderOut.java
41149 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@bug 803017925@summary test if the charset encoder deails with surrogate correctly26* @run testng/othervm -esa StreamEncoderOut27*/28import org.testng.annotations.DataProvider;29import org.testng.annotations.Test;3031import java.io.IOException;32import java.io.OutputStream;33import java.io.OutputStreamWriter;34import java.nio.charset.Charset;35import java.util.stream.Stream;3637import static java.util.stream.Collectors.joining;3839@Test40public class StreamEncoderOut {4142enum Input {43HIGH("\ud834"),44LOW("\udd1e"),45HIGH_LOW("\ud834\udd1e");4647final String value;4849Input(String value) {50this.value = value;51}5253@Override54public String toString() {55return name() + " : \'" + value + "\"";56}57}5859@DataProvider(name = "CharsetAndString")60// [Charset, Input]61public static Object[][] makeStreamTestData() {62// Cross product of supported charsets and inputs63return Charset.availableCharsets().values().stream().64filter(Charset::canEncode).65flatMap(cs -> Stream.of(Input.values()).map(i -> new Object[]{cs, i})).66toArray(Object[][]::new);67}6869private static String generate(String s, int n) {70return Stream.generate(() -> s).limit(n).collect(joining());71}7273static final OutputStream DEV_NULL = new OutputStream() {74@Override75public void write(byte b[], int off, int len) throws IOException {}7677@Override78public void write(int b) throws IOException {}79};8081@Test(dataProvider = "CharsetAndString")82public void test(Charset cs, Input input) throws IOException {83OutputStreamWriter w = new OutputStreamWriter(DEV_NULL, cs);84String t = generate(input.value, 8193);85for (int i = 0; i < 10; i++) {86w.append(t);87}88}89}909192