Path: blob/master/test/jdk/java/util/Scanner/EncodingTest.java
41149 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.io.FileInputStream;25import java.io.FileWriter;26import java.io.IOException;27import java.nio.charset.Charset;28import java.nio.charset.StandardCharsets;29import java.nio.file.Paths;30import java.util.Scanner;31import org.testng.Assert;32import org.testng.annotations.DataProvider;33import org.testng.annotations.Test;3435/**36* @test37* @bug 818374338* @summary Test to verify the new overload method with Charset functions the39* same as the existing method that takes a charset name.40* @run testng EncodingTest41*/42public class EncodingTest {43static String USER_DIR = System.getProperty("user.dir", ".");4445public static enum ConstructorType {46FILE,47PATH,48INPUTSTREAM,49READABLEBYTECHANNEL50}5152static final String TEST_STRING = "abc \u0100 \u0101 \u0555 \u07FD \u07FF";5354@DataProvider(name = "parameters")55public Object[][] getParameters() throws IOException {56String csn = StandardCharsets.UTF_8.name();57Charset charset = StandardCharsets.UTF_8;58File file1 = new File(USER_DIR, "ScannerCharsetTest1.txt");59File file2 = new File(USER_DIR, "ScannerCharsetTest2.txt");6061return new Object[][]{62{ConstructorType.FILE, file1, file2, csn, charset},63{ConstructorType.PATH, file1, file2, csn, charset},64{ConstructorType.INPUTSTREAM, file1, file2, csn, charset},65{ConstructorType.READABLEBYTECHANNEL, file1, file2, csn, charset},};66}6768/**69* Verifies that the overloading constructor behaves the same as the70* existing one.71*72* @param type the type of the constructor73* @param file1 file1 written with the name of a charset74* @param file2 file2 written with a charset75* @param csn the charset name76* @param charset the charset77* @throws IOException78*/79@Test(dataProvider = "parameters")80void test(ConstructorType type, File file1, File file2, String csn, Charset charset)81throws Exception {82prepareFile(file1, TEST_STRING);83prepareFile(file2, TEST_STRING);8485try (Scanner s1 = getScanner(type, file1.getPath(), csn, null);86Scanner s2 = getScanner(type, file2.getPath(), null, charset);) {87String result1 = s1.findInLine(TEST_STRING);88String result2 = s2.findInLine(TEST_STRING);89Assert.assertEquals(result1, result2);90}91}9293/*94* Creates a Scanner over the given input file.95*/96Scanner getScanner(ConstructorType type, String file, String csn, Charset charset)97throws Exception {98if (csn != null) {99switch (type) {100case FILE:101return new Scanner(new File(file), csn);102case PATH:103return new Scanner(Paths.get(file), csn);104case INPUTSTREAM:105FileInputStream fis = new FileInputStream(file);106return new Scanner(fis, csn);107case READABLEBYTECHANNEL:108FileInputStream fis1 = new FileInputStream(file);109return new Scanner(fis1.getChannel(), csn);110}111} else {112switch (type) {113case FILE:114return new Scanner(new File(file), charset);115case PATH:116return new Scanner(Paths.get(file), charset);117case INPUTSTREAM:118FileInputStream fis = new FileInputStream(file);119return new Scanner(fis, charset);120case READABLEBYTECHANNEL:121FileInputStream fis1 = new FileInputStream(file);122return new Scanner(fis1.getChannel(), charset);123}124}125126return null;127}128129void prepareFile(File file, String content) throws IOException {130try (FileWriter writer = new FileWriter(file);) {131writer.write(content);132}133}134}135136137