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.Properties;
25 
26 import java.io.ByteArrayInputStream;
27 import java.io.ByteArrayOutputStream;
28 import java.io.IOException;
29 import java.nio.charset.Charset;
30 import java.nio.charset.StandardCharsets;
31 import java.util.Properties;
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     @DataProvider(name = "parameters")
getParameters()45     public Object[][] getParameters() throws IOException {
46         return new Object[][]{
47             {StandardCharsets.UTF_8.name(), null},
48             {null, StandardCharsets.UTF_8},};
49     }
50 
51     /**
52      * Tests that properties saved with Properties#storeToXML with either an
53      * encoding name or a charset can be read with Properties#loadFromXML that
54      * returns the same Properties object.
55      */
56     @Test(dataProvider = "parameters")
57     // Android-changed: made public, otherwise it is not picked up. b/289181741?
58     // void testLoadAndStore(String encoding, Charset charset) throws IOException {
testLoadAndStore(String encoding, Charset charset)59     public void testLoadAndStore(String encoding, Charset charset) throws IOException {
60         Properties props = new Properties();
61         props.put("k0", "\u6C34");
62         props.put("k1", "foo");
63         props.put("k2", "bar");
64         props.put("k3", "\u0020\u0391\u0392\u0393\u0394\u0395\u0396\u0397");
65         props.put("k4", "\u7532\u9aa8\u6587");
66         props.put("k5", "<java.home>/conf/jaxp.properties");
67         props.put("k6", "\ud800\u00fa");
68 
69         Properties p;
70         try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
71             if (encoding != null) {
72                 props.storeToXML(out, null, encoding);
73             } else {
74                 props.storeToXML(out, null, charset);
75             }   p = new Properties();
76             try (ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())) {
77                 p.loadFromXML(in);
78             }
79         }
80 
81         Assert.assertEquals(props, p);
82     }
83 }
84