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