1/*
2 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26/*
27 * @test
28 * @bug 8008386
29 * @summary (cs) Unmappable leading should be decoded to replacement.
30 *          Tests for Shift_JIS and MS932 decoding
31 * @modules jdk.charsets
32 * @run main TestUnmappable
33 */
34
35import java.nio.charset.Charset;
36
37public class TestUnmappable {
38    public static void main(String args[]) throws Exception {
39
40        // illegal leading character test
41        byte[][] inputBytes = {
42                               // Shift_JIS
43                               {(byte)0xce, (byte)0xa0, (byte)0xce, (byte)0x7a},
44                               // MS932
45                               {(byte)0x3c, (byte)0x21, (byte)0x2d, (byte)0x2d,
46                                (byte)0xe5, (byte)0xaf, (byte)0xbe, (byte)0xe5,
47                                (byte)0xbf, (byte)0x9c, (byte)0x2d, (byte)0x2d,
48                                (byte)0x3e, (byte)0xd,  (byte)0xa },
49                               {(byte)0x81, (byte)0xad},
50                               // PCK
51                               {(byte)0xef, (byte)0x90},
52                               {(byte)0x91, (byte)0xfd}
53                              };
54
55        String[] charsets = { "Shift_JIS", "MS932", "PCK" };
56        String[] expectedStrings = {
57                                    // Shift_JIS
58                                    "0xce 0x3f 0xce 0x7a ",
59                                    // MS932
60                                    "0x3c 0x21 0x2d 0x2d 0xe5 0xaf 0xbe 0xe5 0xbf " +
61                                    "0x3f 0x2d 0x2d 0x3e 0xd 0xa ",
62                                    "0x3f 0xad ",
63                                    // PCK
64                                    "0x3f 0x3f ",
65                                    "0x3f "};
66
67        for (int i = 0; i < charsets.length; i++) {
68            String ret = new String(inputBytes[i], charsets[i]);
69            String bString = getByteString(ret.getBytes(Charset.forName(charsets[i])));
70            if (expectedStrings[i].length() != bString.length()
71               || ! expectedStrings[i].equals(bString)){
72                throw new Exception("ByteToChar for " + charsets[i]
73                    + " does not work correctly.\n" +
74                    "Expected: " + expectedStrings[i] + "\n" +
75                    "Received: " + bString);
76            }
77        }
78    }
79
80    private static String getByteString(byte[] bytes) {
81        StringBuffer sb = new StringBuffer();
82        for (int i = 0; i < bytes.length; i++) {
83            sb.append("0x" + Integer.toHexString((int)(bytes[i] & 0xFF)) + " ");
84        }
85        return sb.toString();
86    }
87}
88