test/jdk/java/util/Formatter/EncodingTest.java
author joehw
Tue, 12 Dec 2017 11:10:12 -0800
changeset 48252 77b88d8f8380
permissions -rw-r--r--
8183743: Umbrella: add overloads that take a Charset parameter Reviewed-by: alanb, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48252
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
     1
/*
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
     4
 *
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
     7
 * published by the Free Software Foundation.
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
     8
 *
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    13
 * accompanied this code).
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    14
 *
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    18
 *
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    21
 * questions.
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    22
 */
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    23
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    24
import java.io.File;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    25
import java.io.FileOutputStream;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    26
import java.io.IOException;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    27
import java.nio.charset.Charset;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    28
import java.nio.charset.StandardCharsets;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    29
import java.nio.file.Files;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    30
import java.nio.file.Paths;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    31
import java.util.Formatter;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    32
import java.util.Locale;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    33
import org.testng.Assert;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    34
import org.testng.annotations.DataProvider;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    35
import org.testng.annotations.Test;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    36
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    37
/**
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    38
 * @test
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    39
 * @bug 8183743
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    40
 * @summary Test to verify the new overload method with Charset functions the same
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    41
 * as the existing method that takes a charset name.
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    42
 * @run testng EncodingTest
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    43
 */
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    44
public class EncodingTest {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    45
    static String USER_DIR = System.getProperty("user.dir", ".");
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    46
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    47
    // Charset added only for the 3-parameter constructors
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    48
    public static enum ConstructorType {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    49
        STRING3,
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    50
        FILE3,
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    51
        OUTPUTSTREAM3
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    52
    }
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    53
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    54
    @DataProvider(name = "parameters")
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    55
    public Object[][] getParameters() throws IOException {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    56
        Locale l = Locale.getDefault();
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    57
        String csn1 = StandardCharsets.ISO_8859_1.name();
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    58
        Charset charset1 = StandardCharsets.ISO_8859_1;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    59
        String csn2 = StandardCharsets.UTF_8.name();
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    60
        Charset charset2 = StandardCharsets.UTF_8;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    61
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    62
        File file1 = new File(USER_DIR, "FormatterCharsetTest1.txt");
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    63
        File file2 = new File(USER_DIR, "FormatterCharsetTest2.txt");
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    64
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    65
        return new Object[][]{
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    66
            {ConstructorType.STRING3, file1, file2, csn1, charset1},
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    67
            {ConstructorType.FILE3, file1, file2, csn1, charset1},
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    68
            {ConstructorType.OUTPUTSTREAM3, file1, file2, csn1, charset1},
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    69
            {ConstructorType.STRING3, file1, file2, csn2, charset2},
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    70
            {ConstructorType.FILE3, file1, file2, csn2, charset2},
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    71
            {ConstructorType.OUTPUTSTREAM3, file1, file2, csn2, charset2},
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    72
        };
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    73
    }
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    74
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    75
    /**
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    76
     * Verifies that the overloading constructor behaves the same as the existing
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    77
     * one.
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    78
     * @param type the type of the constructor
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    79
     * @param file1 file1 written with the name of a charset
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    80
     * @param file2 file2 written with a charset
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    81
     * @param csn the charset name
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    82
     * @param charset the charset
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    83
     * @throws IOException
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    84
     */
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    85
    @Test(dataProvider = "parameters")
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    86
    public void testConstructor(ConstructorType type, File file1, File file2,
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    87
            String csn, Charset charset) throws Exception {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    88
        format(getFormatter(type, file1.getPath(), csn, null));
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    89
        format(getFormatter(type, file2.getPath(), null, charset));
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    90
        Assert.assertEquals(Files.readAllLines(Paths.get(file1.getPath()), charset),
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    91
                Files.readAllLines(Paths.get(file2.getPath()), charset));
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    92
    }
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    93
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    94
    void format(Formatter formatter)
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    95
            throws IOException {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    96
        formatter.format("abcde \u00FA\u00FB\u00FC\u00FD");
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    97
        formatter.format("Java \uff08\u8ba1\u7b97\u673a\u7f16\u7a0b\u8bed\u8a00\uff09");
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    98
        formatter.flush();
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
    99
        formatter.close();
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   100
    }
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   101
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   102
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   103
    Formatter getFormatter(ConstructorType type, String path, String csn, Charset charset)
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   104
            throws IOException {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   105
        Formatter formatter = null;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   106
        if (csn != null) {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   107
            switch (type) {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   108
                case STRING3:
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   109
                    formatter = new Formatter(path, csn, Locale.getDefault());
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   110
                    break;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   111
                case FILE3:
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   112
                    formatter = new Formatter(new File(path), csn, Locale.getDefault());
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   113
                    break;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   114
                case OUTPUTSTREAM3:
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   115
                    formatter = new Formatter(new FileOutputStream(path), csn, Locale.getDefault());
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   116
                    break;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   117
            }
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   118
        } else {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   119
            switch (type) {
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   120
                case STRING3:
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   121
                    formatter = new Formatter(path, charset, Locale.getDefault());
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   122
                    break;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   123
                case FILE3:
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   124
                    formatter = new Formatter(new File(path), charset, Locale.getDefault());
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   125
                    break;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   126
                case OUTPUTSTREAM3:
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   127
                    formatter = new Formatter(new FileOutputStream(path), charset, Locale.getDefault());
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   128
                    break;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   129
            }
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   130
        }
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   131
        return formatter;
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   132
    }
77b88d8f8380 8183743: Umbrella: add overloads that take a Charset parameter
joehw
parents:
diff changeset
   133
}