test/jdk/sun/nio/cs/MalformedSurrogateStringTest.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 4153987 6354525
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    27
 * @summary Malformed surrogates should be handled by the converter in
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    28
 * substitution mode.
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    29
 */
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.ByteArrayOutputStream;
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    32
import java.io.OutputStream;
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    33
import java.io.OutputStreamWriter;
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    34
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    35
public class MalformedSurrogateStringTest {
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    36
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    37
    public static void main(String[] args) throws Exception {
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
        String fe = System.getProperty("file.encoding");
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    40
        if (  fe.equalsIgnoreCase("UTF8")
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    41
              || fe.equalsIgnoreCase("UTF-8")
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    42
              || fe.equalsIgnoreCase("UTF_8"))
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    43
            // This test is meaningless if the default charset
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    44
            // does handle surrogates
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    45
            return;
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    46
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    47
        System.out.println("Testing string conversion...");
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    48
        /* Example with malformed surrogate, and an offset */
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    49
        String t = "abc\uD800\uDB00efgh";
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    50
        String t2 = t.substring(2);
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    51
        byte[] b = t2.getBytes();
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    52
        System.err.println(b.length);
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    53
        for (int i = 0; i < b.length; i++)
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    54
            System.err.println("[" + i + "]" + "=" + (char) b[i]
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    55
                               + "=" + (int) b[i]);
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    56
        if (b.length != 7) {
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    57
            throw new Exception("Bad string conversion for bad surrogate");
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    58
        }
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    59
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    60
        /* Example with a proper surrogate, no offset. Always worked */
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    61
        String t3 = "abc\uD800\uDC00efgh";
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    62
        byte[] b2 = t3.getBytes();
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    63
        System.out.println(b2.length);
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    64
        for(int i = 0; i < b2.length; i++)
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    65
            System.err.println("[" + i + "]" + "=" + (char) b2[i]);
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    66
        if (b2.length != 8) {
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    67
            throw new Exception("Bad string conversion for good surrogate");
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    68
        }
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    69
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    70
        OutputStream os = new ByteArrayOutputStream();
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    71
        OutputStreamWriter osw = new OutputStreamWriter(os);
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    72
        System.out.println("Testing flush....");
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    73
        /* Check for the case where the converter has a left over
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    74
           high surrogate when flush is called on the converter */
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    75
        osw.flush();
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    76
        String s = "abc\uD800"; // High surrogate
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    77
        char[] c = s.toCharArray();
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    78
        osw.write(s, 0, 4);
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    79
        osw.flush();
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    80
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    81
        System.out.println("Testing convert...");
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    82
        /* Verify that all other characters go through */
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    83
        for (int k = 1; k < 65535 ; k++) {
d51e64840b4f 8196211: Move two sun/nio/cs tests into OpenJDK
amlu
parents:
diff changeset
    84
            osw.write("Char[" + k + "]=\"" + ((char) k) + "\"");
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
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
}