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 
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.nio.charset.Charset;
28 import java.nio.charset.StandardCharsets;
29 import java.nio.file.Files;
30 import java.nio.file.Paths;
31 import java.util.Formatter;
32 import java.util.Locale;
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 String USER_DIR = System.getProperty("user.dir", ".");
46 
47     // Charset added only for the 3-parameter constructors
48     public static enum ConstructorType {
49         STRING3,
50         FILE3,
51         OUTPUTSTREAM3
52     }
53 
54     @DataProvider(name = "parameters")
getParameters()55     public Object[][] getParameters() throws IOException {
56         Locale l = Locale.getDefault();
57         String csn1 = StandardCharsets.ISO_8859_1.name();
58         Charset charset1 = StandardCharsets.ISO_8859_1;
59         String csn2 = StandardCharsets.UTF_8.name();
60         Charset charset2 = StandardCharsets.UTF_8;
61 
62         File file1 = new File(USER_DIR, "FormatterCharsetTest1.txt");
63         File file2 = new File(USER_DIR, "FormatterCharsetTest2.txt");
64 
65         return new Object[][]{
66             {ConstructorType.STRING3, file1, file2, csn1, charset1},
67             {ConstructorType.FILE3, file1, file2, csn1, charset1},
68             {ConstructorType.OUTPUTSTREAM3, file1, file2, csn1, charset1},
69             {ConstructorType.STRING3, file1, file2, csn2, charset2},
70             {ConstructorType.FILE3, file1, file2, csn2, charset2},
71             {ConstructorType.OUTPUTSTREAM3, file1, file2, csn2, charset2},
72         };
73     }
74 
75     /**
76      * Verifies that the overloading constructor behaves the same as the existing
77      * one.
78      * @param type the type of the constructor
79      * @param file1 file1 written with the name of a charset
80      * @param file2 file2 written with a charset
81      * @param csn the charset name
82      * @param charset the charset
83      * @throws IOException
84      */
85     @Test(dataProvider = "parameters")
testConstructor(ConstructorType type, File file1, File file2, String csn, Charset charset)86     public void testConstructor(ConstructorType type, File file1, File file2,
87             String csn, Charset charset) throws Exception {
88         format(getFormatter(type, file1.getPath(), csn, null));
89         format(getFormatter(type, file2.getPath(), null, charset));
90         Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset),
91                 Files.readAllLines(Paths.get(file2.getPath()), charset));
92     }
93 
format(Formatter formatter)94     void format(Formatter formatter)
95             throws IOException {
96         formatter.format("abcde \u00FA\u00FB\u00FC\u00FD");
97         formatter.format("Java \uff08\u8ba1\u7b97\u673a\u7f16\u7a0b\u8bed\u8a00\uff09");
98         formatter.flush();
99         formatter.close();
100     }
101 
102 
getFormatter(ConstructorType type, String path, String csn, Charset charset)103     Formatter getFormatter(ConstructorType type, String path, String csn, Charset charset)
104             throws IOException {
105         Formatter formatter = null;
106         if (csn != null) {
107             switch (type) {
108                 case STRING3:
109                     formatter = new Formatter(path, csn, Locale.getDefault());
110                     break;
111                 case FILE3:
112                     formatter = new Formatter(new File(path), csn, Locale.getDefault());
113                     break;
114                 case OUTPUTSTREAM3:
115                     formatter = new Formatter(new FileOutputStream(path), csn, Locale.getDefault());
116                     break;
117             }
118         } else {
119             switch (type) {
120                 case STRING3:
121                     formatter = new Formatter(path, charset, Locale.getDefault());
122                     break;
123                 case FILE3:
124                     formatter = new Formatter(new File(path), charset, Locale.getDefault());
125                     break;
126                 case OUTPUTSTREAM3:
127                     formatter = new Formatter(new FileOutputStream(path), charset, Locale.getDefault());
128                     break;
129             }
130         }
131         return formatter;
132     }
133 }
134