test/jdk/java/io/FileWriter/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.FileOutputStream;
       
    27 import java.io.FileReader;
       
    28 import java.io.FileWriter;
       
    29 import java.io.IOException;
       
    30 import java.io.OutputStreamWriter;
       
    31 import java.io.Reader;
       
    32 import java.nio.charset.Charset;
       
    33 import java.nio.charset.StandardCharsets;
       
    34 import org.testng.Assert;
       
    35 import org.testng.annotations.DataProvider;
       
    36 import org.testng.annotations.Test;
       
    37 
       
    38 /**
       
    39  * @test
       
    40  * @bug 8183554
       
    41  * @summary Test to verify the new Constructors that take a Charset.
       
    42  * @run testng ConstructorTest
       
    43  */
       
    44 public class ConstructorTest {
       
    45     static String USER_DIR = System.getProperty("user.dir", ".");
       
    46 
       
    47     public static enum ConstructorType {
       
    48         STRING,
       
    49         FILE,
       
    50         STRING_APPEND,
       
    51         FILE_APPEND
       
    52     }
       
    53 
       
    54     static final String TEST_STRING = "abc \u0100 \u0101 \u0555 \u07FD \u07FF";
       
    55     static final int BUFFER_SIZE = 8192;
       
    56 
       
    57     @DataProvider(name = "parameters")
       
    58     public Object[][] getParameters() throws IOException {
       
    59         File file1 = new File(USER_DIR, "FileWriterTest1.txt");
       
    60         File file2 = new File(USER_DIR, "FileWriterTest2.txt");
       
    61 
       
    62         return new Object[][]{
       
    63             {ConstructorType.STRING, file1, file2, StandardCharsets.UTF_8},
       
    64             {ConstructorType.FILE, file1, file2, StandardCharsets.UTF_8},
       
    65             {ConstructorType.STRING_APPEND, file1, file2, StandardCharsets.UTF_8},
       
    66             {ConstructorType.FILE_APPEND, file1, file2, StandardCharsets.UTF_8},
       
    67             {ConstructorType.STRING, file1, file2, StandardCharsets.ISO_8859_1},
       
    68             {ConstructorType.FILE, file1, file2, StandardCharsets.ISO_8859_1},
       
    69             {ConstructorType.STRING_APPEND, file1, file2, StandardCharsets.ISO_8859_1},
       
    70             {ConstructorType.FILE_APPEND, file1, file2, StandardCharsets.ISO_8859_1},
       
    71         };
       
    72     }
       
    73 
       
    74     /**
       
    75      * Verifies that the new constructors that take a Charset function the same
       
    76      * as an OutputStreamWriter on a FileOutputStream as was recommended before
       
    77      * this change.
       
    78      *
       
    79      * @param type the type of the constructor
       
    80      * @param file1 file1 to be written with a FileWriter
       
    81      * @param file2 file2 to be written  with an OutputStreamWriter
       
    82      * @param charset the charset
       
    83      * @throws IOException
       
    84      */
       
    85     @Test(dataProvider = "parameters")
       
    86     void test(ConstructorType type, File file1, File file2, Charset charset)
       
    87             throws Exception {
       
    88         writeWithFileWriter(type, file1, TEST_STRING, charset);
       
    89         writeWithOutputStreamWriter(type, file2, TEST_STRING, charset);
       
    90 
       
    91         try (
       
    92                 FileReader r1 = getFileReader(type, file1, charset);
       
    93                 FileReader r2 = getFileReader(type, file2, charset);
       
    94             ) {
       
    95             String result1 = readAll(r1, BUFFER_SIZE);
       
    96             String result2 = readAll(r2, BUFFER_SIZE);
       
    97             Assert.assertEquals(result1, result2);
       
    98         }
       
    99     }
       
   100 
       
   101     public String readAll(Reader reader, int bufferSize) throws IOException {
       
   102         StringBuilder sb = new StringBuilder();
       
   103         char[] buf = new char[bufferSize];
       
   104         int numRead;
       
   105         while ((numRead = reader.read(buf)) != -1) {
       
   106             if (numRead == buf.length) {
       
   107                 sb.append(buf);
       
   108             } else {
       
   109                 sb.append(String.valueOf(buf, 0, numRead));
       
   110             }
       
   111         }
       
   112         return sb.toString();
       
   113     }
       
   114 
       
   115     /*
       
   116      * Creates a FileReader over the given input file.
       
   117      */
       
   118     FileReader getFileReader(ConstructorType type, File file, Charset charset)
       
   119             throws IOException {
       
   120         switch (type) {
       
   121             case STRING:
       
   122             case STRING_APPEND:
       
   123                 return new FileReader(file.getPath(), charset);
       
   124             case FILE:
       
   125             case FILE_APPEND:
       
   126                 return new FileReader(file, charset);
       
   127         }
       
   128 
       
   129         return null;
       
   130     }
       
   131 
       
   132     /*
       
   133      * Creates a FileWriter using the constructor as specified.
       
   134      */
       
   135     FileWriter getFileWriter(ConstructorType type, File file, Charset charset)
       
   136             throws IOException {
       
   137         switch (type) {
       
   138             case STRING:
       
   139                 return new FileWriter(file.getPath(), charset);
       
   140             case FILE:
       
   141                 return new FileWriter(file, charset);
       
   142             case STRING_APPEND:
       
   143                 return new FileWriter(file.getPath(), charset, true);
       
   144             case FILE_APPEND:
       
   145                 return new FileWriter(file, charset, true);
       
   146         }
       
   147 
       
   148         return null;
       
   149     }
       
   150 
       
   151     void writeWithFileWriter(ConstructorType type, File file, String content, Charset charset)
       
   152             throws IOException {
       
   153         if (type == ConstructorType.STRING_APPEND || type == ConstructorType.FILE_APPEND) {
       
   154             try (FileWriter writer = getFileWriter(ConstructorType.FILE, file, charset);) {
       
   155                 writer.write(content);
       
   156             }
       
   157         }
       
   158         try (FileWriter writer = getFileWriter(type, file, charset);) {
       
   159             writer.write(content);
       
   160         }
       
   161     }
       
   162 
       
   163     void writeWithOutputStreamWriter(ConstructorType type, File file, String content, Charset charset)
       
   164             throws IOException {
       
   165         try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), charset)) {
       
   166             writer.write(content);
       
   167             if (type == ConstructorType.STRING_APPEND || type == ConstructorType.FILE_APPEND) {
       
   168                 writer.write(content);
       
   169             }
       
   170         }
       
   171     }
       
   172 }