Path: blob/master/test/jdk/java/io/FileReader/ConstructorTest.java
41149 views
/*1* Copyright (c) 2018, 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.File;24import java.io.FileInputStream;25import java.io.FileReader;26import java.io.FileWriter;27import java.io.IOException;28import java.io.InputStreamReader;29import java.io.Reader;30import java.nio.charset.Charset;31import java.nio.charset.StandardCharsets;32import org.testng.Assert;33import org.testng.annotations.DataProvider;34import org.testng.annotations.Test;3536/**37* @test38* @bug 818355439* @summary Test to verify the new Constructors that take a Charset.40* @run testng ConstructorTest41*/42public class ConstructorTest {43static String USER_DIR = System.getProperty("user.dir", ".");4445public static enum ConstructorType {46STRING,47FILE48}4950static final String TEST_STRING = "abc \u0100 \u0101 \u0555 \u07FD \u07FF";51static final int BUFFER_SIZE = 8192;5253@DataProvider(name = "parameters")54public Object[][] getParameters() throws IOException {55File file1 = new File(USER_DIR, "FileReaderTest1.txt");56File file2 = new File(USER_DIR, "FileReaderTest2.txt");5758return new Object[][]{59{ConstructorType.STRING, file1, file2, StandardCharsets.UTF_8},60{ConstructorType.FILE, file1, file2, StandardCharsets.UTF_8},61{ConstructorType.STRING, file1, file2, StandardCharsets.ISO_8859_1},62{ConstructorType.FILE, file1, file2, StandardCharsets.ISO_8859_1},63};64}6566/**67* Verifies that the new constructors that take a Charset function the same68* as an InputStreamReader on a FileInputStream as was recommended before69* this change.70*71* @param type the type of the constructor72* @param file1 file1 to be read with a FileReader73* @param file2 file2 to be read with an InputStreamReader74* @param charset the charset75* @throws IOException76*/77@Test(dataProvider = "parameters")78void test(ConstructorType type, File file1, File file2, Charset charset)79throws Exception {80prepareFile(file1, TEST_STRING, charset);81prepareFile(file2, TEST_STRING, charset);8283try (FileReader fr = getFileReader(type, file1, charset);84FileInputStream is = new FileInputStream(file2);85InputStreamReader isr = new InputStreamReader(is, charset);) {86String result1 = readAll(fr, BUFFER_SIZE);87String result2 = readAll(isr, BUFFER_SIZE);88Assert.assertEquals(result1, result2);89}90}9192public String readAll(Reader reader, int bufferSize) throws IOException {93StringBuilder sb = new StringBuilder();94char[] buf = new char[bufferSize];95int numRead;96while ((numRead = reader.read(buf)) != -1) {97if (numRead == buf.length) {98sb.append(buf);99} else {100sb.append(String.valueOf(buf, 0, numRead));101}102}103return sb.toString();104}105106/*107* Creates a FileReader over the given input file.108*/109FileReader getFileReader(ConstructorType type, File file, Charset charset)110throws IOException {111switch (type) {112case STRING:113return new FileReader(file.getPath(), charset);114case FILE:115return new FileReader(file, charset);116}117118return null;119}120121void prepareFile(File file, String content, Charset charset) throws IOException {122try (FileWriter writer = new FileWriter(file, charset);) {123writer.write(content);124}125}126}127128129