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