1 /* 2 * Copyright (c) 2017, 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 package test.java.io.ByteArrayOutputStream; 24 25 import java.io.ByteArrayOutputStream; 26 import java.io.IOException; 27 import java.nio.charset.Charset; 28 import java.nio.charset.StandardCharsets; 29 import org.testng.Assert; 30 import org.testng.annotations.DataProvider; 31 import org.testng.annotations.Test; 32 33 /** 34 * @test 35 * @bug 8183743 36 * @summary Test to verify the new overload method with Charset functions the same 37 * as the existing method that takes a charset name. 38 * @run testng EncodingTest 39 */ 40 public class EncodingTest { 41 /* 42 * DataProvider for the toString method test. Provides the following fields: 43 * byte array, charset name string, charset object 44 */ 45 @DataProvider(name = "parameters") getParameters()46 public Object[][] getParameters() throws IOException { 47 byte[] data = getData(); 48 return new Object[][]{ 49 {data, StandardCharsets.UTF_8.name(), StandardCharsets.UTF_8}, 50 {data, StandardCharsets.ISO_8859_1.name(), StandardCharsets.ISO_8859_1}, 51 }; 52 } 53 54 /** 55 * Verifies that the new overload method that takes a Charset is equivalent to 56 * the existing one that takes a charset name. 57 * @param data a byte array 58 * @param csn the charset name 59 * @param charset the charset 60 * @throws Exception if the test fails 61 */ 62 @Test(dataProvider = "parameters") test(byte[] data, String csn, Charset charset)63 public void test(byte[] data, String csn, Charset charset) throws Exception { 64 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 65 baos.write(data); 66 String str1 = baos.toString(csn); 67 String str2 = baos.toString(charset); 68 Assert.assertEquals(str1, str2); 69 } 70 71 /* 72 * Returns an array containing a character that's invalid for UTF-8 73 * but valid for ISO-8859-1 74 */ getData()75 byte[] getData() throws IOException { 76 String str1 = "A string that contains "; 77 String str2 = " , an invalid character for UTF-8."; 78 79 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 80 baos.write(str1.getBytes()); 81 baos.write(0xFA); 82 baos.write(str2.getBytes()); 83 return baos.toByteArray(); 84 } 85 }