395N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
395N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
395N/A *
395N/A * This code is free software; you can redistribute it and/or modify it
395N/A * under the terms of the GNU General Public License version 2 only, as
395N/A * published by the Free Software Foundation.
395N/A *
395N/A * This code is distributed in the hope that it will be useful, but WITHOUT
395N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
395N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
395N/A * version 2 for more details (a copy is included in the LICENSE file that
395N/A * accompanied this code).
395N/A *
395N/A * You should have received a copy of the GNU General Public License version
395N/A * 2 along with this work; if not, write to the Free Software Foundation,
395N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
395N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
395N/A */
395N/A
395N/A/* @test
1571N/A @bug 5066863 5066867 5066874 5066879 5066884 5066887 5065777 6730652
395N/A @summary canEncode() false iff encode() throws CharacterCodingException
395N/A @run main/timeout=1200 FindCanEncodeBugs
395N/A @author Martin Buchholz
395N/A */
395N/A
395N/Aimport java.util.*;
395N/Aimport java.nio.charset.*;
395N/Aimport java.nio.*;
395N/A
395N/Apublic class FindCanEncodeBugs {
395N/A static boolean encodable1(CharsetEncoder enc, char c) {
395N/A enc.reset();
395N/A return enc.canEncode(c);
395N/A }
395N/A
395N/A static boolean encodable2(CharsetEncoder enc, char c) {
395N/A enc.reset();
395N/A try { enc.encode(CharBuffer.wrap(new char[]{c})); return true; }
395N/A catch (CharacterCodingException e) { return false; }
395N/A }
395N/A
395N/A public static void main(String[] args) throws Exception {
395N/A int failures = 0;
395N/A
395N/A for (Map.Entry<String,Charset> e
395N/A : Charset.availableCharsets().entrySet()) {
395N/A String csn = e.getKey();
395N/A Charset cs = e.getValue();
395N/A
1571N/A if (! cs.canEncode() || csn.matches("x-COMPOUND_TEXT"))
395N/A continue;
395N/A
395N/A //System.out.println(csn);
395N/A
395N/A CharsetEncoder enc = cs.newEncoder();
395N/A
395N/A for (int i = Character.MIN_VALUE; i <= Character.MAX_VALUE; i++) {
395N/A boolean encodable1 = encodable1(enc, (char)i);
395N/A boolean encodable2 = encodable2(enc, (char)i);
395N/A if (encodable1 != encodable2) {
395N/A int start = i;
395N/A int end = i;
395N/A for (int j = i;
395N/A j <= '\uffff' &&
395N/A encodable1(enc, (char)j) == encodable1 &&
395N/A encodable2(enc, (char)j) == encodable2;
395N/A j++)
395N/A end = j;
395N/A System.out.printf("charset=%-18s canEncode=%-5b ",
395N/A csn, encodable1);
395N/A if (start == end)
395N/A System.out.printf("\'\\u%04x\'%n", start);
395N/A else
395N/A System.out.printf("\'\\u%04x\' - \'\\u%04x\'%n",
395N/A start, end);
395N/A i = end;
395N/A failures++;
395N/A }
395N/A }
395N/A }
395N/A
395N/A if (failures > 0)
395N/A throw new Exception(failures + " failures");
395N/A }
395N/A}