test/jdk/java/io/FileReader/ConstructorTest.java
author tschatzl
Wed, 24 Jul 2019 11:49:39 +0200
changeset 57508 28ab01c06755
parent 49254 422615764e12
permissions -rw-r--r--
8228388: Add information about dirty/skipped card for Merge HCC in G1 log Summary: Collect and print informatio about the number of processed cards during the Merge HCC phase to improve log output. Reviewed-by: kbarrett, sangheki
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49254
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
     1
/*
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
     4
 *
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
     7
 * published by the Free Software Foundation.
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
     8
 *
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    13
 * accompanied this code).
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    14
 *
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    18
 *
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    21
 * questions.
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    22
 */
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    23
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    24
import java.io.File;
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    25
import java.io.FileInputStream;
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    26
import java.io.FileReader;
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    27
import java.io.FileWriter;
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    28
import java.io.IOException;
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    29
import java.io.InputStreamReader;
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    30
import java.io.Reader;
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    31
import java.nio.charset.Charset;
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    32
import java.nio.charset.StandardCharsets;
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    33
import org.testng.Assert;
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    34
import org.testng.annotations.DataProvider;
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    35
import org.testng.annotations.Test;
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    36
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    37
/**
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    38
 * @test
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    39
 * @bug 8183554
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    40
 * @summary Test to verify the new Constructors that take a Charset.
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    41
 * @run testng ConstructorTest
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    42
 */
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    43
public class ConstructorTest {
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    44
    static String USER_DIR = System.getProperty("user.dir", ".");
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    45
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    46
    public static enum ConstructorType {
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    47
        STRING,
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    48
        FILE
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    49
    }
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    50
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    51
    static final String TEST_STRING = "abc \u0100 \u0101 \u0555 \u07FD \u07FF";
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    52
    static final int BUFFER_SIZE = 8192;
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    53
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    54
    @DataProvider(name = "parameters")
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    55
    public Object[][] getParameters() throws IOException {
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    56
        File file1 = new File(USER_DIR, "FileReaderTest1.txt");
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    57
        File file2 = new File(USER_DIR, "FileReaderTest2.txt");
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    58
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    59
        return new Object[][]{
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    60
            {ConstructorType.STRING, file1, file2, StandardCharsets.UTF_8},
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    61
            {ConstructorType.FILE, file1, file2, StandardCharsets.UTF_8},
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    62
            {ConstructorType.STRING, file1, file2, StandardCharsets.ISO_8859_1},
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    63
            {ConstructorType.FILE, file1, file2, StandardCharsets.ISO_8859_1},
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    64
        };
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    65
    }
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    66
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    67
    /**
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    68
     * Verifies that the new constructors that take a Charset function the same
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    69
     * as an InputStreamReader on a FileInputStream as was recommended before
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    70
     * this change.
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    71
     *
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    72
     * @param type the type of the constructor
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    73
     * @param file1 file1 to be read with a FileReader
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    74
     * @param file2 file2 to be read with an InputStreamReader
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    75
     * @param charset the charset
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    76
     * @throws IOException
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    77
     */
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    78
    @Test(dataProvider = "parameters")
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    79
    void test(ConstructorType type, File file1, File file2, Charset charset)
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    80
            throws Exception {
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    81
        prepareFile(file1, TEST_STRING, charset);
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    82
        prepareFile(file2, TEST_STRING, charset);
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    83
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    84
        try (FileReader fr = getFileReader(type, file1, charset);
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    85
                FileInputStream is = new FileInputStream(file2);
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    86
                InputStreamReader isr = new InputStreamReader(is, charset);) {
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    87
            String result1 = readAll(fr, BUFFER_SIZE);
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    88
            String result2 = readAll(isr, BUFFER_SIZE);
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    89
            Assert.assertEquals(result1, result2);
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    90
        }
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    91
    }
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    92
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    93
    public String readAll(Reader reader, int bufferSize) throws IOException {
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    94
        StringBuilder sb = new StringBuilder();
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    95
        char[] buf = new char[bufferSize];
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    96
        int numRead;
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    97
        while ((numRead = reader.read(buf)) != -1) {
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    98
            if (numRead == buf.length) {
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
    99
                sb.append(buf);
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   100
            } else {
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   101
                sb.append(String.valueOf(buf, 0, numRead));
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   102
            }
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   103
        }
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   104
        return sb.toString();
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   105
    }
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   106
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   107
    /*
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   108
     * Creates a FileReader over the given input file.
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   109
     */
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   110
    FileReader getFileReader(ConstructorType type, File file, Charset charset)
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   111
            throws IOException {
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   112
        switch (type) {
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   113
            case STRING:
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   114
                return new FileReader(file.getPath(), charset);
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   115
            case FILE:
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   116
                return new FileReader(file, charset);
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   117
        }
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   118
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   119
        return null;
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   120
    }
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   121
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   122
    void prepareFile(File file, String content, Charset charset) throws IOException {
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   123
        try (FileWriter writer = new FileWriter(file, charset);) {
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   124
            writer.write(content);
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   125
        }
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   126
    }
422615764e12 8183554: Add constructors with Charset parameter for FileReader and FileWriter
joehw
parents:
diff changeset
   127
}