1/*
2 * Copyright (c) 2008, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24/* @test
25 * @bug 4262894 6233303
26 * @summary Testing substitute character Escape sequence
27 * @modules jdk.charsets
28 */
29
30import java.nio.*;
31import java.nio.charset.*;
32
33public class TestISO2022JPSubBytes {
34    /* \U2460 is not valid character in ISO2022JP and will be substituted
35     * with replacement character. If the replacement character is not the
36     * "current charset" character, correct escape sequence should be output
37     * for changing character set.
38     */
39    static char[][] in = { {'\u25cb', '\u2460', '\u25cb'},
40                           {'\u0061', '\u2460', '\u0061'},
41                           {'\u25cb', '\u2460', '\u25cb'},
42                           {'\u0061', '\u2460', '\u0061'},
43                         };
44    static byte[][] expected = { {0x1b, 0x24, 0x42, 0x21, 0x7b,
45                                  0x21, 0x29,
46                                  0x21, 0x7b,
47                                  0x1b, 0x28, 0x42},
48                                 {0x61,
49                                  0x1b, 0x24, 0x42, 0x21, 0x29,
50                                  0x1b, 0x28, 0x42, 0x61},
51                                 {0x1b, 0x24, 0x42, 0x21, 0x7b,
52                                  0x1b, 0x28, 0x42, 0x3f,
53                                  0x1b, 0x24, 0x42, 0x21, 0x7b,
54                                  0x1b, 0x28, 0x42},
55                                 {0x61,
56                                  0x3f,
57                                  0x61}
58                                };
59
60    public static void main(String args[]) throws Exception {
61        CharsetEncoder enc = Charset.forName("ISO2022JP")
62          .newEncoder()
63          .onUnmappableCharacter(CodingErrorAction.REPLACE);
64
65        test(enc, in[0], expected[0]);
66
67        enc.reset();
68        test(enc, in[1], expected[1]);
69
70        enc.reset();
71        enc.replaceWith(new byte[]{(byte)'?'});
72        test(enc, in[2], expected[2]);
73
74        enc.reset();
75        test(enc, in[3], expected[3]);
76    }
77
78    public static void test (CharsetEncoder enc,
79                             char[] inputChars,
80                             byte[] expectedBytes) throws Exception
81    {
82        ByteBuffer bb = ByteBuffer.allocate(expectedBytes.length);
83        enc.encode(CharBuffer.wrap(inputChars), bb, true);
84        enc.flush(bb);
85        bb.flip();
86        byte[] outputBuff = bb.array();
87        int outputLen = bb.limit();
88        if (outputLen != expectedBytes.length) {
89            throw new Exception("Output bytes does not match");
90        }
91        for (int i = 0; i < outputLen; ++i) {
92            System.out.printf("<%x:%x> ",
93                              expectedBytes[i] & 0xff,
94                              outputBuff[i] & 0xff);
95            if (expectedBytes[i] != outputBuff[i]) {
96                System.out.println("...");
97                throw new Exception("Output bytes does not match");
98            }
99        }
100        System.out.println();
101    }
102}
103