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.FileInputStream;
26 import java.io.FileWriter;
27 import java.io.IOException;
28 import java.nio.charset.Charset;
29 import java.nio.charset.StandardCharsets;
30 import java.nio.file.Paths;
31 import java.util.Scanner;
32 import org.testng.Assert;
33 import org.testng.annotations.DataProvider;
34 import org.testng.annotations.Test;
35 
36 /**
37  * @test
38  * @bug 8183743
39  * @summary Test to verify the new overload method with Charset functions the
40  * same as the existing method that takes a charset name.
41  * @run testng EncodingTest
42  */
43 public class EncodingTest {
44     static String USER_DIR = System.getProperty("user.dir", ".");
45 
46     public static enum ConstructorType {
47         FILE,
48         PATH,
49         INPUTSTREAM,
50         READABLEBYTECHANNEL
51     }
52 
53     static final String TEST_STRING = "abc \u0100 \u0101 \u0555 \u07FD \u07FF";
54 
55     @DataProvider(name = "parameters")
getParameters()56     public Object[][] getParameters() throws IOException {
57         String csn = StandardCharsets.UTF_8.name();
58         Charset charset = StandardCharsets.UTF_8;
59         File file1 = new File(USER_DIR, "ScannerCharsetTest1.txt");
60         File file2 = new File(USER_DIR, "ScannerCharsetTest2.txt");
61 
62         return new Object[][]{
63             {ConstructorType.FILE, file1, file2, csn, charset},
64             {ConstructorType.PATH, file1, file2, csn, charset},
65             {ConstructorType.INPUTSTREAM, file1, file2, csn, charset},
66             {ConstructorType.READABLEBYTECHANNEL, file1, file2, csn, charset},};
67     }
68 
69     /**
70      * Verifies that the overloading constructor behaves the same as the
71      * existing one.
72      *
73      * @param type the type of the constructor
74      * @param file1 file1 written with the name of a charset
75      * @param file2 file2 written with a charset
76      * @param csn the charset name
77      * @param charset the charset
78      * @throws IOException
79      */
80     @Test(dataProvider = "parameters")
test(ConstructorType type, File file1, File file2, String csn, Charset charset)81     void test(ConstructorType type, File file1, File file2, String csn, Charset charset)
82             throws Exception {
83         prepareFile(file1, TEST_STRING);
84         prepareFile(file2, TEST_STRING);
85 
86         try (Scanner s1 = getScanner(type, file1.getPath(), csn, null);
87                 Scanner s2 = getScanner(type, file2.getPath(), null, charset);) {
88             String result1 = s1.findInLine(TEST_STRING);
89             String result2 = s2.findInLine(TEST_STRING);
90             Assert.assertEquals(result1, result2);
91         }
92     }
93 
94     /*
95      * Creates a Scanner over the given input file.
96      */
getScanner(ConstructorType type, String file, String csn, Charset charset)97     Scanner getScanner(ConstructorType type, String file, String csn, Charset charset)
98             throws Exception {
99         if (csn != null) {
100             switch (type) {
101                 case FILE:
102                     return new Scanner(new File(file), csn);
103                 case PATH:
104                     return new Scanner(Paths.get(file), csn);
105                 case INPUTSTREAM:
106                     FileInputStream fis = new FileInputStream(file);
107                     return new Scanner(fis, csn);
108                 case READABLEBYTECHANNEL:
109                     FileInputStream fis1 = new FileInputStream(file);
110                     return new Scanner(fis1.getChannel(), csn);
111             }
112         } else {
113             switch (type) {
114                 case FILE:
115                     return new Scanner(new File(file), charset);
116                 case PATH:
117                     return new Scanner(Paths.get(file), charset);
118                 case INPUTSTREAM:
119                     FileInputStream fis = new FileInputStream(file);
120                     return new Scanner(fis, charset);
121                 case READABLEBYTECHANNEL:
122                     FileInputStream fis1 = new FileInputStream(file);
123                     return new Scanner(fis1.getChannel(), charset);
124             }
125         }
126 
127         return null;
128     }
129 
prepareFile(File file, String content)130     void prepareFile(File file, String content) throws IOException {
131         try (FileWriter writer = new FileWriter(file);) {
132             writer.write(content);
133         }
134     }
135 }
136