test/jdk/java/io/FileReader/ConstructorTest.java
changeset 49254 422615764e12
equal deleted inserted replaced
49253:26f624b33218 49254:422615764e12
       
     1 /*
       
     2  * Copyright (c) 2018, 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.FileInputStream;
       
    26 import java.io.FileReader;
       
    27 import java.io.FileWriter;
       
    28 import java.io.IOException;
       
    29 import java.io.InputStreamReader;
       
    30 import java.io.Reader;
       
    31 import java.nio.charset.Charset;
       
    32 import java.nio.charset.StandardCharsets;
       
    33 import org.testng.Assert;
       
    34 import org.testng.annotations.DataProvider;
       
    35 import org.testng.annotations.Test;
       
    36 
       
    37 /**
       
    38  * @test
       
    39  * @bug 8183554
       
    40  * @summary Test to verify the new Constructors that take a Charset.
       
    41  * @run testng ConstructorTest
       
    42  */
       
    43 public class ConstructorTest {
       
    44     static String USER_DIR = System.getProperty("user.dir", ".");
       
    45 
       
    46     public static enum ConstructorType {
       
    47         STRING,
       
    48         FILE
       
    49     }
       
    50 
       
    51     static final String TEST_STRING = "abc \u0100 \u0101 \u0555 \u07FD \u07FF";
       
    52     static final int BUFFER_SIZE = 8192;
       
    53 
       
    54     @DataProvider(name = "parameters")
       
    55     public Object[][] getParameters() throws IOException {
       
    56         File file1 = new File(USER_DIR, "FileReaderTest1.txt");
       
    57         File file2 = new File(USER_DIR, "FileReaderTest2.txt");
       
    58 
       
    59         return new Object[][]{
       
    60             {ConstructorType.STRING, file1, file2, StandardCharsets.UTF_8},
       
    61             {ConstructorType.FILE, file1, file2, StandardCharsets.UTF_8},
       
    62             {ConstructorType.STRING, file1, file2, StandardCharsets.ISO_8859_1},
       
    63             {ConstructorType.FILE, file1, file2, StandardCharsets.ISO_8859_1},
       
    64         };
       
    65     }
       
    66 
       
    67     /**
       
    68      * Verifies that the new constructors that take a Charset function the same
       
    69      * as an InputStreamReader on a FileInputStream as was recommended before
       
    70      * this change.
       
    71      *
       
    72      * @param type the type of the constructor
       
    73      * @param file1 file1 to be read with a FileReader
       
    74      * @param file2 file2 to be read with an InputStreamReader
       
    75      * @param charset the charset
       
    76      * @throws IOException
       
    77      */
       
    78     @Test(dataProvider = "parameters")
       
    79     void test(ConstructorType type, File file1, File file2, Charset charset)
       
    80             throws Exception {
       
    81         prepareFile(file1, TEST_STRING, charset);
       
    82         prepareFile(file2, TEST_STRING, charset);
       
    83 
       
    84         try (FileReader fr = getFileReader(type, file1, charset);
       
    85                 FileInputStream is = new FileInputStream(file2);
       
    86                 InputStreamReader isr = new InputStreamReader(is, charset);) {
       
    87             String result1 = readAll(fr, BUFFER_SIZE);
       
    88             String result2 = readAll(isr, BUFFER_SIZE);
       
    89             Assert.assertEquals(result1, result2);
       
    90         }
       
    91     }
       
    92 
       
    93     public String readAll(Reader reader, int bufferSize) throws IOException {
       
    94         StringBuilder sb = new StringBuilder();
       
    95         char[] buf = new char[bufferSize];
       
    96         int numRead;
       
    97         while ((numRead = reader.read(buf)) != -1) {
       
    98             if (numRead == buf.length) {
       
    99                 sb.append(buf);
       
   100             } else {
       
   101                 sb.append(String.valueOf(buf, 0, numRead));
       
   102             }
       
   103         }
       
   104         return sb.toString();
       
   105     }
       
   106 
       
   107     /*
       
   108      * Creates a FileReader over the given input file.
       
   109      */
       
   110     FileReader getFileReader(ConstructorType type, File file, Charset charset)
       
   111             throws IOException {
       
   112         switch (type) {
       
   113             case STRING:
       
   114                 return new FileReader(file.getPath(), charset);
       
   115             case FILE:
       
   116                 return new FileReader(file, charset);
       
   117         }
       
   118 
       
   119         return null;
       
   120     }
       
   121 
       
   122     void prepareFile(File file, String content, Charset charset) throws IOException {
       
   123         try (FileWriter writer = new FileWriter(file, charset);) {
       
   124             writer.write(content);
       
   125         }
       
   126     }
       
   127 }