jdk/test/sun/nio/cs/TestCp834_SBCS.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 5506 202f599c92aa
child 10343 2359f68b67c8
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
     2
 * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     4
 *
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     8
 *
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    13
 * accompanied this code).
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    14
 *
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 796
diff changeset
    21
 * questions.
796
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    22
 */
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    23
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    24
/* @test
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    25
   @bug 6379808
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    26
   @summary Check all Cp933 SBCS characters are not supported in Cp834
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    27
 */
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    28
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    29
import sun.io.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    30
import java.io.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    31
import java.nio.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    32
import java.nio.charset.*;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    33
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    34
public class TestCp834_SBCS {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    35
    public static void main(String args[]) throws Exception {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    36
        // The correctness of 1:1 mapping is Coverted by CoderTest.java
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    37
        // and TestConv.java, we only need to verify that SBCS characters
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    38
        // are not supported by this charset.
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    39
        CharToByteConverter cb834 = CharToByteConverter.getConverter("Cp834");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    40
        ByteToCharConverter bc834 = ByteToCharConverter.getConverter("Cp834");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    41
        CharsetEncoder enc834 = Charset.forName("Cp834")
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    42
                                       .newEncoder()
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    43
                                       .onUnmappableCharacter(CodingErrorAction.REPLACE)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    44
                                       .onMalformedInput(CodingErrorAction.REPLACE);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    45
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    46
        CharsetDecoder dec834 = Charset.forName("Cp834")
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    47
                                       .newDecoder()
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    48
                                       .onUnmappableCharacter(CodingErrorAction.REPLACE)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    49
                                       .onMalformedInput(CodingErrorAction.REPLACE);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    50
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    51
        CharsetDecoder dec933 = Charset.forName("Cp933")
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    52
                                       .newDecoder()
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    53
                                       .onUnmappableCharacter(CodingErrorAction.REPLACE)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    54
                                       .onMalformedInput(CodingErrorAction.REPLACE);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    55
        byte[] ba = new byte[1];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    56
        byte[] ba2 = new byte[2];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    57
        ByteBuffer dbb = ByteBuffer.allocateDirect(10);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    58
        char[] ca = new char[1];
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    59
        char c;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    60
        for (int i = 0; i <= 0xff; i++) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    61
            if (i != 0xe && i != 0xf) {   // no SI/SO
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    62
                ba[0] = (byte)i;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    63
                CharBuffer cb = dec933.decode(ByteBuffer.wrap(ba));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    64
                if ((c = cb.get()) != '\ufffd') {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    65
                    // OK, this is a SBCS character in Cp933
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    66
                    if (dec834.decode(ByteBuffer.wrap(ba)).get() != '\ufffd')
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    67
                        throw new Exception("SBCS is supported in IBM834 decoder");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    68
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    69
                    if (enc834.canEncode(c))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    70
                        throw new Exception("SBCS can be encoded in IBM834 encoder");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    71
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    72
                    ca[0] = c;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    73
                    ByteBuffer bb = enc834.encode(CharBuffer.wrap(ca));
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    74
                    if (bb.get() != (byte)0xfe || bb.get() != (byte)0xfe)
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    75
                        throw new Exception("SBCS is supported in IBM834 encoder");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    76
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    77
                    boolean isMalformed = false;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    78
                    int ret = 0;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    79
                    bc834.reset();
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    80
                    try {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    81
                        ret = bc834.convert(ba, 0, 1, ca, 0, 1);
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    82
                    } catch (sun.io.MalformedInputException x) { isMalformed = true; }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    83
                    if (!isMalformed && ret != 0 && ca[0] != '\ufffd') {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    84
                        // three scenarios (1)malformed (2)held as an incomplete
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    85
                        // input or (3)return replacement all mean "no sbcs"
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    86
                        throw new Exception("SBCS is supported in Cp834 b2c");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    87
                    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    88
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    89
                    if (cb834.canConvert(c))
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    90
                        throw new Exception("SBCS can be converted in Cp834 c2b ");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    91
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    92
                    ca[0] = c;
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    93
                    if (cb834.convert(ca, 0, 1, ba2, 0, 2) != 2 ||
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    94
                        ba2[0] != (byte)0xfe || ba2[1] != (byte)0xfe) {
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    95
                        throw new Exception("SBCS is supported in Cp834 c2b");
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    96
                    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    97
                }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    98
            }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
    99
        }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   100
    }
2c3db720c065 6675856: Open charset tests
sherman
parents:
diff changeset
   101
}