Path: blob/master/test/jdk/java/util/Properties/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.ByteArrayInputStream;24import java.io.ByteArrayOutputStream;25import java.io.IOException;26import java.nio.charset.Charset;27import java.nio.charset.StandardCharsets;28import java.util.Properties;29import org.testng.Assert;30import org.testng.annotations.DataProvider;31import org.testng.annotations.Test;3233/**34* @test35* @bug 818374336* @summary Test to verify the new overload method with Charset functions the37* same as the existing method that takes a charset name.38* @run testng EncodingTest39*/40public class EncodingTest {41@DataProvider(name = "parameters")42public Object[][] getParameters() throws IOException {43return new Object[][]{44{StandardCharsets.UTF_8.name(), null},45{null, StandardCharsets.UTF_8},};46}4748/**49* Tests that properties saved with Properties#storeToXML with either an50* encoding name or a charset can be read with Properties#loadFromXML that51* returns the same Properties object.52*/53@Test(dataProvider = "parameters")54void testLoadAndStore(String encoding, Charset charset) throws IOException {55Properties props = new Properties();56props.put("k0", "\u6C34");57props.put("k1", "foo");58props.put("k2", "bar");59props.put("k3", "\u0020\u0391\u0392\u0393\u0394\u0395\u0396\u0397");60props.put("k4", "\u7532\u9aa8\u6587");61props.put("k5", "<java.home>/conf/jaxp.properties");62props.put("k6", "\ud800\u00fa");6364Properties p;65try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {66if (encoding != null) {67props.storeToXML(out, null, encoding);68} else {69props.storeToXML(out, null, charset);70} p = new Properties();71try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {72p.loadFromXML(in);73}74}7576Assert.assertEquals(props, p);77}78}798081