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