1 /* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 package test.java.io.PrintStream; 24 25 import java.io.File; 26 import java.io.FileOutputStream; 27 import java.io.IOException; 28 import java.io.PrintStream; 29 import java.nio.charset.Charset; 30 import java.nio.charset.StandardCharsets; 31 import java.nio.file.Files; 32 import java.nio.file.Paths; 33 import org.testng.Assert; 34 import org.testng.annotations.DataProvider; 35 import org.testng.annotations.Test; 36 37 /** 38 * @test 39 * @bug 8183743 40 * @summary Test to verify the new overload method with Charset functions the same 41 * as the existing method that takes a charset name. 42 * @run testng EncodingTest 43 */ 44 public class EncodingTest { 45 static boolean AUTOFLUSH = true; 46 public static enum ConstructorType { 47 STRING, 48 FILE, 49 OUTPUTSTREAM 50 } 51 52 /* 53 * DataProvider fields: 54 * Type of the constructor, a file to be written with a charset name, 55 * a file to be written with a charset, charset name, charset. 56 */ 57 @DataProvider(name = "parameters") getParameters()58 public Object[][] getParameters() throws IOException { 59 String csn = StandardCharsets.UTF_8.name(); 60 Charset charset = StandardCharsets.UTF_8; 61 File file1 = File.createTempFile("PSCharsetTest1", "txt"); 62 File file2 = File.createTempFile("PSCharsetTest2", "txt"); 63 64 return new Object[][]{ 65 {ConstructorType.STRING, file1, file2, csn, charset}, 66 {ConstructorType.FILE, file1, file2, csn, charset}, 67 {ConstructorType.OUTPUTSTREAM, file1, file2, csn, charset} 68 }; 69 } 70 71 /** 72 * Verifies that the overloading constructor behaves the same as the existing 73 * one. 74 * @param type the type of the constructor 75 * @param file1 file1 written with the name of a charset 76 * @param file2 file2 written with a charset 77 * @param csn the charset name 78 * @param charset the charset 79 * @throws IOException 80 */ 81 @Test(dataProvider = "parameters") test(ConstructorType type, File file1, File file2, String csn, Charset charset)82 public void test(ConstructorType type, File file1, File file2, String csn, Charset charset) 83 throws Exception { 84 createFile(getPrintStream(type, file1.getPath(), csn, null)); 85 createFile(getPrintStream(type, file2.getPath(), null, charset)); 86 87 Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset), 88 Files.readAllLines(Paths.get(file2.getPath()), charset)); 89 } 90 createFile(PrintStream out)91 public void createFile(PrintStream out) throws IOException { 92 out.println("high surrogate"); 93 out.println(Character.MIN_HIGH_SURROGATE); 94 out.println("low surrogate"); 95 out.println(Character.MIN_LOW_SURROGATE); 96 out.flush(); 97 out.close(); 98 } 99 getPrintStream(ConstructorType type, String path, String csn, Charset charset)100 PrintStream getPrintStream(ConstructorType type, String path, String csn, Charset charset) 101 throws IOException { 102 PrintStream out = null; 103 if (csn != null) { 104 switch (type) { 105 case STRING: 106 out = new PrintStream(path, csn); 107 break; 108 case FILE: 109 out = new PrintStream(new File(path), csn); 110 break; 111 case OUTPUTSTREAM: 112 FileOutputStream fout = new FileOutputStream(path); 113 out = new PrintStream(fout, AUTOFLUSH, csn); 114 break; 115 } 116 } else { 117 switch (type) { 118 case STRING: 119 out = new PrintStream(path, charset); 120 break; 121 case FILE: 122 out = new PrintStream(new File(path), charset); 123 break; 124 case OUTPUTSTREAM: 125 FileOutputStream fout = new FileOutputStream(path); 126 out = new PrintStream(fout, AUTOFLUSH, charset); 127 break; 128 } 129 } 130 131 return out; 132 } 133 134 }