Path: blob/master/test/jdk/java/io/PrintWriter/EncodingTest.java
41149 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.File;24import java.io.FileOutputStream;25import java.io.IOException;26import java.io.PrintWriter;27import java.nio.charset.Charset;28import java.nio.charset.StandardCharsets;29import java.nio.file.Files;30import java.nio.file.Paths;31import org.testng.Assert;32import org.testng.annotations.DataProvider;33import org.testng.annotations.Test;3435/**36* @test37* @bug 818374338* @summary Test to verify the new overload method with Charset functions the same39* as the existing method that takes a charset name.40* @run testng EncodingTest41*/42public class EncodingTest {43static String USER_DIR = System.getProperty("user.dir", ".");44static boolean AUTOFLUSH = true;45public static enum ConstructorType {46STRING,47FILE,48OUTPUTSTREAM49}5051/*52* DataProvider fields:53* Type of the constructor, a file to be written with a charset name,54* a file to be written with a charset, charset name, charset.55*/56@DataProvider(name = "parameters")57public Object[][] getParameters() throws IOException {58String csn = StandardCharsets.UTF_8.name();59Charset charset = StandardCharsets.UTF_8;60File file1 = new File(USER_DIR, "PWCharsetTest1.txt");61File file2 = new File(USER_DIR, "PWCharsetTest2.txt");6263return new Object[][]{64{ConstructorType.STRING, file1, file2, csn, charset},65{ConstructorType.FILE, file1, file2, csn, charset},66{ConstructorType.OUTPUTSTREAM, file1, file2, csn, charset}67};68}6970/**71* Verifies that the overloading constructor behaves the same as the existing72* one.73*74* @param type the type of the constructor75* @param file1 file1 written with the name of a charset76* @param file2 file2 written with a charset77* @param csn the charset name78* @param charset the charset79* @throws IOException80*/81@Test(dataProvider = "parameters")82public void test(ConstructorType type, File file1, File file2, String csn, Charset charset)83throws Exception {84createFile(getWriter(type, file1.getPath(), csn, null));85createFile(getWriter(type, file2.getPath(), null, charset));8687Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset),88Files.readAllLines(Paths.get(file2.getPath()), charset));89}9091void createFile(PrintWriter out) throws IOException {92out.println("high surrogate");93out.println(Character.MIN_HIGH_SURROGATE);94out.println("low surrogate");95out.println(Character.MIN_LOW_SURROGATE);96out.flush();97out.close();98}99100PrintWriter getWriter(ConstructorType type, String path, String csn, Charset charset)101throws IOException {102PrintWriter out = null;103if (csn != null) {104switch (type) {105case STRING:106out = new PrintWriter(path, csn);107break;108case FILE:109out = new PrintWriter(new File(path), csn);110break;111case OUTPUTSTREAM:112// No corresponding method with charset name113// compare with PrintWriter(path, csn) instead114out = new PrintWriter(path, csn);115break;116}117} else {118switch (type) {119case STRING:120out = new PrintWriter(path, charset);121break;122case FILE:123out = new PrintWriter(new File(path), charset);124break;125case OUTPUTSTREAM:126FileOutputStream fout = new FileOutputStream(path);127out = new PrintWriter(fout, AUTOFLUSH, charset);128break;129}130}131132return out;133}134}135136137