Path: blob/master/test/jdk/java/io/PrintStream/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.PrintStream;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, "PSCharsetTest1.txt");61File file2 = new File(USER_DIR, "PSCharsetTest2.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* @param type the type of the constructor74* @param file1 file1 written with the name of a charset75* @param file2 file2 written with a charset76* @param csn the charset name77* @param charset the charset78* @throws IOException79*/80@Test(dataProvider = "parameters")81public void test(ConstructorType type, File file1, File file2, String csn, Charset charset)82throws Exception {83createFile(getPrintStream(type, file1.getPath(), csn, null));84createFile(getPrintStream(type, file2.getPath(), null, charset));8586Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset),87Files.readAllLines(Paths.get(file2.getPath()), charset));88}8990public void createFile(PrintStream out) throws IOException {91out.println("high surrogate");92out.println(Character.MIN_HIGH_SURROGATE);93out.println("low surrogate");94out.println(Character.MIN_LOW_SURROGATE);95out.flush();96out.close();97}9899PrintStream getPrintStream(ConstructorType type, String path, String csn, Charset charset)100throws IOException {101PrintStream out = null;102if (csn != null) {103switch (type) {104case STRING:105out = new PrintStream(path, csn);106break;107case FILE:108out = new PrintStream(new File(path), csn);109break;110case OUTPUTSTREAM:111FileOutputStream fout = new FileOutputStream(path);112out = new PrintStream(fout, AUTOFLUSH, csn);113break;114}115} else {116switch (type) {117case STRING:118out = new PrintStream(path, charset);119break;120case FILE:121out = new PrintStream(new File(path), charset);122break;123case OUTPUTSTREAM:124FileOutputStream fout = new FileOutputStream(path);125out = new PrintStream(fout, AUTOFLUSH, charset);126break;127}128}129130return out;131}132133}134135136