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 6196991
26 * @summary Roundtrip Encoding/Decoding of just one ASCII char
27 * @author Martin Buchholz
28 */
29
30import java.util.*;
31import java.nio.*;
32import java.nio.charset.*;
33
34public class FindASCIICodingBugs {
35    private static int failures = 0;
36
37    private static void check(boolean condition) {
38        if (! condition) {
39            new Error("test failed").printStackTrace();
40            failures++;
41        }
42    }
43
44    private static boolean equals(byte[] ba, ByteBuffer bb) {
45        if (ba.length != bb.limit())
46            return false;
47        for (int i = 0; i < ba.length; i++)
48            if (ba[i] != bb.get(i))
49                return false;
50        return true;
51    }
52
53    public static void main(String[] args) throws Exception {
54        for (Map.Entry<String,Charset> e
55                 : Charset.availableCharsets().entrySet()) {
56            String csn = e.getKey();
57            Charset cs = e.getValue();
58
59            // Delete the following lines when these charsets are fixed!
60            if (csn.equals("x-JIS0208"))      continue; // MalformedInput
61            if (csn.equals("JIS_X0212-1990")) continue; // MalformedInput
62
63            if (! cs.canEncode()) continue;
64
65            CharsetEncoder enc = cs.newEncoder();
66            CharsetDecoder dec = cs.newDecoder();
67
68            if (! enc.canEncode('A')) continue;
69
70            System.out.println(csn);
71
72            try {
73                byte[] bytes1 = "A".getBytes(csn);
74                ByteBuffer bb = enc.encode(CharBuffer.wrap(new char[]{'A'}));
75
76                check(equals(bytes1, bb));
77                check(new String(bytes1, csn).equals("A"));
78
79                CharBuffer cb = dec.decode(bb);
80                check(cb.toString().equals("A"));
81            } catch (Throwable t) {
82                t.printStackTrace();
83                failures++;
84            }
85        }
86
87        if (failures > 0)
88            throw new Exception(failures + "tests failed");
89    }
90}
91