Path: blob/master/test/jdk/java/nio/channels/Channels/EncodingTest.java
41152 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.FileInputStream;25import java.io.FileOutputStream;26import java.io.IOException;27import java.io.Reader;28import java.io.Writer;29import java.nio.channels.Channels;30import java.nio.channels.ReadableByteChannel;31import java.nio.channels.WritableByteChannel;32import java.nio.charset.Charset;33import java.nio.charset.MalformedInputException;34import java.nio.charset.StandardCharsets;35import java.nio.file.Paths;36import org.testng.Assert;37import org.testng.annotations.DataProvider;38import org.testng.annotations.Test;3940/**41* @test42* @bug 818374343* @summary Test to verify the new overload method with Charset functions the same44* as the existing method that takes a charset name.45* @run testng EncodingTest46*/47public class EncodingTest {48static final int ITERATIONS = 2;49public static final String CS_UTF8 = StandardCharsets.UTF_8.name();50public static final String CS_ISO8859 = StandardCharsets.ISO_8859_1.name();51static String USER_DIR = System.getProperty("user.dir", ".");5253// malformed input: a high surrogate without the low surrogate54static char[] illChars = {55'\u00fa', '\ud800'56};5758static byte[] data = getData();5960static byte[] getData() {61try {62String str1 = "A string that contains ";63String str2 = " , an invalid character for UTF-8.";6465ByteArrayOutputStream baos = new ByteArrayOutputStream();66baos.write(str1.getBytes());67baos.write(0xFA);68baos.write(str2.getBytes());69return baos.toByteArray();70} catch (IOException ex) {71return null; //shouldn't happen72}73}7475String testFile = Paths.get(USER_DIR, "channelsEncodingTest.txt").toString();76String testIllegalInput = Paths.get(USER_DIR, "channelsIllegalInputTest.txt").toString();77String testIllegalOutput = Paths.get(USER_DIR, "channelsIllegalOutputTest.txt").toString();787980/*81* DataProvider for read and write test.82* Writes and reads with the same encoding83*/84@DataProvider(name = "writeAndRead")85public Object[][] getWRParameters() {86return new Object[][]{87{testFile, StandardCharsets.ISO_8859_1.name(), null,88StandardCharsets.ISO_8859_1.name(), StandardCharsets.ISO_8859_1},89{testFile, null, StandardCharsets.ISO_8859_1,90StandardCharsets.ISO_8859_1.name(), StandardCharsets.ISO_8859_1},91{testFile, StandardCharsets.UTF_8.name(), null,92StandardCharsets.UTF_8.name(), StandardCharsets.UTF_8},93{testFile, null, StandardCharsets.UTF_8,94StandardCharsets.UTF_8.name(), StandardCharsets.UTF_8}95};96}9798/*99* DataProvider for illegal input test100* Writes the data in ISO8859 and reads with UTF8, expects MalformedInputException101*/102@DataProvider(name = "illegalInput")103public Object[][] getParameters() {104return new Object[][]{105{testIllegalInput, StandardCharsets.ISO_8859_1.name(), null, StandardCharsets.UTF_8.name(), null},106{testIllegalInput, StandardCharsets.ISO_8859_1.name(), null, null, StandardCharsets.UTF_8},107{testIllegalInput, null, StandardCharsets.ISO_8859_1, StandardCharsets.UTF_8.name(), null},108{testIllegalInput, null, StandardCharsets.ISO_8859_1, null, StandardCharsets.UTF_8},109};110}111112/*113* DataProvider for illegal output test114* Attemps to write some malformed chars, expects MalformedInputException115*/116@DataProvider(name = "illegalOutput")117public Object[][] getWriteParameters() {118return new Object[][]{119{testIllegalOutput, StandardCharsets.UTF_8.name(), null},120{testIllegalOutput, null, StandardCharsets.UTF_8}121};122}123124/**125* Verifies that the Readers created with the following methods are126* equivalent:127* newReader(ReadableByteChannel ch, String csName)128* newReader(ReadableByteChannel ch, Charset charset)129*130* The verification follows the following steps:131* Writes a file with a writer created with the specified charset132* Reads it with a reader created with newReader using the same charset;133* Compares that the results are the same.134*135* @param file the file name136* @param csnWriter the charset name for creating the writer137* @param charsetWriter the charset for creating the writer138* @param csnReader the charset name for creating the reader139* @param charsetReader the charset for creating the reader140* @throws Exception141*/142@Test(dataProvider = "writeAndRead")143public void testWriteAndRead(String file, String csnWriter, Charset charsetWriter,144String csnReader, Charset charsetReader) throws Exception {145writeToFile(data, file, csnWriter, charsetWriter);146// read using charset name147String result1 = readFileToString(file, csnReader, null);148String result2 = readFileToString(file, null, charsetReader);149150Assert.assertEquals(result1, result2);151}152153/**154* Verifies that MalformedInputException is thrown when an input byte sequence155* is illegal for given charset that is configured for the reader.156*157* @param file the file to be read158* @param csnWriter the charset name for creating the writer159* @param charsetWriter the charset for creating the writer160* @param csnReader the charset name for creating the reader161* @param charsetReader the charset for creating the reader162* @throws Exception163*/164@Test(dataProvider = "illegalInput", expectedExceptions = MalformedInputException.class)165void testMalformedInput(String file, String csnWriter, Charset charsetWriter,166String csnReader, Charset charsetReader) throws Exception {167writeToFile(data, file, csnWriter, charsetWriter);168readFileToString(file, csnReader, charsetReader);169}170171/**172* Attempts to write illegal characters using a writer created by calling173* the newWriter method and expects a MalformedInputException.174*175* @param fileName the file name176* @param csn the charset name177* @param charset the charset178* @throws Exception179*/180@Test(dataProvider = "illegalOutput", expectedExceptions = MalformedInputException.class)181public void testMalformedOutput(String fileName, String csn, Charset charset)182throws Exception {183try (FileOutputStream fos = new FileOutputStream(fileName);184WritableByteChannel wbc = (WritableByteChannel) fos.getChannel();) {185Writer writer;186if (csn != null) {187writer = Channels.newWriter(wbc, csn);188} else {189writer = Channels.newWriter(wbc, charset);190}191192for (int i = 0; i < ITERATIONS; i++) {193writer.write(illChars);194}195writer.flush();196writer.close();197}198}199200/**201* Writes the data to a file using a writer created by calling the newWriter202* method.203*204* @param data the data to be written205* @param fileName the file name206* @param csn the charset name207* @param charset the charset208* @throws Exception209*/210private void writeToFile(byte[] data, String fileName, String csn, Charset charset) throws Exception {211try (FileOutputStream fos = new FileOutputStream(fileName);212WritableByteChannel wbc = (WritableByteChannel) fos.getChannel()) {213Writer writer;214String temp;215if (csn != null) {216writer = Channels.newWriter(wbc, csn);217temp = new String(data, csn);218} else {219writer = Channels.newWriter(wbc, charset);220temp = new String(data, charset);221}222223for (int i = 0; i < ITERATIONS; i++) {224writer.write(temp);225}226writer.flush();227writer.close();228}229}230231/**232* Reads a file into a String.233*234* @param file the file to be read235* @param csn the charset name236* @param charset the charset237* @throws Exception238*/239String readFileToString(String file, String csn, Charset charset) throws Exception {240String result;241try (FileInputStream fis = new FileInputStream(file);242ReadableByteChannel rbc = (ReadableByteChannel) fis.getChannel()) {243Reader reader;244if (csn != null) {245reader = Channels.newReader(rbc, csn);246} else {247reader = Channels.newReader(rbc, charset);248}249250int messageSize = data.length * ITERATIONS;251char data1[] = new char[messageSize];252int totalRead = 0;253int charsRead = 0;254while (totalRead < messageSize) {255totalRead += charsRead;256charsRead = reader.read(data1, totalRead, messageSize - totalRead);257}258259result = new String(data1, 0, totalRead);260reader.close();261}262263return result;264}265}266267268