Path: blob/master/test/jdk/java/util/Formatter/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.FileOutputStream;25import java.io.IOException;26import java.nio.charset.Charset;27import java.nio.charset.StandardCharsets;28import java.nio.file.Files;29import java.nio.file.Paths;30import java.util.Formatter;31import java.util.Locale;32import org.testng.Assert;33import org.testng.annotations.DataProvider;34import org.testng.annotations.Test;3536/**37* @test38* @bug 818374339* @summary Test to verify the new overload method with Charset functions the same40* as the existing method that takes a charset name.41* @run testng EncodingTest42*/43public class EncodingTest {44static String USER_DIR = System.getProperty("user.dir", ".");4546// Charset added only for the 3-parameter constructors47public static enum ConstructorType {48STRING3,49FILE3,50OUTPUTSTREAM351}5253@DataProvider(name = "parameters")54public Object[][] getParameters() throws IOException {55Locale l = Locale.getDefault();56String csn1 = StandardCharsets.ISO_8859_1.name();57Charset charset1 = StandardCharsets.ISO_8859_1;58String csn2 = StandardCharsets.UTF_8.name();59Charset charset2 = StandardCharsets.UTF_8;6061File file1 = new File(USER_DIR, "FormatterCharsetTest1.txt");62File file2 = new File(USER_DIR, "FormatterCharsetTest2.txt");6364return new Object[][]{65{ConstructorType.STRING3, file1, file2, csn1, charset1},66{ConstructorType.FILE3, file1, file2, csn1, charset1},67{ConstructorType.OUTPUTSTREAM3, file1, file2, csn1, charset1},68{ConstructorType.STRING3, file1, file2, csn2, charset2},69{ConstructorType.FILE3, file1, file2, csn2, charset2},70{ConstructorType.OUTPUTSTREAM3, file1, file2, csn2, charset2},71};72}7374/**75* Verifies that the overloading constructor behaves the same as the existing76* one.77* @param type the type of the constructor78* @param file1 file1 written with the name of a charset79* @param file2 file2 written with a charset80* @param csn the charset name81* @param charset the charset82* @throws IOException83*/84@Test(dataProvider = "parameters")85public void testConstructor(ConstructorType type, File file1, File file2,86String csn, Charset charset) throws Exception {87format(getFormatter(type, file1.getPath(), csn, null));88format(getFormatter(type, file2.getPath(), null, charset));89Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset),90Files.readAllLines(Paths.get(file2.getPath()), charset));91}9293void format(Formatter formatter)94throws IOException {95formatter.format("abcde \u00FA\u00FB\u00FC\u00FD");96formatter.format("Java \uff08\u8ba1\u7b97\u673a\u7f16\u7a0b\u8bed\u8a00\uff09");97formatter.flush();98formatter.close();99}100101102Formatter getFormatter(ConstructorType type, String path, String csn, Charset charset)103throws IOException {104Formatter formatter = null;105if (csn != null) {106switch (type) {107case STRING3:108formatter = new Formatter(path, csn, Locale.getDefault());109break;110case FILE3:111formatter = new Formatter(new File(path), csn, Locale.getDefault());112break;113case OUTPUTSTREAM3:114formatter = new Formatter(new FileOutputStream(path), csn, Locale.getDefault());115break;116}117} else {118switch (type) {119case STRING3:120formatter = new Formatter(path, charset, Locale.getDefault());121break;122case FILE3:123formatter = new Formatter(new File(path), charset, Locale.getDefault());124break;125case OUTPUTSTREAM3:126formatter = new Formatter(new FileOutputStream(path), charset, Locale.getDefault());127break;128}129}130return formatter;131}132}133134135