test/jdk/sun/nio/cs/EUCTWBufferBoundaryDecodeTest.java
author amlu
Wed, 31 Jan 2018 12:04:53 +0800
changeset 48687 d51e64840b4f
permissions -rw-r--r--
8196211: Move two sun/nio/cs tests into OpenJDK Reviewed-by: psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48687
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
     1
/*
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
     4
 *
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
     8
 *
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    13
 * accompanied this code).
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    14
 *
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    18
 *
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    21
 * questions.
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    22
 */
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    23
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    24
/*
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    25
 * @test
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    26
 * @bug 4734607 4767225 4863613
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    27
 * @summary Decode a file using EUC-TW, test for decode errors
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    28
 * @modules jdk.charsets
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    29
 * @run main EUCTWBufferBoundaryDecodeTest
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    30
 */
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    31
import java.io.BufferedReader;
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    32
import java.io.BufferedWriter;
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    33
import java.io.ByteArrayOutputStream;
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    34
import java.io.File;
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    35
import java.io.FileInputStream;
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    36
import java.io.InputStreamReader;
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    37
import java.io.OutputStreamWriter;
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    38
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    39
/*
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    40
 * Tests for decode errors in NIO EUC-TW decoder. 4734607 details
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    41
 * decoding errors which occur when the input file > 8k in size
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    42
 * and contains numerous US-ASCII range chars
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    43
 */
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    44
public class EUCTWBufferBoundaryDecodeTest {
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    45
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    46
    public static void main(String[] args) throws Exception {
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    47
        final String inputFileName
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    48
                = System.getProperty("os.name").startsWith("Windows")
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    49
                ? "tradChinese.win.po"
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    50
                : "tradChinese.po";
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    51
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    52
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1024 * 120);
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    53
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    54
        File inputFile = new File(System.getProperty("test.src"), inputFileName);
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    55
        FileInputStream fis = new FileInputStream(inputFile);
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    56
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    57
        // Decode the EUC_TW source file, re-encode it -- (roundtrip)
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    58
        // input and output files ought to be byte for byte identical
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    59
        BufferedReader bin
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    60
                = new BufferedReader(new InputStreamReader(fis, "EUC_TW"));
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    61
        BufferedWriter bout
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    62
                = new BufferedWriter(new OutputStreamWriter(bos, "EUC-TW"));
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    63
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    64
        String line = bin.readLine();
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    65
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    66
        while (line != null) {
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    67
            bout.write(line);
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    68
            bout.newLine();
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    69
            line = bin.readLine();
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    70
        }
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    71
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    72
        bout.close();
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    73
        bin.close();
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    74
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    75
        // Compare the output with the expected output byte by byte
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    76
        byte[] outputBytes = bos.toByteArray();
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    77
        int i = 0;
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    78
        FileInputStream fi = new FileInputStream(inputFile);
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    79
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    80
        while (i < outputBytes.length) {
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    81
            byte b = (byte) fi.read();
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    82
            if (b != outputBytes[i++]) {
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    83
                throw new Exception("bug 4734607: test failed");
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    84
            }
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    85
        }
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    86
        fi.close();
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    87
    }
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    88
}