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.File;
  25 import java.io.FileOutputStream;
  26 import java.io.IOException;
  27 import java.io.PrintWriter;
  28 import java.nio.charset.Charset;
  29 import java.nio.charset.StandardCharsets;
  30 import java.nio.file.Files;
  31 import java.nio.file.Paths;
  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 same
  40  * as the existing method that takes a charset name.
  41  * @run testng EncodingTest
  42  */
  43 public class EncodingTest {
  44     static String USER_DIR = System.getProperty("user.dir", ".");
  45     static boolean AUTOFLUSH = true;
  46     public static enum ConstructorType {
  47         STRING,
  48         FILE,
  49         OUTPUTSTREAM
  50     }
  51 
  52     /*
  53      * DataProvider fields:
  54      * Type of the constructor, a file to be written with a charset name,
  55      * a file to be written with a charset, charset name, charset.
  56      */
  57     @DataProvider(name = "parameters")
  58     public Object[][] getParameters() throws IOException {
  59         String csn = StandardCharsets.UTF_8.name();
  60         Charset charset = StandardCharsets.UTF_8;
  61         File file1 = new File(USER_DIR, "PWCharsetTest1.txt");
  62         File file2 = new File(USER_DIR, "PWCharsetTest2.txt");
  63 
  64         return new Object[][]{
  65             {ConstructorType.STRING, file1, file2, csn, charset},
  66             {ConstructorType.FILE, file1, file2, csn, charset},
  67             {ConstructorType.OUTPUTSTREAM, file1, file2, csn, charset}
  68         };
  69     }
  70 
  71     /**
  72      * Verifies that the overloading constructor behaves the same as the existing
  73      * one.
  74      *
  75      * @param type the type of the constructor
  76      * @param file1 file1 written with the name of a charset
  77      * @param file2 file2 written with a charset
  78      * @param csn the charset name
  79      * @param charset the charset
  80      * @throws IOException
  81      */
  82     @Test(dataProvider = "parameters")
  83     public void test(ConstructorType type, File file1, File file2, String csn, Charset charset)
  84             throws Exception {
  85         createFile(getWriter(type, file1.getPath(), csn, null));
  86         createFile(getWriter(type, file2.getPath(), null, charset));
  87 
  88         Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset),
  89                 Files.readAllLines(Paths.get(file2.getPath()), charset));
  90     }
  91 
  92     void createFile(PrintWriter out) throws IOException {
  93         out.println("high surrogate");
  94         out.println(Character.MIN_HIGH_SURROGATE);
  95         out.println("low surrogate");
  96         out.println(Character.MIN_LOW_SURROGATE);
  97         out.flush();
  98         out.close();
  99     }
 100 
 101     PrintWriter getWriter(ConstructorType type, String path, String csn, Charset charset)
 102             throws IOException {
 103         PrintWriter out = null;
 104         if (csn != null) {
 105             switch (type) {
 106                 case STRING:
 107                     out = new PrintWriter(path, csn);
 108                     break;
 109                 case FILE:
 110                     out = new PrintWriter(new File(path), csn);
 111                     break;
 112                 case OUTPUTSTREAM:
 113                     // No corresponding method with charset name
 114                     // compare with PrintWriter(path, csn) instead
 115                     out = new PrintWriter(path, csn);
 116                     break;
 117             }
 118         } else {
 119             switch (type) {
 120                 case STRING:
 121                     out = new PrintWriter(path, charset);
 122                     break;
 123                 case FILE:
 124                     out = new PrintWriter(new File(path), charset);
 125                     break;
 126                 case OUTPUTSTREAM:
 127                     FileOutputStream fout = new FileOutputStream(path);
 128                     out = new PrintWriter(fout, AUTOFLUSH, charset);
 129                     break;
 130             }
 131         }
 132 
 133         return out;
 134     }
 135 }