Path: blob/master/test/jdk/java/io/ByteArrayOutputStream/EncodingTest.java
41149 views
/*1* Copyright (c) 2017, 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*/2223import java.io.ByteArrayOutputStream;24import java.io.IOException;25import java.nio.charset.Charset;26import java.nio.charset.StandardCharsets;27import org.testng.Assert;28import org.testng.annotations.DataProvider;29import org.testng.annotations.Test;3031/**32* @test33* @bug 818374334* @summary Test to verify the new overload method with Charset functions the same35* as the existing method that takes a charset name.36* @run testng EncodingTest37*/38public class EncodingTest {39/*40* DataProvider for the toString method test. Provides the following fields:41* byte array, charset name string, charset object42*/43@DataProvider(name = "parameters")44public Object[][] getParameters() throws IOException {45byte[] data = getData();46return new Object[][]{47{data, StandardCharsets.UTF_8.name(), StandardCharsets.UTF_8},48{data, StandardCharsets.ISO_8859_1.name(), StandardCharsets.ISO_8859_1},49};50}5152/**53* Verifies that the new overload method that takes a Charset is equivalent to54* the existing one that takes a charset name.55* @param data a byte array56* @param csn the charset name57* @param charset the charset58* @throws Exception if the test fails59*/60@Test(dataProvider = "parameters")61public void test(byte[] data, String csn, Charset charset) throws Exception {62ByteArrayOutputStream baos = new ByteArrayOutputStream();63baos.write(data);64String str1 = baos.toString(csn);65String str2 = baos.toString(charset);66Assert.assertEquals(str1, str2);67}6869/*70* Returns an array containing a character that's invalid for UTF-871* but valid for ISO-8859-172*/73byte[] getData() throws IOException {74String str1 = "A string that contains ";75String str2 = " , an invalid character for UTF-8.";7677ByteArrayOutputStream baos = new ByteArrayOutputStream();78baos.write(str1.getBytes());79baos.write(0xFA);80baos.write(str2.getBytes());81return baos.toByteArray();82}83}848586