Path: blob/master/test/jdk/java/io/FileWriter/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.FileOutputStream;26import java.io.FileReader;27import java.io.FileWriter;28import java.io.IOException;29import java.io.OutputStreamWriter;30import java.io.Reader;31import java.nio.charset.Charset;32import java.nio.charset.StandardCharsets;33import org.testng.Assert;34import org.testng.annotations.DataProvider;35import org.testng.annotations.Test;3637/**38* @test39* @bug 818355440* @summary Test to verify the new Constructors that take a Charset.41* @run testng ConstructorTest42*/43public class ConstructorTest {44static String USER_DIR = System.getProperty("user.dir", ".");4546public static enum ConstructorType {47STRING,48FILE,49STRING_APPEND,50FILE_APPEND51}5253static final String TEST_STRING = "abc \u0100 \u0101 \u0555 \u07FD \u07FF";54static final int BUFFER_SIZE = 8192;5556@DataProvider(name = "parameters")57public Object[][] getParameters() throws IOException {58File file1 = new File(USER_DIR, "FileWriterTest1.txt");59File file2 = new File(USER_DIR, "FileWriterTest2.txt");6061return new Object[][]{62{ConstructorType.STRING, file1, file2, StandardCharsets.UTF_8},63{ConstructorType.FILE, file1, file2, StandardCharsets.UTF_8},64{ConstructorType.STRING_APPEND, file1, file2, StandardCharsets.UTF_8},65{ConstructorType.FILE_APPEND, file1, file2, StandardCharsets.UTF_8},66{ConstructorType.STRING, file1, file2, StandardCharsets.ISO_8859_1},67{ConstructorType.FILE, file1, file2, StandardCharsets.ISO_8859_1},68{ConstructorType.STRING_APPEND, file1, file2, StandardCharsets.ISO_8859_1},69{ConstructorType.FILE_APPEND, file1, file2, StandardCharsets.ISO_8859_1},70};71}7273/**74* Verifies that the new constructors that take a Charset function the same75* as an OutputStreamWriter on a FileOutputStream as was recommended before76* this change.77*78* @param type the type of the constructor79* @param file1 file1 to be written with a FileWriter80* @param file2 file2 to be written with an OutputStreamWriter81* @param charset the charset82* @throws IOException83*/84@Test(dataProvider = "parameters")85void test(ConstructorType type, File file1, File file2, Charset charset)86throws Exception {87writeWithFileWriter(type, file1, TEST_STRING, charset);88writeWithOutputStreamWriter(type, file2, TEST_STRING, charset);8990try (91FileReader r1 = getFileReader(type, file1, charset);92FileReader r2 = getFileReader(type, file2, charset);93) {94String result1 = readAll(r1, BUFFER_SIZE);95String result2 = readAll(r2, BUFFER_SIZE);96Assert.assertEquals(result1, result2);97}98}99100public String readAll(Reader reader, int bufferSize) throws IOException {101StringBuilder sb = new StringBuilder();102char[] buf = new char[bufferSize];103int numRead;104while ((numRead = reader.read(buf)) != -1) {105if (numRead == buf.length) {106sb.append(buf);107} else {108sb.append(String.valueOf(buf, 0, numRead));109}110}111return sb.toString();112}113114/*115* Creates a FileReader over the given input file.116*/117FileReader getFileReader(ConstructorType type, File file, Charset charset)118throws IOException {119switch (type) {120case STRING:121case STRING_APPEND:122return new FileReader(file.getPath(), charset);123case FILE:124case FILE_APPEND:125return new FileReader(file, charset);126}127128return null;129}130131/*132* Creates a FileWriter using the constructor as specified.133*/134FileWriter getFileWriter(ConstructorType type, File file, Charset charset)135throws IOException {136switch (type) {137case STRING:138return new FileWriter(file.getPath(), charset);139case FILE:140return new FileWriter(file, charset);141case STRING_APPEND:142return new FileWriter(file.getPath(), charset, true);143case FILE_APPEND:144return new FileWriter(file, charset, true);145}146147return null;148}149150void writeWithFileWriter(ConstructorType type, File file, String content, Charset charset)151throws IOException {152if (type == ConstructorType.STRING_APPEND || type == ConstructorType.FILE_APPEND) {153try (FileWriter writer = getFileWriter(ConstructorType.FILE, file, charset);) {154writer.write(content);155}156}157try (FileWriter writer = getFileWriter(type, file, charset);) {158writer.write(content);159}160}161162void writeWithOutputStreamWriter(ConstructorType type, File file, String content, Charset charset)163throws IOException {164try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), charset)) {165writer.write(content);166if (type == ConstructorType.STRING_APPEND || type == ConstructorType.FILE_APPEND) {167writer.write(content);168}169}170}171}172173174