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.PrintWriter;
24 
25 import java.io.File;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.PrintWriter;
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  * @bug 8183743
41  * @summary Test to verify the new overload method with Charset functions the same
42  * as the existing method that takes a charset name.
43  * @run testng EncodingTest
44  */
45 public class EncodingTest {
46     static boolean AUTOFLUSH = true;
47     public static enum ConstructorType {
48         STRING,
49         FILE,
50         OUTPUTSTREAM
51     }
52 
53     /*
54      * DataProvider fields:
55      * Type of the constructor, a file to be written with a charset name,
56      * a file to be written with a charset, charset name, charset.
57      */
58     @DataProvider(name = "parameters")
getParameters()59     public Object[][] getParameters() throws IOException {
60         String csn = StandardCharsets.UTF_8.name();
61         Charset charset = StandardCharsets.UTF_8;
62         File file1 = File.createTempFile("PSCharsetTest1", "txt");
63         File file2 = File.createTempFile("PSCharsetTest2", "txt");
64 
65         return new Object[][]{
66                 {ConstructorType.STRING, file1, file2, csn, charset},
67                 {ConstructorType.FILE, file1, file2, csn, charset},
68                 {ConstructorType.OUTPUTSTREAM, file1, file2, csn, charset}
69         };
70     }
71 
72     /**
73      * Verifies that the overloading constructor behaves the same as the existing
74      * one.
75      *
76      * @param type the type of the constructor
77      * @param file1 file1 written with the name of a charset
78      * @param file2 file2 written with a charset
79      * @param csn the charset name
80      * @param charset the charset
81      * @throws IOException
82      */
83     @Test(dataProvider = "parameters")
test(ConstructorType type, File file1, File file2, String csn, Charset charset)84     public void test(ConstructorType type, File file1, File file2, String csn, Charset charset)
85             throws Exception {
86         createFile(getWriter(type, file1.getPath(), csn, null));
87         createFile(getWriter(type, file2.getPath(), null, charset));
88 
89         Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset),
90                 Files.readAllLines(Paths.get(file2.getPath()), charset));
91     }
92 
createFile(PrintWriter out)93     void createFile(PrintWriter out) throws IOException {
94         out.println("high surrogate");
95         out.println(Character.MIN_HIGH_SURROGATE);
96         out.println("low surrogate");
97         out.println(Character.MIN_LOW_SURROGATE);
98         out.flush();
99         out.close();
100     }
101 
getWriter(ConstructorType type, String path, String csn, Charset charset)102     PrintWriter getWriter(ConstructorType type, String path, String csn, Charset charset)
103             throws IOException {
104         PrintWriter out = null;
105         if (csn != null) {
106             switch (type) {
107                 case STRING:
108                     out = new PrintWriter(path, csn);
109                     break;
110                 case FILE:
111                     out = new PrintWriter(new File(path), csn);
112                     break;
113                 case OUTPUTSTREAM:
114                     // No corresponding method with charset name
115                     // compare with PrintWriter(path, csn) instead
116                     out = new PrintWriter(path, csn);
117                     break;
118             }
119         } else {
120             switch (type) {
121                 case STRING:
122                     out = new PrintWriter(path, charset);
123                     break;
124                 case FILE:
125                     out = new PrintWriter(new File(path), charset);
126                     break;
127                 case OUTPUTSTREAM:
128                     FileOutputStream fout = new FileOutputStream(path);
129                     out = new PrintWriter(fout, AUTOFLUSH, charset);
130                     break;
131             }
132         }
133 
134         return out;
135     }
136 }