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.nio.charset.Charset;
  28 import java.nio.charset.StandardCharsets;
  29 import java.nio.file.Files;
  30 import java.nio.file.Paths;
  31 import java.util.Formatter;
  32 import java.util.Locale;
  33 import org.testng.Assert;
  34 import org.testng.annotations.DataProvider;
  35 import org.testng.annotations.Test;
  36 
  37 /**
  38  * @test
  39  * @bug 8183743
  40  * @summary Test to verify the new overload method with Charset functions the same
  41  * as the existing method that takes a charset name.
  42  * @run testng EncodingTest
  43  */
  44 public class EncodingTest {
  45     static String USER_DIR = System.getProperty("user.dir", ".");
  46 
  47     // Charset added only for the 3-parameter constructors
  48     public static enum ConstructorType {
  49         STRING3,
  50         FILE3,
  51         OUTPUTSTREAM3
  52     }
  53 
  54     @DataProvider(name = "parameters")
  55     public Object[][] getParameters() throws IOException {
  56         Locale l = Locale.getDefault();
  57         String csn1 = StandardCharsets.ISO_8859_1.name();
  58         Charset charset1 = StandardCharsets.ISO_8859_1;
  59         String csn2 = StandardCharsets.UTF_8.name();
  60         Charset charset2 = StandardCharsets.UTF_8;
  61 
  62         File file1 = new File(USER_DIR, "FormatterCharsetTest1.txt");
  63         File file2 = new File(USER_DIR, "FormatterCharsetTest2.txt");
  64 
  65         return new Object[][]{
  66             {ConstructorType.STRING3, file1, file2, csn1, charset1},
  67             {ConstructorType.FILE3, file1, file2, csn1, charset1},
  68             {ConstructorType.OUTPUTSTREAM3, file1, file2, csn1, charset1},
  69             {ConstructorType.STRING3, file1, file2, csn2, charset2},
  70             {ConstructorType.FILE3, file1, file2, csn2, charset2},
  71             {ConstructorType.OUTPUTSTREAM3, file1, file2, csn2, charset2},
  72         };
  73     }
  74 
  75     /**
  76      * Verifies that the overloading constructor behaves the same as the existing
  77      * one.
  78      * @param type the type of the constructor
  79      * @param file1 file1 written with the name of a charset
  80      * @param file2 file2 written with a charset
  81      * @param csn the charset name
  82      * @param charset the charset
  83      * @throws IOException
  84      */
  85     @Test(dataProvider = "parameters")
  86     public void testConstructor(ConstructorType type, File file1, File file2,
  87             String csn, Charset charset) throws Exception {
  88         format(getFormatter(type, file1.getPath(), csn, null));
  89         format(getFormatter(type, file2.getPath(), null, charset));
  90         Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset),
  91                 Files.readAllLines(Paths.get(file2.getPath()), charset));
  92     }
  93 
  94 
  95     /**
  96      * Verifies that the overloading constructor behaves the same as the existing
  97      * one.
  98      * @param type the type of the constructor
  99      * @param file1 file1 written with the name of a charset
 100      * @param file2 file2 written with a charset
 101      * @param csn the charset name
 102      * @param charset the charset
 103      * @throws IOException
 104      */
 105     @Test(dataProvider = "parameters")
 106     public void testConstructorIntrinsic(ConstructorType type, File file1, File file2,
 107                                 String csn, Charset charset) throws Exception {
 108         formatIntrinsic(getFormatter(type, file1.getPath(), csn, null));
 109         formatIntrinsic(getFormatter(type, file2.getPath(), null, charset));
 110         Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset),
 111                 Files.readAllLines(Paths.get(file2.getPath()), charset));
 112     }
 113 
 114     void format(Formatter formatter)
 115             throws IOException {
 116         formatter.format("abcde \u00FA\u00FB\u00FC\u00FD");
 117         formatter.format("Java \uff08\u8ba1\u7b97\u673a\u7f16\u7a0b\u8bed\u8a00\uff09");
 118         formatter.flush();
 119         formatter.close();
 120     }
 121 
 122     void formatIntrinsic(Formatter formatter)
 123             throws IOException {
 124         JavacIntrinsicsSupport.formatterFormat(formatter, "abcde \u00FA\u00FB\u00FC\u00FD");
 125         JavacIntrinsicsSupport.formatterFormat(formatter, "Java \uff08\u8ba1\u7b97\u673a\u7f16\u7a0b\u8bed\u8a00\uff09");
 126         formatter.flush();
 127         formatter.close();
 128     }
 129 
 130 
 131     Formatter getFormatter(ConstructorType type, String path, String csn, Charset charset)
 132             throws IOException {
 133         Formatter formatter = null;
 134         if (csn != null) {
 135             switch (type) {
 136                 case STRING3:
 137                     formatter = new Formatter(path, csn, Locale.getDefault());
 138                     break;
 139                 case FILE3:
 140                     formatter = new Formatter(new File(path), csn, Locale.getDefault());
 141                     break;
 142                 case OUTPUTSTREAM3:
 143                     formatter = new Formatter(new FileOutputStream(path), csn, Locale.getDefault());
 144                     break;
 145             }
 146         } else {
 147             switch (type) {
 148                 case STRING3:
 149                     formatter = new Formatter(path, charset, Locale.getDefault());
 150                     break;
 151                 case FILE3:
 152                     formatter = new Formatter(new File(path), charset, Locale.getDefault());
 153                     break;
 154                 case OUTPUTSTREAM3:
 155                     formatter = new Formatter(new FileOutputStream(path), charset, Locale.getDefault());
 156                     break;
 157             }
 158         }
 159         return formatter;
 160     }
 161 }