test/jdk/sun/nio/cs/TestUnicodeReversedBOM.java
author dholmes
Wed, 06 Nov 2019 21:18:42 -0500
changeset 58956 9a0a5e70eeb2
parent 53262 2969ff55c29b
permissions -rw-r--r--
8233454: Test fails with assert(!is_init_completed(), "should only happen during init") after JDK-8229516 Reviewed-by: jiefu, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
53262
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
     1
/*
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
     4
 *
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
     8
 *
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    13
 * accompanied this code).
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    14
 *
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    18
 *
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    21
 * questions.
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    22
 */
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    23
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    24
/*
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    25
 * @test
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    26
 * @bug 8216140
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    27
 * @summary Test reversed BOM (U+FFFE) in the middle of a byte buffer
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    28
 *      passes through during decoding with UnicodeDecoder.
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    29
 * @run testng TestUnicodeReversedBOM
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    30
 */
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    31
import java.nio.charset.*;
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    32
import java.nio.*;
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    33
import java.util.*;
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    34
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    35
import org.testng.annotations.DataProvider;
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    36
import org.testng.annotations.Test;
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    37
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    38
@Test
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    39
public class TestUnicodeReversedBOM {
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    40
    private static byte[] A_REVERSED_BE =
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    41
        {(byte)0x0, (byte)'A', (byte)0xff, (byte)0xfe};
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    42
    private static byte[] A_REVERSED_LE =
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    43
        {(byte)'A', (byte)0x0, (byte)0xfe, (byte)0xff};
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    44
    private static byte[] BOM_REVERSED_BE =
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    45
        {(byte)0xfe, (byte)0xff, (byte)0xff, (byte)0xfe};
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    46
    private static byte[] BOM_REVERSED_LE =
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    47
        {(byte)0xff, (byte)0xfe, (byte)0xfe, (byte)0xff};
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    48
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    49
    @DataProvider
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    50
    // [(byte[])byte array, (Charset)cs]
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    51
    public static Object[][] ReversedBOM() {
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    52
        return new Object[][] {
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    53
            {A_REVERSED_BE, StandardCharsets.UTF_16},
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    54
            {A_REVERSED_LE, StandardCharsets.UTF_16},
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    55
            {A_REVERSED_BE, StandardCharsets.UTF_16BE},
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    56
            {A_REVERSED_LE, StandardCharsets.UTF_16BE},
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    57
            {A_REVERSED_BE, StandardCharsets.UTF_16LE},
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    58
            {A_REVERSED_LE, StandardCharsets.UTF_16LE},
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    59
            {BOM_REVERSED_BE, StandardCharsets.UTF_16},
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    60
            {BOM_REVERSED_LE, StandardCharsets.UTF_16},
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    61
            {BOM_REVERSED_BE, StandardCharsets.UTF_16BE},
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    62
            {BOM_REVERSED_LE, StandardCharsets.UTF_16BE},
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    63
            {BOM_REVERSED_BE, StandardCharsets.UTF_16LE},
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    64
            {BOM_REVERSED_LE, StandardCharsets.UTF_16LE},
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    65
        };
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    66
    }
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    67
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    68
    @Test(dataProvider = "ReversedBOM")
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    69
    public void testReversedBOM(byte[] ba, Charset cs) throws CharacterCodingException {
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    70
        cs.newDecoder()
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    71
            .onMalformedInput(CodingErrorAction.REPORT)
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    72
            .onUnmappableCharacter(CodingErrorAction.REPORT)
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    73
            .decode(ByteBuffer.wrap(ba));
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    74
    }
2969ff55c29b 8216140: Correct UnicodeDecoder U+FFFE handling
naoto
parents:
diff changeset
    75
}